main() { #else void memory_stress(void) { #endif size_t i; int idx; DEBUG_MSG( "Size of umm_heap is %d\n", sizeof(umm_heap) ); DEBUG_MSG( "Size of header is %d\n", sizeof(umm_heap[0]) ); DEBUG_MSG( "Size of nblock is %d\n", sizeof(umm_heap[0].header.used.next) ); DEBUG_MSG( "Size of pblock is %d\n", sizeof(umm_heap[0].header.used.prev) ); DEBUG_MSG( "Size of nfree is %d\n", sizeof(umm_heap[0].body.free.next) ); DEBUG_MSG( "Size of pfree is %d\n", sizeof(umm_heap[0].body.free.prev) ); //memset( umm_heap, 0, sizeof(umm_heap) ); umm_info( NULL, 1 ); for( idx=0; idx<256; ++idx ) ptr_array[idx] = (void *)NULL; //for( idx=0; idx<6553500; ++idx ) { for( idx=0; idx<65535; ++idx ) { i = rand()%256; switch( rand() % 16 ) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: ptr_array[i] = umm_realloc(ptr_array[i], 0); break; case 7: case 8: ptr_array[i] = umm_realloc(ptr_array[i], rand()%40 ); break; case 9: case 10: case 11: case 12: ptr_array[i] = umm_realloc(ptr_array[i], rand()%100 ); break; case 13: case 14: ptr_array[i] = umm_realloc(ptr_array[i], rand()%200 ); break; default: ptr_array[i] = umm_realloc(ptr_array[i], rand()%400 ); break; } } umm_info( NULL, 1 ); }
///////////////////////////////////////////////////////////////////////////// //! Memory allocation Informations ///////////////////////////////////////////////////////////////////////////// s32 TERMINAL_PrintMemoryInfo(void *_output_function) { //void (*out)(char *format, ...) = _output_function; // TODO: umm_info doesn't allow to define output function #if !defined(MIOS32_FAMILY_EMULATION) umm_info( NULL, 1 ); #endif return 0; // no error }
/* * Checks that the incrementally calculated free heap size is equal to the * actual free heap size (calculated by calling `umm_info()`) */ static void free_blocks_check(void) { umm_info(NULL, 0); { size_t actual = ummHeapInfo.freeBlocks; size_t calculated = umm_stat.free_blocks_cnt; if (actual != calculated) { fprintf(stderr, "free blocks mismatch: actual=%d, calculated=%d\n", (int) actual, (int) calculated); exit(1); } } { int actual = ummHeapInfo.freeEntries; int calculated = umm_free_entries_cnt(); if (actual != calculated) { fprintf(stderr, "free entries mismatch: actual=%d, calculated=%d\n", actual, calculated); exit(1); } } }
bool random_stress(void) { void *ptr_array[256]; size_t i; int idx; corruption_cnt = 0; printf("Size of umm_heap is %u\n", (unsigned int) sizeof(test_umm_heap)); umm_init(); umm_info(NULL, 1); for (idx = 0; idx < 256; ++idx) ptr_array[idx] = (void *) NULL; for (idx = 0; idx < 100000; ++idx) { i = rand() % 256; /* try to realloc some pointer to deliberately too large value */ { void *tmp = wrap_realloc(ptr_array[i], UMM_MALLOC_CFG__HEAP_SIZE); if (tmp != NULL) { printf("realloc to too large buffer should return NULL"); return false; } } switch (rand() % 16) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: { ptr_array[i] = wrap_realloc(ptr_array[i], 0); break; } case 7: case 8: { size_t size = rand() % 40; ptr_array[i] = wrap_realloc(ptr_array[i], size); memset(ptr_array[i], 0xfe, size); break; } case 9: case 10: case 11: case 12: { size_t size = rand() % 100; ptr_array[i] = wrap_realloc(ptr_array[i], size); memset(ptr_array[i], 0xfe, size); break; } case 13: case 14: { size_t size = rand() % 200; wrap_free(ptr_array[i]); ptr_array[i] = wrap_calloc(1, size); if (ptr_array[i] != NULL) { int a; for (a = 0; a < size; a++) { if (((char *) ptr_array[i])[a] != 0x00) { printf("calloc returned non-zeroed memory\n"); return false; } } } memset(ptr_array[i], 0xfe, size); break; } default: { size_t size = rand() % 400; wrap_free(ptr_array[i]); ptr_array[i] = wrap_malloc(size); memset(ptr_array[i], 0xfe, size); break; } } } return (corruption_cnt == 0); }