1f220fa62Smrg/*
2f220fa62Smrg * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3f220fa62Smrg * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4f220fa62Smrg *
5f220fa62Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6f220fa62Smrg * copy of this software and associated documentation files (the "Software"),
7f220fa62Smrg * to deal in the Software without restriction, including without limitation
8f220fa62Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9f220fa62Smrg * and/or sell copies of the Software, and to permit persons to whom the
10f220fa62Smrg * Software is furnished to do so, subject to the following conditions:
11f220fa62Smrg *
12f220fa62Smrg * The above copyright notice including the dates of first publication and
13f220fa62Smrg * either this permission notice or a reference to
14f220fa62Smrg * http://oss.sgi.com/projects/FreeB/
15f220fa62Smrg * shall be included in all copies or substantial portions of the Software.
16f220fa62Smrg *
17f220fa62Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18f220fa62Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19f220fa62Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20f220fa62Smrg * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21f220fa62Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22f220fa62Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23f220fa62Smrg * SOFTWARE.
24f220fa62Smrg *
25f220fa62Smrg * Except as contained in this notice, the name of Silicon Graphics, Inc.
26f220fa62Smrg * shall not be used in advertising or otherwise to promote the sale, use or
27f220fa62Smrg * other dealings in this Software without prior written authorization from
28f220fa62Smrg * Silicon Graphics, Inc.
29f220fa62Smrg */
30f220fa62Smrg
31f220fa62Smrg/*
32f220fa62Smrg * bufpool.h
33f220fa62Smrg *
34f220fa62Smrg */
35f220fa62Smrg
36f220fa62Smrg#ifndef __glubufpool_h_
37f220fa62Smrg#define __glubufpool_h_
38f220fa62Smrg
39f220fa62Smrg#include "gluos.h"
40f220fa62Smrg#include "myassert.h"
41f220fa62Smrg#include "mystdlib.h"
42f220fa62Smrg
43f220fa62Smrg#define NBLOCKS	32
44f220fa62Smrg
45f220fa62Smrgclass Buffer {
46f220fa62Smrg	friend class 	Pool;
47f220fa62Smrg	Buffer	*	next;		/* next buffer on free list	*/
48f220fa62Smrg};
49f220fa62Smrg
50f220fa62Smrgclass Pool {
51f220fa62Smrgpublic:
52f220fa62Smrg			Pool( int, int, const char * );
53f220fa62Smrg			~Pool( void );
54f220fa62Smrg    inline void*	new_buffer( void );
55f220fa62Smrg    inline void		free_buffer( void * );
56f220fa62Smrg    void		clear( void );
57f220fa62Smrg
58f220fa62Smrgprivate:
59f220fa62Smrg    void		grow( void );
60f220fa62Smrg
61f220fa62Smrgprotected:
62f220fa62Smrg    Buffer		*freelist;		/* linked list of free buffers */
63f220fa62Smrg    char		*blocklist[NBLOCKS];	/* blocks of malloced memory */
64f220fa62Smrg    int			nextblock;		/* next free block index */
65f220fa62Smrg    char		*curblock;		/* last malloced block */
66f220fa62Smrg    int			buffersize;		/* bytes per buffer */
67f220fa62Smrg    int			nextsize;		/* size of next block of memory	*/
68f220fa62Smrg    int			nextfree;		/* byte offset past next free buffer */
69f220fa62Smrg    int			initsize;
70f220fa62Smrg    enum Magic { is_allocated = 0xf3a1, is_free = 0xf1a2 };
71f220fa62Smrg    const char		*name;			/* name of the pool */
72f220fa62Smrg    Magic		magic;			/* marker for valid pool */
73f220fa62Smrg};
74f220fa62Smrg
75f220fa62Smrg/*-----------------------------------------------------------------------------
76f220fa62Smrg * Pool::free_buffer - return a buffer to a pool
77f220fa62Smrg *-----------------------------------------------------------------------------
78f220fa62Smrg */
79f220fa62Smrg
80f220fa62Smrginline void
81f220fa62SmrgPool::free_buffer( void *b )
82f220fa62Smrg{
83f220fa62Smrg    assert( (this != 0) && (magic == is_allocated) );
84f220fa62Smrg
85f220fa62Smrg    /* add buffer to singly connected free list */
86f220fa62Smrg
87f220fa62Smrg    ((Buffer *) b)->next = freelist;
88f220fa62Smrg    freelist = (Buffer *) b;
89f220fa62Smrg}
90f220fa62Smrg
91f220fa62Smrg
92f220fa62Smrg/*-----------------------------------------------------------------------------
93f220fa62Smrg * Pool::new_buffer - allocate a buffer from a pool
94f220fa62Smrg *-----------------------------------------------------------------------------
95f220fa62Smrg */
96f220fa62Smrg
97f220fa62Smrginline void *
98f220fa62SmrgPool::new_buffer( void )
99f220fa62Smrg{
100f220fa62Smrg    void *buffer;
101f220fa62Smrg
102f220fa62Smrg    assert( (this != 0) && (magic == is_allocated) );
103f220fa62Smrg
104f220fa62Smrg    /* find free buffer */
105f220fa62Smrg
106f220fa62Smrg    if( freelist ) {
107f220fa62Smrg    	buffer = (void *) freelist;
108f220fa62Smrg    	freelist = freelist->next;
109f220fa62Smrg    } else {
110f220fa62Smrg    	if( ! nextfree )
111f220fa62Smrg    	    grow( );
112f220fa62Smrg    	nextfree -= buffersize;;
113f220fa62Smrg    	buffer = (void *) (curblock + nextfree);
114f220fa62Smrg    }
115f220fa62Smrg    return buffer;
116f220fa62Smrg}
117f220fa62Smrg
118f220fa62Smrgclass PooledObj {
119f220fa62Smrgpublic:
120f220fa62Smrg    inline void *	operator new( size_t, Pool & );
121f220fa62Smrg    inline void * 	operator new( size_t, void *);
122f220fa62Smrg    inline void * 	operator new( size_t s)
123f220fa62Smrg				{ return ::new char[s]; }
124f220fa62Smrg    inline void 	operator delete( void * ) { assert( 0 ); }
125f220fa62Smrg    inline void         operator delete( void *, Pool & ) { assert( 0 ); }
126f220fa62Smrg    inline void		deleteMe( Pool & );
127f220fa62Smrg};
128f220fa62Smrg
129f220fa62Smrginline void *
130f220fa62SmrgPooledObj::operator new( size_t, Pool& pool )
131f220fa62Smrg{
132f220fa62Smrg    return pool.new_buffer();
133f220fa62Smrg}
134f220fa62Smrg
135f220fa62Smrginline void
136f220fa62SmrgPooledObj::deleteMe( Pool& pool )
137f220fa62Smrg{
138f220fa62Smrg    pool.free_buffer( (void *) this );
139f220fa62Smrg}
140f220fa62Smrg
141f220fa62Smrg#endif /* __glubufpool_h_ */
142