Changeset 31588 for branches/eam_branches/ipp-20110505/Ohana
- Timestamp:
- May 30, 2011, 1:04:04 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20110505/Ohana/src/libohana/src/ohana_allocate.c
r27491 r31588 31 31 # define STATE_EXTERNAL 3 32 32 33 typedef struct { 33 # define OHANA_MEMMAGIC (uint32_t) 0xdeadbeef 34 35 typedef struct Memblock { 36 uint32_t startblock; 37 struct Memblock *prevBlock; 38 struct Memblock *nextBlock; 39 size_t size; 34 40 char *file; 35 int line; 36 void *ptr; // location of externally visible entry 37 int size; 38 int state; 39 } Memlist; 40 41 // XXX consider fixing the memory tracking model (list?) 42 // static long *memsort; 43 // static long *memhash; 44 static Memlist *memlist = NULL; 45 int Nmemlist = 0; 46 int NMEMLIST = 0; 47 int NMEMBYTE = 0; 41 char *func; 42 int line; 43 uint32_t endblock; 44 } Memblock; 45 46 static Memblock *lastMemBlock = NULL; 47 48 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 48 49 49 50 void ohana_meminit () { 50 Nmemlist = 0; 51 NMEMLIST = 1000; 52 OHANA_ALLOCATE (memlist, Memlist, NMEMLIST); 53 // OHANA_ALLOCATE (memsort, long, NMEMLIST); 54 // OHANA_ALLOCATE (memhash, long, NMEMLIST); 55 NMEMBYTE = sizeof(size_t); 51 return; 56 52 } 57 53 … … 71 67 size_t *marker; 72 68 73 if (memlist == NULL) ohana_meminit ();74 75 69 Nelem = MAX (1, Nelem); 76 70 size = Nelem * esize; 77 71 78 new = malloc (size + 2*NMEMBYTE); /* 2 extra locations to save endposts*/72 new = malloc (sizeof(Memblock) + size + sizeof(void *)); /* memblock + data + endpost */ 79 73 if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line); 80 ptr = new + NMEMBYTE; 81 82 marker = (size_t *) new; 83 *marker = 0xdeadbeef; 84 marker = (size_t *)(new + size + NMEMBYTE); 85 *marker = 0xdeadbeef; 74 ptr = new + sizeof(Memblock); 75 76 new->startblock = OHANA_MEMMAGIC; 77 new->endblock = OHANA_MEMMAGIC; 78 *(uint32_t *)((char *)(new + 1) + size) = OHANA_MEMMAGIC; 79 80 // marker = (size_t *)(new + size + NMEMBYTE); 81 // *marker = 0xdeadbeef; 86 82 87 83 /* new memory, add to stack */ 88 memlist[Nmemlist].ptr = ptr; 89 memlist[Nmemlist].file = file; 90 memlist[Nmemlist].line = line; 91 memlist[Nmemlist].size = size; 92 memlist[Nmemlist].state = STATE_ALLOC; 93 94 // before each free, we sort this pair to speed up searching 95 // memsort[Nmemlist] = ptr; 96 // memhash[Nmemlist] = Nmemlist; 97 98 Nmemlist ++; 99 if (Nmemlist == NMEMLIST) { 100 NMEMLIST += 1000; 101 OHANA_REALLOCATE (memlist, Memlist, NMEMLIST); 102 } 84 new->file = file; 85 new->func = func; 86 new->line = line; 87 new->size = size; 88 89 // new memblock becomes the 'lastMemBlock': 90 // lastMemBlock = new 91 // new->prev = (new - 1) 92 // (new - 1)->next = new 93 94 new->nextBlock = NULL; 95 96 pthread_mutex_lock(&memBlockListMutex); 97 98 if (lastMemBlock) { 99 lastMemBlock->nextBlock = new; 100 } 101 new->prevBlock = lastMemBlock; 102 lastMemBlock = new; 103 104 pthread_mutex_unlock(&memBlockListMutex); 105 103 106 return (ptr); 104 107 } … … 110 113 size_t *marker; 111 114 112 if (memlist == NULL) ohana_memabort ("REALLOCATE before ALLOCATE"); 115 if (!in) { 116 ptr = ohana_alloc (file, line, Nelem, esize); 117 return ptr; 118 } 119 120 ref = ((Memblock *)in) - 1; 113 121 114 122 Nelem = MAX (1, Nelem); 115 123 size = Nelem * esize; 116 124 117 ref = in - NMEMBYTE; 118 119 /* find old entry, update ptr, file, line */ 120 for (i = 0; i < Nmemlist; i++) { 121 if (memlist[i].state == STATE_FREE) continue; 122 if (memlist[i].ptr == in) { 123 124 if (memlist[i].state == STATE_EXTERNAL) ohana_memabort ("ERROR: realloc of external memory"); 125 126 /* ask for new memory */ 127 new = realloc (ref, size + 2*NMEMBYTE); /* 2 extra slots to save endposts */ 128 if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line); 129 ptr = new + NMEMBYTE; 130 131 /* set the marker */ 132 marker = (size_t *) new; 133 *marker = 0xdeadbeef; 134 marker = (size_t *)(ptr + size); 135 *marker = 0xdeadbeef; 136 137 /* otherwise, update memory new location */ 138 memlist[i].ptr = ptr; 139 memlist[i].size = size; 140 memlist[i].state = STATE_REALLOC; 141 142 /* if new memory in new location, update references */ 143 if (ptr != in) { 144 memlist[i].file = file; 145 memlist[i].line = line; 146 } 147 148 return (ptr); 149 } 150 } 151 ohana_memabort ("allocated memory not found for realloc (%s, %d)\n", file, line); 152 return (NULL); 125 // requested same size as current allocation 126 if (size == ref->size) { 127 return ptr; 128 } 129 130 pthread_mutex_lock(&memBlockListMutex); 131 132 Memblock *nextBlock = ref->nextBlock; 133 Memblock *prevBlock = ref->prevBlock; 134 135 int isLast = (ref == lastMemBlock); 136 137 /* ask for new memory */ 138 new = realloc (ref, sizeof(Memblock) + size + sizeof(void *)); /* memblock + data + endpost */ 139 if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line); 140 ptr = new + sizeof(Memblock); 141 142 new->size = size; 143 *(uint32_t *)((char *)(new + 1) + size) = OHANA_MEMMAGIC; 144 145 if (isLast) { 146 lastMemBlock = new; 147 } 148 149 if (nextBlock) { 150 nextBlock->prevBlock = memBlock; 151 } 152 if (prevBlock) { 153 prevBlock->nextBlock = memBlock; 154 } 155 156 pthread_mutex_unlock(&memBlockListMutex); 157 158 return (ptr); 153 159 } 154 160 … … 158 164 int i; 159 165 160 if (memlist == NULL) ohana_memabort ("FREE before ALLOCATE"); 161 162 /* find old entry, set state */ 163 for (i = 0; i < Nmemlist; i++) { 164 if (memlist[i].state == STATE_FREE) continue; 165 if (memlist[i].ptr == in) { 166 memlist[i].state = STATE_FREE; 167 return; 168 } 169 } 170 171 /* find already freed examples */ 172 for (i = 0; i < Nmemlist; i++) { 173 if (memlist[i].ptr == in) { 174 fprintf (stderr, "used here: %4d %s %d\n", i, memlist[i].file, memlist[i].line); 175 } 176 } 177 178 ohana_memabort ("allocated memory not found for free (%s, %d : %x)\n", file, line, in); 166 if (!in) return NULL; 167 168 ref = ((Memblock *) ptr) - 1; 169 170 pthread_mutex_lock(&memBlockListMutex); 171 172 Memblock *nextBlock = ref->nextBlock; 173 Memblock *prevBlock = ref->prevBlock; 174 175 if (nextBlock) { 176 nextBlock->prevBlock = prevBlock; 177 } 178 if (prevBlock) { 179 prevBlock->nextBlock = nextBlock; 180 } 181 if (lastMemBlock == ref) { 182 lastMemBlock = prevBlock; 183 } 184 185 pthread_mutex_unlock(&memBlockListMutex); 186 187 free (ref); 188 179 189 return; 180 190 } … … 183 193 void ohana_memregister_func (char *file, int line, void *ptr) { 184 194 185 if (memlist == NULL) ohana_meminit (); 186 187 /* add to stack */ 188 memlist[Nmemlist].ptr = ptr; 189 memlist[Nmemlist].file = file; 190 memlist[Nmemlist].line = line; 191 memlist[Nmemlist].state = STATE_EXTERNAL; 192 Nmemlist ++; 193 if (Nmemlist == NMEMLIST) { 194 NMEMLIST += 1000; 195 OHANA_REALLOCATE (memlist, Memlist, NMEMLIST); 196 } 197 return; 198 } 199 195 // is this needed? 196 return; 197 } 198 199 # if (0) 200 200 void ohana_memcheck_func (int allmemory) { 201 201 … … 291 291 return; 292 292 } 293 # endif
Note:
See TracChangeset
for help on using the changeset viewer.
