1 #ifndef _DEMEMPOOL_H
2 #define _DEMEMPOOL_H
3 /*-------------------------------------------------------------------------
4  * drawElements Memory Pool Library
5  * --------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Memory pool management.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "deDefs.h"
27 
28 #if defined(DE_DEV_BUILD)
29 	/** Enable support for failure-simulating pool allocations. */
30 #	define DE_SUPPORT_FAILING_POOL_ALLOC
31 
32 	/** Enable support for debug pools, which directly pass all allocations to deMalloc(). */
33 #	define DE_SUPPORT_DEBUG_POOLS
34 
35 	/** Enable support for memory tracking of pools (will collect the maximum memory consumption to root pool). */
36 #	define DE_SUPPORT_POOL_MEMORY_TRACKING
37 #endif /* DE_DEV_BUILD */
38 
39 typedef enum deMemPoolFlag_e
40 {
41 	DE_MEMPOOL_ENABLE_FAILING_ALLOCS	= (1<<0),
42 	DE_MEMPOOL_ENABLE_DEBUG_ALLOCS		= (1<<1)
43 } deMemPoolFlag;
44 
45 enum
46 {
47 	DE_POOL_DEFAULT_ALLOC_ALIGNMENT = DE_PTR_SIZE		/*!< Default alignment for pool allocations (in bytes). */
48 };
49 
50 /** Macro for allocating a new struct from a pool (leaves it uninitialized!). */
51 #define DE_POOL_NEW(POOL, TYPE)		((TYPE*)deMemPool_alloc(POOL, sizeof(TYPE)))
52 
53 typedef void (*deMemPoolAllocFailFunc) (void* userPtr);
54 
55 typedef struct deMemPoolUtil_s
56 {
57 	void*						userPointer;
58 	deMemPoolAllocFailFunc		allocFailCallback;
59 } deMemPoolUtil;
60 
61 typedef struct deMemPool_s deMemPool;
62 
63 DE_BEGIN_EXTERN_C
64 
65 deMemPool*	deMemPool_createRoot				(const deMemPoolUtil* util, deUint32 flags);
66 deMemPool*	deMemPool_create					(deMemPool* parent);
67 void		deMemPool_destroy					(deMemPool* pool);
68 int			deMemPool_getNumChildren			(const deMemPool* pool);
69 int			deMemPool_getNumAllocatedBytes		(const deMemPool* pool, deBool recurse);
70 int			deMemPool_getCapacity				(const deMemPool* pool, deBool recurse);
71 
72 void*		deMemPool_alloc						(deMemPool* pool, int numBytes);
73 void*		deMemPool_alignedAlloc				(deMemPool* pool, int numBytes, deUint32 alignBytes);
74 void*		deMemPool_memDup					(deMemPool* pool, const void* ptr, int numBytes);
75 char*		deMemPool_strDup					(deMemPool* pool, const char* str);
76 char*		deMemPool_strnDup					(deMemPool* pool, const char* str, int maxLength);
77 
78 #if defined(DE_SUPPORT_POOL_MEMORY_TRACKING)
79 int			deMemPool_getMaxNumAllocatedBytes	(const deMemPool* pool);
80 int			deMemPool_getMaxCapacity			(const deMemPool* pool);
81 #endif
82 
83 DE_END_EXTERN_C
84 
85 #endif /* _DEMEMPOOL_H */
86