3 * $Id: lifespan.c 100 1998-11-30 22:30:41Z phil $
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;
23 void lifespan_init(void* heap_base, unsigned long heap_size)
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");
32 if (!lifespan_objects || !lifespan_file) {
33 fprintf(stderr, "Failed to allocate memory for lifespan-object index / popen lifespan output stream\n");
37 memset(lifespan_objects, 0, heap_size);
40 static __inline__ void lifespan_free_object(lifespan_object** o)
42 /* file format: alloc time, size, lifespan */
45 fprintf(lifespan_file, "%ld\t%ld\t%ld\n", (*o)->time, (*o)->size, current_time - (*o)->time);
53 if (lifespan_objects) {
54 while(lifespan_objects < lifespan_objects_end)
55 lifespan_free_object(--lifespan_objects_end);
57 free(lifespan_objects);
61 pclose(lifespan_file);
69 void lifespan_free(void** from, void** limit)
71 lifespan_object** object;
72 object = (lifespan_object**)((unsigned long)from + (unsigned long)lifespan_objects_off);
74 while (from < limit) {
75 lifespan_free_object(object++);
80 void lifespan_alloc(void* addr, unsigned long size)
82 lifespan_object** object;
83 object = (lifespan_object**)((unsigned long)addr + (unsigned long)lifespan_objects_off);
85 *object = (lifespan_object*)malloc(sizeof(lifespan_object));
87 fprintf(stderr, "oops.\n");
90 (*object)->time = current_time;
91 (*object)->size = size;
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 * ---------------------------------------------------------------------
103 * indent-tabs-mode: t