void *mhandle_realloc(void *ptr, unsigned long size, char *at) { void *eff_ptr; unsigned long eff_size; struct mhandle_item_t *item; /* Equivalent to malloc for NULL pointer */ if (!ptr) return mhandle_malloc(size, at); /* Equivalent to free for size zero */ if (!size) { mhandle_free(ptr, at); return NULL; } /* Reallocate */ mhandle_init(); /* Search pointer */ item = mhandle_hash_table_get(ptr); if (!item) { fprintf(stderr, "\n%s: realloc: invalid pointer %p\n", at, ptr); abort(); } /* Reallocation not supported for pointers registered externally, i.e., * those that don't have corruption information. */ if (!item->corrupt_info) { fprintf(stderr, "\n%s: realloc: not supported for pointers not allocated\n" "\twith malloc/calloc/realloc/strdup (%p)", at, ptr); abort(); } /* Effective sizes */ eff_ptr = ptr - MHANDLE_CORRUPT_RANGE; eff_size = item->size + MHANDLE_CORRUPT_TOTAL; /* Check corruption and remove old pointer */ mhandle_check_corrupt(eff_ptr, eff_size, at); mhandle_hash_table_remove(ptr, at); /* Reallocate */ eff_size = size + MHANDLE_CORRUPT_TOTAL; eff_ptr = realloc(eff_ptr, eff_size); if (!eff_ptr) mhandle_out_of_memory(at); /* Mark corruption */ ptr = eff_ptr + MHANDLE_CORRUPT_RANGE; mhandle_mark_corrupt(eff_ptr, eff_size); /* Record pointer and return */ mhandle_hash_table_insert(ptr, size, at, 1); return ptr; }
void *mhandle_realloc(void *ptr, unsigned long size, char *at) { /* equivalent to malloc or free*/ if (!ptr) return mhandle_malloc(size, at); if (!size) { mhandle_free(ptr, at); return NULL; } /* realloc */ initialize(); ptr -= CORRUPT_RANGE; ht_remove(ptr, at); ptr = realloc(ptr, size + CORRUPT_TOTAL); if (!ptr) outofmem(at); mark_corruption(ptr, size); ht_insert(ptr, size, at); return ptr + CORRUPT_RANGE; }