1 /****************************************************************************
3 * Copyright (C) 2005 - 2011 by Vivante Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the license, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *****************************************************************************/
25 ** Include file for the local memory management.
28 #ifndef __gc_hal_mem_h_
29 #define __gc_hal_mem_h_
36 /*******************************************************************************
39 The macros to declare MemPool type and functions are
40 gcmMEM_DeclareFSMemPool (Type, TypeName, Prefix)
41 gcmMEM_DeclareVSMemPool (Type, TypeName, Prefix)
42 gcmMEM_DeclareAFSMemPool(Type, TypeName, Prefix)
44 The data structures for MemPool are
45 typedef struct _gcsMEM_FS_MEM_POOL * gcsMEM_FS_MEM_POOL;
46 typedef struct _gcsMEM_VS_MEM_POOL * gcsMEM_VS_MEM_POOL;
47 typedef struct _gcsMEM_AFS_MEM_POOL * gcsMEM_AFS_MEM_POOL;
49 The MemPool constructor and destructor functions are
50 gcfMEM_InitFSMemPool(gcsMEM_FS_MEM_POOL *, gcoOS, gctUINT, gctUINT);
51 gcfMEM_FreeFSMemPool(gcsMEM_FS_MEM_POOL *);
52 gcfMEM_InitVSMemPool(gcsMEM_VS_MEM_POOL *, gcoOS, gctUINT, gctBOOL);
53 gcfMEM_FreeVSMemPool(gcsMEM_VS_MEM_POOL *);
54 gcfMEM_InitAFSMemPool(gcsMEM_AFS_MEM_POOL *, gcoOS, gctUINT);
55 gcfMEM_FreeAFSMemPool(gcsMEM_AFS_MEM_POOL *);
57 FS: for Fixed-Size data structures
58 VS: for Variable-size data structures
59 AFS: for Array of Fixed-Size data structures
62 // Example 1: For a fixed-size data structure, struct gcsNode.
63 // It is used locally in a file, so the functions are static without prefix.
64 // At top level, declear allocate and free functions.
65 // The first argument is the data type.
66 // The second armument is the short name used in the fuctions.
67 gcmMEM_DeclareFSMemPool(struct gcsNode, Node, );
69 // The previous macro creates two inline functions,
70 // _AllocateNode and _FreeNode.
72 // In function or struct
73 gcsMEM_FS_MEM_POOL nodeMemPool;
76 struct gcsNode * node;
79 // Before using the memory pool, initialize it.
80 // The second argument is the gcoOS object.
81 // The third argument is the number of data structures to allocate for each chunk.
82 status = gcfMEM_InitFSMemPool(&nodeMemPool, os, 100, sizeof(struct gcsNode));
86 status = _AllocateNode(nodeMemPool, &node);
89 _FreeNode(nodeMemPool, node);
91 // After using the memory pool, free it.
92 gcfMEM_FreeFSMemPool(&nodeMemPool);
95 // Example 2: For array of fixed-size data structures, struct gcsNode.
96 // It is used in several files, so the functions are extern with prefix.
97 // At top level, declear allocate and free functions.
98 // The first argument is the data type, and the second one is the short name
99 // used in the fuctions.
100 gcmMEM_DeclareAFSMemPool(struct gcsNode, NodeArray, gcfOpt);
102 // The previous macro creates two inline functions,
103 // gcfOpt_AllocateNodeArray and gcfOpt_FreeNodeArray.
105 // In function or struct
106 gcsMEM_AFS_MEM_POOL nodeArrayMemPool;
109 struct gcsNode * nodeArray;
112 // Before using the array memory pool, initialize it.
113 // The second argument is the gcoOS object, the third is the number of data
114 // structures to allocate for each chunk.
115 status = gcfMEM_InitAFSMemPool(&nodeArrayMemPool, os, sizeof(struct gcsNode));
118 // Allocate a node array of size 100.
119 status = gcfOpt_AllocateNodeArray(nodeArrayMemPool, &nodeArray, 100);
121 // Free a node array.
122 gcfOpt_FreeNodeArray(&nodeArrayMemPool, nodeArray);
124 // After using the array memory pool, free it.
125 gcfMEM_FreeAFSMemPool(&nodeArrayMemPool);
127 *******************************************************************************/
129 /*******************************************************************************
130 ** To switch back to use gcoOS_Allocate and gcoOS_Free, add
131 ** #define USE_LOCAL_MEMORY_POOL 0
132 ** before including this file.
133 *******************************************************************************/
134 #ifndef USE_LOCAL_MEMORY_POOL
136 USE_LOCAL_MEMORY_POOL
138 This define enables the local memory management to improve performance.
140 #define USE_LOCAL_MEMORY_POOL 1
143 /*******************************************************************************
144 ** Memory Pool Data Structures
145 *******************************************************************************/
146 #if USE_LOCAL_MEMORY_POOL
147 typedef struct _gcsMEM_FS_MEM_POOL
* gcsMEM_FS_MEM_POOL
;
148 typedef struct _gcsMEM_VS_MEM_POOL
* gcsMEM_VS_MEM_POOL
;
149 typedef struct _gcsMEM_AFS_MEM_POOL
* gcsMEM_AFS_MEM_POOL
;
151 typedef gcoOS gcsMEM_FS_MEM_POOL
;
152 typedef gcoOS gcsMEM_VS_MEM_POOL
;
153 typedef gcoOS gcsMEM_AFS_MEM_POOL
;
156 /*******************************************************************************
157 ** Memory Pool Macros
158 *******************************************************************************/
159 #if USE_LOCAL_MEMORY_POOL
160 #define gcmMEM_DeclareFSMemPool(Type, TypeName, Prefix) \
162 Prefix##_Allocate##TypeName( \
163 gcsMEM_FS_MEM_POOL MemPool, \
167 return(gcfMEM_FSMemPoolGetANode(MemPool, (gctPOINTER *) Pointer)); \
171 Prefix##_CAllocate##TypeName( \
172 gcsMEM_FS_MEM_POOL MemPool, \
177 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
178 gcmERR_RETURN(gcfMEM_FSMemPoolGetANode(MemPool, (gctPOINTER *) Pointer)); \
179 gcmVERIFY_OK(gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, gcmSIZEOF(Type))); \
181 return gcvSTATUS_OK; \
185 Prefix##_Free##TypeName( \
186 gcsMEM_FS_MEM_POOL MemPool, \
191 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
192 status = gcfMEM_FSMemPoolFreeANode(MemPool, (gctPOINTER) Pointer); \
198 Prefix##_Free##TypeName##List( \
199 gcsMEM_FS_MEM_POOL MemPool, \
200 Type * FirstPointer, \
205 gcmHEADER_ARG("MemPool=0x%x FirstPointer=0x%x LastPointer=0x%x", MemPool, FirstPointer, LastPointer); \
206 status = gcfMEM_FSMemPoolFreeAList(MemPool, (gctPOINTER) FirstPointer, (gctPOINTER) LastPointer); \
211 #define gcmMEM_DeclareVSMemPool(Type, TypeName, Prefix) \
213 Prefix##_Allocate##TypeName( \
214 gcsMEM_FS_MEM_POOL MemPool, \
220 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
221 status = gcfMEM_VSMemPoolGetANode(MemPool, Size, (gctPOINTER *) Pointer); \
227 Prefix##_CAllocate##TypeName( \
228 gcsMEM_FS_MEM_POOL MemPool, \
234 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
235 gcmERR_RETURN(gcfMEM_VSMemPoolGetANode(MemPool, Size, (gctPOINTER *) Pointer)); \
236 gcmVERIFY_OK(gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, size)); \
238 return gcvSTATUS_OK; \
242 Prefix##_Free##TypeName( \
243 gcsMEM_FS_MEM_POOL MemPool, \
248 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pinter); \
249 status = gcfMEM_VSMemPoolFreeANode(MemPool, (gctPOINTER) Pointer); \
254 #define gcmMEM_DeclareAFSMemPool(Type, TypeName, Prefix) \
256 Prefix##_Allocate##TypeName( \
257 gcsMEM_AFS_MEM_POOL MemPool, \
263 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
264 status = gcfMEM_AFSMemPoolGetANode(MemPool, Count, (gctPOINTER *) Pointer); \
270 Prefix##_CAllocate##TypeName( \
271 gcsMEM_AFS_MEM_POOL MemPool, \
277 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
278 gcmERR_RETURN(gcfMEM_AFSMemPoolGetANode(MemPool, Count, (gctPOINTER *) Pointer)); \
279 gcmVERIFY_OK(gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, Count * gcmSIZEOF(Type))); \
281 return gcvSTATUS_OK; \
285 Prefix##_Free##TypeName( \
286 gcsMEM_AFS_MEM_POOL MemPool, \
291 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
292 status = gcfMEM_AFSMemPoolFreeANode(MemPool, (gctPOINTER) Pointer); \
299 #define gcmMEM_DeclareFSMemPool(Type, TypeName, Prefix) \
301 Prefix##_Allocate##TypeName( \
302 gcsMEM_FS_MEM_POOL MemPool, \
307 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
308 status = gcoOS_Allocate(MemPool, \
310 (gctPOINTER *) Pointer); \
316 Prefix##_CAllocate##TypeName( \
317 gcsMEM_FS_MEM_POOL MemPool, \
322 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
323 gcmERR_RETURN(gcoOS_Allocate(MemPool, \
325 (gctPOINTER *) Pointer)); \
326 gcmVERIFY_OK(gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, gcmSIZEOF(Type))); \
328 return gcvSTATUS_OK; \
332 Prefix##_Free##TypeName( \
333 gcsMEM_FS_MEM_POOL MemPool, \
338 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
339 status = gcmOS_SAFE_FREE(MemPool, Pointer); \
344 #define gcmMEM_DeclareVSMemPool(Type, TypeName, Prefix) \
346 Prefix##_Allocate##TypeName( \
347 gcsMEM_VS_MEM_POOL MemPool, \
353 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
354 status = gcoOS_Allocate(MemPool, \
356 (gctPOINTER *) Pointer); \
362 Prefix##_CAllocate##TypeName( \
363 gcsMEM_VS_MEM_POOL MemPool, \
369 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
370 gcmERR_RETURN(gcoOS_Allocate(MemPool, \
372 (gctPOINTER *) Pointer)); \
373 gcmVERIFY_OK(gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, Size)); \
375 return gcvSTATUS_OK; \
379 Prefix##_Free##TypeName( \
380 gcsMEM_VS_MEM_POOL MemPool, \
385 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
386 status = gcmOS_SAFE_FREE(MemPool, Pointer); \
391 #define gcmMEM_DeclareAFSMemPool(Type, TypeName, Prefix) \
393 Prefix##_Allocate##TypeName( \
394 gcsMEM_AFS_MEM_POOL MemPool, \
400 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
401 status = gcoOS_Allocate(MemPool, \
402 Count * gcmSIZEOF(Type), \
403 (gctPOINTER *) Pointer); \
409 Prefix##_CAllocate##TypeName( \
410 gcsMEM_AFS_MEM_POOL MemPool, \
416 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
417 gcmERR_RETURN(gcoOS_Allocate(MemPool, \
418 Count * gcmSIZEOF(Type), \
419 (gctPOINTER *) Pointer)); \
420 gcmVERIFY_OK(gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, Count * gcmSIZEOF(Type))); \
422 return gcvSTATUS_OK; \
426 Prefix##_Free##TypeName( \
427 gcsMEM_AFS_MEM_POOL MemPool, \
432 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
433 status = gcmOS_SAFE_FREE(MemPool, Pointer); \
439 /*******************************************************************************
440 ** Memory Pool Data Functions
441 *******************************************************************************/
443 gcfMEM_InitFSMemPool(
444 IN gcsMEM_FS_MEM_POOL
* MemPool
,
446 IN gctUINT NodeCount
,
451 gcfMEM_FreeFSMemPool(
452 IN gcsMEM_FS_MEM_POOL
* MemPool
456 gcfMEM_FSMemPoolGetANode(
457 IN gcsMEM_FS_MEM_POOL MemPool
,
458 OUT gctPOINTER
* Node
462 gcfMEM_FSMemPoolFreeANode(
463 IN gcsMEM_FS_MEM_POOL MemPool
,
468 gcfMEM_FSMemPoolFreeAList(
469 IN gcsMEM_FS_MEM_POOL MemPool
,
470 IN gctPOINTER FirstNode
,
471 IN gctPOINTER LastNode
475 gcfMEM_InitVSMemPool(
476 IN gcsMEM_VS_MEM_POOL
* MemPool
,
478 IN gctUINT BlockSize
,
479 IN gctBOOL RecycleFreeNode
483 gcfMEM_FreeVSMemPool(
484 IN gcsMEM_VS_MEM_POOL
* MemPool
488 gcfMEM_VSMemPoolGetANode(
489 IN gcsMEM_VS_MEM_POOL MemPool
,
491 IN gctUINT Alignment
,
492 OUT gctPOINTER
* Node
496 gcfMEM_VSMemPoolFreeANode(
497 IN gcsMEM_VS_MEM_POOL MemPool
,
502 gcfMEM_InitAFSMemPool(
503 IN gcsMEM_AFS_MEM_POOL
*MemPool
,
505 IN gctUINT NodeCount
,
510 gcfMEM_FreeAFSMemPool(
511 IN gcsMEM_AFS_MEM_POOL
*MemPool
515 gcfMEM_AFSMemPoolGetANode(
516 IN gcsMEM_AFS_MEM_POOL MemPool
,
518 OUT gctPOINTER
* Node
522 gcfMEM_AFSMemPoolFreeANode(
523 IN gcsMEM_AFS_MEM_POOL MemPool
,
531 #endif /* VIVANTE_NO_3D */
532 #endif /* __gc_hal_mem_h_ */