/******************************************************************************\ Run some test to see if memory checking actually works. Note that code here is intentionally poor, so do not fix "bugs" here they are there for a reason. \******************************************************************************/ void C_test_mem_check(void) { char *ptr; int i; switch (c_mem_check.value.n) { case 0: case 1: return; case 2: C_debug("Normal operation, shouldn't fail"); ptr = C_malloc(1024); C_free(ptr); ptr = C_calloc(1024); C_realloc(ptr, 2048); C_realloc(ptr, 512); C_free(ptr); return; case 3: C_debug("Intentionally leaking memory"); ptr = C_malloc(1024); return; case 4: C_debug("Freeing unallocated memory"); C_free((void *)0x12345678); break; case 5: C_debug("Double freeing memory"); ptr = C_malloc(1024); C_free(ptr); C_free(ptr); break; case 6: C_debug("Simulating memory underrun"); ptr = C_malloc(1024); for (i = 0; i > -NO_MANS_LAND_SIZE / 2; i--) ptr[i] = 42; C_free(ptr); break; case 7: C_debug("Simulating memory overrun"); ptr = C_malloc(1024); for (i = 1024; i < 1024 + NO_MANS_LAND_SIZE / 2; i++) ptr[i] = 42; C_free(ptr); break; case 8: C_debug("Reallocating unallocated memory"); ptr = C_realloc((void *)0x12345678, 1024); break; case 9: C_debug("Intentionally leaking string"); ptr = C_malloc(1024); C_strncpy(ptr, "This string was leaked", 1024); return; default: C_error("Unknown memory check test %d", c_mem_check.value.n); } C_error("Memory check test %d failed", c_mem_check.value.n); }
// Allocate a list. Frees any previously allocated memory. virtual ListType *Calloc(CountType in_count, const char *name_string = NULL) { MLB::Utility::CheckCountTypeAndValue(in_count, name_string); if (!in_count) Free(); else { ListType *tmp_list; tmp_list = reinterpret_cast<ListType *>( C_calloc(in_count, sizeof(ListType), name_string)); the_count_ = in_count; the_list_ = tmp_list; } return(the_list_); }