mips changes
[cacao.git] / mm / lifespan.c
1 /*
2  * cacao/mm/lifespan.c
3  * $Id: lifespan.c 100 1998-11-30 22:30:41Z phil $
4  */
5
6 #include "mm.h"
7 #include "lifespan.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 typedef struct {
13         unsigned long time;
14         unsigned long size;
15 } lifespan_object;
16
17 static unsigned long     current_time = 0;
18 static lifespan_object** lifespan_objects = NULL;
19 static lifespan_object** lifespan_objects_end = NULL;
20 static void*             lifespan_objects_off = NULL;
21 static FILE*             lifespan_file = NULL;
22
23 void lifespan_init(void* heap_base, unsigned long heap_size)
24 {
25         current_time = 0;
26
27         lifespan_objects = (lifespan_object**)malloc(heap_size);
28         lifespan_objects_end = ((lifespan_object**)((unsigned long)lifespan_objects + (unsigned long)heap_size));
29         lifespan_objects_off = (void*)((unsigned long)lifespan_objects - (unsigned long)heap_base);
30         lifespan_file = popen("gzip -9 >lifespans.gz", "w");
31
32         if (!lifespan_objects || !lifespan_file) {
33                 fprintf(stderr, "Failed to allocate memory for lifespan-object index / popen lifespan output stream\n");
34                 exit(-1);
35         }
36
37         memset(lifespan_objects, 0, heap_size);
38 }
39
40 static __inline__ void lifespan_free_object(lifespan_object** o)
41 {
42         /* file format: alloc time, size, lifespan */
43
44         if (*o) {
45                 fprintf(lifespan_file, "%ld\t%ld\t%ld\n", (*o)->time, (*o)->size, current_time - (*o)->time);
46                 free(*o);
47                 *o = NULL;
48         }
49 }
50
51 void lifespan_close()
52 {
53         if (lifespan_objects) {
54                 while(lifespan_objects < lifespan_objects_end)
55                         lifespan_free_object(--lifespan_objects_end);
56
57                 free(lifespan_objects);
58         }
59
60         if (lifespan_file)
61                 pclose(lifespan_file);
62 }
63
64 void lifespan_emit()
65 {
66         /* emit summary */
67 }
68
69 void lifespan_free(void** from, void** limit)
70 {
71         lifespan_object**  object;
72         object = (lifespan_object**)((unsigned long)from + (unsigned long)lifespan_objects_off);
73
74         while (from < limit) {
75                 lifespan_free_object(object++);
76                 ++from;
77         }
78 }
79
80 void lifespan_alloc(void* addr, unsigned long size) 
81 {
82         lifespan_object**  object;
83         object = (lifespan_object**)((unsigned long)addr + (unsigned long)lifespan_objects_off);
84
85         *object = (lifespan_object*)malloc(sizeof(lifespan_object));
86         if (!*object) {
87                 fprintf(stderr, "oops.\n");
88                 exit(-10);
89         }
90         (*object)->time = current_time;
91         (*object)->size = size;
92         
93         current_time += size;
94 }
95
96 /*
97  * These are local overrides for various environment variables in Emacs.
98  * Please do not remove this and leave it at the end of the file, where
99  * Emacs will automagically detect them.
100  * ---------------------------------------------------------------------
101  * Local variables:
102  * mode: c
103  * indent-tabs-mode: t
104  * c-basic-offset: 4
105  * tab-width: 4
106  * End:
107  */