1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_new_memory_c[] = "$Id$";
20 #if !defined(SABER) && (defined(DEBUG) || defined(MEMORY_DEBUG))
23 * memory - module wrapping debugging code around normal malloc/free/etc.
31 #define memory__PROVIDER
32 #include "new_memory.h"
38 #define assert(x) if (!(x)) abort()
48 extern void record_request();
49 char *current_module = 0;
50 int current_line = -1;
55 * string string_CreateFromData(char *data, int length):
56 * Requires: data[0], data[1], ..., data[length-1] != 0
57 * Effects: Takes the first length characters at data and
58 * creates a string containing them. The returned string
59 * is on the heap & must be freed eventually.
60 * I.e., if passed "foobar" and 3, it would return
64 char *memory__malloc(size)
69 result = malloc(size + memory__size_of_header);
74 ((memory_block_header *)result)->size = size;
75 ((memory_block_header *)result)->creating_module = current_module;
76 ((memory_block_header *)result)->line_number_in_creating_module =
78 ((memory_block_header *)result)->check_field = CHECK_FIELD_VALUE;
79 result += memory__size_of_header;
81 record_request(current_module, current_line, 1, size);
87 char *memory__realloc(ptr, size)
96 if (!memory__on_heap_p(ptr)) {
97 printf("realloced non-memory block in %s on line %d!\n",
98 current_module, current_line);
100 return(realloc(ptr, size));
104 result = realloc(ptr-memory__size_of_header, size+memory__size_of_header);
106 abort(); /* <<<>>> */
108 return(result+memory__size_of_header);
111 char *memory__calloc(nelem, elsize)
118 printf("in calloc\n"); fflush(stdout);
124 result = calloc(nelem, elsize);
134 void memory__free(ptr)
140 if (!memory__on_heap_p(ptr)) {
141 printf("freed non-memory block in %s on line %d!\n", current_module,
148 record_request(memory__get_header(ptr)->creating_module,
149 memory__get_header(ptr)->line_number_in_creating_module,
151 memory__get_header(ptr)->size);
154 (void)free(ptr-memory__size_of_header);
159 #include "int_dictionary.h"
161 static int request_off = 0;
162 static int_dictionary requests = 0;
163 static int outstanding_requests = 0;
164 static int outstanding_memory = 0;
166 void record_request(module, line_number, dir, size)
172 int_dictionary_binding *binding;
183 requests = int_dictionary_Create(101);
186 module = string_Concat(module, ":");
187 sprintf(buffer, "%d", line_number);
188 module = string_Concat2(module, buffer);
191 binding = int_dictionary_Define(requests, module, &already_exists);
199 binding->value += dir;
200 outstanding_requests += dir;
201 outstanding_memory += size*dir;
207 int_dictionary_binding *binding;
210 printf(" %-30s %6d blocks allocated\n", binding->key, binding->value);
213 void report_memory_usage()
215 printf("\n# of blocks on the heap = %d\n", outstanding_requests);
216 printf("Total heap space in use: %d bytes\n", outstanding_memory);
218 printf("\nHeap Allocations by module:\n");
219 int_dictionary_Enumerate(requests, proc);
225 void set_module(file, line)
232 if (!strcmp(file, "new_string.c"))
234 if (!strcmp(file, "string_dictionary_aux.c"))
238 current_module = file;