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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
|
/* ----------------------------------------------------------------------------
@COPYRIGHT :
Copyright 1993,1994,1995 David MacDonald,
McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
#include <internal_volume_io.h>
/* ----------------------------- MNI Header -----------------------------------
@NAME : alloc_check.c
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: Maintains a skiplist structure to list all memory allocated,
: and check for errors such as freeing a pointer twice or
: overlapping allocations.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
#define MAX_SKIP_LEVELS 50
#define SKIP_P 0.5
#define MEMORY_DIFFERENCE 1000000
typedef struct skip_entry
{
void *ptr;
size_t n_bytes;
STRING source_file;
int line_number;
int sequence_number;
struct skip_entry *forward[1];
} skip_entry;
typedef struct
{
size_t next_memory_threshold;
size_t total_memory_allocated;
skip_entry *header;
int level;
} alloc_struct;
typedef struct
{
skip_entry *update[MAX_SKIP_LEVELS];
} update_struct;
static void update_total_memory( alloc_struct *, size_t );
static int get_random_level( void );
static void output_entry( FILE *, skip_entry * );
static BOOLEAN size_display_enabled( void );
static size_t skip_alloc_size = 0;
typedef void *alloc_ptr;
#define ALLOC_SKIP_STRUCT( ptr, n_level ) \
(ptr) = (skip_entry *) malloc( \
(sizeof(skip_entry)+((size_t)(n_level)-1) * sizeof(skip_entry *)) );
/* ----------------------------- MNI Header -----------------------------------
@NAME : initialize_alloc_list
@INPUT : alloc_list
@OUTPUT :
@RETURNS :
@DESCRIPTION: Initializes the allocation list to empty.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void initialize_alloc_list(
alloc_struct *alloc_list )
{
int i;
alloc_list->next_memory_threshold = MEMORY_DIFFERENCE;
alloc_list->total_memory_allocated = 0;
ALLOC_SKIP_STRUCT( alloc_list->header, MAX_SKIP_LEVELS );
skip_alloc_size += sizeof(skip_entry)+(MAX_SKIP_LEVELS-1) *
sizeof(skip_entry *);
alloc_list->level = 1;
for_less( i, 0, MAX_SKIP_LEVELS )
alloc_list->header->forward[i] = (skip_entry *) 0;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : check_initialized_alloc_list
@INPUT : alloc_list
@OUTPUT :
@RETURNS :
@DESCRIPTION: Checks to make sure the allocation list is initialized.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void check_initialized_alloc_list(
alloc_struct *alloc_list )
{
static BOOLEAN first = TRUE;
if( first )
{
first = FALSE;
initialize_alloc_list( alloc_list );
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : find_pointer_position
@INPUT : alloc_list
: ptr
@OUTPUT : update
@RETURNS : TRUE if found
@DESCRIPTION: Searches the alloc_list for the given ptr, and sets the update
: struct so that it can provide an insert.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN find_pointer_position(
alloc_struct *alloc_list,
void *ptr,
update_struct *update )
{
int i;
skip_entry *x;
BOOLEAN found;
x = alloc_list->header;
for( i = alloc_list->level-1; i >= 0; --i )
{
while( x->forward[i] != NULL && (void *) x->forward[i]->ptr < ptr )
{
x = x->forward[i];
}
update->update[i] = x;
}
x = update->update[0]->forward[0];
found = (x != NULL) && (x->ptr == ptr);
return( found );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : insert_ptr_in_alloc_list
@INPUT : alloc_list
: update - the set of pointers indicating where to insert
: ptr }
: n_bytes }}
: source_file }}} these are recorded in the list
: line_number }}
: sequence_number }
@OUTPUT :
@RETURNS :
@DESCRIPTION: Records the allocated pointer in the allocation list.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void insert_ptr_in_alloc_list(
alloc_struct *alloc_list,
update_struct *update,
void *ptr,
size_t n_bytes,
STRING source_file,
int line_number,
int sequence_number )
{
int i, new_level;
skip_entry *x;
new_level = get_random_level();
if( new_level > alloc_list->level )
{
for( i = alloc_list->level; i < new_level; ++i )
update->update[i] = alloc_list->header;
alloc_list->level = new_level;
}
ALLOC_SKIP_STRUCT( x, new_level );
skip_alloc_size += sizeof(skip_entry)+((size_t)new_level-1) *
sizeof(skip_entry *);
x->ptr = ptr;
x->n_bytes = n_bytes;
x->source_file = source_file;
x->line_number = line_number;
x->sequence_number = sequence_number;
update_total_memory( alloc_list, n_bytes );
for( i = 0; i < new_level; ++i )
{
x->forward[i] = update->update[i]->forward[i];
update->update[i]->forward[i] = x;
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : check_overlap
@INPUT : update
: ptr
: n_bytes
@OUTPUT : entry
@RETURNS : TRUE if an overlap
@DESCRIPTION: Checks the new ptr to see if it overlaps with the previous and
: following memory allocations in the list, and returns the result.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN check_overlap(
alloc_struct *alloc_list,
update_struct *update,
void *ptr,
size_t n_bytes,
skip_entry **entry )
{
BOOLEAN overlap;
overlap = FALSE;
*entry = update->update[0];
if( *entry != alloc_list->header && *entry != (skip_entry *) 0 )
{
if( (void *) ((char *) (*entry)->ptr + (*entry)->n_bytes) > ptr )
overlap = TRUE;
else
{
(*entry) = (*entry)->forward[0];
if( *entry != (skip_entry *) 0 &&
(void *) ((char*)ptr + n_bytes) > (*entry)->ptr )
overlap = TRUE;
}
}
return( overlap );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : remove_ptr_from_alloc_list
@INPUT : alloc_list
: ptr
@OUTPUT : source_file
: line_number
: sequence_number
@RETURNS : TRUE if it existed
@DESCRIPTION: Finds and deletes the entry in the skip list associated with
: ptr, and returns the information associated with the entry
: (source_file, line_number, sequence_number).
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN remove_ptr_from_alloc_list(
alloc_struct *alloc_list,
void *ptr,
STRING *source_file,
int *line_number,
int *sequence_number )
{
int i;
BOOLEAN found;
skip_entry *x;
update_struct update;
found = find_pointer_position( alloc_list, ptr, &update );
if( found )
{
x = update.update[0]->forward[0];
*source_file = x->source_file;
*line_number = x->line_number;
*sequence_number = x->sequence_number;
update_total_memory( alloc_list, -x->n_bytes );
for( i = 0; i < alloc_list->level; ++i )
{
if( update.update[i]->forward[i] != x )
break;
update.update[i]->forward[i] = x->forward[i];
}
skip_alloc_size -= sizeof(skip_entry) +
(size_t) (i-1) * sizeof(skip_entry *);
free( (alloc_ptr) x );
while( alloc_list->level > 1 &&
alloc_list->header->forward[alloc_list->level-1] ==
(skip_entry *) 0 )
{
--alloc_list->level;
}
}
return( found );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_random_0_to_1
@INPUT :
@OUTPUT :
@RETURNS : random number
@DESCRIPTION: Returns a random number >= 0 and < 1.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static Real get_random_0_to_1( void )
{
return( (Real) rand() );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_random_level
@INPUT :
@OUTPUT :
@RETURNS : a random level between 1 and MAX_LEVELS
@DESCRIPTION: Determines a random level with exponential probability of higher
: levels.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_random_level( void )
{
int level;
level = 1;
while( get_random_0_to_1() < SKIP_P && level < MAX_SKIP_LEVELS )
++level;
return( level );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : memory_still_alloced
@INPUT : alloc_list
@OUTPUT :
@RETURNS : TRUE or FALSE
@DESCRIPTION: Decides if any memory is still alloced, thus checking for
memory leaks.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN memory_still_alloced(
alloc_struct *alloc_list )
{
return( alloc_list->header->forward[0] != (skip_entry *) NULL );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : output_alloc_list
@INPUT : file
: alloc_list
@OUTPUT :
@RETURNS :
@DESCRIPTION: Outputs the list of allocated memory to the file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void output_alloc_list(
FILE *file,
alloc_struct *alloc_list )
{
skip_entry *ptr;
ptr = alloc_list->header->forward[0];
while( ptr != (skip_entry *) 0 )
{
output_entry( file, ptr );
ptr = ptr->forward[0];
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : update_total_memory
@INPUT : alloc_list
: n_bytes
@OUTPUT :
@RETURNS :
@DESCRIPTION: Adds n_bytes to the size of memory recorded.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void update_total_memory(
alloc_struct *alloc_list,
size_t n_bytes )
{
alloc_list->total_memory_allocated += n_bytes;
if( size_display_enabled() &&
alloc_list->total_memory_allocated >
alloc_list->next_memory_threshold )
{
alloc_list->next_memory_threshold = MEMORY_DIFFERENCE *
(alloc_list->total_memory_allocated / MEMORY_DIFFERENCE + 1);
print( "Memory allocated =%5.1f Megabytes (Overhead = %5.1f Mb)\n",
(Real) alloc_list->total_memory_allocated / 1000000.0,
(Real) skip_alloc_size / 1000000.0 );
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : print_source_location
@INPUT : source_file
: line_number
: sequence_number
@OUTPUT :
@RETURNS :
@DESCRIPTION: Prints the information about a particular allocation.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void print_source_location(
STRING source_file,
int line_number,
int sequence_number )
{
print_error( "%s:%d\t%d'th alloc",
source_file, line_number, sequence_number );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : output_entry
@INPUT : file
: entry
@OUTPUT :
@RETURNS :
@DESCRIPTION: Outputs the information about an allocation entry to the file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static void output_entry(
FILE *file,
skip_entry *entry )
{
(void) fprintf( file, "%s:%d\t%d'th alloc\n",
entry->source_file,
entry->line_number,
entry->sequence_number );
}
/*
--------------------------------------------------------------------------
Routines that are to be called from outside this file
--------------------------------------------------------------------------
*/
static alloc_struct alloc_list;
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_total_memory_alloced
@INPUT :
@OUTPUT :
@RETURNS : size_t - the number of bytes allocated
@DESCRIPTION: Returns the total amount of memory allocated by the program,
: not counting that used by the skip list.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI size_t get_total_memory_alloced( void )
{
return( alloc_list.total_memory_allocated );
}
static BOOLEAN checking_enabled;
static BOOLEAN enabled_initialized = FALSE;
/* ----------------------------- MNI Header -----------------------------------
@NAME : alloc_checking_enabled
@INPUT :
@OUTPUT :
@RETURNS : TRUE if alloc checking is turned on
@DESCRIPTION: Checks an environment variable to see if alloc checking is
: not disabled.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI BOOLEAN alloc_checking_enabled( void )
{
#ifdef NO_DEBUG_ALLOC
return( FALSE );
#else
if( !enabled_initialized )
{
set_alloc_checking( ENV_EXISTS( "DEBUG_ALLOC" ) );
}
return( checking_enabled );
#endif
}
VIOAPI void set_alloc_checking( BOOLEAN state )
{
enabled_initialized = TRUE;
checking_enabled = state;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : size_display_enabled
@INPUT :
@OUTPUT :
@RETURNS : TRUE if size displaying is turned on
@DESCRIPTION: Checks an environment variable to see if memory size display
: is disabled.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN size_display_enabled( void )
{
#ifdef NO_DEBUG_ALLOC
return( FALSE );
#else
static BOOLEAN first = TRUE;
static BOOLEAN enabled;
if( first )
{
enabled = ENV_EXISTS( "ALLOC_SIZE" );
first = FALSE;
}
return( enabled );
#endif
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_stop_sequence_number
@INPUT :
@OUTPUT :
@RETURNS : which allocation number
@DESCRIPTION: Returns the number at which allocation should stop. This is
used for debugging. For instance, if an error message indicates
a problem with the 100'th alloc of the program, then do a
SETENV STOP_ALLOC_AT 100 and run the program from the debugger.
It will stop at the requested allocation.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_stop_sequence_number( void )
{
static int first = TRUE;
static int stop_sequence_number = -1;
STRING str;
if( first )
{
first = FALSE;
str = getenv( "STOP_ALLOC_AT" );
if( str == NULL ||
sscanf( str, "%d", &stop_sequence_number ) != 1 )
stop_sequence_number = -1;
}
return( stop_sequence_number );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_current_sequence_number
@INPUT :
@OUTPUT :
@RETURNS : the index of this alloc
@DESCRIPTION: Returns the count of how many allocations have been done, so that
each allocation can be assigned a value equal to its cardinality
in the set of allocations over the life of the program.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 1993 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_current_sequence_number( void )
{
static int current_sequence_number = 0;
++current_sequence_number;
if( current_sequence_number == get_stop_sequence_number() )
handle_internal_error( "get_current_sequence_number" );
return( current_sequence_number );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : record_ptr
@INPUT : ptr
: n_bytes
: source_file
: line_number
@OUTPUT :
@RETURNS :
@DESCRIPTION: Records the information about a single allocation in the list.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI void record_ptr_alloc_check(
void *ptr,
size_t n_bytes,
STRING source_file,
int line_number )
{
update_struct update_ptrs;
skip_entry *entry;
if( alloc_checking_enabled() )
{
check_initialized_alloc_list( &alloc_list );
if( n_bytes == 0 )
{
print_source_location( source_file, line_number, -1 );
print_error( ": Alloc called with zero size.\n" );
abort_if_allowed();
}
else if( ptr == (void *) 0 )
{
print_source_location( source_file, line_number, -1 );
print_error( ": Alloc returned a NIL pointer.\n" );
abort_if_allowed();
}
else
{
(void) find_pointer_position( &alloc_list, ptr, &update_ptrs );
if( check_overlap( &alloc_list, &update_ptrs, ptr, n_bytes, &entry))
{
print_source_location( source_file, line_number, -1 );
print_error(
": Alloc returned a pointer overlapping an existing block:\n"
);
print_source_location( entry->source_file, entry->line_number,
entry->sequence_number );
print_error( "\n" );
abort_if_allowed();
}
else
insert_ptr_in_alloc_list( &alloc_list,
&update_ptrs, ptr, n_bytes,
source_file, line_number,
get_current_sequence_number() );
}
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : change_ptr
@INPUT : old_ptr
: new_ptr
: n_bytes
: source_file
: line_number
@OUTPUT :
@RETURNS :
@DESCRIPTION: Changes the information (mainly the n_bytes) associated with a
: given pointer. This function is called from the def_alloc
: macros after a realloc().
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI void change_ptr_alloc_check(
void *old_ptr,
void *new_ptr,
size_t n_bytes,
STRING source_file,
int line_number )
{
STRING orig_source;
int orig_line;
int sequence_number;
skip_entry *entry;
update_struct update_ptrs;
if( alloc_checking_enabled() )
{
check_initialized_alloc_list( &alloc_list );
if( n_bytes == 0 )
{
print_source_location( source_file, line_number, -1 );
print_error( ": Realloc called with zero size.\n" );
abort_if_allowed();
}
else if( !remove_ptr_from_alloc_list( &alloc_list, old_ptr,
&orig_source, &orig_line, &sequence_number ) )
{
print_source_location( source_file, line_number, -1 );
print_error( ": Tried to realloc a pointer not already alloced.\n");
abort_if_allowed();
}
else
{
(void) find_pointer_position( &alloc_list, new_ptr, &update_ptrs );
if( check_overlap( &alloc_list, &update_ptrs, new_ptr, n_bytes,
&entry ) )
{
print_source_location( source_file, line_number, -1 );
print_error(
": Realloc returned a pointer overlapping an existing block:\n");
print_source_location( entry->source_file, entry->line_number,
entry->sequence_number );
print_error( "\n" );
abort_if_allowed();
}
else
insert_ptr_in_alloc_list( &alloc_list,
&update_ptrs, new_ptr, n_bytes,
orig_source, orig_line, sequence_number );
}
}
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : unrecord_ptr
@INPUT : ptr
: source_file
: line_number
@OUTPUT :
@RETURNS : TRUE if ptr was in list
@DESCRIPTION: Removes the entry for the given ptr from the list. Called by
: the macros during a FREE. Returns TRUE if the pointer was
: in the list.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI BOOLEAN unrecord_ptr_alloc_check(
void *ptr,
STRING source_file,
int line_number )
{
BOOLEAN was_previously_alloced;
STRING orig_source;
int orig_line;
int sequence_number;
was_previously_alloced = TRUE;
if( alloc_checking_enabled() )
{
check_initialized_alloc_list( &alloc_list );
if( ptr == (void *) 0 )
{
print_source_location( source_file, line_number, -1 );
print_error( ": Tried to free a NIL pointer.\n" );
abort_if_allowed();
was_previously_alloced = FALSE;
}
else if( !remove_ptr_from_alloc_list( &alloc_list, ptr, &orig_source,
&orig_line, &sequence_number ) )
{
print_source_location( source_file, line_number, -1 );
print_error( ": Tried to free a pointer not alloced.\n" );
abort_if_allowed();
was_previously_alloced = FALSE;
}
}
return( was_previously_alloced );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : output_alloc_to_file
@INPUT : filename
@OUTPUT :
@RETURNS :
@DESCRIPTION: Outputs a list of all memory allocated to the given file. Usually
: done at the end of the program to see if there is any memory that
: was orphaned.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI void output_alloc_to_file(
STRING filename )
{
FILE *file;
STRING date_str;
if( alloc_checking_enabled() )
{
check_initialized_alloc_list( &alloc_list );
if( memory_still_alloced( &alloc_list ) )
{
print_error( "\n" );
print_error( "\n" );
print_error( "A memory leak was found in this program.\n" );
if( filename != NULL )
print_error(
"A description has been recorded in the file %s.\n",
filename );
print_error(
"Please report this file to the author of the program.\n" );
print_error( "\n" );
if( filename != NULL && filename[0] != (char) 0 )
file = fopen( filename, "w" );
else
file = stdout;
if( file != (FILE *) 0 )
{
date_str = get_date();
(void) fprintf( file, "Alloc table at %s\n", date_str );
delete_string( date_str );
output_alloc_list( file, &alloc_list );
if( file != stdout )
(void) fclose( file );
}
}
}
}
#ifndef NO_DEBUG_ALLOC
VIOAPI void print_alloc_source_line(
STRING filename,
int line_number )
{
print_error( " Source position: %s:%d\n", filename, line_number );
}
#endif
|