zmem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright (C) 2004 Michael Liebscher
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * zmem.c: Zone memory management.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. *
  21. * Acknowledgement:
  22. * This code was derived from Quake II, and was originally
  23. * written by Id Software, Inc.
  24. *
  25. */
  26. /*
  27. Notes:
  28. Add the following line in your initization function:
  29. z_chain.next = z_chain.prev = &z_chain;
  30. */
  31. #include "../wolfiphone.h"
  32. // just cleared malloc with counters now...
  33. #define Z_MAGIC 0x1d1d
  34. PRIVATE int z_count, z_bytes;
  35. zhead_t z_chain;
  36. /*
  37. -----------------------------------------------------------------------------
  38. Function: Z_Free -Deallocates or frees a zone memory block.
  39. Parameters: memblock -[in] Previously allocated zone memory block to be freed.
  40. Returns: Nothing.
  41. Notes:
  42. -----------------------------------------------------------------------------
  43. */
  44. PUBLIC void Z_Free( void *memblock )
  45. {
  46. zhead_t *z;
  47. z = ( (zhead_t *)memblock ) - 1;
  48. if( z->magic != Z_MAGIC )
  49. {
  50. Com_Error( ERR_FATAL, "Z_Free: bad magic" );
  51. }
  52. z->prev->next = z->next;
  53. z->next->prev = z->prev;
  54. z_count--;
  55. z_bytes -= z->size;
  56. MM_FREE( z );
  57. }
  58. /*
  59. -----------------------------------------------------------------------------
  60. Function: Z_Stats_f -Console function to list zone memory usage.
  61. Parameters: Nothing.
  62. Returns: Nothing.
  63. Notes: Lists number of bytes and blocks of zone memory allocated.
  64. -----------------------------------------------------------------------------
  65. */
  66. PUBLIC void Z_Stats_f( void )
  67. {
  68. Com_Printf( "%i bytes in %i blocks\n", z_bytes, z_count );
  69. }
  70. /*
  71. -----------------------------------------------------------------------------
  72. Function: Z_FreeTags -Free allocated zone memory blocks based on tag.
  73. Parameters: tag -[in] Tag of zone memory blocks to free (see header for tag).
  74. Returns: Nothing.
  75. Notes:
  76. -----------------------------------------------------------------------------
  77. */
  78. PUBLIC void Z_FreeTags( int tag )
  79. {
  80. zhead_t *z, *next;
  81. for( z = z_chain.next; z != &z_chain; z = next )
  82. {
  83. next = z->next;
  84. if( z->tag == tag )
  85. {
  86. Z_Free( (void *)(z+1) );
  87. }
  88. }
  89. }
  90. /*
  91. -----------------------------------------------------------------------------
  92. Function: Z_TagMalloc -Allocates zone memory blocks.
  93. Parameters:
  94. size -[in] Bytes to allocate.
  95. tag -[in] Tag to associate with memory (see header for tag).
  96. Returns:
  97. A void pointer to the allocated space, or will shutdown application
  98. if there is insufficient memory available.
  99. Notes:
  100. -----------------------------------------------------------------------------
  101. */
  102. PUBLIC void *Z_TagMalloc( size_t size, int tag )
  103. {
  104. zhead_t *z;
  105. // Allocate memory
  106. size += sizeof( zhead_t );
  107. z = MM_MALLOC( size );
  108. if( ! z )
  109. {
  110. Com_Error( ERR_FATAL, "Z_Malloc: failed on allocation of %i bytes", size );
  111. }
  112. // Set memory block to zero and fill in header.
  113. memset( z, 0, size );
  114. z_count++;
  115. z_bytes += size;
  116. z->magic = Z_MAGIC;
  117. z->tag = tag;
  118. z->size = size;
  119. // Add new memory block to chain.
  120. z->next = z_chain.next;
  121. z->prev = &z_chain;
  122. z_chain.next->prev = z;
  123. z_chain.next = z;
  124. return (void *)(z+1);
  125. }
  126. /*
  127. -----------------------------------------------------------------------------
  128. Function: Z_Malloc -Allocates zone memory blocks.
  129. Parameters: size -[in] Bytes to allocate.
  130. Returns:
  131. A void pointer to the allocated space, or will shutdown application
  132. if there is insufficient memory available.
  133. Notes: Calls Z_TagMalloc() with tag set to zero.
  134. -----------------------------------------------------------------------------
  135. */
  136. PUBLIC void *Z_Malloc( size_t size )
  137. {
  138. return Z_TagMalloc( size, 0 );
  139. }