| 1 | /* $Id: bufpool.h,v 1.1 2000-02-09 08:49:49 jeroen Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | ** License Applicability. Except to the extent portions of this file are
|
|---|
| 4 | ** made subject to an alternative license as permitted in the SGI Free
|
|---|
| 5 | ** Software License B, Version 1.0 (the "License"), the contents of this
|
|---|
| 6 | ** file are subject only to the provisions of the License. You may not use
|
|---|
| 7 | ** this file except in compliance with the License. You may obtain a copy
|
|---|
| 8 | ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
|
|---|
| 9 | ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
|
|---|
| 10 | **
|
|---|
| 11 | ** http://oss.sgi.com/projects/FreeB
|
|---|
| 12 | **
|
|---|
| 13 | ** Note that, as provided in the License, the Software is distributed on an
|
|---|
| 14 | ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
|
|---|
| 15 | ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
|
|---|
| 16 | ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
|
|---|
| 17 | ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
|
|---|
| 18 | **
|
|---|
| 19 | ** Original Code. The Original Code is: OpenGL Sample Implementation,
|
|---|
| 20 | ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
|
|---|
| 21 | ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
|
|---|
| 22 | ** Copyright in any portions created by third parties is as indicated
|
|---|
| 23 | ** elsewhere herein. All Rights Reserved.
|
|---|
| 24 | **
|
|---|
| 25 | ** Additional Notice Provisions: The application programming interfaces
|
|---|
| 26 | ** established by SGI in conjunction with the Original Code are The
|
|---|
| 27 | ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
|
|---|
| 28 | ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
|
|---|
| 29 | ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
|
|---|
| 30 | ** Window System(R) (Version 1.3), released October 19, 1998. This software
|
|---|
| 31 | ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
|
|---|
| 32 | ** published by SGI, but has not been independently verified as being
|
|---|
| 33 | ** compliant with the OpenGL(R) version 1.2.1 Specification.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | /*
|
|---|
| 37 | * bufpool.h
|
|---|
| 38 | *
|
|---|
| 39 | * $Date: 2000-02-09 08:49:49 $ $Revision: 1.1 $
|
|---|
| 40 | * $Header: /home/ktk/tmp/odin/2007/netlabs.cvs/odin32/src/opengl/glu/nurbs/internals/bufpool.h,v 1.1 2000-02-09 08:49:49 jeroen Exp $
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | #ifndef __glubufpool_h_
|
|---|
| 44 | #define __glubufpool_h_
|
|---|
| 45 |
|
|---|
| 46 | #include "zlassert.h"
|
|---|
| 47 | #include "myassert.h"
|
|---|
| 48 | #include "mystdlib.h"
|
|---|
| 49 |
|
|---|
| 50 | #define NBLOCKS 32
|
|---|
| 51 |
|
|---|
| 52 | class Buffer {
|
|---|
| 53 | friend class Pool;
|
|---|
| 54 | Buffer * next; /* next buffer on free list */
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | class Pool {
|
|---|
| 58 | public:
|
|---|
| 59 | Pool( int, int, char * );
|
|---|
| 60 | ~Pool( void );
|
|---|
| 61 | inline void* new_buffer( void );
|
|---|
| 62 | inline void free_buffer( void * );
|
|---|
| 63 | void clear( void );
|
|---|
| 64 |
|
|---|
| 65 | private:
|
|---|
| 66 | void grow( void );
|
|---|
| 67 |
|
|---|
| 68 | protected:
|
|---|
| 69 | Buffer *freelist; /* linked list of free buffers */
|
|---|
| 70 | char *blocklist[NBLOCKS]; /* blocks of malloced memory */
|
|---|
| 71 | int nextblock; /* next free block index */
|
|---|
| 72 | char *curblock; /* last malloced block */
|
|---|
| 73 | int buffersize; /* bytes per buffer */
|
|---|
| 74 | int nextsize; /* size of next block of memory */
|
|---|
| 75 | int nextfree; /* byte offset past next free buffer */
|
|---|
| 76 | int initsize;
|
|---|
| 77 | enum Magic { is_allocated = 0xf3a1, is_free = 0xf1a2 };
|
|---|
| 78 | char *name; /* name of the pool */
|
|---|
| 79 | Magic magic; /* marker for valid pool */
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | /*-----------------------------------------------------------------------------
|
|---|
| 83 | * Pool::free_buffer - return a buffer to a pool
|
|---|
| 84 | *-----------------------------------------------------------------------------
|
|---|
| 85 | */
|
|---|
| 86 |
|
|---|
| 87 | inline void
|
|---|
| 88 | Pool::free_buffer( void *b )
|
|---|
| 89 | {
|
|---|
| 90 | assert( (this != 0) && (magic == is_allocated) );
|
|---|
| 91 |
|
|---|
| 92 | /* add buffer to singly connected free list */
|
|---|
| 93 |
|
|---|
| 94 | ((Buffer *) b)->next = freelist;
|
|---|
| 95 | freelist = (Buffer *) b;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | /*-----------------------------------------------------------------------------
|
|---|
| 100 | * Pool::new_buffer - allocate a buffer from a pool
|
|---|
| 101 | *-----------------------------------------------------------------------------
|
|---|
| 102 | */
|
|---|
| 103 |
|
|---|
| 104 | inline void *
|
|---|
| 105 | Pool::new_buffer( void )
|
|---|
| 106 | {
|
|---|
| 107 | void *buffer;
|
|---|
| 108 |
|
|---|
| 109 | assert( (this != 0) && (magic == is_allocated) );
|
|---|
| 110 |
|
|---|
| 111 | /* find free buffer */
|
|---|
| 112 |
|
|---|
| 113 | if( freelist ) {
|
|---|
| 114 | buffer = (void *) freelist;
|
|---|
| 115 | freelist = freelist->next;
|
|---|
| 116 | } else {
|
|---|
| 117 | if( ! nextfree )
|
|---|
| 118 | grow( );
|
|---|
| 119 | nextfree -= buffersize;;
|
|---|
| 120 | buffer = (void *) (curblock + nextfree);
|
|---|
| 121 | }
|
|---|
| 122 | return buffer;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | class PooledObj {
|
|---|
| 126 | public:
|
|---|
| 127 | #ifdef __DEBUG_ALLOC__
|
|---|
| 128 | inline void * operator new( size_t, const char *filename, size_t lineno, Pool &);
|
|---|
| 129 | inline void * operator new( size_t, const char *filename, size_t lineno, void *);
|
|---|
| 130 | inline void * operator new( size_t s , const char *filename, size_t lineno)
|
|---|
| 131 | { return ::new char[s]; }
|
|---|
| 132 | #else
|
|---|
| 133 | inline void * operator new( size_t, Pool & );
|
|---|
| 134 | inline void * operator new( size_t, void *);
|
|---|
| 135 | inline void * operator new( size_t s)
|
|---|
| 136 | { return ::new char[s]; }
|
|---|
| 137 | #endif
|
|---|
| 138 | /* inline void operator delete( void* ) { assert( 0 ); }*/
|
|---|
| 139 | inline void deleteMe( Pool & );
|
|---|
| 140 | };
|
|---|
| 141 |
|
|---|
| 142 | inline void *
|
|---|
| 143 | #ifdef __DEBUG_ALLOC__
|
|---|
| 144 | PooledObj::operator new( size_t, const char *filename, size_t lineno, Pool &pool)
|
|---|
| 145 | #else
|
|---|
| 146 | PooledObj::operator new( size_t, Pool& pool )
|
|---|
| 147 | #endif
|
|---|
| 148 | {
|
|---|
| 149 | return pool.new_buffer();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | inline void
|
|---|
| 153 | PooledObj::deleteMe( Pool& pool )
|
|---|
| 154 | {
|
|---|
| 155 | pool.free_buffer( (void *) this );
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | #endif /* __glubufpool_h_ */
|
|---|