pa_allocation.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * $Id: pa_allocation.c 1097 2006-08-26 08:27:53Z rossb $
  3. * Portable Audio I/O Library allocation group implementation
  4. * memory allocation group for tracking allocation groups
  5. *
  6. * Based on the Open Source API proposed by Ross Bencina
  7. * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files
  11. * (the "Software"), to deal in the Software without restriction,
  12. * including without limitation the rights to use, copy, modify, merge,
  13. * publish, distribute, sublicense, and/or sell copies of the Software,
  14. * and to permit persons to whom the Software is furnished to do so,
  15. * subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  24. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  25. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * The text above constitutes the entire PortAudio license; however,
  30. * the PortAudio community also makes the following non-binding requests:
  31. *
  32. * Any person wishing to distribute modifications to the Software is
  33. * requested to send the modifications to the original developer so that
  34. * they can be incorporated into the canonical version. It is also
  35. * requested that these non-binding requests be included along with the
  36. * license above.
  37. */
  38. /** @file
  39. @ingroup common_src
  40. @brief Allocation Group implementation.
  41. */
  42. #include "pa_allocation.h"
  43. #include "pa_util.h"
  44. /*
  45. Maintain 3 singly linked lists...
  46. linkBlocks: the buffers used to allocate the links
  47. spareLinks: links available for use in the allocations list
  48. allocations: the buffers currently allocated using PaUtil_ContextAllocateMemory()
  49. Link block size is doubled every time new links are allocated.
  50. */
  51. #define PA_INITIAL_LINK_COUNT_ 16
  52. struct PaUtilAllocationGroupLink
  53. {
  54. struct PaUtilAllocationGroupLink *next;
  55. void *buffer;
  56. };
  57. /*
  58. Allocate a block of links. The first link will have it's buffer member
  59. pointing to the block, and it's next member set to <nextBlock>. The remaining
  60. links will have NULL buffer members, and each link will point to
  61. the next link except the last, which will point to <nextSpare>
  62. */
  63. static struct PaUtilAllocationGroupLink *AllocateLinks( long count,
  64. struct PaUtilAllocationGroupLink *nextBlock,
  65. struct PaUtilAllocationGroupLink *nextSpare )
  66. {
  67. struct PaUtilAllocationGroupLink *result;
  68. int i;
  69. result = (struct PaUtilAllocationGroupLink *)PaUtil_AllocateMemory(
  70. sizeof(struct PaUtilAllocationGroupLink) * count );
  71. if( result )
  72. {
  73. /* the block link */
  74. result[0].buffer = result;
  75. result[0].next = nextBlock;
  76. /* the spare links */
  77. for( i=1; i<count; ++i )
  78. {
  79. result[i].buffer = 0;
  80. result[i].next = &result[i+1];
  81. }
  82. result[count-1].next = nextSpare;
  83. }
  84. return result;
  85. }
  86. PaUtilAllocationGroup* PaUtil_CreateAllocationGroup( void )
  87. {
  88. PaUtilAllocationGroup* result = 0;
  89. struct PaUtilAllocationGroupLink *links;
  90. links = AllocateLinks( PA_INITIAL_LINK_COUNT_, 0, 0 );
  91. if( links != 0 )
  92. {
  93. result = (PaUtilAllocationGroup*)PaUtil_AllocateMemory( sizeof(PaUtilAllocationGroup) );
  94. if( result )
  95. {
  96. result->linkCount = PA_INITIAL_LINK_COUNT_;
  97. result->linkBlocks = &links[0];
  98. result->spareLinks = &links[1];
  99. result->allocations = 0;
  100. }
  101. else
  102. {
  103. PaUtil_FreeMemory( links );
  104. }
  105. }
  106. return result;
  107. }
  108. void PaUtil_DestroyAllocationGroup( PaUtilAllocationGroup* group )
  109. {
  110. struct PaUtilAllocationGroupLink *current = group->linkBlocks;
  111. struct PaUtilAllocationGroupLink *next;
  112. while( current )
  113. {
  114. next = current->next;
  115. PaUtil_FreeMemory( current->buffer );
  116. current = next;
  117. }
  118. PaUtil_FreeMemory( group );
  119. }
  120. void* PaUtil_GroupAllocateMemory( PaUtilAllocationGroup* group, long size )
  121. {
  122. struct PaUtilAllocationGroupLink *links, *link;
  123. void *result = 0;
  124. /* allocate more links if necessary */
  125. if( !group->spareLinks )
  126. {
  127. /* double the link count on each block allocation */
  128. links = AllocateLinks( group->linkCount, group->linkBlocks, group->spareLinks );
  129. if( links )
  130. {
  131. group->linkCount += group->linkCount;
  132. group->linkBlocks = &links[0];
  133. group->spareLinks = &links[1];
  134. }
  135. }
  136. if( group->spareLinks )
  137. {
  138. result = PaUtil_AllocateMemory( size );
  139. if( result )
  140. {
  141. link = group->spareLinks;
  142. group->spareLinks = link->next;
  143. link->buffer = result;
  144. link->next = group->allocations;
  145. group->allocations = link;
  146. }
  147. }
  148. return result;
  149. }
  150. void PaUtil_GroupFreeMemory( PaUtilAllocationGroup* group, void *buffer )
  151. {
  152. struct PaUtilAllocationGroupLink *current = group->allocations;
  153. struct PaUtilAllocationGroupLink *previous = 0;
  154. if( buffer == 0 )
  155. return;
  156. /* find the right link and remove it */
  157. while( current )
  158. {
  159. if( current->buffer == buffer )
  160. {
  161. if( previous )
  162. {
  163. previous->next = current->next;
  164. }
  165. else
  166. {
  167. group->allocations = current->next;
  168. }
  169. current->buffer = 0;
  170. current->next = group->spareLinks;
  171. group->spareLinks = current;
  172. break;
  173. }
  174. previous = current;
  175. current = current->next;
  176. }
  177. PaUtil_FreeMemory( buffer ); /* free the memory whether we found it in the list or not */
  178. }
  179. void PaUtil_FreeAllAllocations( PaUtilAllocationGroup* group )
  180. {
  181. struct PaUtilAllocationGroupLink *current = group->allocations;
  182. struct PaUtilAllocationGroupLink *previous = 0;
  183. /* free all buffers in the allocations list */
  184. while( current )
  185. {
  186. PaUtil_FreeMemory( current->buffer );
  187. current->buffer = 0;
  188. previous = current;
  189. current = current->next;
  190. }
  191. /* link the former allocations list onto the front of the spareLinks list */
  192. if( previous )
  193. {
  194. previous->next = group->spareLinks;
  195. group->spareLinks = group->allocations;
  196. group->allocations = 0;
  197. }
  198. }