3 * $Id: lifespan.c 106 1998-12-11 02:03:33Z phil $
18 static unsigned long current_time = 0;
19 static unsigned long current_number = 0;
20 static lifespan_object** lifespan_objects = NULL;
21 static lifespan_object** lifespan_objects_end = NULL;
22 static void* lifespan_objects_off = NULL;
23 static FILE* lifespan_file = NULL;
25 static unsigned long lifespan_histo_size[64] = {};
26 static unsigned long lifespan_histo_lifespan[64] = {};
28 void lifespan_init(void* heap_base, unsigned long heap_size)
32 lifespan_objects = (lifespan_object**)malloc(heap_size);
33 lifespan_objects_end = ((lifespan_object**)((unsigned long)lifespan_objects + (unsigned long)heap_size));
34 lifespan_objects_off = (void*)((unsigned long)lifespan_objects - (unsigned long)heap_base);
35 lifespan_file = popen("gzip -9 >lifespans.gz", "w");
37 if (!lifespan_objects || !lifespan_file) {
38 fprintf(stderr, "Failed to allocate memory for lifespan-object index / popen lifespan output stream\n");
42 memset(lifespan_objects, 0, heap_size);
45 static __inline__ void lifespan_free_object(lifespan_object** o)
48 /* file format: alloc time, size, lifespan */
51 fprintf(lifespan_file,
52 "%ld\t%ld\t%ld\t%ld\t%ld\n",
56 current_number - (*o)->number,
57 current_time - (*o)->time);
66 ++lifespan_histo_size[high];
70 size = current_time - (*o)->time;
76 ++lifespan_histo_lifespan[high];
85 if (lifespan_objects) {
86 while(lifespan_objects < lifespan_objects_end)
87 lifespan_free_object(--lifespan_objects_end);
89 free(lifespan_objects);
93 pclose(lifespan_file);
101 for (i = 4; i < 32; ++i)
102 fprintf(stderr, "%Lu-%Lu\t%Lu\n",
105 lifespan_histo_size[i]);
107 fprintf(stderr, "\n\n\n");
109 for (i = 4; i < 32; ++i)
110 fprintf(stderr, "%Lu-%Lu\t%Lu\n",
113 lifespan_histo_lifespan[i]);
116 void lifespan_free(void** from, void** limit)
118 lifespan_object** object;
119 object = (lifespan_object**)((unsigned long)from + (unsigned long)lifespan_objects_off);
121 while (from < limit) {
122 lifespan_free_object(object++);
127 void lifespan_alloc(void* addr, unsigned long size)
129 lifespan_object** object;
130 object = (lifespan_object**)((unsigned long)addr + (unsigned long)lifespan_objects_off);
132 *object = (lifespan_object*)malloc(sizeof(lifespan_object));
134 fprintf(stderr, "oops.\n");
137 (*object)->time = current_time;
138 (*object)->size = size;
139 (*object)->number = ++current_number;
141 current_time += size;
145 * These are local overrides for various environment variables in Emacs.
146 * Please do not remove this and leave it at the end of the file, where
147 * Emacs will automagically detect them.
148 * ---------------------------------------------------------------------
151 * indent-tabs-mode: t