65 #undef DEBUG_TEST_APPHEAP 66 #undef DEBUG_TEST_STBHEAP 74 #ifdef STB_HEAP_PRINT_REQUIRED 75 #define STB_HEAP_PRINT(x) DEBUG_PRINTX_CONDITIONAL(DEBUG_STB_HEAP) x 78 #define STB_HEAP_PRINT(x) STB_SPDebugWrite x 80 #define STB_HEAP_PRINT(x) 85 #ifdef IGNORE_MEMCOPY_IN_SOURCEFILE 93 #define PGW_GOOD_MAGIC 0xdeadbeef 94 #define PGW_BAD_MAGIC 0xfeebdaed 95 #define LAST_FREED_MSK 0x7 108 #define HEAP_HDR_SIZE 16 109 #define HEAP_LIST_SIZE 10000 110 #define TEST_BUFF_SIZE 8 113 U8BIT *addr[HEAP_LIST_SIZE];
114 U32BIT size[HEAP_LIST_SIZE];
121 #ifdef DEBUG_HEAP_STATS 122 #define SIZEOF_SIZE_FIELD 4 127 BOOLEAN (*callback_function)(void);
133 typedef struct mem_info S_MEM_INFO;
152 static DEBUG_HEAP_STRUCT heap_list;
153 static DEBUG_HEAP_STRUCT app_list;
154 static U8BIT test_buffer[TEST_BUFF_SIZE] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
156 #ifdef DEBUG_HEAP_STATS 159 #ifdef DEBUG_APP_STATS 169 static S_MEM_INFO *sys_head = NULL;
170 static S_MEM_INFO *sys_tail = NULL;
171 static S_MEM_INFO *app_head = NULL;
172 static S_MEM_INFO *app_tail = NULL;
173 static void *pgw_mutex = NULL;
174 #elif defined(PGW_LAST_ON) 175 static S_MEM_INFO sys_last_alloc[LAST_FREED_MSK + 1];
176 static S_MEM_INFO app_last_alloc[LAST_FREED_MSK + 1];
177 static unsigned int sys_alloc_cnt = 0;
178 static unsigned int app_alloc_cnt = 0;
179 static S_MEM_INFO sys_last_freed[LAST_FREED_MSK + 1];
180 static S_MEM_INFO app_last_freed[LAST_FREED_MSK + 1];
181 static unsigned int sys_freed_cnt = 0;
182 static unsigned int app_freed_cnt = 0;
189 static void CheckHeapMemory(U8BIT *destn, U16BIT nbytes);
196 void STB_HeapInitialise(
void)
200 if (pgw_mutex == NULL)
203 ASSERT( pgw_mutex != NULL );
231 #ifndef DEBUG_HEAP_STATS 233 #ifdef DEBUG_HEAP_FULL 234 if (ret_addr == NULL)
236 STB_HEAP_PRINT((
"Heap Malloc FAILED"));
244 blk_size = bytes + SIZEOF_SIZE_FIELD;
246 if (blk_addr != NULL)
248 *(U32BIT *)blk_addr = blk_size;
249 ret_addr = blk_addr + SIZEOF_SIZE_FIELD;
252 heap_stats.total += blk_size;
253 if (heap_stats.total > heap_stats.maximum)
255 heap_stats.maximum = heap_stats.total;
262 STB_HEAP_PRINT((
"M(h): % 6d @ %p (num=% 5d tot=% 7d max=% 7d)", bytes, ret_addr, heap_stats.sum, heap_stats.total, heap_stats.maximum));
273 blk_size = (bytes + (TEST_BUFF_SIZE * 2));
278 memcpy((
void *)blk_ptr, (
void *)test_buffer, TEST_BUFF_SIZE);
279 memcpy((
void *)(blk_ptr + bytes + TEST_BUFF_SIZE), (
void *)test_buffer, TEST_BUFF_SIZE);
280 ret_addr = (
void *)(blk_ptr + TEST_BUFF_SIZE);
283 for (count = 0; count < HEAP_LIST_SIZE; count++)
285 if (heap_list.addr[count] == NULL)
287 heap_list.addr[count] = ret_addr;
288 heap_list.size[count] = bytes;
290 heap_list.total += (bytes + (TEST_BUFF_SIZE * 2));
291 if (heap_list.total > heap_list.max)
292 heap_list.max = heap_list.total;
294 #ifdef DEBUG_HEAP_STATS 295 STB_HEAP_PRINT((
"Malloc(heap) : %6lu @ %p - num=%3u tot=%6lu hi=%6lu",
296 bytes, ret_addr, heap_list.sum, heap_list.total, heap_list.max));
303 if (count >= HEAP_LIST_SIZE)
305 STB_HEAP_PRINT((
"Malloc(heap) : ERROR: list full"));
312 STB_HEAP_PRINT((
"Malloc(heap) : ERROR: refused %lu bytes", bytes));
318 ASSERT(ret_addr != NULL);
342 #ifndef DEBUG_HEAP_STATS 349 #if defined (STB_HEAP_PRINT_REQUIRED) || defined (STB_DEBUG) 353 blk_addr = (U8BIT *)addr - SIZEOF_SIZE_FIELD;
354 blk_size = *(U32BIT *)blk_addr;
355 #if defined (STB_HEAP_PRINT_REQUIRED) || defined (STB_DEBUG) 356 bytes = blk_size - SIZEOF_SIZE_FIELD;
361 if (heap_stats.sum > 0)
365 if (heap_stats.total > blk_size)
367 heap_stats.total -= blk_size;
371 heap_stats.total = 0;
373 STB_HEAP_PRINT((
"F(h): % 6d @ %p (num=% 5d tot=% 7d max=% 7d)", bytes, addr, heap_stats.sum, heap_stats.total, heap_stats.maximum));
385 for (count = 0; count < HEAP_LIST_SIZE; count++)
387 if (heap_list.addr[count] == addr)
390 blk_ptr = ((U8BIT *)addr - TEST_BUFF_SIZE);
393 if ((memcmp((
void *)blk_ptr, (
void *)test_buffer, TEST_BUFF_SIZE) != 0) ||
394 (memcmp((
void *)(blk_ptr + heap_list.size[count] + TEST_BUFF_SIZE), (
void *)test_buffer, TEST_BUFF_SIZE) != 0))
397 STB_HEAP_PRINT((
"Free(heap) : ERROR: this block corrupt..."));
402 heap_list.total -= (heap_list.size[count] + (TEST_BUFF_SIZE * 2));
404 #ifdef DEBUG_HEAP_STATS 405 STB_HEAP_PRINT((
"Free(heap) : %6lu @ %p - num=%3u tot=%6lu hi=%6lu",
406 heap_list.size[count], addr, heap_list.sum, heap_list.total,
411 heap_list.addr[count] = NULL;
416 if (count >= HEAP_LIST_SIZE)
419 STB_HEAP_PRINT((
"Free(heap) : ERROR: unallocated pointer: %p", addr));
426 STB_HEAP_PRINT((
"Free(heap) : ERROR: NULL pointer"));
438 static void* DbgResizeMemory(
void *ptr, U32BIT new_num_bytes, DEBUG_HEAP_STRUCT *hp_ptr )
444 for (count = 0; count != HEAP_LIST_SIZE; count++)
446 if (hp_ptr->addr[count] == ptr)
449 blk_ptr = ((U8BIT *)ptr - TEST_BUFF_SIZE);
452 if ((memcmp((
void *)blk_ptr, (
void *)test_buffer, TEST_BUFF_SIZE) != 0) ||
453 (memcmp((
void *)(blk_ptr + hp_ptr->size[count] + TEST_BUFF_SIZE), (
void *)test_buffer, TEST_BUFF_SIZE) != 0))
456 STB_HEAP_PRINT((
"Realloc(heap) : ERROR: this block corrupt.sz=%d, new_sz=%d", hp_ptr->size[count], new_num_bytes));
459 if (new_num_bytes >= hp_ptr->size[count])
461 hp_ptr->total += new_num_bytes - hp_ptr->size[count];
462 if (hp_ptr->total > hp_ptr->max)
463 hp_ptr->max = hp_ptr->total;
467 hp_ptr->total -= hp_ptr->size[count] - new_num_bytes;
470 #ifdef DEBUG_HEAP_STATS 471 STB_HEAP_PRINT((
"Realloc(heap) : %6lu @ %p - num=%3u tot=%6lu hi=%6lu",
472 hp_ptr->size[count], addr, hp_ptr->sum, hp_ptr->total,
479 if (memcmp((
void *)blk_ptr, (
void *)test_buffer, TEST_BUFF_SIZE) != 0)
481 STB_HEAP_PRINT((
"Realloc(heap) : ERROR: Lost header : %p", blk_ptr));
484 memcpy((
void *)(blk_ptr + new_num_bytes + TEST_BUFF_SIZE), (
void *)test_buffer, TEST_BUFF_SIZE);
485 hp_ptr->size[count] = new_num_bytes;
486 blk_ptr += TEST_BUFF_SIZE;
488 hp_ptr->addr[count] = blk_ptr;
493 if (count == HEAP_LIST_SIZE)
496 STB_HEAP_PRINT((
"Realloc(heap) : ERROR: unallocated pointer: %p", ptr));
504 #ifdef DEBUG_HEAP_STATS 505 static void* StatResizeMemory(
void *ptr, U32BIT new_num_bytes,
S_STAT_MEM *stat_ptr )
509 blk_ptr = (U8BIT *)ptr - SIZEOF_SIZE_FIELD;
510 blk_size = *(U32BIT *)blk_ptr;
511 blk_size -= SIZEOF_SIZE_FIELD;
512 if (blk_size < new_num_bytes)
514 stat_ptr->total += new_num_bytes - blk_size;
515 if (stat_ptr->total > stat_ptr->maximum)
516 stat_ptr->maximum = stat_ptr->total;
520 if (stat_ptr->total > (blk_size - new_num_bytes))
522 stat_ptr->total -= blk_size - new_num_bytes;
530 *(U32BIT *)blk_ptr = new_num_bytes + SIZEOF_SIZE_FIELD;
531 blk_ptr += SIZEOF_SIZE_FIELD;
558 if (new_num_bytes == 0)
574 #ifndef DEBUG_HEAP_STATS 577 retval = StatResizeMemory(ptr, new_num_bytes, &heap_stats );
580 retval = DbgResizeMemory(ptr, new_num_bytes, &heap_list);
597 static BOOLEAN AppFreeCachedMemory(
void)
605 if (cache_marker_ptr != NULL)
608 ptr = cache_marker_ptr;
615 retval = (*ptr->callback_function)();
622 ptr = cache_registration_ptr;
630 cache_marker_ptr = ptr;
633 while (ptr != cache_marker_ptr);
661 #ifndef DEBUG_APP_STATS 663 #ifdef DEBUG_APP_FULL 664 if (ret_addr == NULL)
666 STB_HEAP_PRINT((
"APP Heap Malloc FAILED"));
674 blk_size = bytes + SIZEOF_SIZE_FIELD;
676 if (blk_addr != NULL)
678 *(U32BIT *)blk_addr = blk_size;
679 ret_addr = blk_addr + SIZEOF_SIZE_FIELD;
682 app_stats.total += blk_size;
683 if (app_stats.total > app_stats.maximum)
685 app_stats.maximum = app_stats.total;
695 while ((ret_addr == NULL) && (bytes > 0))
697 if (AppFreeCachedMemory() == FALSE)
711 blk_size = (bytes + (TEST_BUFF_SIZE * 2));
713 while ((blk_ptr == NULL) && (bytes > 0))
715 if (AppFreeCachedMemory() == FALSE)
724 memcpy((
void *)blk_ptr, (
void *)test_buffer, TEST_BUFF_SIZE);
725 memcpy((
void *)(blk_ptr + bytes + TEST_BUFF_SIZE), (
void *)test_buffer, TEST_BUFF_SIZE);
726 ret_addr = (
void *)(blk_ptr + TEST_BUFF_SIZE);
729 for (count = 0; count < HEAP_LIST_SIZE; count++)
731 if (app_list.addr[count] == NULL)
733 app_list.addr[count] = ret_addr;
734 app_list.size[count] = bytes;
736 app_list.total += (bytes + (TEST_BUFF_SIZE * 2));
737 if (app_list.total > app_list.max)
738 app_list.max = app_list.total;
740 #ifdef DEBUG_APP_STATS 741 STB_HEAP_PRINT((
"Malloc(app) : %6lu @ %p - num=%3u tot=%6lu hi=%6lu",
742 bytes, ret_addr, app_list.sum, app_list.total, app_list.max));
748 if (count >= HEAP_LIST_SIZE)
750 STB_HEAP_PRINT((
"Malloc(app) : ERROR: list full"));
757 STB_HEAP_PRINT((
"Malloc(app) : ERROR: refused %lu bytes", bytes));
763 ASSERT(ret_addr != NULL);
787 #ifndef DEBUG_APP_STATS 796 blk_addr = (U8BIT *)addr - SIZEOF_SIZE_FIELD;
797 blk_size = *(U32BIT *)blk_addr;
802 if (app_stats.sum > 0)
806 if (app_stats.total > blk_size)
808 app_stats.total -= blk_size;
826 for (count = 0; count < HEAP_LIST_SIZE; count++)
828 if (app_list.addr[count] == addr)
831 blk_ptr = ((U8BIT *)addr - TEST_BUFF_SIZE);
834 if ((memcmp((
void *)blk_ptr, (
void *)test_buffer, TEST_BUFF_SIZE) != 0) ||
835 (memcmp((
void *)(blk_ptr + app_list.size[count] + TEST_BUFF_SIZE), (
void *)test_buffer, TEST_BUFF_SIZE) != 0))
838 STB_HEAP_PRINT((
"Free(app) : ERROR: this block corrupt..."));
843 app_list.total -= (app_list.size[count] + (TEST_BUFF_SIZE * 2));
845 #ifdef DEBUG_APP_STATS 846 STB_HEAP_PRINT((
"Free(app) : %6lu @ %p - num=%3u tot=%6lu hi=%6lu",
847 app_list.size[count], addr, app_list.sum, app_list.total,
852 app_list.addr[count] = NULL;
857 if (count >= HEAP_LIST_SIZE)
860 STB_HEAP_PRINT((
"Free(app) : ERROR: unallocated pointer: %p", addr));
867 STB_HEAP_PRINT((
"Free(app) : ERROR: NULL pointer"));
899 if (new_num_bytes == 0)
915 #ifndef DEBUG_APP_STATS 918 retval = StatResizeMemory(ptr, new_num_bytes, &app_stats );
921 retval = DbgResizeMemory(ptr, new_num_bytes, &app_list);
931 void STB_AppRegisterCacheFreeFunction(BOOLEAN (*callback_function)(
void))
935 FUNCTION_START(STB_AppRegisterCacheFreeFunction);
937 if (callback_function != NULL)
941 ptr = cache_registration_ptr;
945 if (ptr->callback_function == callback_function)
963 ptr->callback_function = callback_function;
964 ptr->next_ptr = cache_registration_ptr;
966 cache_registration_ptr = ptr;
969 if (cache_marker_ptr == NULL)
971 cache_marker_ptr = cache_registration_ptr;
977 FUNCTION_FINISH(STB_AppRegisterCacheFreeFunction);
994 void* STB_GetMemoryPGW(U32BIT size,
char *filename,
int lineno)
996 S_MEM_INFO *mem_info;
1001 mem_info = (S_MEM_INFO *)
STB_MEMGetSysRAM(
sizeof(S_MEM_INFO) + size -
sizeof(
unsigned long));
1002 if (mem_info != NULL)
1004 mem_info->magic = PGW_GOOD_MAGIC;
1005 mem_info->size = size;
1006 mem_info->filename = filename;
1007 mem_info->lineno = lineno;
1010 ASSERT( pgw_mutex != NULL );
1011 mem_info->next = NULL;
1013 mem_info->prev = sys_tail;
1014 if (sys_tail != NULL)
1016 sys_tail->next = mem_info;
1018 sys_tail = mem_info;
1019 if (sys_head == NULL)
1021 sys_head = sys_tail;
1024 #elif defined(PGW_LAST_ON) 1025 sys_last_alloc[sys_alloc_cnt & LAST_FREED_MSK] = *mem_info;
1029 buffer = &mem_info->align;
1033 STB_HEAP_PRINT((
"PGW: Cannot allocate %d bytes at %s:%d\n", size,
1050 void* STB_ResizeMemoryPGW(
void *buffer, U32BIT size,
char *filename,
int lineno)
1052 S_MEM_INFO *mem_info;
1060 STB_FreeMemoryPGW(buffer, filename, lineno);
1064 else if (buffer == NULL)
1066 buffer = STB_GetMemoryPGW(size, filename, lineno);
1070 mem_info = (S_MEM_INFO *)((
char *)buffer - offsetof(S_MEM_INFO, align));
1071 if (mem_info->magic != PGW_GOOD_MAGIC)
1073 STB_HEAP_PRINT((
"PGW: Reallocating invalid sys pointer %p at %s:%d\n", buffer,
1075 STB_MemoryDumpPGW(0);
1079 mem_info->magic = PGW_BAD_MAGIC;
1083 ASSERT( pgw_mutex != NULL );
1085 if (sys_tail == mem_info)
1087 sys_tail = sys_tail->prev;
1089 if (sys_head == mem_info)
1091 sys_head = sys_head->next;
1093 if (mem_info->next != NULL)
1095 mem_info->next->prev = mem_info->prev;
1098 if (mem_info->prev != NULL)
1100 mem_info->prev->next = mem_info->next;
1106 mem_info =
STB_MEMResizeSysRAM(mem_info,
sizeof(S_MEM_INFO) + size -
sizeof(
unsigned long));
1108 mem_info->magic = PGW_GOOD_MAGIC;
1109 mem_info->size = size;
1110 mem_info->filename = filename;
1111 mem_info->lineno = lineno;
1114 mem_info->next = NULL;
1116 mem_info->prev = sys_tail;
1117 if (sys_tail != NULL)
1119 sys_tail->next = mem_info;
1121 sys_tail = mem_info;
1122 if (sys_head == NULL)
1124 sys_head = sys_tail;
1127 #elif defined(PGW_LAST_ON) 1128 sys_last_alloc[sys_alloc_cnt & LAST_FREED_MSK] = *mem_info;
1132 buffer = &mem_info->align;
1146 void STB_FreeMemoryPGW(
void *buffer,
char *filename,
int lineno)
1148 S_MEM_INFO *mem_info;
1154 mem_info = (S_MEM_INFO *)((
char *)buffer - offsetof(S_MEM_INFO, align));
1155 if (mem_info->magic != PGW_GOOD_MAGIC)
1157 STB_HEAP_PRINT((
"PGW: Freeing invalid pointer %p at %s:%d\n", buffer,
1159 STB_MemoryDumpPGW(0);
1164 mem_info->magic = PGW_BAD_MAGIC;
1166 ASSERT( pgw_mutex != NULL );
1168 if (sys_tail == mem_info)
1170 sys_tail = sys_tail->prev;
1172 if (sys_head == mem_info)
1174 sys_head = sys_head->next;
1176 if (mem_info->next != NULL)
1178 mem_info->next->prev = mem_info->prev;
1181 if (mem_info->prev != NULL)
1183 mem_info->prev->next = mem_info->next;
1186 #elif defined(PGW_LAST_ON) 1187 sys_last_freed[sys_freed_cnt & LAST_FREED_MSK] = *mem_info;
1189 mem_info->magic = PGW_BAD_MAGIC;
1206 void* STB_AppGetMemoryPGW(U32BIT size,
char *filename,
int lineno)
1208 S_MEM_INFO *mem_info;
1209 void *buffer = NULL;
1215 mem_info = (S_MEM_INFO *)
STB_MEMGetAppRAM(
sizeof(S_MEM_INFO) + size -
sizeof(
unsigned long));
1216 while (mem_info == NULL && AppFreeCachedMemory())
1218 mem_info = (S_MEM_INFO *)
STB_MEMGetAppRAM(
sizeof(S_MEM_INFO) + size -
sizeof(
unsigned long));
1220 if (mem_info != NULL)
1222 mem_info->magic = PGW_GOOD_MAGIC;
1223 mem_info->size = size;
1224 mem_info->filename = filename;
1225 mem_info->lineno = lineno;
1228 ASSERT( pgw_mutex != NULL );
1229 mem_info->next = NULL;
1231 mem_info->prev = app_tail;
1232 if (app_tail != NULL)
1234 app_tail->next = mem_info;
1236 app_tail = mem_info;
1237 if (app_head == NULL)
1239 app_head = app_tail;
1242 #elif defined(PGW_LAST_ON) 1243 app_last_alloc[app_alloc_cnt & LAST_FREED_MSK] = *mem_info;
1247 buffer = &mem_info->align;
1251 STB_HEAP_PRINT((
"PGW: Cannot allocate %d bytes at %s:%d\n", size,
1268 void* STB_AppResizeMemoryPGW(
void *buffer, U32BIT size,
char *filename,
int lineno)
1270 S_MEM_INFO *mem_info;
1279 STB_AppFreeMemoryPGW(buffer, filename, lineno);
1283 else if (buffer == NULL)
1285 buffer = STB_AppGetMemoryPGW(size, filename, lineno);
1289 mem_info = (S_MEM_INFO *)((
char *)buffer - offsetof(S_MEM_INFO, align));
1290 if (mem_info->magic != PGW_GOOD_MAGIC)
1292 STB_HEAP_PRINT((
"PGW: Reallocating invalid app pointer %p at %s:%d\n", buffer,
1294 STB_MemoryDumpPGW(0);
1298 mem_info->magic = PGW_BAD_MAGIC;
1302 ASSERT( pgw_mutex != NULL );
1304 if (app_tail == mem_info)
1306 app_tail = app_tail->prev;
1308 if (app_head == mem_info)
1310 app_head = app_head->next;
1312 if (mem_info->next != NULL)
1314 mem_info->next->prev = mem_info->prev;
1317 if (mem_info->prev != NULL)
1319 mem_info->prev->next = mem_info->next;
1325 mem_info =
STB_MEMResizeAppRAM(mem_info,
sizeof(S_MEM_INFO) + size -
sizeof(
unsigned long));
1327 mem_info->magic = PGW_GOOD_MAGIC;
1328 mem_info->size = size;
1329 mem_info->filename = filename;
1330 mem_info->lineno = lineno;
1333 mem_info->next = NULL;
1335 mem_info->prev = app_tail;
1336 if (app_tail != NULL)
1338 app_tail->next = mem_info;
1340 app_tail = mem_info;
1341 if (app_head == NULL)
1343 app_head = app_tail;
1346 #elif defined(PGW_LAST_ON) 1347 app_last_alloc[app_alloc_cnt & LAST_FREED_MSK] = *mem_info;
1351 buffer = &mem_info->align;
1365 void STB_AppFreeMemoryPGW(
void *buffer,
char *filename,
int lineno)
1367 S_MEM_INFO *mem_info;
1373 mem_info = (S_MEM_INFO *)((
char *)buffer - offsetof(S_MEM_INFO, align));
1374 if (mem_info->magic != PGW_GOOD_MAGIC)
1376 STB_HEAP_PRINT((
"PGW: Freeing invalid pointer %p at %s:%d\n", buffer,
1378 STB_MemoryDumpPGW(0);
1383 mem_info->magic = PGW_BAD_MAGIC;
1385 ASSERT( pgw_mutex != NULL );
1387 if (app_tail == mem_info)
1389 app_tail = app_tail->prev;
1391 if (app_head == mem_info)
1393 app_head = app_head->next;
1395 if (mem_info->next != NULL)
1397 mem_info->next->prev = mem_info->prev;
1400 if (mem_info->prev != NULL)
1402 mem_info->prev->next = mem_info->next;
1405 #elif defined(PGW_LAST_ON) 1406 app_last_freed[app_freed_cnt & LAST_FREED_MSK] = *mem_info;
1408 mem_info->magic = PGW_BAD_MAGIC;
1419 static void DumpLast(
const char *str, S_MEM_INFO *info,
unsigned int cnt )
1421 S_MEM_INFO *mem_info;
1423 STB_HEAP_PRINT((
"%s Count=%d", str, cnt));
1424 if (cnt > (LAST_FREED_MSK + 1))
1426 c = cnt - (LAST_FREED_MSK + 1);
1435 mem_info = &info[ cnt & LAST_FREED_MSK ];
1436 STB_HEAP_PRINT((
"%s:%d\t%d bytes \tmgc=0x%x", mem_info->filename, mem_info->lineno, mem_info->size, mem_info->magic));
1449 void STB_MemoryDumpPGW(BOOLEAN full)
1451 S_MEM_INFO *mem_info;
1454 FUNCTION_START(STB_MemoryDumpPGW);
1458 ASSERT( pgw_mutex != NULL );
1461 STB_HEAP_PRINT((
"*** SYSTEM MEMORY DUMP BEGIN ***\n"));
1462 for (mem_info = sys_head; mem_info != NULL; mem_info = mem_info->next)
1466 STB_HEAP_PRINT((
"%s:%d\t%d bytes", mem_info->filename, mem_info->lineno, mem_info->size));
1468 total += mem_info->size;
1470 STB_HEAP_PRINT((
"TOTAL\t%d bytes\n", total));
1472 STB_HEAP_PRINT((
"*** APP MEMORY DUMP BEGIN ***\n"));
1473 for (mem_info = app_head; mem_info != NULL; mem_info = mem_info->next)
1477 STB_HEAP_PRINT((
"%s:%d\t%d bytes", mem_info->filename, mem_info->lineno, mem_info->size));
1479 total += mem_info->size;
1481 STB_HEAP_PRINT((
"TOTAL\t%d bytes\n", total));
1482 STB_HEAP_PRINT((
"*** MEMORY DUMP END ***\n"));
1487 USE_UNWANTED_PARAM(full);
1488 USE_UNWANTED_PARAM(total);
1489 USE_UNWANTED_PARAM(mem_info);
1492 DumpLast(
"SYS ALLOC", sys_last_alloc, sys_alloc_cnt );
1493 DumpLast(
"SYS FREED", sys_last_freed, sys_freed_cnt );
1494 DumpLast(
"APP ALLOC", app_last_alloc, app_alloc_cnt );
1495 DumpLast(
"APP FREED", app_last_freed, app_freed_cnt );
1500 FUNCTION_FINISH(STB_MemoryDumpPGW);
1511 #ifdef DEBUG_MEMCOPY 1512 static void CheckHeapMemory(U8BIT *destn, U16BIT nbytes)
1520 for (sum = 0, count = 0; ((count < HEAP_LIST_SIZE) && (sum < app_list.sum)); count++)
1522 if (*(app_list.addr + count) != NULL)
1525 blk_start = *(app_list.addr + count) - TEST_BUFF_SIZE;
1526 blk_end = *(app_list.addr + count) + (*(app_list.size + count));
1528 if (destn <= (blk_end + TEST_BUFF_SIZE))
1530 if ((destn + nbytes) >= blk_start)
1532 if (destn < (blk_start + TEST_BUFF_SIZE))
1534 STB_HEAP_PRINT((
"MemCopy : ERROR: App Heap Corruption"));
1536 else if ((destn + nbytes) > blk_end)
1538 STB_HEAP_PRINT((
"MemCopy : ERROR: App Heap Corruption"));
1546 for (sum = 0, count = 0; ((count < HEAP_LIST_SIZE) && (sum < heap_list.sum)); count++)
1548 if (*(heap_list.addr + count) != NULL)
1551 blk_start = *(heap_list.addr + count) - TEST_BUFF_SIZE;
1552 blk_end = *(heap_list.addr + count) + (*(heap_list.size + count));
1554 if (destn <= (blk_end + TEST_BUFF_SIZE))
1556 if ((destn + nbytes) >= blk_start)
1558 if (destn < (blk_start + TEST_BUFF_SIZE))
1560 STB_HEAP_PRINT((
"MemCopy : ERROR: Stb Heap Corruption"));
1562 else if ((destn + nbytes) > blk_end)
1564 STB_HEAP_PRINT((
"MemCopy : ERROR: Stb Heap Corruption"));
1572 #endif //#ifdef DEBUG_MEMCOPY 1575 #ifdef DEBUG_TEST_APPHEAP 1576 void STB_CheckAppHeap(
void)
1584 for (sum = 0, count = 0; ((count < HEAP_LIST_SIZE) && (sum < app_list.sum)); count++)
1586 if (*(app_list.addr + count) != NULL)
1589 blk_start = *(app_list.addr + count) - TEST_BUFF_SIZE;
1590 blk_end = *(app_list.addr + count) + (*(app_list.size + count));
1592 if ((memcmp((
void *)blk_start, (
void *)test_buffer, TEST_BUFF_SIZE) != 0) ||
1593 (memcmp((
void *)blk_end, (
void *)test_buffer, TEST_BUFF_SIZE) != 0))
1595 STB_HEAP_PRINT((
"HeapTest(App) : ERROR: Corrupt Block: start = %p end = %p", blk_start, blk_end));
1604 #ifdef DEBUG_TEST_STBHEAP 1605 void STB_CheckStbHeap(
void)
1613 for (sum = 0, count = 0; ((count < HEAP_LIST_SIZE) && (sum < heap_list.sum)); count++)
1615 if (*(heap_list.addr + count) != NULL)
1618 blk_start = *(heap_list.addr + count) - TEST_BUFF_SIZE;
1619 blk_end = *(heap_list.addr + count) + (*(heap_list.size + count));
1621 if ((memcmp((
void *)blk_start, (
void *)test_buffer, TEST_BUFF_SIZE) != 0) ||
1622 (memcmp((
void *)blk_end, (
void *)test_buffer, TEST_BUFF_SIZE) != 0))
1624 STB_HEAP_PRINT((
"HeapTest(Stb) : ERROR: Corrupt Block: start = %p end = %p", blk_start, blk_end));
1642 #ifdef DEBUG_MEMCOPY 1643 void* STB_MemCopy(
void *s1,
const void *s2, U16BIT n)
1645 CheckHeapMemory((U8BIT *)s1, n);
1650 return memcpy(s1, s2, n);
1653 void* STB_MemMove(
void *s1,
const void *s2, U16BIT n)
1655 CheckHeapMemory((U8BIT *)s1, n);
1658 return memmove(s1, s2, n);
1661 void* STB_MemSet(
void *s,
int c, U16BIT n)
1663 CheckHeapMemory((U8BIT *)s, n);
1666 return memset(s, c, n);
1669 char* STB_StrCopy(
char *s1,
const char *s2)
1671 CheckHeapMemory((U8BIT *)s1, (U16BIT)strlen(s2));
1674 return strcpy(s1, s2);
1677 char* STB_StrNCopy(
char *s1,
const char *s2, U16BIT n)
1679 CheckHeapMemory((U8BIT *)s1, n);
1682 return strncpy(s1, s2, n);
1685 char* STB_StrCat(
char *s1,
const char *s2)
1687 CheckHeapMemory((U8BIT *)s1 + strlen(s1), strlen(s2) + 1);
1690 return strcat(s1, s2);
1693 char* STB_StrNCat(
char *s1,
const char *s2, U16BIT n)
1696 #define min_number(a, b) (((a) < (b)) ? (a) : (b)) 1697 CheckHeapMemory((U8BIT *)s1 + strlen(s1), min_number(n, strlen(s2)) + 1);
1700 return strncat(s1, s2, n);
1704 int STB_VSPrintF(
char *s1,
const char *fmt, va_list va)
1708 count = vsprintf(s1, fmt, va);
1711 CheckHeapMemory((U8BIT *)s1, (U16BIT)(count + 1));
1715 int STB_SPrintF(
char *s1,
const char *fmt, ...)
1720 va_start(va_ptr, fmt);
1721 count = vsprintf(s1, fmt, va_ptr);
1725 CheckHeapMemory((U8BIT *)s1, (U16BIT)(count + 1));
1729 int STB_SNPrintF(
char *s1, U16BIT n,
const char *fmt, ...)
1735 CheckHeapMemory((U8BIT *)s1, n);
1739 va_start(va_ptr, fmt);
1740 count = vsprintf(tmpbuff, fmt, va_ptr);
1753 memcpy(s1, tmpbuff, count);
1754 *(s1 + count) =
'\0';
1761 #endif //#ifdef DEBUG_MEMCOPY 1763 #ifdef DEBUG_HEAP_STATS 1764 #ifdef DEBUG_APP_STATS 1765 void STB_GetHeapStats(U32BIT *total_app, U32BIT *max_app, U32BIT *num_app,
1766 U32BIT *total_mem, U32BIT *max_mem, U32BIT *num_mem)
1768 *total_app = app_stats.total;
1769 *max_app = app_stats.maximum;
1770 *num_app = app_stats.sum;
1772 *total_mem = heap_stats.total;
1773 *max_mem = heap_stats.maximum;
1774 *num_mem = heap_stats.sum;
void * STB_MEMResizeAppRAM(void *ptr, U32BIT new_num_bytes)
Changes the size of the given block of memory ensuring data contained within the original memory bloc...
void STB_FreeMemory(void *addr)
Releases previously allocated heap memory.
void * STB_GetMemory(U32BIT bytes)
Attempts to allocate memory from the heap.
void STB_OSMutexUnlock(void *mutex)
Unlock a mutex (a.k.a. 'leave', 'signal' or 'release')
void * STB_MEMGetSysRAM(U32BIT bytes)
Allocates a new block of memory for system use.
Debug functions header file.
void STB_OSMutexLock(void *mutex)
Lock a mutex (a.k.a. 'enter', 'wait' or 'get').
void * STB_MEMGetAppRAM(U32BIT bytes)
Allocates a new block of memory for application use.
void STB_MEMFreeSysRAM(void *block)
Releases a previously allocated block of system memory.
void * STB_AppGetMemory(U32BIT bytes)
Attempts to allocate memory from the application heap.
Header file - Function prototypes for NVM and Heap.
System Wide Global Technical Data Type Definitions.
void * STB_MEMResizeSysRAM(void *ptr, U32BIT new_num_bytes)
Changes the size of the given block of memory ensuring data contained within the original memory bloc...
Header file - Function prototypes for heap memory.
void * STB_OSCreateMutex(void)
Create a mutex.
void STB_MEMFreeAppRAM(void *block)
Releases a previously allocated block of system memory.
void * STB_ResizeMemory(void *ptr, U32BIT new_num_bytes)
Re-allocates a given memory area to the new size, ensuring data contained within the original memory ...
void STB_AppFreeMemory(void *addr)
Releases previously allocated application heap memory.
void * STB_AppResizeMemory(void *ptr, U32BIT new_num_bytes)
Re-allocates a given memory area to the new size, ensuring data contained within the original memory ...