| 181 | } |
| 182 | |
| 183 | void *mrealloc(CSOUND *csound, void *oldp, size_t size) |
| 184 | { |
| 185 | memAllocBlock_t *pp; |
| 186 | void *p; |
| 187 | |
| 188 | if (UNLIKELY(oldp == NULL)) |
| 189 | return mmalloc(csound, size); |
| 190 | if (UNLIKELY(size == (size_t) 0)) { |
| 191 | mfree(csound, oldp); |
| 192 | return NULL; |
| 193 | } |
| 194 | pp = HDR_PTR(oldp); |
| 195 | #ifdef MEMDEBUG |
| 196 | if (UNLIKELY(pp->magic != MEMALLOC_MAGIC || pp->ptr != oldp)) { |
| 197 | csound->DebugMsg(csound, " *** internal error: mrealloc() called with invalid " |
| 198 | "pointer (%p)\n", oldp); |
| 199 | /* exit() is ugly, but this is a fatal error that can only occur */ |
| 200 | /* as a result of a bug */ |
| 201 | exit(-1); |
| 202 | } |
| 203 | /* mark old header as invalid */ |
| 204 | pp->magic = 0; |
| 205 | pp->ptr = NULL; |
| 206 | #endif |
| 207 | /* allocate memory */ |
| 208 | p = realloc((void*) pp, ALLOC_BYTES(size)); |
| 209 | if (UNLIKELY(p == NULL)) { |
| 210 | #ifdef MEMDEBUG |
| 211 | CSOUND_MEM_SPINLOCK |
| 212 | /* alloc failed, restore original header */ |
| 213 | pp->magic = MEMALLOC_MAGIC; |
| 214 | pp->ptr = oldp; |
| 215 | CSOUND_MEM_SPINUNLOCK |
| 216 | #endif |
| 217 | memdie(csound, size); |
| 218 | return NULL; |
| 219 | } |
| 220 | CSOUND_MEM_SPINLOCK |
| 221 | /* create new header and update chain pointers */ |
| 222 | pp = (memAllocBlock_t*) p; |
| 223 | #ifdef MEMDEBUG |
| 224 | pp->magic = MEMALLOC_MAGIC; |
| 225 | pp->ptr = DATA_PTR(pp); |
| 226 | #endif |
| 227 | { |
| 228 | memAllocBlock_t *prv = pp->prv, *nxt = pp->nxt; |
| 229 | if (nxt != NULL) |
| 230 | nxt->prv = pp; |
| 231 | if (prv != NULL) |
| 232 | prv->nxt = pp; |
| 233 | else |
| 234 | MEMALLOC_DB = (void*) pp; |
| 235 | } |
| 236 | CSOUND_MEM_SPINUNLOCK |
| 237 | /* return with data pointer */ |
| 238 | return DATA_PTR(pp); |
| 239 | } |
| 240 |
no test coverage detected