Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.9 KiB

  1. /**************************************************************************
  2. * *
  3. * Copyright (C) 1988, Silicon Graphics, Inc. *
  4. * *
  5. * These coded instructions, statements, and computer programs contain *
  6. * unpublished proprietary information of Silicon Graphics, Inc., and *
  7. * are protected by Federal copyright law. They may not be disclosed *
  8. * to third parties or copied or duplicated in any form, in whole or *
  9. * in part, without the prior written consent of Silicon Graphics, Inc. *
  10. * *
  11. **************************************************************************/
  12. /*
  13. * bufpool.c
  14. *
  15. * $Revision: 1.1 $
  16. */
  17. #ifndef NT
  18. #include <assert.h>
  19. #include <stdlib.h>
  20. #else
  21. #include "winmem.h"
  22. #endif
  23. #include "bufpool.h"
  24. /* local functions */
  25. static void grow_pool( register Pool * );
  26. /*-----------------------------------------------------------------------------
  27. * new_pool - allocate a new pool of buffers
  28. *-----------------------------------------------------------------------------
  29. */
  30. Pool *
  31. __gl_new_pool( int buffersize, int initpoolsize, char *name )
  32. {
  33. register Pool *p;
  34. p = (Pool *) malloc( sizeof (Pool) );
  35. p->buffersize= (buffersize < sizeof(Buffer)) ? sizeof(Buffer)
  36. : buffersize;
  37. p->nextsize = initpoolsize * p->buffersize;
  38. #ifndef NDEBUG
  39. p->name = name;
  40. p->magic = is_allocated;
  41. #endif
  42. p->nextblock= 0;
  43. p->curblock = 0;
  44. p->freelist = 0;
  45. p->nextfree = 0;
  46. return p;
  47. }
  48. /*-----------------------------------------------------------------------------
  49. * new_buffer - allocate a buffer from a pool
  50. *-----------------------------------------------------------------------------
  51. */
  52. char *
  53. __gl_new_buffer( register Pool *p )
  54. {
  55. char *buffer;
  56. #ifndef NT
  57. #ifndef NDEBUG
  58. assert( p && (p->magic == is_allocated) );
  59. #endif
  60. #endif
  61. /* find free buffer */
  62. if( p->freelist ) {
  63. buffer = (char *) p->freelist;
  64. p->freelist = p->freelist->next;
  65. } else {
  66. if( ! p->nextfree )
  67. grow_pool( p );
  68. p->nextfree -= p->buffersize;;
  69. buffer = p->curblock + p->nextfree;
  70. }
  71. return buffer;
  72. }
  73. static void
  74. grow_pool( register Pool *p )
  75. {
  76. #ifndef NT
  77. #ifndef NDEBUG
  78. assert( p && (p->magic == is_allocated) );
  79. #endif
  80. #endif
  81. p->curblock = (char *) malloc( p->nextsize );
  82. p->blocklist[p->nextblock++] = p->curblock;
  83. p->nextfree = p->nextsize;
  84. p->nextsize *= 2;
  85. }
  86. /*-----------------------------------------------------------------------------
  87. * free_buffer - return a buffer to a pool
  88. *-----------------------------------------------------------------------------
  89. */
  90. void
  91. __gl_free_buffer( Pool *p, void *b )
  92. {
  93. #ifndef NT
  94. #ifndef NDEBUG
  95. assert( p && (p->magic == is_allocated) );
  96. #endif
  97. #endif
  98. /* add buffer to singly connected free list */
  99. ((Buffer *) b)->next = p->freelist;
  100. p->freelist = (Buffer *) b;
  101. }
  102. /*-----------------------------------------------------------------------------
  103. * free_pool - free a pool of buffers and the pool itself
  104. *-----------------------------------------------------------------------------
  105. */
  106. void
  107. __gl_free_pool( Pool *p )
  108. {
  109. #ifndef NT
  110. #ifndef NDEBUG
  111. assert( p && (p->magic == is_allocated) );
  112. #endif
  113. #endif
  114. while( p->nextblock )
  115. free( p->blocklist[--(p->nextblock)] );
  116. #ifndef NDEBUG
  117. p->magic = is_free;
  118. #endif
  119. free( p );
  120. }
  121. /*-----------------------------------------------------------------------------
  122. * clear_pool - free buffers associated with pool but keep pool
  123. *-----------------------------------------------------------------------------
  124. */
  125. void
  126. __gl_clear_pool( Pool *p )
  127. {
  128. #ifndef NT
  129. #ifndef NDEBUG
  130. assert( p && (p->magic == is_allocated) );
  131. #endif
  132. #endif
  133. while( p->nextblock )
  134. free( p->blocklist[--(p->nextblock)] );
  135. p->curblock = 0;
  136. p->freelist = 0;
  137. p->nextfree = 0;
  138. if( p->nextsize >= 2 * p->buffersize )
  139. p->nextsize /= 2;
  140. }