Leaked source code of windows server 2003
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.

120 lines
3.1 KiB

  1. #ifndef __glubufpool_h_
  2. #define __glubufpool_h_
  3. /**************************************************************************
  4. * *
  5. * Copyright (C) 1992, Silicon Graphics, Inc. *
  6. * *
  7. * These coded instructions, statements, and computer programs contain *
  8. * unpublished proprietary information of Silicon Graphics, Inc., and *
  9. * are protected by Federal copyright law. They may not be disclosed *
  10. * to third parties or copied or duplicated in any form, in whole or *
  11. * in part, without the prior written consent of Silicon Graphics, Inc. *
  12. * *
  13. **************************************************************************/
  14. /*
  15. * bufpool.h - $Revision: 1.3 $
  16. */
  17. #include "myassert.h"
  18. #include "mystdlib.h"
  19. #define NBLOCKS 32
  20. class Buffer {
  21. friend class Pool;
  22. Buffer * next; /* next buffer on free list */
  23. };
  24. class Pool {
  25. public:
  26. Pool( int, int, char * );
  27. ~Pool( void );
  28. inline void* new_buffer( void );
  29. inline void free_buffer( void * );
  30. void clear( void );
  31. private:
  32. void grow( void );
  33. protected:
  34. Buffer *freelist; /* linked list of free buffers */
  35. char *blocklist[NBLOCKS]; /* blocks of malloced memory */
  36. int nextblock; /* next free block index */
  37. char *curblock; /* last malloced block */
  38. int buffersize; /* bytes per buffer */
  39. int nextsize; /* size of next block of memory */
  40. int nextfree; /* byte offset past next free buffer */
  41. int initsize;
  42. enum Magic { is_allocated = 0xf3a1, is_free = 0xf1a2 };
  43. char *name; /* name of the pool */
  44. Magic magic; /* marker for valid pool */
  45. };
  46. /*-----------------------------------------------------------------------------
  47. * Pool::free_buffer - return a buffer to a pool
  48. *-----------------------------------------------------------------------------
  49. */
  50. inline void
  51. Pool::free_buffer( void *b )
  52. {
  53. assert( (this != 0) && (magic == is_allocated) );
  54. /* add buffer to singly connected free list */
  55. ((Buffer *) b)->next = freelist;
  56. freelist = (Buffer *) b;
  57. }
  58. /*-----------------------------------------------------------------------------
  59. * Pool::new_buffer - allocate a buffer from a pool
  60. *-----------------------------------------------------------------------------
  61. */
  62. inline void *
  63. Pool::new_buffer( void )
  64. {
  65. void *buffer;
  66. assert( (this != 0) && (magic == is_allocated) );
  67. /* find free buffer */
  68. if( freelist ) {
  69. buffer = (void *) freelist;
  70. freelist = freelist->next;
  71. } else {
  72. if( ! nextfree )
  73. grow( );
  74. nextfree -= buffersize;;
  75. buffer = (void *) (curblock + nextfree);
  76. }
  77. return buffer;
  78. }
  79. class PooledObj {
  80. public:
  81. inline void * operator new( size_t, Pool & );
  82. inline void * operator new( size_t, void *);
  83. inline void * operator new( size_t s)
  84. { return ::new char[s]; }
  85. inline void operator delete( void * ) { assert( 0 ); }
  86. inline void deleteMe( Pool & );
  87. };
  88. inline void *
  89. PooledObj::operator new( size_t, Pool& pool )
  90. {
  91. return pool.new_buffer();
  92. }
  93. inline void
  94. PooledObj::deleteMe( Pool& pool )
  95. {
  96. pool.free_buffer( (void *) this );
  97. }
  98. #endif /* __glubufpool_h_ */