MEM.C 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /****************************************************************************
  2. * mem.c
  3. *
  4. * This module contains the code for our own memory allocation/deallocation,
  5. * providing memory tracing, statistics, and garbage collection options.
  6. *
  7. * from Persistence of Vision(tm) Ray Tracer
  8. * Copyright 1996,1999 Persistence of Vision Team
  9. *---------------------------------------------------------------------------
  10. * NOTICE: This source code file is provided so that users may experiment
  11. * with enhancements to POV-Ray and to port the software to platforms other
  12. * than those supported by the POV-Ray Team. There are strict rules under
  13. * which you are permitted to use this file. The rules are in the file
  14. * named POVLEGAL.DOC which should be distributed with this file.
  15. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  16. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  17. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  18. *
  19. * This program is based on the popular DKB raytracer version 2.12.
  20. * DKBTrace was originally written by David K. Buck.
  21. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  22. *
  23. *****************************************************************************/
  24. #include "frame.h"
  25. #include "povproto.h" /* Error() */
  26. #include "mem.h"
  27. #include "parse.h" /* MAError() */
  28. #include "povray.h" /* stats[] global var */
  29. /************************************************************************
  30. * AUTHOR
  31. *
  32. * Steve Anger:70714,3113
  33. *
  34. * DESCRIPTION
  35. *
  36. This module replaces the memory allocation calls malloc, calloc, realloc
  37. and free with the macros POV_MALLOC, POV_CALLOC, POV_REALLOC, and POV_FREE.
  38. These macros work the same as the standard C functions except that the
  39. POV_xALLOC functions also take a message as the last parameter and
  40. automatically call MAError(msg) if the allocation fails. That means that
  41. instead of writing
  42. if ((New = malloc(sizeof(*New))) == NULL)
  43. {
  44. MAError ("new object");
  45. }
  46. you'd just use
  47. New = POV_MALLOC (sizeof(*New), "new object");
  48. This also expands the function of the macros to include error checking and
  49. memory tracking.
  50. The following macros need to be defined in config.h, depending of what
  51. features the compile needs:
  52. #define MEM_TAG - Enables memory tag debugging
  53. --------------------------------------------------
  54. Memory tag debugging adds a 32-bit identifier to the beginning of each
  55. allocated memory block and erases it after the block has been free'd. This
  56. lets POV_FREE verify that the block it's freeing is valid and issue an
  57. error message if it isn't. Makes it easy to find those nasty double free's
  58. which usually corrupt the heap.
  59. #define MEM_RECLAIM - Enables garbage collection
  60. ------------------------------------------------
  61. Garbage collection maintains a list of all currently allocated memory so
  62. that it can be free'd when the program exits. Normally POV-Ray will free all
  63. of its memory on its own, however abnormal exits such as parser errors or
  64. user aborts bypass the destructors. There are four functions which control
  65. the garbage collection:
  66. mem_init()
  67. Initializes global variables used by the garbage collection routines.
  68. This function should be called once before any memory allocation functions
  69. are called.
  70. mem_mark()
  71. Starts a new memory pool. The next call to mem_release() will only release
  72. memory allocated after this call.
  73. mem_release (int LogFile)
  74. Releases all unfree'd memory allocated since the last call to mem_mark().
  75. The LogFile parameter determines if it dumps the list of unfree'd memory to
  76. a file.
  77. mem_release_all (int LogFile)
  78. Releases all unfree'd memory allocated since the program started running.
  79. POV-Ray only uses the mem_release_all() function however mem_mark() and
  80. mem_release() might be useful for implenting a leak-free animation loop.
  81. #define MEM_TRACE - Enables garbage collection and memory tracing
  82. -------------------------------------------------------------------
  83. Memory tracing stores the file name and line number for ever POV_xALLOC
  84. call and dumps a list of unfree'd blocks when POV-Ray terminates.
  85. #define MEM_STATS 1 - enables tracking of memory statistics
  86. -------------------------------------------------------------------
  87. Memory statistics enables routines that will track overall memory usage.
  88. After all memory allocation/deallocation has taken place, and before you
  89. re-initialize everything with another mem_init() call, you can call some
  90. accessor routines to determine how memory was used. Setting MEM_STATS
  91. to 1 only tracks peak memory usage. Setting it to 2 additionally tracks
  92. number of calls to malloc/free and some other statistics.
  93. *
  94. * CHANGES
  95. *
  96. * Aug 1995 : Steve Anger - Creation.
  97. * Apr 1996 : Eduard Schwan - Added MEM_STATS code
  98. * Jul 1996 : Andreas Dilger - Force mem_header to align on double boundary
  99. **************************************************************************/
  100. /****************************************************************************/
  101. /* Allow user definable replacements for memory functions */
  102. /****************************************************************************/
  103. #ifndef MALLOC
  104. #define MALLOC malloc
  105. #endif
  106. #ifndef CALLOC
  107. #define CALLOC calloc
  108. #endif
  109. #ifndef REALLOC
  110. #define REALLOC realloc
  111. #endif
  112. #ifndef FREE
  113. #define FREE free
  114. #endif
  115. /****************************************************************************/
  116. /* internal use */
  117. /****************************************************************************/
  118. /* if TRACE is on, the RECLAIM must also be on */
  119. #if defined(MEM_TRACE) && !defined (MEM_RECLAIM)
  120. #define MEM_RECLAIM
  121. #endif
  122. /* This is the filename created for memory leakage information */
  123. #if defined(MEM_TRACE)
  124. #define MEM_LOG_FNAME "Memory.Log"
  125. #endif
  126. /* determine if we need to add a header to our memory records */
  127. #if defined(MEM_TAG) || defined(MEM_RECLAIM) || defined(MEM_TRACE) || defined(MEM_STATS)
  128. #define MEM_HEADER
  129. #endif
  130. #define MEMNODE struct mem_node
  131. #if defined(MEM_HEADER)
  132. struct mem_node
  133. {
  134. /* We have to do lots of testing here to make sure that the size of the
  135. * mem_node struct is an even multiple of the sizeof(double) (usually 8
  136. * bytes, or we royally screw up some architectures. Yuck!!! To make
  137. * things easier, we have smaller groups of variables, and make them
  138. * work out to multiples of 8 bytes, rather than trying to do it for the
  139. * whole structure. In most cases, only 4 bytes are wasted, as this is
  140. * mostly for debugging anyways.
  141. */
  142. #if defined(MEM_TAG)
  143. long tag;
  144. #if !defined(MEM_STATS) || defined(MEM_TRACE)
  145. long junk1;
  146. #endif /* !MEM_STATS */
  147. #endif /* MEM_TAG */
  148. #if defined(MEM_STATS) && !defined(MEM_TRACE)
  149. size_t size;
  150. #if !defined(MEM_TAG)
  151. long junk1;
  152. #endif /* !MEM_TAG */
  153. #endif /* MEM_STATS */
  154. #if defined(MEM_RECLAIM)
  155. MEMNODE *prev;
  156. MEMNODE *next;
  157. long poolno;
  158. #if defined(MEM_TRACE)
  159. char *file;
  160. long line;
  161. size_t size;
  162. #else
  163. long junk2;
  164. #endif /* MEM_TRACE */
  165. #endif /* MEM_RECLAIM */
  166. };
  167. #endif /* MEM_HEADER */
  168. #if defined(MEM_RECLAIM)
  169. static int poolno = 0;
  170. static MEMNODE *memlist = NULL;
  171. #endif
  172. static int leak_msg = FALSE;
  173. #if defined(MEM_HEADER)
  174. #define NODESIZE ((sizeof(MEMNODE)+3)/4)*4 /* Align memory on 4 byte boundary */
  175. #else
  176. #define NODESIZE 0
  177. #endif
  178. #if defined(MEM_RECLAIM)
  179. static void add_node(MEMNODE * node);
  180. static void remove_node(MEMNODE * node);
  181. #endif
  182. #if defined(MEM_TAG)
  183. /* the tag value that marks our used memory */
  184. #define MEMTAG_VALUE 0x4D546167L
  185. static int mem_check_tag(MEMNODE * node);
  186. #endif
  187. #if defined(MEM_RECLAIM)
  188. static long num_nodes; /* keep track of valence of node list */
  189. #endif /* MEM_RECLAIM */
  190. #if defined(MEM_STATS)
  191. typedef struct MemStats_Struct MEMSTATS;
  192. struct MemStats_Struct
  193. {
  194. size_t smallest_alloc; /* smallest # of bytes in one malloc() */
  195. size_t largest_alloc; /* largest # of bytes in one malloc() */
  196. size_t current_mem_usage; /* current total # of bytes allocated */
  197. size_t largest_mem_usage; /* peak total # of bytes allocated */
  198. #if (MEM_STATS>=2)
  199. /* could add a running average size too, someday */
  200. long int total_allocs; /* total # of alloc calls */
  201. long int total_frees; /* total # of free calls */
  202. char *smallest_file; /* file name of largest alloc */
  203. int smallest_line; /* file line of largest alloc */
  204. char *largest_file; /* file name of largest alloc */
  205. int largest_line; /* file line of largest alloc */
  206. #endif
  207. };
  208. /* keep track of memory allocation statistics */
  209. static MEMSTATS mem_stats;
  210. /* local prototypes */
  211. static void mem_stats_init (void);
  212. static void mem_stats_alloc (size_t nbytes, char *file, int line);
  213. static void mem_stats_free (size_t nbytes);
  214. #endif
  215. /****************************************************************************/
  216. void mem_init()
  217. {
  218. #if defined(MEM_RECLAIM)
  219. num_nodes = 0;
  220. poolno = 0;
  221. memlist = NULL;
  222. #endif
  223. #if defined(MEM_STATS)
  224. mem_stats_init();
  225. #endif
  226. leak_msg = FALSE;
  227. }
  228. #if defined(MEM_TAG)
  229. /****************************************************************************/
  230. /* return TRUE if pointer is non-null and has a valid tag */
  231. static int mem_check_tag(MEMNODE *node)
  232. {
  233. int isOK = FALSE;
  234. if (node != NULL)
  235. if (node->tag == MEMTAG_VALUE)
  236. isOK = TRUE;
  237. return isOK;
  238. }
  239. #endif /* MEM_TAG */
  240. /****************************************************************************/
  241. void *pov_malloc(size_t size, char *file, int line, char *msg)
  242. {
  243. void *block;
  244. size_t totalsize;
  245. #if defined(MEM_HEADER)
  246. MEMNODE *node;
  247. #endif
  248. #if defined(MEM_HEADER)
  249. if (size == 0)
  250. {
  251. Error("Attempt to malloc zero size block (File: %s Line: %d).\n", file, line);
  252. }
  253. #endif
  254. totalsize=size+NODESIZE; /* number of bytes allocated in OS */
  255. block = (void *)MALLOC(totalsize);
  256. if (block == NULL)
  257. MAError(msg, size);
  258. #if defined(MEM_HEADER)
  259. node = (MEMNODE *) block;
  260. #endif
  261. #if defined(MEM_TAG)
  262. node->tag = MEMTAG_VALUE;
  263. #endif
  264. #if defined(MEM_TRACE) || defined(MEM_STATS)
  265. node->size = totalsize;
  266. #endif
  267. #if defined(MEM_TRACE)
  268. node->file = file;
  269. node->line = line;
  270. #endif
  271. #if defined(MEM_RECLAIM)
  272. add_node(node);
  273. #endif
  274. #if defined(MEM_STATS)
  275. mem_stats_alloc(totalsize, file, line);
  276. #endif
  277. return (void *)((char *)block + NODESIZE);
  278. }
  279. /****************************************************************************/
  280. void *pov_calloc(size_t nitems, size_t size, char *file, int line, char *msg)
  281. {
  282. void *block;
  283. size_t actsize;
  284. size_t totalsize; /* number of bytes allocated in OS */
  285. #if defined(MEM_HEADER)
  286. MEMNODE *node;
  287. #endif
  288. actsize=nitems*size;
  289. totalsize=actsize+NODESIZE;
  290. #if defined(MEM_HEADER)
  291. if (actsize == 0)
  292. {
  293. Error("Attempt to calloc zero size block (File: %s Line: %d).\n", file, line);
  294. }
  295. #endif
  296. block = (void *)MALLOC(totalsize);
  297. if (block == NULL)
  298. MAError(msg, actsize);
  299. memset(block, 0, totalsize);
  300. #if defined(MEM_HEADER)
  301. node = (MEMNODE *) block;
  302. #endif
  303. #if defined(MEM_TAG)
  304. node->tag = MEMTAG_VALUE;
  305. #endif
  306. #if defined(MEM_TRACE) || defined(MEM_STATS)
  307. node->size = totalsize;
  308. #endif
  309. #if defined(MEM_TRACE)
  310. node->file = file;
  311. node->line = line;
  312. #endif
  313. #if defined(MEM_RECLAIM)
  314. add_node(node);
  315. #endif
  316. #if defined(MEM_STATS)
  317. mem_stats_alloc(totalsize, file, line);
  318. #endif
  319. return (void *)((char *)block + NODESIZE);
  320. }
  321. /****************************************************************************/
  322. void *pov_realloc(void *ptr, size_t size, char *file, int line, char *msg)
  323. {
  324. void *block;
  325. #if defined(MEM_STATS)
  326. size_t oldsize;
  327. #endif
  328. #if defined(MEM_HEADER)
  329. MEMNODE *node;
  330. #endif
  331. #if defined(MEM_RECLAIM)
  332. MEMNODE *prev;
  333. MEMNODE *next;
  334. #endif
  335. if (size == 0)
  336. {
  337. if (ptr)
  338. pov_free(ptr, file, line);
  339. return NULL;
  340. }
  341. else if (ptr == NULL)
  342. return pov_malloc(size, file, line, msg);
  343. block = (void *)((char *)ptr - NODESIZE);
  344. #if defined(MEM_HEADER)
  345. node = (MEMNODE *) block;
  346. #endif
  347. #if defined(MEM_TAG)
  348. if (node->tag != MEMTAG_VALUE)
  349. Error("Attempt to realloc invalid block (File: %s Line: %d).\n", file, line);
  350. node->tag = ~node->tag;
  351. #endif
  352. #if defined(MEM_RECLAIM)
  353. prev = node->prev;
  354. next = node->next;
  355. #endif
  356. block = (void *)REALLOC(block, NODESIZE + size);
  357. if (block == NULL)
  358. MAError(msg, size);
  359. #if defined(MEM_STATS)
  360. /* REALLOC does an implied FREE... */
  361. oldsize = ((MEMNODE *)block)->size;
  362. mem_stats_free(oldsize);
  363. /* ...and an implied MALLOC... */
  364. mem_stats_alloc(NODESIZE + size, file, line);
  365. #endif
  366. #if defined(MEM_HEADER)
  367. node = (MEMNODE *) block;
  368. #endif
  369. #if defined(MEM_TAG)
  370. node->tag = MEMTAG_VALUE;
  371. #endif
  372. #if defined(MEM_TRACE) || defined(MEM_STATS)
  373. node->size = size + NODESIZE;
  374. #endif
  375. #if defined(MEM_TRACE)
  376. node->file = file;
  377. node->line = line;
  378. #endif
  379. #if defined(MEM_RECLAIM)
  380. if (prev == NULL)
  381. memlist = node;
  382. else
  383. prev->next = node;
  384. if (node->next != NULL)
  385. node->next->prev = node;
  386. if (next != NULL)
  387. next->prev = node;
  388. #endif
  389. return (void *)((char *)block + NODESIZE);
  390. }
  391. /****************************************************************************/
  392. void pov_free(void *ptr, char *file, int line)
  393. {
  394. void *block;
  395. #if defined(MEM_HEADER)
  396. MEMNODE *node;
  397. #endif
  398. if (ptr == NULL)
  399. Error("Attempt to free NULL pointer (File: %s Line: %d).\n", file, line);
  400. block = (void *)((char *)ptr - NODESIZE);
  401. #if defined(MEM_HEADER)
  402. node = (MEMNODE *) block;
  403. #endif
  404. #if defined(MEM_TAG)
  405. if (node->tag == ~MEMTAG_VALUE)
  406. {
  407. Warning(0.0, "Attempt to free already free'd block (File: %s Line: %d).\n", file, line);
  408. return;
  409. }
  410. else if (node->tag != MEMTAG_VALUE)
  411. {
  412. Warning(0.0, "Attempt to free invalid block (File: %s Line: %d).\n", file, line);
  413. return;
  414. }
  415. #endif
  416. #if defined(MEM_RECLAIM)
  417. remove_node(node);
  418. #endif
  419. #if defined(MEM_TAG)
  420. /* do this After remove_node, so remove_node can check validity of nodes */
  421. node->tag = ~node->tag;
  422. #endif
  423. #if defined(MEM_STATS)
  424. mem_stats_free(((MEMNODE*)block)->size);
  425. #endif
  426. FREE(block);
  427. }
  428. /****************************************************************************/
  429. /* Starts a new memory pool. The next mem_release() call will
  430. only release memory allocated after this call. */
  431. void mem_mark()
  432. {
  433. #if defined(MEM_RECLAIM)
  434. poolno++;
  435. #endif
  436. }
  437. /****************************************************************************/
  438. /* Releases all unfree'd memory from current memory pool */
  439. void mem_release(int LogFile)
  440. {
  441. #if defined(MEM_RECLAIM)
  442. FILE *f = NULL;
  443. MEMNODE *p, *tmp;
  444. size_t totsize;
  445. p = memlist;
  446. totsize = 0;
  447. #if defined(MEM_TRACE)
  448. if (LogFile)
  449. {
  450. if (p != NULL && (p->poolno == poolno))
  451. f = fopen(MEM_LOG_FNAME, APPEND_TXTFILE_STRING);
  452. }
  453. #endif /* MEM_TRACE */
  454. while (p != NULL && (p->poolno == poolno))
  455. {
  456. #if defined(MEM_TRACE)
  457. #if defined(MEM_TAG)
  458. if (!mem_check_tag(p))
  459. Debug_Info("mem_release(): Memory pointer corrupt!\n");
  460. #endif /* MEM_TAG */
  461. totsize += (p->size-NODESIZE);
  462. if (LogFile)
  463. {
  464. if (!leak_msg)
  465. {
  466. Debug_Info("Memory leakage detected, see file '%s' for list\n",MEM_LOG_FNAME);
  467. leak_msg = TRUE;
  468. }
  469. if (f != NULL)
  470. fprintf(f, "File:%13s Line:%4d Size:%lu\n", p->file, p->line, (unsigned long)(p->size-NODESIZE));
  471. }
  472. #endif /* MEM_TRACE */
  473. #if defined(MEM_STATS)
  474. mem_stats_free(p->size);
  475. #endif
  476. tmp = p;
  477. p = p->next;
  478. remove_node(tmp);
  479. FREE(tmp);
  480. }
  481. if (f != NULL)
  482. fclose(f);
  483. if (totsize > 0)
  484. Debug_Info("%lu bytes reclaimed (pool #%d)\n", totsize, poolno);
  485. if (poolno > 0)
  486. poolno--;
  487. #if defined(MEM_STATS)
  488. /* reinitialize the stats structure for next time through */
  489. mem_stats_init();
  490. #endif
  491. #endif /* MEM_RECLAIM */
  492. }
  493. /****************************************************************************/
  494. /* Released all unfree'd memory from all pools */
  495. void mem_release_all(int LogFile)
  496. {
  497. #if defined(MEM_RECLAIM)
  498. FILE *f = NULL;
  499. MEMNODE *p, *tmp;
  500. size_t totsize;
  501. Status_Info("Reclaiming memory\n");
  502. p = memlist;
  503. totsize = 0;
  504. #if defined(MEM_TRACE)
  505. if (LogFile)
  506. {
  507. if (p != NULL)
  508. f = fopen(MEM_LOG_FNAME, APPEND_TXTFILE_STRING);
  509. }
  510. #endif
  511. while (p != NULL)
  512. {
  513. #if defined(MEM_TRACE)
  514. #if defined(MEM_TAG)
  515. if (!mem_check_tag(p))
  516. Debug_Info("mem_release_all(): Memory pointer corrupt!\n");
  517. #endif /* MEM_TAG */
  518. totsize += (p->size-NODESIZE);
  519. if (LogFile)
  520. {
  521. if (!leak_msg)
  522. {
  523. Debug_Info("Memory leakage detected, see file '%s' for list\n",MEM_LOG_FNAME);
  524. leak_msg = TRUE;
  525. }
  526. if (f != NULL)
  527. fprintf(f, "File:%13s Line:%4d Size:%lu\n", p->file, p->line, (unsigned long)(p->size-NODESIZE));
  528. }
  529. #endif
  530. #if defined(MEM_STATS)
  531. /* This is after we have printed stats, and this may slow us down a little, */
  532. /* so we may want to simply re-initialize the mem-stats at the end of this loop. */
  533. mem_stats_free(p->size);
  534. #endif
  535. tmp = p;
  536. p = p->next;
  537. remove_node(tmp);
  538. FREE(tmp);
  539. }
  540. if (f != NULL)
  541. fclose(f);
  542. if (totsize > 0)
  543. Debug_Info("\n%lu bytes reclaimed\n", totsize);
  544. poolno = 0;
  545. #endif
  546. #if defined(MEM_STATS)
  547. /* reinitialize the stats structure for next time through */
  548. mem_stats_init();
  549. #endif
  550. }
  551. /****************************************************************************/
  552. #if defined(MEM_RECLAIM)
  553. /* Adds a new node to the 'allocated' list */
  554. static void add_node(MEMNODE *node)
  555. {
  556. #if defined(MEM_TAG)
  557. if (!mem_check_tag(node))
  558. Debug_Info("add_node(): Memory pointer corrupt!\n");
  559. #endif /* MEM_TAG */
  560. if (memlist == NULL)
  561. {
  562. memlist = node;
  563. node->poolno = poolno;
  564. node->prev = NULL;
  565. node->next = NULL;
  566. num_nodes = 0;
  567. }
  568. else
  569. {
  570. memlist->prev = node;
  571. node->poolno = poolno;
  572. node->prev = NULL;
  573. node->next = memlist;
  574. memlist = node;
  575. }
  576. num_nodes++;
  577. }
  578. /****************************************************************************/
  579. /* Detatches a node from the 'allocated' list but doesn't free it */
  580. static void remove_node(MEMNODE *node)
  581. {
  582. #if defined(MEM_TAG)
  583. if (!mem_check_tag(node))
  584. Debug_Info("remove_node(): Memory pointer corrupt!\n");
  585. #endif /* MEM_TAG */
  586. num_nodes--;
  587. if (node->prev != NULL)
  588. node->prev->next = node->next;
  589. if (node->next != NULL)
  590. node->next->prev = node->prev;
  591. if (memlist == node)
  592. {
  593. #if defined(MEM_TAG)
  594. /* check node->next if it is non-null, to insure it is safe to assign. */
  595. /* if it is null, it is safe since it is the last in the list. */
  596. if (node->next)
  597. if (!mem_check_tag(node->next))
  598. Debug_Info("remove_node(): memlist pointer corrupt!\n");
  599. #endif /* MEM_TAG */
  600. memlist = node->next;
  601. }
  602. }
  603. #endif /* MEM_RECLAIM */
  604. /****************************************************************************/
  605. /* A strdup routine that uses POV_MALLOC */
  606. /****************************************************************************/
  607. char *pov_strdup(char *s)
  608. {
  609. char *New;
  610. New=(char *)POV_MALLOC(strlen(s)+1,s);
  611. strcpy(New,s);
  612. return (New);
  613. }
  614. /****************************************************************************/
  615. /* A memmove routine for those systems that don't have one */
  616. /****************************************************************************/
  617. void *pov_memmove (void *dest, void *src, size_t length)
  618. {
  619. char *csrc =(char *)src;
  620. char *cdest=(char *)dest;
  621. if (csrc < cdest && csrc + length >= cdest)
  622. {
  623. size_t size = cdest - csrc;
  624. while (length > 0)
  625. {
  626. memcpy(cdest + length - size, csrc + length - size, size);
  627. length -= size;
  628. if (length < size)
  629. size = length;
  630. }
  631. }
  632. /* I'm not sure if this is needed, but my docs on memcpy say the regions
  633. * can't overlap, so theoretically we need to special case this. If you
  634. * don't think it's necessary, you can just comment this part out.
  635. */
  636. else if (cdest < csrc && cdest + length >= csrc)
  637. {
  638. char *new_dest = cdest;
  639. size_t size = csrc - cdest;
  640. while (length > 0)
  641. {
  642. memcpy(new_dest, csrc, length);
  643. new_dest += size;
  644. csrc += size;
  645. length -= size;
  646. if (length < size)
  647. size = length;
  648. }
  649. }
  650. else
  651. {
  652. memcpy(cdest, csrc, length);
  653. }
  654. return cdest;
  655. }
  656. /****************************************************************************/
  657. /* Memory Statistics gathering routines */
  658. /****************************************************************************/
  659. #if defined(MEM_STATS)
  660. /****************************************************************************/
  661. static void mem_stats_init()
  662. {
  663. mem_stats.smallest_alloc = 65535; /* Must be an unsigned number */
  664. mem_stats.largest_alloc = 0;
  665. mem_stats.current_mem_usage = 0;
  666. mem_stats.largest_mem_usage = 0;
  667. #if (MEM_STATS>=2)
  668. mem_stats.total_allocs = 0;
  669. mem_stats.total_frees = 0;
  670. mem_stats.largest_file = "none";
  671. mem_stats.largest_line = -1;
  672. mem_stats.smallest_file = "none";
  673. mem_stats.smallest_line = -1;
  674. #endif
  675. }
  676. /****************************************************************************/
  677. /* update appropriate fields when an allocation takes place */
  678. static void mem_stats_alloc(size_t nbytes, char *file, int line)
  679. {
  680. /* update the fields */
  681. if ((mem_stats.smallest_alloc<0) || (nbytes<mem_stats.smallest_alloc))
  682. {
  683. mem_stats.smallest_alloc = nbytes;
  684. #if (MEM_STATS>=2)
  685. mem_stats.smallest_file = file;
  686. mem_stats.smallest_line = line;
  687. #endif
  688. }
  689. if (nbytes>mem_stats.largest_alloc)
  690. {
  691. mem_stats.largest_alloc = nbytes;
  692. #if (MEM_STATS>=2)
  693. mem_stats.largest_file = file;
  694. mem_stats.largest_line = line;
  695. #endif
  696. }
  697. #if (MEM_STATS>=2)
  698. mem_stats.total_allocs++;
  699. #endif
  700. mem_stats.current_mem_usage += nbytes;
  701. if (mem_stats.current_mem_usage>mem_stats.largest_mem_usage)
  702. {
  703. mem_stats.largest_mem_usage = mem_stats.current_mem_usage;
  704. }
  705. }
  706. /****************************************************************************/
  707. /* update appropriate fields when a free takes place */
  708. static void mem_stats_free(size_t nbytes)
  709. {
  710. /* update the fields */
  711. mem_stats.current_mem_usage -= nbytes;
  712. #if (MEM_STATS>=2)
  713. mem_stats.total_frees++;
  714. #endif
  715. }
  716. /****************************************************************************/
  717. /* Level 1 */
  718. /****************************************************************************/
  719. size_t mem_stats_smallest_alloc()
  720. {
  721. return mem_stats.smallest_alloc;
  722. }
  723. /****************************************************************************/
  724. size_t mem_stats_largest_alloc()
  725. {
  726. return mem_stats.largest_alloc;
  727. }
  728. /****************************************************************************/
  729. size_t mem_stats_current_mem_usage()
  730. {
  731. return mem_stats.current_mem_usage;
  732. }
  733. /****************************************************************************/
  734. size_t mem_stats_largest_mem_usage()
  735. {
  736. return mem_stats.largest_mem_usage;
  737. }
  738. /****************************************************************************/
  739. /* Level 2 */
  740. #if (MEM_STATS>=2)
  741. /****************************************************************************/
  742. char *mem_stats_smallest_file()
  743. {
  744. return mem_stats.smallest_file;
  745. }
  746. /****************************************************************************/
  747. int mem_stats_smallest_line()
  748. {
  749. return mem_stats.smallest_line;
  750. }
  751. /****************************************************************************/
  752. char *mem_stats_largest_file()
  753. {
  754. return mem_stats.largest_file;
  755. }
  756. /****************************************************************************/
  757. int mem_stats_largest_line()
  758. {
  759. return mem_stats.largest_line;
  760. }
  761. /****************************************************************************/
  762. long int mem_stats_total_allocs()
  763. {
  764. return mem_stats.total_allocs;
  765. }
  766. /****************************************************************************/
  767. long int mem_stats_total_frees()
  768. {
  769. return mem_stats.total_frees;
  770. }
  771. #endif
  772. #endif /* MEM_STATS */