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_MEM_SEG_EM_H
18 #define bbs_MEM_SEG_EM_H
19 
20 /* ---- includes ----------------------------------------------------------- */
21 
22 #include "b_BasicEm/Basic.h"
23 #include "b_BasicEm/DynMemManager.h"
24 
25 /* ---- related objects  --------------------------------------------------- */
26 
27 struct bbs_Context;
28 
29 /* ---- typedefs ----------------------------------------------------------- */
30 
31 /* ---- constants ---------------------------------------------------------- */
32 
33 /* overhead memory needed for each memory block allocated (exclusive memory only) */
34 #define bbs_MEM_BLOCK_OVERHD 2
35 
36 /* Segment IDs */
37 #define bbs_SEG_DEFAULT			0
38 
39 #if defined( HW_TMS320C5x ) || defined( HW_MeP ) || defined( bbs_MEP_MEM_CONFIG )
40 	#define bbs_SEG_DA			1
41 	#define bbs_SEG_DA_ALT		2
42 	#define bbs_SEG_SA			3
43 	#define bbs_SEG_SA_ALT		4
44 	#define bbs_SEG_EXT			5
45 	#define bbs_SEG_EXT_ALT		6
46 #elif defined ( bbs_KD_MEM_CONFIG ) || defined ( HW_KD_EASYSHARE )
47 /* on-chip optimization for Kodak Easyshare project */
48 	#define bbs_SEG_DA			1  /* = internal RAM segment */
49 	#define bbs_SEG_DA_ALT		0
50 	#define bbs_SEG_SA			0
51 	#define bbs_SEG_SA_ALT		0
52 	#define bbs_SEG_EXT			0
53 	#define bbs_SEG_EXT_ALT		0
54 #endif
55 
56 /* ---- object definition -------------------------------------------------- */
57 
58 /** Descriptor of a coherent memory segment available for memory management.
59  *  How management works
60  *  - Memory is arranged in blocks
61  *  - Each block refers to a single call of function alloc()
62  *  - Each block is aligned at 32bit
63  *  - The size of each block is even (32bit aligned size)
64  *  Uique (non-shared) segments:
65  *  - Each block has a preceding 32 bit value indication its length
66  *  - Function free() marks the corresponding block 'unused' and
67  *    removes subsequently any unused block at the last position of allocated memory
68  *  Shared segments:
69  *	- No write access to memory block by function alloc()
70  *	- Function free has no effect
71  *  Identifier:
72  *  - Each segment contains an ID. The segment with the ID 0 is the default segment.
73  */
74 struct bbs_MemSeg
75 {
76 	/* all member variables are considered read only. Only change them through functions */
77 
78 	/** pointer to memory */
79 	uint16* memPtrE;
80 
81 	/** size of memory segment in 16 bit units */
82 	uint32 sizeE;
83 
84 	/** current allocation index in 16 bit units (index is always even -> 32 bit alignment enforced) */
85 	uint32 allocIndexE;
86 
87 	/** Indicates that this isegment is to be shared among multiple objects */
88 	flag sharedE;
89 
90 	/** ID of segment, id=0: unspecified */
91 	uint32 idE;
92 
93 	/** pointer to external memory manager */
94 	struct bbs_DynMemManager* dynMemManagerPtrE;
95 };
96 
97 /* ---- associated objects ------------------------------------------------- */
98 
99 /* ---- external functions ------------------------------------------------- */
100 
101 /* ---- \ghd{ constructor/destructor } ------------------------------------- */
102 
103 /** initializes bbs_MemSeg  */
104 void bbs_MemSeg_init( struct bbs_Context* cpA,
105 					  struct bbs_MemSeg* ptrA );
106 
107 /** resets bbs_MemSeg  */
108 void bbs_MemSeg_exit( struct bbs_Context* cpA,
109 					  struct bbs_MemSeg* ptrA );
110 
111 /* ---- \ghd{ operators } -------------------------------------------------- */
112 
113 /* ---- \ghd{ query functions } -------------------------------------------- */
114 
115 /** returns available 16bit units of memeory in given segment; (allocation is always 32 bit aligned) */
116 uint32 bbs_MemSeg_availableSize( struct bbs_Context* cpA,
117 								 const struct bbs_MemSeg* ptrA );
118 
119 /** returns currently allocated size in 16bit units of memeory in given segment */
120 uint32 bbs_MemSeg_allocatedSize( struct bbs_Context* cpA,
121 								 const struct bbs_MemSeg* ptrA );
122 
123 /** returns effectively used memory amount allocated size - unused blocks - overhead */
124 uint32 bbs_MemSeg_usedSize( struct bbs_Context* cpA,
125 						    const struct bbs_MemSeg* ptrA );
126 
127 /** counts amount of memory blocks allocated */
128 uint32 bbs_MemSeg_blocks( struct bbs_Context* cpA,
129 						  const struct bbs_MemSeg* ptrA );
130 
131 /** counts amount of memory blocks currently used */
132 uint32 bbs_MemSeg_usedBlocks( struct bbs_Context* cpA,
133 							  const struct bbs_MemSeg* ptrA );
134 
135 /* ---- \ghd{ modify functions } ------------------------------------------- */
136 
137 /* ---- \ghd{ memory I/O } ------------------------------------------------- */
138 
139 /* ---- \ghd{ exec functions } --------------------------------------------- */
140 
141 /** creation of a exclusive memory segment; memPtrA must be 32-bit aligned */
142 struct bbs_MemSeg bbs_MemSeg_create( struct bbs_Context* cpA,
143 									 void* memPtrA,
144 									 uint32 sizeA );
145 
146 /** creation of a shared memory segment; memPtrA must be 32-bit aligned */
147 struct bbs_MemSeg bbs_MemSeg_createShared( struct bbs_Context* cpA,
148 										   void* memPtrA,
149 										   uint32 sizeA );
150 
151 /** allocation of memory (very fast); sizeA specifies number of 16bit units; (allocation is always 32 bit aligned) */
152 void* bbs_MemSeg_alloc( struct bbs_Context* cpA,
153 					    struct bbs_MemSeg* ptrA,
154 						uint32 sizeA );
155 
156 /** Frees allocated memory
157  *  If segment is shared, ptrA == NULL or memPtrA == NULL, nothing happens
158  */
159 void bbs_MemSeg_free( struct bbs_Context* cpA,
160 					  struct bbs_MemSeg* ptrA,
161 					  void* memPtrA );
162 
163 /** checks consistency of memory */
164 void bbs_MemSeg_checkConsistency( struct bbs_Context* cpA,
165 								  const struct bbs_MemSeg* ptrA );
166 
167 #endif /* bbs_MEM_SEG_EM_H */
168 
169