This source file includes following definitions.
- rf_record_malloc
- rf_unrecord_malloc
- rf_print_unfreed
- rf_ConfigureDebugMem
- rf_memory_hash_insert
- rf_memory_hash_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 #include "rf_types.h"
44 #include "rf_threadstuff.h"
45 #include "rf_options.h"
46 #include "rf_debugMem.h"
47 #include "rf_general.h"
48
49 static long tot_mem_in_use = 0;
50
51
52 #define RF_MH_TABLESIZE 1000
53
54 struct mh_struct {
55 void *address;
56 int size;
57 int line;
58 char *filen;
59 char allocated;
60 struct mh_struct *next;
61 };
62
63 static struct mh_struct *mh_table[RF_MH_TABLESIZE];
64
65 RF_DECLARE_MUTEX(rf_debug_mem_mutex);
66 static int mh_table_initialized = 0;
67
68 void rf_memory_hash_insert(void *, int, int, char *);
69 int rf_memory_hash_remove(void *, int);
70
71 void
72 rf_record_malloc(void *p, int size, int line, char *filen)
73 {
74 RF_ASSERT(size != 0);
75
76
77 rf_memory_hash_insert(p, size, line, filen);
78 tot_mem_in_use += size;
79
80
81 if ((long) p == rf_memDebugAddress) {
82 printf("Allocate: debug address allocated from line %d file"
83 " %s\n", line, filen);
84 }
85 }
86
87 void
88 rf_unrecord_malloc(void *p, int sz)
89 {
90 int size;
91
92
93 size = rf_memory_hash_remove(p, sz);
94 tot_mem_in_use -= size;
95
96 if ((long) p == rf_memDebugAddress) {
97
98 printf("Free: Found debug address\n");
99 }
100 }
101
102 void
103 rf_print_unfreed(void)
104 {
105 int i, foundone = 0;
106 struct mh_struct *p;
107
108 for (i = 0; i < RF_MH_TABLESIZE; i++) {
109 for (p = mh_table[i]; p; p = p->next)
110 if (p->allocated) {
111 if (!foundone)
112 printf("\n\nThere are unfreed"
113 " memory locations at"
114 " program shutdown:\n");
115 foundone = 1;
116 printf("Addr 0x%lx Size %d line %d file %s\n",
117 (long) p->address, p->size, p->line,
118 p->filen);
119 }
120 }
121 if (tot_mem_in_use) {
122 printf("%ld total bytes in use\n", tot_mem_in_use);
123 }
124 }
125
126 int
127 rf_ConfigureDebugMem(RF_ShutdownList_t **listp)
128 {
129 int i, rc;
130
131 rc = rf_create_managed_mutex(listp, &rf_debug_mem_mutex);
132 if (rc) {
133 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
134 __FILE__, __LINE__, rc);
135 return (rc);
136 }
137 if (rf_memDebug) {
138 for (i = 0; i < RF_MH_TABLESIZE; i++)
139 mh_table[i] = NULL;
140 mh_table_initialized = 1;
141 }
142 return (0);
143 }
144
145 #define HASHADDR(_a_) ( (((unsigned long) _a_)>>3) % RF_MH_TABLESIZE )
146
147 void
148 rf_memory_hash_insert(void *addr, int size, int line, char *filen)
149 {
150 unsigned long bucket = HASHADDR(addr);
151 struct mh_struct *p;
152
153 RF_ASSERT(mh_table_initialized);
154
155
156 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
157 if (!p) {
158 RF_Malloc(p, sizeof(struct mh_struct), (struct mh_struct *));
159 RF_ASSERT(p);
160 p->next = mh_table[bucket];
161 mh_table[bucket] = p;
162 p->address = addr;
163 p->allocated = 0;
164 }
165 if (p->allocated) {
166 printf("ERROR: Reallocated address 0x%lx from line %d,"
167 " file %s without intervening free\n", (long) addr,
168 line, filen);
169 printf(" Last allocated from line %d file %s\n",
170 p->line, p->filen);
171 RF_ASSERT(0);
172 }
173 p->size = size;
174 p->line = line;
175 p->filen = filen;
176 p->allocated = 1;
177 }
178
179 int
180 rf_memory_hash_remove(void *addr, int sz)
181 {
182 unsigned long bucket = HASHADDR(addr);
183 struct mh_struct *p;
184
185 RF_ASSERT(mh_table_initialized);
186 for (p = mh_table[bucket]; p && (p->address != addr); p = p->next);
187 if (!p) {
188 printf("ERROR: Freeing never-allocated address 0x%lx\n",
189 (long) addr);
190 RF_PANIC();
191 }
192 if (!p->allocated) {
193 printf("ERROR: Freeing unallocated address 0x%lx."
194 " Last allocation line %d file %s\n", (long) addr,
195 p->line, p->filen);
196 RF_PANIC();
197 }
198 if (sz > 0 && p->size != sz) {
199
200
201
202
203 printf("ERROR: Incorrect size at free for address 0x%lx:"
204 " is %d should be %d. Alloc at line %d of file %s\n",
205 (unsigned long) addr, sz, p->size, p->line, p->filen);
206 RF_PANIC();
207 }
208 p->allocated = 0;
209 return (p->size);
210 }