Changeset 4300
- Timestamp:
- Jun 17, 2005, 12:16:29 PM (21 years ago)
- Location:
- trunk/Ohana/src/libohana
- Files:
-
- 2 edited
-
include/ohana_allocate.h (modified) (1 diff)
-
src/ohana_allocate.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/libohana/include/ohana_allocate.h
r3903 r4300 5 5 # ifdef OHANA_MEMORY 6 6 7 void *ohana_malloc (char *file, int line, size_tsize);8 void *ohana_realloc (char *file, int line, void *in, size_tsize);7 void *ohana_malloc (char *file, int line, int Nelem, size_t esize); 8 void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize); 9 9 void ohana_free (char *file, int line, void *in); 10 10 void ohana_memregister_func (char *file, int line, void *ptr); 11 11 void ohana_memdump_func (int mode); 12 void ohana_memcheck_func (int mode); 12 13 13 14 # define ohana_memregister(X) ohana_memregister_func (__FILE__, __LINE__, (X)); 15 # define ohana_memcheck(X) ohana_memcheck_func (X); 14 16 # define ohana_memdump(X) ohana_memdump_func (X); 15 17 16 # define ALLOCATE(X,T,S) \ 17 X = (T *) ohana_malloc (__FILE__, __LINE__, (unsigned)(MAX(((S)*((int)sizeof(T))),1))); \ 18 if (X == NULL) { \ 19 fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);\ 20 exit (10); } 18 # define ALLOCATE(X,T,S) \ 19 X = (T *) ohana_malloc (__FILE__, __LINE__, (S), sizeof(T)) 21 20 # define REALLOCATE(X,T,S) \ 22 X = (T *) ohana_realloc(__FILE__, __LINE__, X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \ 23 if (X == NULL) { \ 24 fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__);\ 25 exit (10); } 21 X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T)); 26 22 # define CHECK_REALLOCATE(X,T,S,N,D) \ 27 if ((N) >= (S)) { \ 28 S += D; \ 29 X = (T *) ohana_realloc(__FILE__, __LINE__, X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \ 30 if (X == NULL) { \ 31 fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__);\ 32 exit (10); } } 23 if ((N) >= (S)) { S += D; \ 24 X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T)); } 33 25 # define free(X) ohana_free(__FILE__, __LINE__, X) 34 26 35 27 # else 36 28 # define ohana_memregister(X) /* NOP */ 29 # define ohana_memcheck(X) /* NOP */ 37 30 # define ohana_memdump(X) /* NOP */ 38 31 # endif /* OHANA_MEMORY */ -
trunk/Ohana/src/libohana/src/ohana_allocate.c
r3921 r4300 4 4 5 5 /* need an internal version that does not use ohana_memory functions */ 6 # define FALSE 0 7 # define TRUE 1 6 8 # define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) 7 9 # define OHANA_ALLOCATE(X,T,S) \ … … 24 26 # define OHANA_FREE(X) free(X) 25 27 26 # define STATE_ALLOC 0 27 # define STATE_REALLOC 1 28 # define STATE_FREE 2 28 # define STATE_ALLOC 0 29 # define STATE_REALLOC 1 30 # define STATE_FREE 2 31 # define STATE_EXTERNAL 3 29 32 30 33 typedef struct { 31 34 char *file; 32 35 int line; 33 void *ptr; 36 void *ptr; // location of externally visible entry 37 int size; 34 38 int state; 35 39 } Memlist; … … 54 58 } 55 59 56 void *ohana_malloc (char *file, int line, size_t size) { 57 58 void *ptr; 60 void *ohana_malloc (char *file, int line, int Nelem, size_t esize) { 61 62 void *ptr, *new; 63 int size; 64 int *marker; 59 65 60 66 if (memlist == NULL) ohana_meminit (); 61 67 62 ptr = malloc (size); 63 if (ptr == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line); 68 Nelem = MAX (1, Nelem); 69 size = Nelem * esize; 70 71 new = malloc (size + 8); /* 8 extra to save endposts */ 72 if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line); 73 ptr = new + 4; 74 75 marker = (int *) new; 76 *marker = 0xdeadbeef; 77 marker = (int *)(new + size + 4); 78 *marker = 0xdeadbeef; 64 79 65 80 /* new memory, add to stack */ … … 67 82 memlist[Nmemlist].file = file; 68 83 memlist[Nmemlist].line = line; 84 memlist[Nmemlist].size = size; 69 85 memlist[Nmemlist].state = STATE_ALLOC; 70 86 Nmemlist ++; … … 76 92 } 77 93 78 /* register externally allocated memory with ohana memory manager */ 79 void ohana_memregister_func (char *file, int line, void *ptr) { 80 81 if (memlist == NULL) ohana_meminit (); 82 83 /* add to stack */ 84 memlist[Nmemlist].ptr = ptr; 85 memlist[Nmemlist].file = file; 86 memlist[Nmemlist].line = line; 87 memlist[Nmemlist].state = STATE_ALLOC; 88 Nmemlist ++; 89 if (Nmemlist == NMEMLIST) { 90 NMEMLIST += 1000; 91 OHANA_REALLOCATE (memlist, Memlist, NMEMLIST); 92 } 93 return; 94 } 95 96 void *ohana_realloc (char *file, int line, void *in, size_t size) { 97 98 int i; 99 void *ptr; 94 void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize) { 95 96 int i, size; 97 void *ptr, *ref, *new; 98 int *marker; 100 99 101 100 if (memlist == NULL) ohana_memabort ("REALLOCATE before ALLOCATE"); 101 102 Nelem = MAX (1, Nelem); 103 size = Nelem * esize; 104 105 ref = in - 4; 102 106 103 107 /* find old entry, update ptr, file, line */ … … 105 109 if (memlist[i].state == STATE_FREE) continue; 106 110 if (memlist[i].ptr == in) { 111 112 if (memlist[i].state == STATE_EXTERNAL) ohana_memabort ("ERROR: realloc of external memory"); 113 107 114 /* ask for new memory */ 108 ptr = realloc (in, size); 109 if (ptr == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line); 110 /* if new memory is same as old, just return it */ 111 if (in == ptr) return (ptr); 112 /* otherwise, update with new location */ 115 new = realloc (ref, size + 8); /* 8 extra to save endposts */ 116 if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line); 117 ptr = new + 4; 118 119 /* set the marker */ 120 marker = (int *) new; 121 *marker = 0xdeadbeef; 122 marker = (int *)(ptr + size); 123 *marker = 0xdeadbeef; 124 125 /* otherwise, update memory new location */ 113 126 memlist[i].ptr = ptr; 114 memlist[i].file = file; 115 memlist[i].line = line; 127 memlist[i].size = size; 116 128 memlist[i].state = STATE_REALLOC; 129 130 /* if new memory in new location, update references */ 131 if (ptr != in) { 132 memlist[i].file = file; 133 memlist[i].line = line; 134 } 135 117 136 return (ptr); 118 137 } 119 138 } 120 121 139 ohana_memabort ("allocated memory not found for realloc (%s, %d)\n", file, line); 122 140 return (NULL); … … 126 144 127 145 int i; 146 128 147 if (memlist == NULL) ohana_memabort ("FREE before ALLOCATE"); 129 148 … … 141 160 } 142 161 143 void ohana_memdump_func (int allmemory) { 144 145 int i, Ns[3], N; 146 char S[3]; 162 /* register externally allocated memory with ohana memory manager */ 163 void ohana_memregister_func (char *file, int line, void *ptr) { 164 165 if (memlist == NULL) ohana_meminit (); 166 167 /* add to stack */ 168 memlist[Nmemlist].ptr = ptr; 169 memlist[Nmemlist].file = file; 170 memlist[Nmemlist].line = line; 171 memlist[Nmemlist].state = STATE_EXTERNAL; 172 Nmemlist ++; 173 if (Nmemlist == NMEMLIST) { 174 NMEMLIST += 1000; 175 OHANA_REALLOCATE (memlist, Memlist, NMEMLIST); 176 } 177 return; 178 } 179 180 void ohana_memcheck_func (int allmemory) { 181 182 int i, header; 183 int *marker; 184 char top, bottom; 147 185 148 186 if (Nmemlist == 0) { … … 151 189 } 152 190 191 fprintf (stderr, "checking for memory corruption...\n"); 192 header = FALSE; 193 for (i = 0; i < Nmemlist; i++) { 194 if (memlist[i].state == STATE_EXTERNAL) continue; 195 if ((allmemory == 0) && (memlist[i].state == STATE_FREE)) continue; 196 197 top = bottom = 'Y'; 198 marker = (int *)(memlist[i].ptr - 4); 199 if (*marker == 0xdeadbeef) bottom = 'N'; 200 marker = (int *)(memlist[i].ptr + memlist[i].size); 201 if (*marker == 0xdeadbeef) top = 'N'; 202 203 if ((top == 'N') && (bottom == 'N')) continue; 204 if (!header) { 205 fprintf (stderr, " Nmem bot top file line\n"); 206 header = TRUE; 207 } 208 fprintf (stderr, "corrupt: %3d %c %c %-15s %3d\n", i, bottom, top, memlist[i].file, memlist[i].line); 209 } 210 return; 211 } 212 213 void ohana_memdump_func (int allmemory) { 214 215 int i, Ns[3], N; 216 char S[3]; 217 218 if (Nmemlist == 0) { 219 fprintf (stderr, "no memory allocated\n"); 220 return; 221 } 222 153 223 S[0] = 'A'; 154 224 S[1] = 'R'; 155 225 S[2] = 'F'; 156 Ns[0] = Ns[1] = Ns[2] = 0; 157 158 for (i = 0; i < Nmemlist; i++) { 159 if (memlist[i].state == STATE_ALLOC) N = 0; 160 if (memlist[i].state == STATE_REALLOC) N = 1; 161 if (memlist[i].state == STATE_FREE) N = 2; 226 S[3] = 'X'; 227 Ns[0] = Ns[1] = Ns[2] = Ns[3] = 0; 228 229 for (i = 0; i < Nmemlist; i++) { 230 if (memlist[i].state == STATE_ALLOC) N = 0; 231 if (memlist[i].state == STATE_REALLOC) N = 1; 232 if (memlist[i].state == STATE_FREE) N = 2; 233 if (memlist[i].state == STATE_EXTERNAL) N = 2; 162 234 Ns[N] ++; 163 235 if ((allmemory == 0) && (memlist[i].state == STATE_FREE)) continue;
Note:
See TracChangeset
for help on using the changeset viewer.
