Beispiel #1
0
void mhandle_free(void *ptr, char *at) {
  struct mhandle_item_t *item;
  void *eff_ptr;
  unsigned long eff_size;

  /* initialization */
  if (!ptr) return;
  mhandle_init();

  /* Read item */
  item = mhandle_hash_table_get(ptr);
  if (!item) {
    fprintf(stderr, "\n%s: free: invalid pointer %p\n", at, ptr);
    abort();
  }

  /* Check corruption */
  if (item->corrupt_info) {
    eff_ptr = ptr - MHANDLE_CORRUPT_RANGE;
    eff_size = item->size + MHANDLE_CORRUPT_TOTAL;
    mhandle_check_corrupt(eff_ptr, eff_size, item->at);
    memset(eff_ptr, 0, eff_size);
    free(eff_ptr);
  } else {
    memset(ptr, 0, item->size);
    free(ptr);
  }

  /* Remove pointer from data base */
  mhandle_hash_table_remove(ptr, at);
}
Beispiel #2
0
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;
}