AUD_Memory.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*****************************************************************************
  19. ** **
  20. ** Westwood Studios Pacific. **
  21. ** **
  22. ** Confidential Information **
  23. ** Copyright (C) 2000 - All Rights Reserved **
  24. ** **
  25. ******************************************************************************
  26. ** **
  27. ** Project: Dune Emperor **
  28. ** **
  29. ** Module: <module> (<prefix>_) **
  30. ** **
  31. ** Version: $ID$ **
  32. ** **
  33. ** File name: audmpool.cpp **
  34. ** **
  35. ** Created by: 10/23/95 TR **
  36. ** **
  37. ** Description: <description> **
  38. ** **
  39. *****************************************************************************/
  40. /*****************************************************************************
  41. ** Includes **
  42. *****************************************************************************/
  43. #include <string.h>
  44. #include <wpaudio/altypes.h>
  45. #include <wpaudio/memory.h>
  46. // 'assignment within condition expression'.
  47. #pragma warning(disable : 4706)
  48. DBG_DECLARE_TYPE ( AudioMemoryPool )
  49. DBG_DECLARE_TYPE ( MemoryItem )
  50. /*****************************************************************************
  51. ** Externals **
  52. *****************************************************************************/
  53. /*****************************************************************************
  54. ** Defines **
  55. *****************************************************************************/
  56. /*****************************************************************************
  57. ** Private Types **
  58. *****************************************************************************/
  59. /*****************************************************************************
  60. ** Private Data **
  61. *****************************************************************************/
  62. /*****************************************************************************
  63. ** Public Data **
  64. *****************************************************************************/
  65. /*****************************************************************************
  66. ** Private Prototypes **
  67. *****************************************************************************/
  68. /*****************************************************************************
  69. ** Private Functions **
  70. *****************************************************************************/
  71. /*****************************************************************************
  72. ** Public Functions **
  73. *****************************************************************************/
  74. /******************************************************************/
  75. /* */
  76. /* */
  77. /******************************************************************/
  78. AudioMemoryPool* MemoryPoolCreate( uint items, uint size )
  79. {
  80. uint poolsize;
  81. AudioMemoryPool *pool;
  82. MemoryItem *item;
  83. uint i;
  84. DBG_ASSERT ( items > 0 );
  85. DBG_ASSERT ( size > 0 );
  86. poolsize = items*(size + sizeof(MemoryItem)) + sizeof (AudioMemoryPool);
  87. if ((pool = (AudioMemoryPool *) AudioMemAlloc ( poolsize )))
  88. {
  89. item = (MemoryItem *)( (uint)pool + (uint) sizeof(AudioMemoryPool));
  90. pool->next = item;
  91. DBG_CODE ( pool->itemsOut = 0;)
  92. DBG_CODE ( pool->numItems = items;)
  93. DBG_SET_TYPE ( pool, AudioMemoryPool );
  94. for ( i=0; i < items-1; i++)
  95. {
  96. DBG_SET_TYPE ( item, MemoryItem );
  97. item->next = (MemoryItem *) ( (uint) item + (uint) (sizeof(MemoryItem) + size) );
  98. item = item->next;
  99. }
  100. item->next = NULL;
  101. DBG_SET_TYPE ( item, MemoryItem );
  102. }
  103. return pool;
  104. }
  105. /******************************************************************/
  106. /* */
  107. /* */
  108. /******************************************************************/
  109. void MemoryPoolDestroy ( AudioMemoryPool *pool )
  110. {
  111. DBG_ASSERT_TYPE ( pool, AudioMemoryPool );
  112. DBG_CODE
  113. (
  114. if ( pool->itemsOut > 0 )
  115. {
  116. DBGPRINTF ( ( "Destroying memory pool with %d items still out\n", pool->itemsOut) );
  117. }
  118. )
  119. AudioMemFree ( pool );
  120. }
  121. /******************************************************************/
  122. /* */
  123. /* */
  124. /******************************************************************/
  125. void *MemoryPoolGetItem ( AudioMemoryPool *pool )
  126. {
  127. MemoryItem *item = NULL;
  128. DBG_ASSERT_TYPE ( pool, AudioMemoryPool );
  129. if (! (item = pool->next) )
  130. {
  131. return NULL;
  132. }
  133. DBG_CODE ( pool->itemsOut++;)
  134. DBG_MSGASSERT ( pool->itemsOut <= pool->numItems,( "pool overflow" ));
  135. DBG_ASSERT_TYPE ( item, MemoryItem ); // !!! Memory corruption !!!
  136. pool->next = item->next;
  137. return (void *) ( (uint) item + (uint) sizeof(MemoryItem));
  138. }
  139. /******************************************************************/
  140. /* */
  141. /* */
  142. /******************************************************************/
  143. void MemoryPoolReturnItem ( AudioMemoryPool *pool, void *data )
  144. {
  145. MemoryItem *item;
  146. DBG_ASSERT_TYPE ( pool, AudioMemoryPool );
  147. DBG_ASSERT_PTR ( data );
  148. item = (MemoryItem *) ( (uint) data - (uint) sizeof(MemoryItem));
  149. DBG_ASSERT_TYPE ( item, MemoryItem ); // returning invalid item to pool
  150. item->next = pool->next;
  151. pool->next = item;
  152. DBG_MSGASSERT ( pool->itemsOut > 0,( "Pool underflow" )); // returning more items than were taken
  153. DBG_CODE ( pool->itemsOut--; )
  154. }
  155. /******************************************************************/
  156. /* */
  157. /* */
  158. /******************************************************************/
  159. int MemoryPoolCount ( AudioMemoryPool *pool )
  160. {
  161. MemoryItem *item = NULL;
  162. int count = 0;
  163. DBG_ASSERT_TYPE ( pool, AudioMemoryPool );
  164. if ( (item = pool->next) )
  165. {
  166. while ( item )
  167. {
  168. count++;
  169. item = item->next;
  170. }
  171. }
  172. return count;
  173. }
  174. /******************************************************************/
  175. /* */
  176. /* */
  177. /******************************************************************/
  178. void AudioAddSlash ( char *string )
  179. {
  180. int len = strlen ( string );
  181. if ( len )
  182. {
  183. if ( string[len-1] != '\\' )
  184. {
  185. string[len] = '\\';
  186. string[len+1] = 0;
  187. }
  188. }
  189. }
  190. /******************************************************************/
  191. /* */
  192. /* */
  193. /******************************************************************/
  194. int AudioHasPath ( const char *string )
  195. {
  196. return ( strchr ( string, ':' ) || strchr ( string, '\\' ) || strchr ( string, '/' ) || strchr ( string, '.'));
  197. }