1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 // -*- c++ -*-
19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
20
21 // O S C L _ M E M P O O L _ A L L O C A T O R
22
23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
24
25 /*! \addtogroup osclerror OSCL Error
26 *
27 * @{
28 */
29
30
31 /*! \file oscl_mempool_allocator.cpp
32 \brief This file contains the implementation of memory pool allocator for leave/trap
33 */
34
35 /***************************************************************************************
36 File Name : oscl_mempool_allocator.cpp
37 Description : File containing implementation of class OsclMemPoolAllocator that provides
38 methods for creating, deleting memory pool.
39 Coding History :
40 Achint Kaur April 11, 2006 Initial Draft
41 ***************************************************************************************/
42
43 #include "oscl_mempool_allocator.h"
44
45 #include "oscl_exception.h"
46
47 #include "oscl_mem_basic_functions.h"
48
49 /***************************************************************************************
50 Description : Constructor method for memory pool allocation. Sets iCustomAllocator
51 to the passed allocator pointer. Also initializes iBaseAddress to NULL.
52 Arguments : gen_alloc - Custom allocator for memory. This is an input argument.
53 Return Values : None
54 Assumptions : None
55 Known Issues : None
56 ***************************************************************************************/
OsclMemPoolAllocator(Oscl_DefAlloc * gen_alloc)57 OsclMemPoolAllocator::OsclMemPoolAllocator(Oscl_DefAlloc* gen_alloc)
58 : iCustomAllocator(gen_alloc),
59 iBaseAddress(0)
60 {
61 }
62
63 /***************************************************************************************
64 Description : Virtual destructor for memory pool allocation. Calls DestroyMemPool, if
65 iBaseAddress is not NULL i.e. memory pool exists.
66 Arguments : None
67 Return Values : None
68 Assumptions : None
69 Known Issues : None
70 ***************************************************************************************/
~OsclMemPoolAllocator()71 OsclMemPoolAllocator::~OsclMemPoolAllocator()
72 {
73 if (iBaseAddress)
74 {
75 DestroyMemPool();
76 }
77 }
78
79 /***************************************************************************************
80 Description : Method for creating memory pool given the number of chunks and chunk size.
81 Arguments : aNumChunk - Default number of chunks in a memory pool is 2.
82 aChunkSize - Default size of each chunk is 4.
83 Return Values : OsclAny* - Base address for the memory pool
84 Assumptions : It is assumed that memory audit is not required so, _oscl_malloc used directly.
85 Client can pass its custom allocator for memory.
86 Malloc will be used if custom allocator is not set.
87 Memory alignment is taken from osclmemory.
88 uint32 and int32 are assumed to be oscl compliant.
89 Known Issues : Is there a naming convention for leaving methods in oscl ?
90 Is there a need for memory alignment as done ?
91 Is OSCL_MEM_FILL_WITH_PATTERN required ?
92 ***************************************************************************************/
CreateMemPool(const uint32 aNumChunk,const uint32 aChunkSize)93 OsclAny* OsclMemPoolAllocator::CreateMemPool(const uint32 aNumChunk, const uint32 aChunkSize)
94 {
95 if (aNumChunk == 0 || aChunkSize == 0)
96 {
97 OSCL_LEAVE(OsclErrArgument);
98
99 // OSCL_UNUSED_RETURN(NULL); This statement was removed to avoid compiler warning for Unreachable Code
100 }
101
102 // Heap memory alligned chunk size
103 uint32 lChunkSizeMemAligned;
104
105 lChunkSizeMemAligned = oscl_mem_aligned_size(aChunkSize);
106
107 if (iCustomAllocator)
108 {
109 iBaseAddress = iCustomAllocator->ALLOCATE(aNumChunk * lChunkSizeMemAligned);
110 }
111 else
112 {
113 iBaseAddress = _oscl_malloc(aNumChunk * lChunkSizeMemAligned);
114 }
115
116 if (iBaseAddress == NULL)
117 {
118 OSCL_LEAVE(OsclErrNoMemory);
119
120 // OSCL_UNUSED_RETURN(NULL); This statement was removed to avoid compiler warning for Unreachable Code
121
122 }
123
124 #if OSCL_MEM_FILL_WITH_PATTERN
125
126 oscl_memset(iMemPool, 0x55, aNumChunk*lChunkSizeMemAligned);
127
128 #endif
129
130 return iBaseAddress;
131
132 }
133
134 /***************************************************************************************
135 Description : Performs memory alignment for the passed size argument.
136 Arguments : x - uint representing the size to be memory aligned
137 Return Values : uint - Memory aligned size
138 Assumptions : None
139 Known Issues : None
140 ***************************************************************************************/
oscl_mem_aligned_size(uint x)141 uint OsclMemPoolAllocator::oscl_mem_aligned_size(uint x)
142 {
143
144 uint y;
145
146 if (x & 0x7)
147 {
148 y = x & (~0x7);
149 y += 8;
150 }
151 else
152 {
153 y = x;
154 }
155
156 return y;
157 }
158
159 /***************************************************************************************
160 Description : Method for destroying memory pool
161 Arguments : None
162 Return Values : None
163 Assumptions : It is assumed that memory audit is not required so, _oscl_free used directly.
164 Known Issues : None
165 Condition : Is there any naming convention for leaving methods in oscl ?
166 ***************************************************************************************/
DestroyMemPool()167 void OsclMemPoolAllocator::DestroyMemPool()
168 {
169 // If client class calls DestroyMemPool without CreateMemPool i.e iBaseAddress is NULL then leave
170 if (!iBaseAddress)
171 {
172 OSCL_LEAVE(OsclErrArgument);
173
174 }
175
176 if (iCustomAllocator)
177 {
178 iCustomAllocator->deallocate(iBaseAddress);
179 }
180 else
181 {
182 _oscl_free(iBaseAddress);
183 }
184
185 iCustomAllocator = 0;
186
187 iBaseAddress = 0;
188
189 return;
190 }
191
192
193
194