added files that were missing
[neuro.git] / src / video / pool.c
blob45586239f609f2636a92ef46418b2a8a722e82e2
1 /*
2 * libneuro, a light weight abstraction of high or lower libraries
3 * and toolkit for applications.
4 * Copyright (C) 2005-2006 Nicholas Niro, Robert Lemay
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 /* pool.c
22 * Module : Pool
24 * memory management system, not really performant though, its actually better
25 * to free and reallocate then trying not to free...
28 /*-------------------- Extern Headers Including --------------------*/
31 /*-------------------- Local Headers Including ---------------------*/
33 /*-------------------- Main Module Header --------------------------*/
34 #include "pool.h"
37 /*-------------------- Other ----------------------------*/
39 enum POOL_TYPES
41 POOL_AVAILABLE = 0, /* an available spot */
42 POOL_RAWENGINE = 1, /* RAW_ENGINE */
43 POOL_QUEUE, /* INSTRUCTION_ENGINE */
44 /*POOL_PIXELS,*/ /* PIXEL_ENGINE */
46 POOL_LAST
49 /* this is the memory pool structure
50 * made to keep track of unused memory
51 * so it can be used again if necessary.
53 * ** note **
54 * this test (although working well technically) was
55 * an awful performance failure so it was dropped.
57 * the code was kept for future reference,
58 * see the macro use_memory_pool to toggle the use
59 * of this feature.
61 typedef struct STRUCT_POOL
63 u8 type;
64 void *data;
65 }STRUCT_POOL;
67 /*-------------------- Global Variables ----------------------------*/
69 /*-------------------- Static Variables ----------------------------*/
70 /* pool used to reuse allocated memory */
71 static EBUF *_pool;
74 /*-------------------- Static Prototypes ---------------------------*/
78 /*-------------------- Static Functions ----------------------------*/
80 /*-------------------- Global Functions ----------------------------*/
82 /* function to put a new element in the pool */
83 void
84 Pool_PushData(u8 type, void *data)
86 STRUCT_POOL *tmp;
87 u32 total = 0;
89 /* Debug_Val(0, "Pushed a struct to be in the pool\n"); */
90 if (!Neuro_EBufIsEmpty(_pool))
92 total = Neuro_GiveEBufCount(_pool) + 1;
94 /* loop the buffer to attempt to put the data
95 * into an available spot
97 while (total-- > 0)
99 tmp = Neuro_GiveEBuf(_pool, total);
101 if (tmp->type == POOL_AVAILABLE)
103 /* Debug_Val(0, "putting data into an available slot\n"); */
104 tmp->type = type;
105 tmp->data = data;
107 return;
112 /* Debug_Val(0, "no more available slots, we need to allocate a new one\n"); */
114 /* if we are here, it means there was no available
115 * spot found. We will have to create a new one.
116 * and put the data in it.
118 Neuro_AllocEBuf(_pool, sizeof(STRUCT_POOL*), sizeof(STRUCT_POOL));
120 tmp = Neuro_GiveCurEBuf(_pool);
122 tmp->type = type;
123 tmp->data = data;
127 /* returns a pointer corresponding to the type or
128 * NULL if none found
130 void *
131 Pool_PullData(u8 type)
133 STRUCT_POOL *tmp;
134 u32 total = 0;
135 void *ret = NULL;
137 if (Neuro_EBufIsEmpty(_pool))
138 return NULL;
140 total = Neuro_GiveEBufCount(_pool) + 1;
142 while (total-- > 0)
144 tmp = Neuro_GiveEBuf(_pool, total);
146 if (tmp->type == type)
148 tmp->type = POOL_AVAILABLE;
149 ret = tmp->data;
150 tmp->data = NULL;
152 return ret;
156 return NULL;
158 /*-------------------- Poll ----------------------------------------*/
160 void
161 Pool_Poll()
166 /*-------------------- Constructor Destructor ----------------------*/
169 Pool_Init()
171 Neuro_CreateEBuf(&_pool);
172 return 0;
175 void
176 Pool_Clean()
178 Neuro_CleanEBuf(&_pool);