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 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
|
/*******************************************************************************
* pov_mem.cpp
*
* This module contains the code for our own memory allocation/deallocation,
* providing memory tracing, statistics, and garbage collection options.
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/public/povray/3.x/source/pov_mem.cpp $
* $Revision: #1 $
* $Change: 6069 $
* $DateTime: 2013/11/06 11:59:40 $
* $Author: chrisc $
*******************************************************************************/
// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "pov_mem.h"
#include "backend/parser/parse.h" /* MAError() */
#include "povray.h" /* stats[] global var */
// this must be the last file included
#include "base/povdebug.h"
namespace pov
{
/************************************************************************
* AUTHOR
*
* Steve Anger:70714,3113
*
* DESCRIPTION
*
This module replaces the memory allocation calls malloc, calloc, realloc
and free with the macros POV_MALLOC, POV_CALLOC, POV_REALLOC, and POV_FREE.
These macros work the same as the standard C functions except that the
POV_xALLOC functions also take a message as the last parameter and
automatically call MAError(msg) if the allocation fails. That means that
instead of writing
if ((New = malloc(sizeof(*New))) == NULL)
{
MAError ("new object");
}
you'd just use
New = POV_MALLOC (sizeof(*New), "new object");
This also expands the function of the macros to include error checking and
memory tracking.
The following macros need to be defined in config.h, depending of what
features the compile needs:
#define MEM_TAG - Enables memory tag debugging
--------------------------------------------------
Memory tag debugging adds a 32-bit identifier to the beginning of each
allocated memory block and erases it after the block has been free'd. This
lets POV_FREE verify that the block it's freeing is valid and issue an
error message if it isn't. Makes it easy to find those nasty double free's
which usually corrupt the heap.
#define MEM_RECLAIM - Enables garbage collection
------------------------------------------------
Garbage collection maintains a list of all currently allocated memory so
that it can be free'd when the program exits. Normally POV-Ray will free all
of its memory on its own, however abnormal exits such as parser errors or
user aborts bypass the destructors. There are four functions which control
the garbage collection:
mem_init()
Initializes global variables used by the garbage collection routines.
This function should be called once before any memory allocation functions
are called.
mem_mark()
Starts a new memory pool. The next call to mem_release() will only release
memory allocated after this call.
mem_release ()
Releases all unfree'd memory allocated since the last call to mem_mark().
The LogFile parameter determines if it dumps the list of unfree'd memory to
a file.
mem_release_all ()
Releases all unfree'd memory allocated since the program started running.
POV-Ray only uses the mem_release_all() function however mem_mark() and
mem_release() might be useful for implenting a leak-free animation loop.
#define MEM_TRACE - Enables garbage collection and memory tracing
-------------------------------------------------------------------
Memory tracing stores the file name and line number for ever POV_xALLOC
call and dumps a list of unfree'd blocks when POV-Ray terminates.
#define MEM_STATS 1 - enables tracking of memory statistics
-------------------------------------------------------------------
Memory statistics enables routines that will track overall memory usage.
After all memory allocation/deallocation has taken place, and before you
re-initialize everything with another mem_init() call, you can call some
accessor routines to determine how memory was used. Setting MEM_STATS
to 1 only tracks peak memory usage. Setting it to 2 additionally tracks
number of calls to malloc/free and some other statistics.
*
* CHANGES
*
* Aug 1995 : Steve Anger - Creation.
* Apr 1996 : Eduard Schwan - Added MEM_STATS code
* Jul 1996 : Andreas Dilger - Force mem_header to align on double boundary
**************************************************************************/
/****************************************************************************/
/* Allow user definable replacements for memory functions */
/****************************************************************************/
#ifndef MALLOC
#define MALLOC malloc
#endif
#ifndef CALLOC
#define CALLOC calloc
#endif
#ifndef REALLOC
#define REALLOC realloc
#endif
#ifndef FREE
#define FREE free
#endif
/****************************************************************************/
/* internal use */
/****************************************************************************/
// if PREFILL or GUARD on, STATS must also be on
#if (defined(MEM_PREFILL) || defined (MEM_GUARD)) && !defined (MEM_STATS)
#define MEM_STATS
#endif
// if TRACE is on, the RECLAIM must also be on
#if defined(MEM_TRACE) && !defined (MEM_RECLAIM)
#define MEM_RECLAIM
#endif
// This is the filename created for memory leakage information
#if defined(MEM_TRACE)
#define MEM_LOG_FNAME "Memory.Log"
#endif
// determine if we need to add a header to our memory records
#if defined(MEM_TAG) || defined(MEM_RECLAIM) || defined(MEM_TRACE) || defined(MEM_STATS)
#define MEM_HEADER
#endif
#ifdef MEM_HEADER
#define MEMNODE struct mem_node
#ifndef MEM_HEADER_ALIGNMENT
#define MEM_HEADER_ALIGNMENT sizeof(double)
#endif
struct mem_node
{
#ifdef MEM_TAG
int tag;
#endif
#ifdef MEM_STATS
size_t size;
#endif
#ifdef MEM_RECLAIM
MEMNODE *prev;
MEMNODE *next;
int poolno;
#ifdef MEM_TRACE
const char *file;
int line;
#endif
#endif
};
#endif /* MEM_HEADER */
#if defined(MEM_RECLAIM)
static int poolno = 0; // GLOBAL VARIABLE
static MEMNODE *memlist = NULL; // GLOBAL VARIABLE
#endif
#if !defined(MEM_PREFILL_STRING)
#define MEM_PREFILL_STRING "POVR"
#endif
#if !defined(MEM_CLEAR_STRING)
#define MEM_CLEAR_STRING "CLEA"
#endif
#if !defined(MEM_GUARD_STRING)
#define MEM_GUARD_STRING "GURD"
#endif
#if defined(MEM_GUARD)
static char *mem_guard_string = MEM_GUARD_STRING;
static size_t mem_guard_string_len = 0;
#if !defined(MEM_GUARD_SIZE)
#define MEM_GUARD_SIZE MEM_HEADER_ALIGNMENT
#endif
#else
#define MEM_GUARD_SIZE 0
#endif
#if defined(MEM_PREFILL)
static char *mem_prefill_string = MEM_PREFILL_STRING; // GLOBAL VARIABLE
static size_t mem_prefill_string_len = 0; // GLOBAL VARIABLE
static char *mem_clear_string = MEM_CLEAR_STRING; // GLOBAL VARIABLE
static size_t mem_clear_string_len = 0; // GLOBAL VARIABLE
#endif
static int leak_msg = false; // GLOBAL VARIABLE
#ifdef MEM_HEADER
const int NODESIZE = (((sizeof(MEMNODE) + (MEM_HEADER_ALIGNMENT - 1)) / MEM_HEADER_ALIGNMENT) * MEM_HEADER_ALIGNMENT);
#else
const int NODESIZE = 0;
#endif
#if defined(MEM_RECLAIM)
static void add_node(MEMNODE * node);
static void remove_node(MEMNODE * node);
#endif
#if defined(MEM_TAG)
// the tag value that marks our used memory
#define MEMTAG_VALUE 0x4D546167L
static int mem_check_tag(MEMNODE * node);
#endif
#if defined(MEM_RECLAIM)
static long num_nodes; /* keep track of valence of node list */ // GLOBAL VARIABLE
#endif /* MEM_RECLAIM */
#if defined(MEM_STATS)
typedef struct MemStats_Struct MEMSTATS;
struct MemStats_Struct
{
size_t smallest_alloc; /* smallest # of bytes in one malloc() */
size_t largest_alloc; /* largest # of bytes in one malloc() */
size_t current_mem_usage; /* current total # of bytes allocated */
size_t largest_mem_usage; /* peak total # of bytes allocated */
#if (MEM_STATS>=2)
/* could add a running average size too, someday */
long int total_allocs; /* total # of alloc calls */
long int total_frees; /* total # of free calls */
const char *smallest_file; /* file name of largest alloc */
int smallest_line; /* file line of largest alloc */
const char *largest_file; /* file name of largest alloc */
int largest_line; /* file line of largest alloc */
#endif
};
/* keep track of memory allocation statistics */
static MEMSTATS mem_stats; // GLOBAL VARIABLE
/* local prototypes */
static void mem_stats_init (void);
static void mem_stats_alloc (size_t nbytes, const char *file, int line);
static void mem_stats_free (size_t nbytes);
#endif
/****************************************************************************/
void mem_init()
{
#if defined(MEM_RECLAIM)
num_nodes = 0;
poolno = 0;
memlist = NULL;
#endif
#if defined(MEM_GUARD)
mem_guard_string_len = strlen(mem_guard_string);
#endif
#if defined(MEM_PREFILL)
mem_prefill_string_len = strlen(mem_prefill_string);
mem_clear_string_len = strlen(mem_clear_string);
#endif
#if defined(MEM_STATS)
mem_stats_init();
#endif
leak_msg = false;
}
#if defined(MEM_TAG)
/****************************************************************************/
/* return true if pointer is non-null and has a valid tag */
static int mem_check_tag(MEMNODE *node)
{
int isOK = false;
if (node != NULL)
if (node->tag == MEMTAG_VALUE)
isOK = true;
return isOK;
}
#endif /* MEM_TAG */
/****************************************************************************/
void *pov_malloc(size_t size, const char *file, int line, const char *msg)
{
void *block;
size_t totalsize;
#if defined(MEM_HEADER)
MEMNODE *node;
#endif
#if defined(MEM_PREFILL) || defined(MEM_GUARD)
char *memptr;
size_t i;
#endif
#if defined(MEM_HEADER)
if (size == 0)
{
// TODO MESSAGE Error("Attempt to malloc zero size block (File: %s Line: %d).", file, line);
}
#endif
totalsize = size + NODESIZE + (MEM_GUARD_SIZE * 2); /* number of bytes allocated in OS */
block = (void *)MALLOC(totalsize);
if (block == NULL)
throw std::bad_alloc();; // TODO FIXME !!! // Parser::MAError(msg, (int)size);
#if defined(MEM_HEADER)
node = (MEMNODE *) block;
#endif
#if defined(MEM_TAG)
node->tag = MEMTAG_VALUE;
#endif
#if defined(MEM_TRACE) || defined(MEM_STATS)
node->size = totalsize;
#endif
#if defined(MEM_TRACE)
node->file = file;
node->line = line;
#endif
#if defined(MEM_PREFILL)
memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE;
for(i = 0; i < size; i++)
memptr[i] = mem_prefill_string[i % mem_prefill_string_len];
#endif
#if defined(MEM_GUARD)
memptr = (char *)block + NODESIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
memptr[i] = mem_guard_string[i % mem_guard_string_len];
memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
memptr[i] = mem_guard_string[i % mem_guard_string_len];
#endif
#if defined(MEM_RECLAIM)
add_node(node);
#endif
#if defined(MEM_STATS)
mem_stats_alloc(totalsize, file, line);
#endif
return (void *)((char *)block + NODESIZE + MEM_GUARD_SIZE);
}
/****************************************************************************/
void *pov_calloc(size_t nitems, size_t size, const char *file, int line, const char *msg)
{
void *block;
size_t actsize;
actsize = nitems * size;
#if defined(MEM_HEADER)
if (actsize == 0)
{
// TODO MESSAGE Error("Attempt to calloc zero size block (File: %s Line: %d).", file, line);
}
#endif
block = (void *)pov_malloc(actsize, file, line, msg);
if (block != NULL)
memset(block, 0, actsize);
return block;
}
/****************************************************************************/
void *pov_realloc(void *ptr, size_t size, const char *file, int line, const char *msg)
{
void *block;
#if defined(MEM_STATS)
size_t oldsize;
#endif
#if defined(MEM_HEADER)
MEMNODE *node;
#endif
#if defined(MEM_RECLAIM)
MEMNODE *prev;
MEMNODE *next;
#endif
#if defined(MEM_PREFILL) || defined(MEM_GUARD)
char *memptr;
size_t i;
#endif
if (size == 0)
{
if (ptr)
pov_free(ptr, file, line);
return NULL;
}
else if (ptr == NULL)
return pov_malloc(size, file, line, msg);
block = (void *)((char *)ptr - NODESIZE - MEM_GUARD_SIZE);
#if defined(MEM_GUARD)
memptr = (char *)block + NODESIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
{
if(memptr[i] != mem_guard_string[i % mem_guard_string_len])
{
Warning(0, "Damaged start guard detected in resized block (File: %s Line: %d).", file, line);
break;
}
}
memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
{
if(memptr[i] != mem_guard_string[i % mem_guard_string_len])
{
Warning(0, "Damaged end guard detected in resized block (File: %s Line: %d).", file, line);
break;
}
}
#endif
#if defined(MEM_HEADER)
node = (MEMNODE *) block;
#endif
#if defined(MEM_TAG)
if (node->tag != MEMTAG_VALUE)
Error("Attempt to realloc invalid block (File: %s Line: %d).", file, line);
node->tag = ~node->tag;
#endif
#if defined(MEM_RECLAIM)
prev = node->prev;
next = node->next;
#endif
#if defined(MEM_STATS)
oldsize = ((MEMNODE *)block)->size;
#endif
#if defined(MEM_PREFILL)
memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE;
for(i = size; i < oldsize - NODESIZE - (MEM_GUARD_SIZE * 2); i++)
memptr[i] = mem_clear_string[i % mem_clear_string_len];
#endif
block = (void *)REALLOC(block, NODESIZE + (MEM_GUARD_SIZE * 2) + size);
if (block == NULL)
throw std::bad_alloc(); // TODO FIXME !!! // Parser::MAError(msg, (int)size);
#if defined(MEM_STATS)
/* REALLOC does an implied FREE... */
mem_stats_free(oldsize);
/* ...and an implied MALLOC... */
mem_stats_alloc(NODESIZE + (MEM_GUARD_SIZE * 2) + size, file, line);
#endif
#if defined(MEM_PREFILL)
memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE;
for(i = oldsize - NODESIZE - (MEM_GUARD_SIZE * 2); i < size; i++)
memptr[i] = mem_prefill_string[i % mem_prefill_string_len];
#endif
#if defined(MEM_HEADER)
node = (MEMNODE *) block;
#endif
#if defined(MEM_TAG)
node->tag = MEMTAG_VALUE;
#endif
#if defined(MEM_TRACE) || defined(MEM_STATS)
node->size = size + NODESIZE + (MEM_GUARD_SIZE * 2);
#endif
#if defined(MEM_TRACE)
node->file = file;
node->line = line;
#endif
#if defined(MEM_GUARD)
memptr = (char *)block + NODESIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
memptr[i] = mem_guard_string[i % mem_guard_string_len];
memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
memptr[i] = mem_guard_string[i % mem_guard_string_len];
#endif
#if defined(MEM_RECLAIM)
if (prev == NULL)
memlist = node;
else
prev->next = node;
if (node->next != NULL)
node->next->prev = node;
if (next != NULL)
next->prev = node;
#endif
return (void *)((char *)block + NODESIZE + MEM_GUARD_SIZE);
}
/****************************************************************************/
void pov_free(void *ptr, const char *file, int line)
{
void *block;
#if defined(MEM_HEADER)
MEMNODE *node;
#endif
#if defined(MEM_PREFILL) || defined(MEM_GUARD)
char *memptr;
size_t size;
size_t i;
#endif
if (ptr == NULL)
throw pov_base::Exception(NULL, file, (unsigned int)line, "Attempt to free NULL pointer.");
block = (void *)((char *)ptr - NODESIZE - MEM_GUARD_SIZE);
#if defined(MEM_HEADER)
node = (MEMNODE *) block;
#endif
#if defined(MEM_TAG)
if (node->tag == ~MEMTAG_VALUE)
{
Warning(0, "Attempt to free already free'd block (File: %s Line: %d).", file, line);
return;
}
else if (node->tag != MEMTAG_VALUE)
{
Warning(0, "Attempt to free invalid block (File: %s Line: %d).", file, line);
return;
}
#endif
#if defined(MEM_GUARD)
memptr = (char *)block + NODESIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
{
if(memptr[i] != mem_guard_string[i % mem_guard_string_len])
{
Warning(0, "Damaged start guard detected in free'd block (File: %s Line: %d).", file, line);
break;
}
}
memptr = (char *)block + ((MEMNODE *)block)->size - MEM_GUARD_SIZE;
for(i = 0; i < MEM_GUARD_SIZE; i++)
{
if(memptr[i] != mem_guard_string[i % mem_guard_string_len])
{
Warning(0, "Damaged end guard detected in free'd block (File: %s Line: %d).", file, line);
break;
}
}
#endif
#if defined(MEM_RECLAIM)
remove_node(node);
#endif
#if defined(MEM_TAG)
/* do this After remove_node, so remove_node can check validity of nodes */
node->tag = ~node->tag;
#endif
#if defined(MEM_STATS)
mem_stats_free(((MEMNODE*)block)->size);
#endif
#if defined(MEM_PREFILL)
size = ((MEMNODE *)block)->size;
memptr = (char *)block + NODESIZE + MEM_GUARD_SIZE;
for(i = 0; i < size - NODESIZE - (MEM_GUARD_SIZE * 2); i++)
memptr[i] = mem_clear_string[i % mem_clear_string_len];
#endif
FREE(block);
}
/****************************************************************************/
/* Starts a new memory pool. The next mem_release() call will
only release memory allocated after this call. */
void mem_mark()
{
#if defined(MEM_RECLAIM)
poolno++;
#endif
}
/****************************************************************************/
/* Releases all unfree'd memory from current memory pool */
void mem_release()
{
#if defined(MEM_RECLAIM)
OStream *f = NULL;
MEMNODE *p, *tmp;
size_t totsize;
p = memlist;
totsize = 0;
#if defined(MEM_TRACE)
if (p != NULL && (p->poolno == poolno))
f = New_OStream(MEM_LOG_FNAME, POV_File_Data_LOG, true);
#endif /* MEM_TRACE */
while (p != NULL && (p->poolno == poolno))
{
#if defined(MEM_TRACE)
#if defined(MEM_TAG)
if (!mem_check_tag(p))
Debug_Info("mem_release(): Memory pointer corrupt!\n");
#endif /* MEM_TAG */
totsize += (p->size - NODESIZE - (MEM_GUARD_SIZE * 2));
if (!leak_msg)
{
Debug_Info("Memory leakage detected, see file '%s' for list\n",MEM_LOG_FNAME);
leak_msg = true;
}
if (f != NULL)
f->printf("File:%13s Line:%4d Size:%lu\n", p->file, p->line, (unsigned long)(p->size - NODESIZE - (MEM_GUARD_SIZE * 2)));
#endif /* MEM_TRACE */
#if defined(MEM_STATS)
mem_stats_free(p->size);
#endif
tmp = p;
p = p->next;
remove_node(tmp);
FREE(tmp);
}
if (f != NULL)
delete f;
// if (totsize > 0)
// Debug_Info("%lu bytes reclaimed (pool #%d)\n", totsize, poolno);
if (poolno > 0)
poolno--;
#if defined(MEM_STATS)
/* reinitialize the stats structure for next time through */
mem_stats_init();
#endif
#endif /* MEM_RECLAIM */
}
/****************************************************************************/
/* Released all unfree'd memory from all pools */
void mem_release_all()
{
#if defined(MEM_RECLAIM)
OStream *f = NULL;
MEMNODE *p, *tmp;
size_t totsize;
// Send_Progress("Reclaiming memory", PROGRESS_RECLAIMING_MEMORY);
p = memlist;
totsize = 0;
#if defined(MEM_TRACE)
if (p != NULL)
f = New_OStream(MEM_LOG_FNAME, POV_File_Data_LOG, true);
#endif
while (p != NULL)
{
#if defined(MEM_TRACE)
#if defined(MEM_TAG)
if (!mem_check_tag(p))
Debug_Info("mem_release_all(): Memory pointer corrupt!\n");
#endif /* MEM_TAG */
totsize += (p->size - NODESIZE - (MEM_GUARD_SIZE * 2));
if (!leak_msg)
{
Debug_Info("Memory leakage detected, see file '%s' for list\n",MEM_LOG_FNAME);
leak_msg = true;
}
if (f != NULL)
f->printf("File:%13s Line:%4d Size:%lu\n", p->file, p->line, (unsigned long)(p->size - NODESIZE - (MEM_GUARD_SIZE * 2)));
#endif
#if defined(MEM_STATS)
/* This is after we have printed stats, and this may slow us down a little, */
/* so we may want to simply re-initialize the mem-stats at the end of this loop. */
mem_stats_free(p->size);
#endif
tmp = p;
p = p->next;
remove_node(tmp);
FREE(tmp);
}
if (f != NULL)
delete f;
// if (totsize > 0)
// Debug_Info("\n%lu bytes reclaimed\n", totsize);
poolno = 0;
memlist = NULL;
#endif
#if defined(MEM_STATS)
/* reinitialize the stats structure for next time through */
mem_stats_init();
#endif
}
/****************************************************************************/
#if defined(MEM_RECLAIM)
/* Adds a new node to the 'allocated' list */
static void add_node(MEMNODE *node)
{
#if defined(MEM_TAG)
if (!mem_check_tag(node))
Debug_Info("add_node(): Memory pointer corrupt!\n");
#endif /* MEM_TAG */
if (memlist == NULL)
{
memlist = node;
node->poolno = poolno;
node->prev = NULL;
node->next = NULL;
num_nodes = 0;
}
else
{
memlist->prev = node;
node->poolno = poolno;
node->prev = NULL;
node->next = memlist;
memlist = node;
}
num_nodes++;
}
/****************************************************************************/
/* Detatches a node from the 'allocated' list but doesn't free it */
static void remove_node(MEMNODE *node)
{
#if defined(MEM_TAG)
if (!mem_check_tag(node))
Debug_Info("remove_node(): Memory pointer corrupt!\n");
#endif /* MEM_TAG */
num_nodes--;
if (node->prev != NULL)
node->prev->next = node->next;
if (node->next != NULL)
node->next->prev = node->prev;
if (memlist == node)
{
#if defined(MEM_TAG)
/* check node->next if it is non-null, to insure it is safe to assign. */
/* if it is null, it is safe since it is the last in the list. */
if (node->next)
if (!mem_check_tag(node->next))
Debug_Info("remove_node(): memlist pointer corrupt!\n");
#endif /* MEM_TAG */
memlist = node->next;
}
node->prev = NULL;
node->next = NULL;
}
#endif /* MEM_RECLAIM */
/****************************************************************************/
/* A strdup routine that uses POV_MALLOC */
/****************************************************************************/
char *pov_strdup(const char *s)
{
char *New;
New=(char *)POV_MALLOC(strlen(s)+1,s);
strcpy(New,s);
return (New);
}
/****************************************************************************/
/* A memmove routine for those systems that don't have one */
/****************************************************************************/
void *pov_memmove (void *dest, void *src, size_t length)
{
char *csrc =(char *)src;
char *cdest=(char *)dest;
if (csrc < cdest && csrc + length >= cdest)
{
size_t size = cdest - csrc;
while (length > 0)
{
POV_MEMCPY(cdest + length - size, csrc + length - size, size);
length -= size;
if (length < size)
size = length;
}
}
/* I'm not sure if this is needed, but my docs on memcpy say the regions
* can't overlap, so theoretically we need to special case this. If you
* don't think it's necessary, you can just comment this part out.
*/
else if (cdest < csrc && cdest + length >= csrc)
{
char *new_dest = cdest;
size_t size = csrc - cdest;
while (length > 0)
{
POV_MEMCPY(new_dest, csrc, length);
new_dest += size;
csrc += size;
length -= size;
if (length < size)
size = length;
}
}
else
{
POV_MEMCPY(cdest, csrc, length);
}
return cdest;
}
/****************************************************************************/
/* Memory Statistics gathering routines */
/****************************************************************************/
#if defined(MEM_STATS)
/****************************************************************************/
static void mem_stats_init()
{
mem_stats.smallest_alloc = 65535; /* Must be an unsigned number */
mem_stats.largest_alloc = 0;
mem_stats.current_mem_usage = 0;
mem_stats.largest_mem_usage = 0;
#if (MEM_STATS>=2)
mem_stats.total_allocs = 0;
mem_stats.total_frees = 0;
mem_stats.largest_file = "none";
mem_stats.largest_line = -1;
mem_stats.smallest_file = "none";
mem_stats.smallest_line = -1;
#endif
}
/****************************************************************************/
/* update appropriate fields when an allocation takes place */
static void mem_stats_alloc(size_t nbytes, const char *file, int line)
{
/* update the fields */
if (((int) mem_stats.smallest_alloc<0) || (nbytes<mem_stats.smallest_alloc))
{
mem_stats.smallest_alloc = nbytes;
#if (MEM_STATS>=2)
mem_stats.smallest_file = file;
mem_stats.smallest_line = line;
#endif
}
if (nbytes>mem_stats.largest_alloc)
{
mem_stats.largest_alloc = nbytes;
#if (MEM_STATS>=2)
mem_stats.largest_file = file;
mem_stats.largest_line = line;
#endif
}
#if (MEM_STATS>=2)
mem_stats.total_allocs++;
#endif
mem_stats.current_mem_usage += nbytes;
if (mem_stats.current_mem_usage>mem_stats.largest_mem_usage)
{
mem_stats.largest_mem_usage = mem_stats.current_mem_usage;
}
}
/****************************************************************************/
/* update appropriate fields when a free takes place */
static void mem_stats_free(size_t nbytes)
{
/* update the fields */
mem_stats.current_mem_usage -= nbytes;
#if (MEM_STATS>=2)
mem_stats.total_frees++;
#endif
}
/****************************************************************************/
/* Level 1 */
/****************************************************************************/
size_t mem_stats_smallest_alloc()
{
return mem_stats.smallest_alloc;
}
/****************************************************************************/
size_t mem_stats_largest_alloc()
{
return mem_stats.largest_alloc;
}
/****************************************************************************/
size_t mem_stats_current_mem_usage()
{
return mem_stats.current_mem_usage;
}
/****************************************************************************/
size_t mem_stats_largest_mem_usage()
{
return mem_stats.largest_mem_usage;
}
/****************************************************************************/
/* Level 2 */
#if (MEM_STATS>=2)
/****************************************************************************/
const char *mem_stats_smallest_file()
{
return mem_stats.smallest_file;
}
/****************************************************************************/
int mem_stats_smallest_line()
{
return mem_stats.smallest_line;
}
/****************************************************************************/
const char *mem_stats_largest_file()
{
return mem_stats.largest_file;
}
/****************************************************************************/
int mem_stats_largest_line()
{
return mem_stats.largest_line;
}
/****************************************************************************/
long int mem_stats_total_allocs()
{
return mem_stats.total_allocs;
}
/****************************************************************************/
long int mem_stats_total_frees()
{
return mem_stats.total_frees;
}
#endif
#endif /* MEM_STATS */
}
|