char *mhandle_strdup(const char *s, char *at) { char *ptr; void *eff_ptr; unsigned long size = strlen(s) + 1; unsigned long eff_size; mhandle_init(); /* Allocate */ size = strlen(s) + 1; eff_size = size + MHANDLE_CORRUPT_TOTAL; eff_ptr = malloc(eff_size); if (!eff_ptr) mhandle_out_of_memory(at); /* Copy string and mark corruption */ ptr = eff_ptr + MHANDLE_CORRUPT_RANGE; memcpy(ptr, s, size); mhandle_mark_corrupt(eff_ptr, eff_size); /* Record pointer and return */ mhandle_hash_table_insert(ptr, size, at, 1); return ptr; }
void *mhandle_calloc(unsigned long nmemb, unsigned long size, char *at) { void *ptr; void *eff_ptr; unsigned long total; unsigned long eff_total; mhandle_init(); /* Effective size */ total = nmemb * size; eff_total = total + MHANDLE_CORRUPT_TOTAL; /* Allocate */ eff_ptr = calloc(1, eff_total); if (!eff_ptr) mhandle_out_of_memory(at); /* Mark corruption */ ptr = eff_ptr + MHANDLE_CORRUPT_RANGE; mhandle_mark_corrupt(eff_ptr, eff_total); /* Record pointer and return */ mhandle_hash_table_insert(ptr, total, at, 1); return ptr; }
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_malloc(unsigned long size, char *at) { void *ptr; void *eff_ptr; unsigned long eff_size; mhandle_init(); /* Allocate */ eff_size = size + MHANDLE_CORRUPT_TOTAL; eff_ptr = malloc(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; }