]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/new_memory.c
new upstream version
[1ts-debian.git] / zephyr / zwgc / new_memory.c
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
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id: new_memory.c 2091 2007-12-20 01:17:23Z kcr $
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #include <sysdep.h>
15
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_new_memory_c[] = "$Id: new_memory.c 2091 2007-12-20 01:17:23Z kcr $";
18 #endif
19
20 #if !defined(SABER) && (defined(DEBUG) || defined(MEMORY_DEBUG))
21
22 /*
23  * memory - module wrapping debugging code around normal malloc/free/etc.
24  *          routines.
25  *
26  * Overview:
27  *
28  *        ...
29  */
30
31 #define  memory__PROVIDER
32 #include "new_memory.h"
33
34 /*
35  *
36  */
37 #ifdef DEBUG
38 #define  assert(x)          if (!(x)) abort()
39 #else
40 #define  assert(x)          
41 #endif
42
43 /*
44  *
45  */
46 #ifdef DEBUG_MEMORY
47
48 extern void record_request();
49 char *current_module = 0;
50 int current_line = -1;
51
52 #endif
53
54 /*
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
61  *                 string_Copy("foo").
62  */
63
64 char *memory__malloc(size)
65      unsigned size;
66 {
67     char *result;
68
69     result = malloc(size + memory__size_of_header);
70     if (!result)
71       abort();       /* <<<>>> */
72
73 #ifdef DEBUG_MEMORY
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 =
77       current_line;
78     ((memory_block_header *)result)->check_field = CHECK_FIELD_VALUE;
79     result += memory__size_of_header;
80
81     record_request(current_module, current_line, 1, size);
82 #endif
83
84     return(result);
85 }
86
87 char *memory__realloc(ptr, size)
88      char *ptr;
89      unsigned size;
90 {
91     char *result;
92
93     assert(ptr);
94
95 #ifdef DEBUG_MEMORY
96     if (!memory__on_heap_p(ptr)) {
97         printf("realloced non-memory block in %s on line %d!\n",
98                current_module, current_line);
99         fflush(stdout);
100         return(realloc(ptr, size));
101     }
102 #endif
103
104     result = realloc(ptr-memory__size_of_header, size+memory__size_of_header);
105     if (!result)
106       abort(); /* <<<>>> */
107
108     return(result+memory__size_of_header);
109 }
110
111 char *memory__calloc(nelem, elsize)
112      unsigned nelem;
113      unsigned elsize;
114 {
115     char *result;
116
117 #ifdef DEBUG_MEMORY
118     printf("in calloc\n"); fflush(stdout);
119 #endif
120
121     abort();
122
123 #ifdef FRED
124     result = calloc(nelem, elsize);
125     if (!result)
126       abort();
127
128     record_request(1);
129 #endif
130
131     return(result);
132 }
133
134 void memory__free(ptr)
135      char *ptr;
136 {
137     assert(ptr);
138
139 #ifdef DEBUG_MEMORY
140     if (!memory__on_heap_p(ptr)) {
141         printf("freed non-memory block in %s on line %d!\n", current_module,
142                current_line);
143         fflush(stdout);
144         (void)free(ptr);
145         return;
146     }
147
148     record_request(memory__get_header(ptr)->creating_module,
149                    memory__get_header(ptr)->line_number_in_creating_module,
150                    -1,
151                    memory__get_header(ptr)->size);
152 #endif
153
154     (void)free(ptr-memory__size_of_header);
155 }
156
157 #ifdef DEBUG_MEMORY
158
159 #include "int_dictionary.h"
160
161 static int request_off = 0;
162 static int_dictionary requests = 0;
163 static int outstanding_requests = 0;
164 static int outstanding_memory = 0;
165
166 void record_request(module, line_number, dir, size)
167      char *module;
168      int line_number;
169      int dir;
170      unsigned int size;
171 {
172     int_dictionary_binding *binding;
173     int already_exists;
174 #ifdef LINE
175     char buffer[20];
176 #endif
177
178     if (request_off)
179       return;
180     request_off = 1;
181
182     if (!requests)
183       requests = int_dictionary_Create(101);
184
185 #ifdef LINE
186     module = string_Concat(module, ":");
187     sprintf(buffer, "%d", line_number);
188     module = string_Concat2(module, buffer);
189 #endif
190
191     binding = int_dictionary_Define(requests, module, &already_exists);
192     if (!already_exists)
193       binding->value = 0;
194
195 #ifdef LINE
196     free(module);
197 #endif
198
199     binding->value += dir;
200     outstanding_requests += dir;
201     outstanding_memory += size*dir;
202
203     request_off = 0;
204 }
205
206 void proc(binding)
207      int_dictionary_binding *binding;
208 {
209     if (binding->value)
210       printf("    %-30s %6d blocks allocated\n", binding->key, binding->value);
211 }
212
213 void report_memory_usage()
214 {
215     printf("\n# of blocks on the heap = %d\n", outstanding_requests);
216     printf("Total heap space in use: %d bytes\n", outstanding_memory);
217
218     printf("\nHeap Allocations by module:\n");
219     int_dictionary_Enumerate(requests, proc);
220     printf("\n");
221
222     fflush(stdout);
223 }
224
225 void set_module(file, line)
226      char *file;
227      int line;
228 {
229     if (request_off)
230       return;
231
232     if (!strcmp(file, "new_string.c"))
233       return;
234     if (!strcmp(file, "string_dictionary_aux.c"))
235       return;
236
237     current_line = line;
238     current_module = file;
239 }
240
241 #endif
242
243 #endif /* SABER */