1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
|
/*
* CUnit - A Unit testing framework library for C.
* Copyright (C) 2001 Anil Kumar
* Copyright (C) 2004,2005,2006 Anil Kumar, Jerry St.Clair
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Memory management functions used throughout CUnit.
*
* 13/Oct/2001 Moved some of the generic functions definitions from other
* files to this one so as to use the functions consitently.
* This file is not included in the distribution headers because
* it is used internally by CUnit. (AK)
*
* 18-Jul-2004 New interface, doxygen comments, made local functions &
* constants static, fixed reporting of memory tracking (valid
* vs invalid cycles), restructured memory tracking to detect
* reallocations & multiple deletions. (JDS)
*
* 24-Apr-2005 Changed type of allocated sizes to size_t to avoid
* signed-unsigned mismatch. (JDS)
*/
/** @file
* Memory management & reporting functions (implementation).
*/
/** @addtogroup Framework
@{
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "CUnit.h"
#include "MyMem.h"
#ifdef MEMTRACE
#define MAX_FILE_NAME_LENGTH 256
/** Default name for memory dump file. */
static const char* f_szDefaultDumpFileName = "CUnit-Memory-Dump.xml";
/**< Default name for memory dump file. */
#ifdef CUNIT_BUILD_TESTS
/** For testing use (only) to simulate memory exhaustion -
* if CU_FALSE, allocation requests will always fail and return NULL.
*/
static CU_BOOL f_bTestCunitMallocActive = CU_TRUE;
#endif
/** Structure holding the details of a memory allocation/deallocation event. */
typedef struct mem_event {
size_t Size;
unsigned int AllocLine;
char AllocFilename[MAX_FILE_NAME_LENGTH];
unsigned int DeallocLine;
char DeallocFilename[MAX_FILE_NAME_LENGTH];
struct mem_event* pNext;
} MEMORY_EVENT;
typedef MEMORY_EVENT* PMEMORY_EVENT;
#define NOT_ALLOCATED 0
#define NOT_DELETED 0
/** Structure holding the details of a memory node having allocation/deallocation events. */
typedef struct mem_node {
void* pLocation;
unsigned int EventCount;
PMEMORY_EVENT pFirstEvent;
struct mem_node* pNext;
} MEMORY_NODE;
typedef MEMORY_NODE* PMEMORY_NODE;
static PMEMORY_NODE f_pMemoryTrackerHead = NULL; /**< Head of double-linked list of memory nodes. */
static unsigned int f_nMemoryNodes = 0; /**< Counter for memory nodes created. */
/*------------------------------------------------------------------------*/
/** Locate the memory node for the specified memory location (returns NULL if none). */
static PMEMORY_NODE find_memory_node(void* pLocation)
{
PMEMORY_NODE pMemoryNode = f_pMemoryTrackerHead;
while (NULL != pMemoryNode) {
if (pLocation == pMemoryNode->pLocation) {
break;
}
pMemoryNode = pMemoryNode->pNext;
}
return pMemoryNode;
}
/*------------------------------------------------------------------------*/
/** Create a new memory node for the specified memory location. */
static PMEMORY_NODE create_memory_node(void* pLocation)
{
PMEMORY_NODE pTempNode = NULL;
PMEMORY_NODE pMemoryNode = find_memory_node(pLocation);
/* a memory node for pLocation should not exist yet */
if (NULL == pMemoryNode) {
pMemoryNode = (PMEMORY_NODE)malloc(sizeof(MEMORY_NODE));
assert(NULL != pMemoryNode);
pMemoryNode->pLocation = pLocation;
pMemoryNode->EventCount = 0;
pMemoryNode->pFirstEvent = NULL;
pMemoryNode->pNext = NULL;
/* add new node to linked list */
pTempNode = f_pMemoryTrackerHead;
if (NULL == pTempNode) {
f_pMemoryTrackerHead = pMemoryNode;
}
else {
while (NULL != pTempNode->pNext) {
pTempNode = pTempNode->pNext;
}
pTempNode->pNext = pMemoryNode;
}
++f_nMemoryNodes;
}
return pMemoryNode;
}
/*------------------------------------------------------------------------*/
/** Add a new memory event having the specified parameters. */
static PMEMORY_EVENT add_memory_event(PMEMORY_NODE pMemoryNode,
size_t size,
unsigned int alloc_line,
const char* alloc_filename)
{
PMEMORY_EVENT pMemoryEvent = NULL;
PMEMORY_EVENT pTempEvent = NULL;
assert (NULL != pMemoryNode);
/* create and set up the new event */
pMemoryEvent = malloc(sizeof(MEMORY_EVENT));
assert(NULL != pMemoryEvent);
pMemoryEvent->Size = size;
pMemoryEvent->AllocLine = alloc_line;
strncpy(pMemoryEvent->AllocFilename, alloc_filename, (size_t) MAX_FILE_NAME_LENGTH-1);
pMemoryEvent->AllocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0;
pMemoryEvent->DeallocLine = NOT_DELETED;
pMemoryEvent->DeallocFilename[0] = (char)0;
pMemoryEvent->pNext = NULL;
/* add the new event to the end of the linked list */
pTempEvent = pMemoryNode->pFirstEvent;
if (NULL == pTempEvent) {
pMemoryNode->pFirstEvent = pMemoryEvent;
}
else {
while (NULL != pTempEvent->pNext) {
pTempEvent = pTempEvent->pNext;
}
pTempEvent->pNext = pMemoryEvent;
}
++pMemoryNode->EventCount;
return pMemoryEvent;
}
/*------------------------------------------------------------------------*/
/** Record memory allocation event. */
static PMEMORY_NODE allocate_memory(size_t nSize,
void* pLocation,
unsigned int uiAllocationLine,
const char* szAllocationFile)
{
PMEMORY_NODE pMemoryNode = NULL;
/* attempt to locate an existing record for this pLocation */
pMemoryNode = find_memory_node(pLocation);
/* pLocation not found - create a new event record */
if (NULL == pMemoryNode) {
pMemoryNode = create_memory_node(pLocation);
}
/* add the new event record */
add_memory_event(pMemoryNode, nSize, uiAllocationLine, szAllocationFile);
return pMemoryNode;
}
/*------------------------------------------------------------------------*/
/** Record memory deallocation event. */
static void deallocate_memory(void* pLocation, unsigned int uiDeletionLine, const char* szDeletionFileName)
{
PMEMORY_NODE pMemoryNode = NULL;
PMEMORY_EVENT pTempEvent = NULL;
assert(0 != uiDeletionLine);
assert(NULL != szDeletionFileName);
/* attempt to locate an existing record for this pLocation */
pMemoryNode = find_memory_node(pLocation);
/* if no entry, then an unallocated pointer was freed */
if (NULL == pMemoryNode) {
pMemoryNode = create_memory_node(pLocation);
pTempEvent = add_memory_event(pMemoryNode, 0, NOT_ALLOCATED, "");
}
else {
/* there should always be at least 1 event for an existing memory node */
assert(NULL != pMemoryNode->pFirstEvent);
/* locate last memory event for this pLocation */
pTempEvent = pMemoryNode->pFirstEvent;
while (NULL != pTempEvent->pNext) {
pTempEvent = pTempEvent->pNext;
}
/* if pointer has already been freed, create a new event for double deletion */
if (NOT_DELETED != pTempEvent->DeallocLine) {
pTempEvent = add_memory_event(pMemoryNode, pTempEvent->Size, NOT_ALLOCATED, "");
}
}
pTempEvent->DeallocLine = uiDeletionLine;
strncpy(pTempEvent->DeallocFilename, szDeletionFileName, MAX_FILE_NAME_LENGTH-1);
pTempEvent->DeallocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0;
}
/*------------------------------------------------------------------------*/
/** Custom calloc function with memory event recording. */
void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName)
{
void* pVoid = NULL;
#ifdef CUNIT_BUILD_TESTS
if (CU_FALSE == f_bTestCunitMallocActive) {
return NULL;
}
#endif
pVoid = calloc(nmemb, size);
if (NULL != pVoid) {
allocate_memory(nmemb * size, pVoid, uiLine, szFileName);
}
return pVoid;
}
/*------------------------------------------------------------------------*/
/** Custom malloc function with memory event recording. */
void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName)
{
void* pVoid = NULL;
#ifdef CUNIT_BUILD_TESTS
if (CU_FALSE == f_bTestCunitMallocActive) {
return NULL;
}
#endif
pVoid = malloc(size);
if (NULL != pVoid) {
allocate_memory(size, pVoid, uiLine, szFileName);
}
return pVoid;
}
/*------------------------------------------------------------------------*/
/** Custom free function with memory event recording. */
void CU_free(void *ptr, unsigned int uiLine, const char* szFileName)
{
deallocate_memory(ptr, uiLine, szFileName);
free(ptr);
}
/*------------------------------------------------------------------------*/
/** Custom realloc function with memory event recording. */
void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName)
{
void* pVoid = NULL;
deallocate_memory(ptr, uiLine, szFileName);
#ifdef CUNIT_BUILD_TESTS
if (CU_FALSE == f_bTestCunitMallocActive) {
free(ptr);
return NULL;
}
#endif
pVoid = realloc(ptr, size);
if (NULL != pVoid) {
allocate_memory(size, pVoid, uiLine, szFileName);
}
return pVoid;
}
/*------------------------------------------------------------------------*/
/** Print a report of memory events to file. */
void CU_dump_memory_usage(const char* szFilename)
{
char* szDumpFileName = (char*)f_szDefaultDumpFileName;
unsigned int nValid;
unsigned int nInvalid;
PMEMORY_NODE pTempNode = NULL;
PMEMORY_EVENT pTempEvent = NULL;
FILE* pFile = NULL;
time_t tTime = 0;
/* use the specified file name, if supplied) */
if ((NULL != szFilename) && strlen(szFilename) > 0) {
szDumpFileName = (char*)szFilename;
}
if (NULL == (pFile = fopen(szDumpFileName, "w"))) {
fprintf(stderr, "Failed to open file \"%s\" : %s", szDumpFileName, strerror(errno));
return;
}
setvbuf(pFile, NULL, _IONBF, 0);
fprintf(pFile, "<\?xml version=\"1.0\" \?>");
fprintf(pFile, "\n<\?xml-stylesheet type=\"text/xsl\" href=\"Memory-Dump.xsl\" \?>");
fprintf(pFile, "\n<!DOCTYPE MEMORY_DUMP_REPORT SYSTEM \"Memory-Dump.dtd\">");
fprintf(pFile, "\n<MEMORY_DUMP_REPORT>");
fprintf(pFile, "\n <MD_HEADER/>");
fprintf(pFile, "\n <MD_RUN_LISTING>");
nValid = 0;
nInvalid = 0;
pTempNode = f_pMemoryTrackerHead;
while (NULL != pTempNode) {
fprintf(pFile, "\n <MD_RUN_RECORD>");
fprintf(pFile, "\n <MD_POINTER> %p </MD_POINTER>", pTempNode->pLocation);
fprintf(pFile, "\n <MD_EVENT_COUNT> %u </MD_EVENT_COUNT>", pTempNode->EventCount);
pTempEvent = pTempNode->pFirstEvent;
while (NULL != pTempEvent) {
fprintf(pFile, "\n <MD_EVENT_RECORD>");
fprintf(pFile, "\n <MD_SIZE> %u </MD_SIZE>", pTempEvent->Size);
fprintf(pFile, "\n <MD_ALLOC_FILE> %s </MD_ALLOC_FILE>", pTempEvent->AllocFilename);
fprintf(pFile, "\n <MD_ALLOC_LINE> %u </MD_ALLOC_LINE>", pTempEvent->AllocLine);
fprintf(pFile, "\n <MD_DEALLOC_FILE> %s </MD_DEALLOC_FILE>", pTempEvent->DeallocFilename);
fprintf(pFile, "\n <MD_DEALLOC_LINE> %u </MD_DEALLOC_LINE>", pTempEvent->DeallocLine);
fprintf(pFile, "\n </MD_EVENT_RECORD>");
if ((0 != pTempEvent->AllocLine) && (0 != pTempEvent->DeallocLine)) {
++nValid;
}
else {
++nInvalid;
}
pTempEvent = pTempEvent->pNext;
}
fprintf(pFile, "\n </MD_RUN_RECORD>");
pTempNode = pTempNode->pNext;
}
fprintf(pFile, "\n </MD_RUN_LISTING>");
fprintf(pFile, "\n <MD_SUMMARY>");
fprintf(pFile, "\n <MD_SUMMARY_VALID_RECORDS> %u </MD_SUMMARY_VALID_RECORDS>", nValid);
fprintf(pFile, "\n <MD_SUMMARY_INVALID_RECORDS> %u </MD_SUMMARY_INVALID_RECORDS>", nInvalid);
fprintf(pFile, "\n <MD_SUMMARY_TOTAL_RECORDS> %u </MD_SUMMARY_TOTAL_RECORDS>", nValid + nInvalid);
fprintf(pFile, "\n </MD_SUMMARY>");
time(&tTime);
fprintf(pFile, "\n <MD_FOOTER> Memory Trace for CUnit Run at %s </MD_FOOTER>", ctime(&tTime));
fprintf(pFile, "</MEMORY_DUMP_REPORT>");
fclose(pFile);
}
#endif /* MEMTRACE */
/** @} */
#ifdef CUNIT_BUILD_TESTS
#include "test_cunit.h"
/** Deactivate CUnit memory allocation
* After calling this function, all Cunit memory
* allocation routines will fail and return NULL.
*/
void test_cunit_deactivate_malloc(void)
{
f_bTestCunitMallocActive = CU_FALSE;
}
/** Activate CUnit memory allocation
* After calling this function, all Cunit memory
* allocation routines will behave normally (allocating
* memory if it is available).
*/
void test_cunit_activate_malloc(void)
{
f_bTestCunitMallocActive = CU_TRUE;
}
/** Retrieve the number of memory events recorded for a given pointer. */
unsigned int test_cunit_get_n_memevents(void* pLocation)
{
PMEMORY_NODE pNode = find_memory_node(pLocation);
return (pNode) ? pNode->EventCount : 0;
}
/** Retrieve the number of memory allocations recorded for a given pointer. */
unsigned int test_cunit_get_n_allocations(void* pLocation)
{
PMEMORY_NODE pNode = find_memory_node(pLocation);
PMEMORY_EVENT pEvent = NULL;
int result = 0;
if (NULL != pNode) {
pEvent = pNode->pFirstEvent;
while (NULL != pEvent) {
if (pEvent->AllocLine != NOT_ALLOCATED)
++result;
pEvent = pEvent->pNext;
}
}
return result;
}
/** Retrieve the number of memory deallocations recorded for a given pointer. */
unsigned int test_cunit_get_n_deallocations(void* pLocation)
{
PMEMORY_NODE pNode = find_memory_node(pLocation);
PMEMORY_EVENT pEvent = NULL;
int result = 0;
if (NULL != pNode) {
pEvent = pNode->pFirstEvent;
while (NULL != pEvent) {
if (pEvent->DeallocLine != NOT_DELETED)
++result;
pEvent = pEvent->pNext;
}
}
return result;
}
void test_CU_calloc(void)
{
void* ptr1 = NULL;
void* ptr2 = calloc(2, sizeof(int));
unsigned int n2 = test_cunit_get_n_memevents(ptr2);
/* test allocation failure */
test_cunit_deactivate_malloc();
ptr1 = CU_CALLOC(2, sizeof(int));
TEST(NULL == ptr1);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
test_cunit_activate_malloc();
/* normal allocation */
ptr1 = CU_CALLOC(2, sizeof(int));
TEST_FATAL(NULL != ptr1);
TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
CU_FREE(ptr1);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
TEST(n2 == test_cunit_get_n_memevents(ptr2));
free(ptr2);
}
void test_CU_malloc(void)
{
void* ptr1 = NULL;
void* ptr2 = malloc(sizeof(int));
unsigned int n2 = test_cunit_get_n_memevents(ptr2);
/* test allocation failure */
test_cunit_deactivate_malloc();
ptr1 = CU_MALLOC(sizeof(int));
TEST(NULL == ptr1);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
test_cunit_activate_malloc();
/* normal allocation */
ptr1 = CU_MALLOC(sizeof(int));
TEST_FATAL(NULL != ptr1);
TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
CU_FREE(ptr1);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
TEST(n2 == test_cunit_get_n_memevents(ptr2));
free(ptr2);
}
void test_CU_free(void)
{
/* covered by other test functions */
}
void test_CU_realloc(void)
{
void* ptr1 = CU_MALLOC(sizeof(int));
void* ptr2 = malloc(sizeof(int));
void* ptr3;
void* ptr4;
unsigned int n2 = test_cunit_get_n_memevents(ptr2);
/* test allocation failure */
test_cunit_deactivate_malloc();
ptr1 = CU_REALLOC(ptr1, sizeof(long int));
TEST(NULL == ptr1);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
test_cunit_activate_malloc();
/* normal allocation */
ptr3 = CU_MALLOC(sizeof(int));
TEST_FATAL(NULL != ptr3);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
TEST(test_cunit_get_n_allocations(ptr3) != test_cunit_get_n_deallocations(ptr3));
ptr4 = CU_REALLOC(ptr3, sizeof(long int));
TEST_FATAL(NULL != ptr4);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
if (ptr3 != ptr4)
TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3));
TEST(test_cunit_get_n_allocations(ptr4) != test_cunit_get_n_deallocations(ptr4));
CU_FREE(ptr4);
TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1));
TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2));
TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3));
TEST(test_cunit_get_n_allocations(ptr4) == test_cunit_get_n_deallocations(ptr4));
TEST(n2 == test_cunit_get_n_memevents(ptr2));
free(ptr2);
}
/** The main internal testing function for MyMem.c. */
void test_cunit_MyMem(void)
{
test_cunit_start_tests("MyMem.c");
test_CU_calloc();
test_CU_malloc();
test_CU_free();
test_CU_realloc();
test_cunit_end_tests();
}
#endif /* CUNIT_BUILD_TESTS */
|