1 /****************************************************************************
3 * Copyright (C) 2005 - 2013 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 *****************************************************************************/
23 ** Include file for the local memory management.
26 #ifndef __gc_hal_mem_h_
27 #define __gc_hal_mem_h_
34 /*******************************************************************************
37 The macros to declare MemPool type and functions are
38 gcmMEM_DeclareFSMemPool (Type, TypeName, Prefix)
39 gcmMEM_DeclareVSMemPool (Type, TypeName, Prefix)
40 gcmMEM_DeclareAFSMemPool(Type, TypeName, Prefix)
42 The data structures for MemPool are
43 typedef struct _gcsMEM_FS_MEM_POOL * gcsMEM_FS_MEM_POOL;
44 typedef struct _gcsMEM_VS_MEM_POOL * gcsMEM_VS_MEM_POOL;
45 typedef struct _gcsMEM_AFS_MEM_POOL * gcsMEM_AFS_MEM_POOL;
47 The MemPool constructor and destructor functions are
48 gcfMEM_InitFSMemPool(gcsMEM_FS_MEM_POOL *, gcoOS, gctUINT, gctUINT);
49 gcfMEM_FreeFSMemPool(gcsMEM_FS_MEM_POOL *);
50 gcfMEM_InitVSMemPool(gcsMEM_VS_MEM_POOL *, gcoOS, gctUINT, gctBOOL);
51 gcfMEM_FreeVSMemPool(gcsMEM_VS_MEM_POOL *);
52 gcfMEM_InitAFSMemPool(gcsMEM_AFS_MEM_POOL *, gcoOS, gctUINT);
53 gcfMEM_FreeAFSMemPool(gcsMEM_AFS_MEM_POOL *);
55 FS: for Fixed-Size data structures
56 VS: for Variable-size data structures
57 AFS: for Array of Fixed-Size data structures
60 // Example 1: For a fixed-size data structure, struct gcsNode.
61 // It is used locally in a file, so the functions are static without prefix.
62 // At top level, declear allocate and free functions.
63 // The first argument is the data type.
64 // The second armument is the short name used in the fuctions.
65 gcmMEM_DeclareFSMemPool(struct gcsNode, Node, );
67 // The previous macro creates two inline functions,
68 // _AllocateNode and _FreeNode.
70 // In function or struct
71 gcsMEM_FS_MEM_POOL nodeMemPool;
74 struct gcsNode * node;
77 // Before using the memory pool, initialize it.
78 // The second argument is the gcoOS object.
79 // The third argument is the number of data structures to allocate for each chunk.
80 status = gcfMEM_InitFSMemPool(&nodeMemPool, os, 100, sizeof(struct gcsNode));
84 status = _AllocateNode(nodeMemPool, &node);
87 _FreeNode(nodeMemPool, node);
89 // After using the memory pool, free it.
90 gcfMEM_FreeFSMemPool(&nodeMemPool);
93 // Example 2: For array of fixed-size data structures, struct gcsNode.
94 // It is used in several files, so the functions are extern with prefix.
95 // At top level, declear allocate and free functions.
96 // The first argument is the data type, and the second one is the short name
97 // used in the fuctions.
98 gcmMEM_DeclareAFSMemPool(struct gcsNode, NodeArray, gcfOpt);
100 // The previous macro creates two inline functions,
101 // gcfOpt_AllocateNodeArray and gcfOpt_FreeNodeArray.
103 // In function or struct
104 gcsMEM_AFS_MEM_POOL nodeArrayMemPool;
107 struct gcsNode * nodeArray;
110 // Before using the array memory pool, initialize it.
111 // The second argument is the gcoOS object, the third is the number of data
112 // structures to allocate for each chunk.
113 status = gcfMEM_InitAFSMemPool(&nodeArrayMemPool, os, sizeof(struct gcsNode));
116 // Allocate a node array of size 100.
117 status = gcfOpt_AllocateNodeArray(nodeArrayMemPool, &nodeArray, 100);
119 // Free a node array.
120 gcfOpt_FreeNodeArray(&nodeArrayMemPool, nodeArray);
122 // After using the array memory pool, free it.
123 gcfMEM_FreeAFSMemPool(&nodeArrayMemPool);
125 *******************************************************************************/
127 /*******************************************************************************
128 ** To switch back to use gcoOS_Allocate and gcoOS_Free, add
129 ** #define USE_LOCAL_MEMORY_POOL 0
130 ** before including this file.
131 *******************************************************************************/
132 #ifndef USE_LOCAL_MEMORY_POOL
134 USE_LOCAL_MEMORY_POOL
136 This define enables the local memory management to improve performance.
138 #define USE_LOCAL_MEMORY_POOL 1
141 /*******************************************************************************
142 ** Memory Pool Data Structures
143 *******************************************************************************/
144 #if USE_LOCAL_MEMORY_POOL
145 typedef struct _gcsMEM_FS_MEM_POOL * gcsMEM_FS_MEM_POOL;
146 typedef struct _gcsMEM_VS_MEM_POOL * gcsMEM_VS_MEM_POOL;
147 typedef struct _gcsMEM_AFS_MEM_POOL * gcsMEM_AFS_MEM_POOL;
149 typedef gcoOS gcsMEM_FS_MEM_POOL;
150 typedef gcoOS gcsMEM_VS_MEM_POOL;
151 typedef gcoOS gcsMEM_AFS_MEM_POOL;
154 /*******************************************************************************
155 ** Memory Pool Macros
156 *******************************************************************************/
157 #if USE_LOCAL_MEMORY_POOL
158 #define gcmMEM_DeclareFSMemPool(Type, TypeName, Prefix) \
160 Prefix##_Allocate##TypeName( \
161 gcsMEM_FS_MEM_POOL MemPool, \
165 return(gcfMEM_FSMemPoolGetANode(MemPool, (gctPOINTER *) Pointer)); \
169 Prefix##_CAllocate##TypeName( \
170 gcsMEM_FS_MEM_POOL MemPool, \
175 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
176 gcmERR_RETURN(gcfMEM_FSMemPoolGetANode(MemPool, (gctPOINTER *) Pointer)); \
177 gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, gcmSIZEOF(Type)); \
179 return gcvSTATUS_OK; \
183 Prefix##_Free##TypeName( \
184 gcsMEM_FS_MEM_POOL MemPool, \
189 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
190 status = gcfMEM_FSMemPoolFreeANode(MemPool, (gctPOINTER) Pointer); \
196 Prefix##_Free##TypeName##List( \
197 gcsMEM_FS_MEM_POOL MemPool, \
198 Type * FirstPointer, \
203 gcmHEADER_ARG("MemPool=0x%x FirstPointer=0x%x LastPointer=0x%x", MemPool, FirstPointer, LastPointer); \
204 status = gcfMEM_FSMemPoolFreeAList(MemPool, (gctPOINTER) FirstPointer, (gctPOINTER) LastPointer); \
209 #define gcmMEM_DeclareVSMemPool(Type, TypeName, Prefix) \
211 Prefix##_Allocate##TypeName( \
212 gcsMEM_FS_MEM_POOL MemPool, \
218 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
219 status = gcfMEM_VSMemPoolGetANode(MemPool, Size, (gctPOINTER *) Pointer); \
225 Prefix##_CAllocate##TypeName( \
226 gcsMEM_FS_MEM_POOL MemPool, \
232 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
233 gcmERR_RETURN(gcfMEM_VSMemPoolGetANode(MemPool, Size, (gctPOINTER *) Pointer)); \
234 gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, size); \
236 return gcvSTATUS_OK; \
240 Prefix##_Free##TypeName( \
241 gcsMEM_FS_MEM_POOL MemPool, \
246 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pinter); \
247 status = gcfMEM_VSMemPoolFreeANode(MemPool, (gctPOINTER) Pointer); \
252 #define gcmMEM_DeclareAFSMemPool(Type, TypeName, Prefix) \
254 Prefix##_Allocate##TypeName( \
255 gcsMEM_AFS_MEM_POOL MemPool, \
261 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
262 status = gcfMEM_AFSMemPoolGetANode(MemPool, Count, (gctPOINTER *) Pointer); \
268 Prefix##_CAllocate##TypeName( \
269 gcsMEM_AFS_MEM_POOL MemPool, \
275 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
276 gcmERR_RETURN(gcfMEM_AFSMemPoolGetANode(MemPool, Count, (gctPOINTER *) Pointer)); \
277 gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, Count * gcmSIZEOF(Type)); \
279 return gcvSTATUS_OK; \
283 Prefix##_Free##TypeName( \
284 gcsMEM_AFS_MEM_POOL MemPool, \
289 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
290 status = gcfMEM_AFSMemPoolFreeANode(MemPool, (gctPOINTER) Pointer); \
297 #define gcmMEM_DeclareFSMemPool(Type, TypeName, Prefix) \
299 Prefix##_Allocate##TypeName( \
300 gcsMEM_FS_MEM_POOL MemPool, \
305 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
306 status = gcoOS_Allocate(MemPool, \
308 (gctPOINTER *) Pointer); \
314 Prefix##_CAllocate##TypeName( \
315 gcsMEM_FS_MEM_POOL MemPool, \
320 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
321 gcmERR_RETURN(gcoOS_Allocate(MemPool, \
323 (gctPOINTER *) Pointer)); \
324 gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, gcmSIZEOF(Type)); \
326 return gcvSTATUS_OK; \
330 Prefix##_Free##TypeName( \
331 gcsMEM_FS_MEM_POOL MemPool, \
336 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
337 status = gcmOS_SAFE_FREE(MemPool, Pointer); \
342 #define gcmMEM_DeclareVSMemPool(Type, TypeName, Prefix) \
344 Prefix##_Allocate##TypeName( \
345 gcsMEM_VS_MEM_POOL MemPool, \
351 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
352 status = gcoOS_Allocate(MemPool, \
354 (gctPOINTER *) Pointer); \
360 Prefix##_CAllocate##TypeName( \
361 gcsMEM_VS_MEM_POOL MemPool, \
367 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Size=%u", MemPool, Pointer, Size); \
368 gcmERR_RETURN(gcoOS_Allocate(MemPool, \
370 (gctPOINTER *) Pointer)); \
371 gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, Size); \
373 return gcvSTATUS_OK; \
377 Prefix##_Free##TypeName( \
378 gcsMEM_VS_MEM_POOL MemPool, \
383 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
384 status = gcmOS_SAFE_FREE(MemPool, Pointer); \
389 #define gcmMEM_DeclareAFSMemPool(Type, TypeName, Prefix) \
391 Prefix##_Allocate##TypeName( \
392 gcsMEM_AFS_MEM_POOL MemPool, \
398 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
399 status = gcoOS_Allocate(MemPool, \
400 Count * gcmSIZEOF(Type), \
401 (gctPOINTER *) Pointer); \
407 Prefix##_CAllocate##TypeName( \
408 gcsMEM_AFS_MEM_POOL MemPool, \
414 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x Count=%u", MemPool, Pointer, Count); \
415 gcmERR_RETURN(gcoOS_Allocate(MemPool, \
416 Count * gcmSIZEOF(Type), \
417 (gctPOINTER *) Pointer)); \
418 gcoOS_ZeroMemory(*(gctPOINTER *) Pointer, Count * gcmSIZEOF(Type)); \
420 return gcvSTATUS_OK; \
424 Prefix##_Free##TypeName( \
425 gcsMEM_AFS_MEM_POOL MemPool, \
430 gcmHEADER_ARG("MemPool=0x%x Pointer=0x%x", MemPool, Pointer); \
431 status = gcmOS_SAFE_FREE(MemPool, Pointer); \
437 /*******************************************************************************
438 ** Memory Pool Data Functions
439 *******************************************************************************/
441 gcfMEM_InitFSMemPool(
442 IN gcsMEM_FS_MEM_POOL * MemPool,
444 IN gctUINT NodeCount,
449 gcfMEM_FreeFSMemPool(
450 IN gcsMEM_FS_MEM_POOL * MemPool
454 gcfMEM_FSMemPoolGetANode(
455 IN gcsMEM_FS_MEM_POOL MemPool,
456 OUT gctPOINTER * Node
460 gcfMEM_FSMemPoolFreeANode(
461 IN gcsMEM_FS_MEM_POOL MemPool,
466 gcfMEM_FSMemPoolFreeAList(
467 IN gcsMEM_FS_MEM_POOL MemPool,
468 IN gctPOINTER FirstNode,
469 IN gctPOINTER LastNode
473 gcfMEM_InitVSMemPool(
474 IN gcsMEM_VS_MEM_POOL * MemPool,
476 IN gctUINT BlockSize,
477 IN gctBOOL RecycleFreeNode
481 gcfMEM_FreeVSMemPool(
482 IN gcsMEM_VS_MEM_POOL * MemPool
486 gcfMEM_VSMemPoolGetANode(
487 IN gcsMEM_VS_MEM_POOL MemPool,
489 IN gctUINT Alignment,
490 OUT gctPOINTER * Node
494 gcfMEM_VSMemPoolFreeANode(
495 IN gcsMEM_VS_MEM_POOL MemPool,
500 gcfMEM_InitAFSMemPool(
501 IN gcsMEM_AFS_MEM_POOL *MemPool,
503 IN gctUINT NodeCount,
508 gcfMEM_FreeAFSMemPool(
509 IN gcsMEM_AFS_MEM_POOL *MemPool
513 gcfMEM_AFSMemPoolGetANode(
514 IN gcsMEM_AFS_MEM_POOL MemPool,
516 OUT gctPOINTER * Node
520 gcfMEM_AFSMemPoolFreeANode(
521 IN gcsMEM_AFS_MEM_POOL MemPool,
529 #endif /* VIVANTE_NO_3D */
530 #endif /* __gc_hal_mem_h_ */