1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef bbs_DYN_MEM_MANAGER_EM_H
18 #define bbs_DYN_MEM_MANAGER_EM_H
19 
20 /* ---- includes ----------------------------------------------------------- */
21 
22 #include "b_BasicEm/Basic.h"
23 
24 /* ---- related objects  --------------------------------------------------- */
25 
26 struct bbs_Context;
27 struct bbs_MemSeg;
28 
29 /* ---- typedefs ----------------------------------------------------------- */
30 
31 /** 'malloc' function pointer.
32   * Allocated memory block must be 32-bit-aligned.
33   * sizeA refers to the size of a memory block in bytes
34   */
35 typedef void* ( *bbs_mallocFPtr )( struct bbs_Context* cpA,
36 								   const struct bbs_MemSeg* memSegPtrA,
37 								   uint32 sizeA );
38 
39 /** free function pointer */
40 typedef void ( *bbs_freeFPtr )( void* memPtrA );
41 
42 /* ---- constants ---------------------------------------------------------- */
43 
44 /* ---- object definition -------------------------------------------------- */
45 
46 /** Dynamic memory manager.
47   * Handles allocation and deallocation of memory blocks via function pointers
48   * to malloc and free.
49   *
50   * Each memory block is organized as follows:
51   * - The first 8 bytes are reserved for the pointer to the next
52   *    memory block (8 to allow support of 64-bit platforms).
53   * - Next a 32-bit value stores the allocated memory size in 16-bit units.
54   * - Finally the actual allocated memory area.
55   * This means for each new memory block an additional 12 bytes are allocated.
56   */
57 struct bbs_DynMemManager
58 {
59 
60 	/* ---- private data --------------------------------------------------- */
61 
62 	/* ---- public data ---------------------------------------------------- */
63 
64 	/** pointer to first memory block */
65 	uint16* memPtrE;
66 
67 	/** function pointer to external mem alloc function (s. comment of type declaration)*/
68 	bbs_mallocFPtr mallocFPtrE;
69 
70 	/** function pointer to external mem free function */
71 	bbs_freeFPtr freeFPtrE;
72 };
73 
74 /* ---- associated objects ------------------------------------------------- */
75 
76 /* ---- external functions ------------------------------------------------- */
77 
78 /* ---- \ghd{ constructor/destructor } ------------------------------------- */
79 
80 /** initializes bbs_DynMemManager  */
81 void bbs_DynMemManager_init( struct bbs_Context* cpA, struct bbs_DynMemManager* ptrA );
82 
83 /** frees bbs_DynMemManager  */
84 void bbs_DynMemManager_exit( struct bbs_Context* cpA, struct bbs_DynMemManager* ptrA );
85 
86 /* ---- \ghd{ operators } -------------------------------------------------- */
87 
88 /* ---- \ghd{ query functions } -------------------------------------------- */
89 
90 /** returns size of currently allocated memory in 16bit units */
91 uint32 bbs_DynMemManager_allocatedSize( struct bbs_Context* cpA, const struct bbs_DynMemManager* ptrA );
92 
93 /* ---- \ghd{ modify functions } ------------------------------------------- */
94 
95 /* ---- \ghd{ memory I/O } ------------------------------------------------- */
96 
97 /* ---- \ghd{ exec functions } --------------------------------------------- */
98 
99 /** allocates sizeA words of memory */
100 uint16* bbs_DynMemManager_alloc( struct bbs_Context* cpA,
101 								 struct bbs_DynMemManager* ptrA,
102 								 const struct bbs_MemSeg* memSegPtrA,
103 								 uint32 sizeA );
104 
105 /** frees previously allocated memory */
106 void bbs_DynMemManager_free( struct bbs_Context* cpA,
107 							 struct bbs_DynMemManager* ptrA,
108 							 uint16* memPtrA );
109 
110 /** returns the next memory block of at least minSizeA length; allocates new block if neccessary */
111 uint16* bbs_DynMemManager_nextBlock( struct bbs_Context* cpA,
112 									 struct bbs_DynMemManager* ptrA,
113 									 const struct bbs_MemSeg* memSegPtrA,
114 									 uint16* curBlockPtrA,
115 									 uint32 minSizeA,
116 									 uint32* actualSizePtrA );
117 
118 /** frees all allocated memory */
119 void bbs_DynMemManager_freeAll( struct bbs_Context* cpA,
120 							    struct bbs_DynMemManager* ptrA );
121 
122 
123 #endif /* bbs_DYN_MEM_MANAGER_EM_H */
124 
125