• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
22 
23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
24 
25 /*! \addtogroup osclmemory OSCL Memory
26  *
27  * @{
28  */
29 
30 
31 /*! \file oscl_mem.h
32     \brief This file contains basic memory definitions for common use across platforms.
33 
34     This is the main entry point header file for the OSCL memory library.  It should be
35     the only one users directly include.  Basic memory copy, compare, and move functions
36     are defined here as well as the allocation functions.
37 */
38 
39 #ifndef OSCL_MEM_H_INCLUDED
40 #define OSCL_MEM_H_INCLUDED
41 
42 #ifndef OSCLCONFIG_MEMORY_H_INCLUDED
43 #include "osclconfig_memory.h"
44 #endif
45 
46 #ifndef OSCL_BASE_H_INCLUDED
47 #include "oscl_base.h"
48 #endif
49 
50 #ifndef OSCL_TYPES_H_INCLUDE
51 #include "oscl_types.h"
52 #endif
53 
54 #ifndef OSCL_ASSERT_H_INCLUDED
55 #include "oscl_assert.h"
56 #endif
57 
58 #ifndef OSCL_MEM_BASIC_FUNCTIONS_H
59 #include "oscl_mem_basic_functions.h"
60 #endif
61 
62 #ifndef OSCL_LOCK_BASE_H_INCLUDED
63 #include "oscl_lock_base.h"
64 #endif
65 
66 #define OSCL_DISABLE_WARNING_TRUNCATE_DEBUG_MESSAGE
67 #include "osclconfig_compiler_warnings.h"
68 
69 #ifndef OSCL_MEM_INST_H_INCLUDED
70 #include "oscl_mem_inst.h"
71 #endif
72 
73 #ifndef OSCL_HEAPBASE_H_INCLUDED
74 #include "oscl_heapbase.h"
75 #endif
76 
77 //Default for OSCL_HAS_GLOBAL_NEW_DELETE in case it is *not* defined
78 //in the osclconfig_memory.h
79 #ifndef OSCL_HAS_GLOBAL_NEW_DELETE
80 #ifdef NDEBUG
81 //Release Mode - No definition for global new and delete.
82 #define OSCL_HAS_GLOBAL_NEW_DELETE 0
83 #else
84 //Debug Mode - Define global new and delete.
85 #define OSCL_HAS_GLOBAL_NEW_DELETE 1
86 #endif //NDEBUG
87 #endif //OSCL_HAS_GLOBAL_NEW_DELETE
88 
89 class OsclMem
90 {
91     public:
92         /** Per-thread initialization of Oscl Memory
93         **   @param lock: A lock class for use with multi-threaded applications.
94         **      The lock is needed in use cases where memory may be allocated
95         **      in one thread and freed in another.  In this case, there must
96         **      be a single lock object, and its pointer must be passed to
97         **      the OsclMem::Init call in each thread.
98         **      If no lock is provided, the memory manager will not be thread-safe.
99         ** @exception: Leaves on error
100         */
101         OSCL_IMPORT_REF static void Init();
102 
103         /** Per-thread cleanup of Oscl Memory
104         ** @exception: Leaves on error;
105         */
106         OSCL_IMPORT_REF static void Cleanup();
107 
108 };
109 
110 /*
111 ** Choose whether to use per-thread or singleton registry for auditing
112 */
113 #include "oscl_base.h"
114 
115 /*
116 ** Audit control block
117 */
118 #if (OSCL_BYPASS_MEMMGT)
119 //empty class for compilation only
120 class OsclAuditCB
121 {
122     public:
123 };
124 #else
125 class OsclMemStatsNode;
126 class OsclMemAudit;
127 class OsclAuditCB
128 {
129     public:
130         const OsclMemStatsNode* pStatsNode;
131         OsclMemAudit *pAudit;
132 
OsclAuditCB()133         OsclAuditCB() :
134                 pStatsNode(NULL),
135                 pAudit(NULL)
136         {}
137 
OsclAuditCB(const OsclMemStatsNode * myStatsNode,OsclMemAudit * ptr)138         OsclAuditCB(const OsclMemStatsNode* myStatsNode,
139                     OsclMemAudit *ptr)
140                 :
141                 pStatsNode(myStatsNode),
142                 pAudit(ptr)
143         {
144         }
145 };
146 #endif//OSCL_BYPASS_MEMMGT
147 
148 /**
149  * Get memory-aligned size of an object.
150  *
151  * @param size size of object
152  *
153  * @returns memory-aligned size
154  */
155 OSCL_COND_IMPORT_REF uint oscl_mem_aligned_size(uint size);
156 
157 /**
158  * Initialize an OsclAuditCB object.
159  * Sets the stats node pointer to null, and sets the
160  * audit pointer to the global audit object.
161  *
162  * @param auditCB memory management audit object
163  */
164 OSCL_IMPORT_REF void OsclMemInit(OsclAuditCB & auditCB);
165 
166 /**
167  * Cleans up the base class of a partially-constructed
168  * derived class.  This macro will call the destructor
169  * if necessary, based on the error-handling implementation.
170  *
171  * @param T: name of the base class.
172  */
173 #define OSCL_CLEANUP_BASE_CLASS(T) _OSCL_CLEANUP_BASE_CLASS(T)
174 
175 /** *******************************************************
176  * Macros for new/delete with a given allocator/deallocator.
177  */
178 
179 /**
180  * Creates an object of type T using the given allocator to
181  * acquire the memory needed.
182  *
183  * @param T_allocator allocator for objects of type T, must be
184  *    an Oscl_TAlloc<T, Allocator>, where Allocator is an Oscl_DefAlloc
185  * @param T           type of object to create
186  * @param params      object initialization parameters
187  *
188  * @return pointer to created object
189  *
190  * @exception none, unless thrown by the given allocator
191  */
192 #if(OSCL_BYPASS_MEMMGT)
193 #define OSCL_ALLOC_NEW(T_allocator, T, params) new(T_allocator.allocate(1)) T params
194 #elif(PVMEM_INST_LEVEL>0)
195 #define OSCL_ALLOC_NEW(T_allocator, T, params) new(T_allocator.allocate_fl(1,__FILE__,__LINE__)) T params
196 #else
197 #define OSCL_ALLOC_NEW(T_allocator, T, params) new(T_allocator.allocate(1)) T params
198 #endif
199 
200 /**
201  * Creates an object of type T using the given allocator to
202  * acquire the memory needed.
203  * This macro is similar to OSCL_ALLOC_NEW except that it
204  * handles constructors that leave.
205  * If the constructor leaves, the destructor will be called,
206  * and allocated memory will be freed before allowing the
207  * leave to propagate to the next level.
208  *
209  * @param T_ptr    variable to hold return value-- pointer to
210  *                 new object of type T.
211  * @param T_allocator allocator for objects of type T, must be
212  *    an Oscl_TAlloc<T, Allocator>, where Allocator is an
213  *    Oscl_DefAlloc
214  * @param T        type of object to create
215  * @param params   object initialization parameters
216  *
217  * @return pointer to created object
218  *
219  * @exception none, unless thrown by the given allocator
220  */
221 #if(OSCL_BYPASS_MEMMGT)
222 #define OSCL_TRAP_ALLOC_NEW(T_ptr,T_allocator,T,params) _OSCL_TRAP_NEW(T_allocator.allocate(1),T_allocator.deallocate,T_ptr,T,params)
223 #elif(PVMEM_INST_LEVEL>0)
224 #define OSCL_TRAP_ALLOC_NEW(T_ptr,T_allocator,T,params) _OSCL_TRAP_NEW(T_allocator.allocate_fl(1,__FILE__,__LINE__),T_allocator.deallocate,T_ptr,T,params)
225 #else
226 #define OSCL_TRAP_ALLOC_NEW(T_ptr,T_allocator,T,params) _OSCL_TRAP_NEW(T_allocator.allocate(1),T_allocator.deallocate,T_ptr,T,params)
227 #endif
228 
229 /**
230  * Deletes the object of type T using the given allocator
231  *
232  * @param T_allocator allocator for objects of type T
233  * @param T           type of object to delete
234  * @param ptr         pointer to previously created object
235  *
236  * @exception none, unless thrown by the given allocator
237  */
238 #define OSCL_ALLOC_DELETE(ptr, T_allocator, T) \
239   {\
240   ptr->~T();\
241   T_allocator.deallocate(ptr);\
242   }
243 
244 
245 /** *******************************************************
246  * Macros for malloc/free with memory management.
247  */
248 
249 //These are for internal use but need to be visible since they're used
250 //in macros.
251 #if(!OSCL_BYPASS_MEMMGT)
252 OSCL_IMPORT_REF void* _oscl_audit_malloc(size_t , OsclAuditCB & , const char * f = NULL, const int l = 0);
253 OSCL_IMPORT_REF void* _oscl_audit_calloc(size_t , size_t, OsclAuditCB & , const char * f = NULL, const int l = 0);
254 OSCL_IMPORT_REF void* _oscl_audit_realloc(void*, size_t , OsclAuditCB & , const char * f = NULL, const int l = 0);
255 OSCL_IMPORT_REF void* _oscl_audit_new(size_t , OsclAuditCB & , const char * f = NULL, const int l = 0) ;
256 OSCL_IMPORT_REF void* _oscl_default_audit_malloc(size_t , const char * f = NULL, const int l = 0);
257 OSCL_IMPORT_REF void* _oscl_default_audit_calloc(size_t , size_t, const char * f = NULL, const int l = 0);
258 OSCL_IMPORT_REF void* _oscl_default_audit_realloc(void*, size_t , const char * f = NULL, const int l = 0);
259 OSCL_IMPORT_REF void* _oscl_default_audit_new(size_t , const char * f = NULL, const int l = 0) ;
260 OSCL_IMPORT_REF void _oscl_audit_free(void *);
261 #else
262 OSCL_IMPORT_REF void* _oscl_default_new(size_t nBytes);
263 #endif//OSCL_BYPASS_MEMMGT
264 
265 #if (OSCL_HAS_GLOBAL_NEW_DELETE)
266 //Global New operator overloaded to check native new operators called
267 
268 #if(!OSCL_BYPASS_MEMMGT)
new(size_t aSize,const char * aFile,int aLine)269 inline void * operator new(size_t aSize, const char *aFile, int aLine)
270 {
271 #if(PVMEM_INST_LEVEL>0)
272     //in case NULL is passed in, record this file & line #
273     if (!aFile)
274         return _oscl_default_audit_new(aSize, __FILE__, __LINE__);
275 #endif
276     return _oscl_default_audit_new(aSize, aFile, aLine);
277 };
278 #endif
279 
new(size_t aSize)280 inline void * operator new(size_t aSize)
281 {
282 #if(!OSCL_BYPASS_MEMMGT)
283 #if(PVMEM_INST_LEVEL>0)
284     return _oscl_default_audit_new(aSize, __FILE__, __LINE__);
285 #else
286     return _oscl_default_audit_new(aSize);
287 #endif
288 #else
289     return _oscl_default_new(aSize);
290 #endif
291 };
292 
delete(void * aPtr)293 inline void operator delete(void *aPtr)
294 {
295 #if(!OSCL_BYPASS_MEMMGT)
296     _oscl_audit_free(aPtr);
297 #else
298     _oscl_free(aPtr);
299 #endif
300 };
301 
302 #if(!OSCL_BYPASS_MEMMGT)
303 inline void * operator new[](size_t aSize, const char *aFile, int aLine)
304 {
305 #if(PVMEM_INST_LEVEL>0)
306     //in case NULL is passed in, record this file & line #
307     if (!aFile)
308         return _oscl_default_audit_new(aSize, __FILE__, __LINE__);
309 #endif
310     return _oscl_default_audit_new(aSize, aFile, aLine);
311 };
312 #endif
313 
314 inline void * operator new[](size_t aSize)
315 {
316 #if(!OSCL_BYPASS_MEMMGT)
317 #if(PVMEM_INST_LEVEL>0)
318     return _oscl_default_audit_new(aSize, __FILE__, __LINE__);
319 #else
320     return _oscl_default_audit_new(aSize);
321 #endif
322 #else
323     return _oscl_default_new(aSize);
324 #endif
325 };
326 
327 inline void operator delete[](void *aPtr)
328 {
329 #if(!OSCL_BYPASS_MEMMGT)
330     _oscl_audit_free(aPtr);
331 #else
332     _oscl_free(aPtr);
333 #endif
334 };
335 #endif //OSCL_HAS_GLOBAL_NEW_DELETE
336 
337 /**
338  * Allocates a memory block using the memory management's
339  * global audit object.
340  *
341  * @param count  number of bytes to allocate
342  *
343  * @return a void pointer to the allocated space, or NULL if there is insufficient
344  *         memory available.
345  *
346  * @exception none
347  */
348 #if(OSCL_BYPASS_MEMMGT)
349 #define OSCL_MALLOC(count) _oscl_malloc(count)
350 #elif(PVMEM_INST_LEVEL>0)
351 #define OSCL_MALLOC(count) _oscl_default_audit_malloc(count,__FILE__,__LINE__)
352 #else
353 #define OSCL_MALLOC(count) _oscl_default_audit_malloc(count)
354 #endif
355 
356 /*
357 ** The public oscl_malloc call has been deprecated.
358 ** PV code should call OSCL_MALLOC.
359 ** This macro is defined for back-compatibility.
360 */
361 #define oscl_malloc(a) OSCL_MALLOC(a)
362 
363 /**
364  * Another back-compatibility definition.
365  */
366 #define OSCL_DEFAULT_MALLOC(x) OSCL_MALLOC(x)
367 
368 /**
369 * Allocates a memory block using the given audit object.
370 *
371 * @param auditCB input memory management audit object
372 * @param count   number of bytes to allocate
373 *
374 * @return a void pointer to the allocated space, or NULL if there is insufficient
375 *         memory available.
376 *
377 * @exception none
378 */
379 #if(OSCL_BYPASS_MEMMGT)
380 #define OSCL_AUDIT_MALLOC(auditCB, count) _oscl_malloc(count)
381 #elif(PVMEM_INST_LEVEL>0)
382 #define OSCL_AUDIT_MALLOC(auditCB, count) _oscl_audit_malloc(count, auditCB, __FILE__, __LINE__)
383 #else
384 #define OSCL_AUDIT_MALLOC(auditCB, count) _oscl_audit_malloc(count, auditCB)
385 #endif
386 
387 /**
388  * Allocates a memory block using the memory management's
389  * global audit object.  The block is initialized to zero.
390  *
391  * @param num  number of elements
392  * @param size  number of bytes to allocate for each element
393  *
394  * @return a void pointer to the allocated space, or NULL if there is insufficient
395  *         memory available.
396  *
397  * @exception none
398  */
399 #if(OSCL_BYPASS_MEMMGT)
400 #define OSCL_CALLOC(num,size) _oscl_calloc(num,size)
401 #elif(PVMEM_INST_LEVEL>0)
402 #define OSCL_CALLOC(num,size) _oscl_default_audit_calloc(num,size,__FILE__,__LINE__)
403 #else
404 #define OSCL_CALLOC(num,size) _oscl_default_audit_calloc(num,size)
405 #endif
406 
407 /*
408 ** The public oscl_calloc call has been deprecated.
409 ** PV code should call OSCL_CALLOC.
410 ** This macro is defined for back-compatibility.
411 */
412 #define oscl_calloc(a,b) OSCL_CALLOC(a,b)
413 
414 /**
415 * Allocates a memory block using the specified
416 * audit object.  The block is initialized to zero.
417 *
418 * @param auditCB input memory management audit object
419 * @param num  number of elements
420 * @param size  number of bytes to allocate for each element
421 *
422 * @return a void pointer to the allocated space, or NULL if there is insufficient
423 *         memory available.
424 *
425 * @exception none
426 */
427 #if(OSCL_BYPASS_MEMMGT)
428 #define OSCL_AUDIT_CALLOC(auditCB, num,size) _oscl_calloc(num,size)
429 #elif(PVMEM_INST_LEVEL>0)
430 #define OSCL_AUDIT_CALLOC(auditCB, num,size) _oscl_audit_calloc(num,size, auditCB, __FILE__, __LINE__)
431 #else
432 #define OSCL_AUDIT_CALLOC(auditCB, num,size) _oscl_audit_calloc(num,size, auditCB)
433 #endif
434 
435 /**
436  * Re-Allocates a memory block using the memory management's
437  * global audit object.
438  *
439  * @param ptr  original memory block
440  * @param new_size  New size of the block
441  *
442  * @return a void pointer to the allocated space, or NULL if there is insufficient
443  *         memory available.
444  *
445  * @exception none
446  */
447 #if(OSCL_BYPASS_MEMMGT)
448 #define OSCL_REALLOC(ptr,new_size) _oscl_realloc(ptr,new_size)
449 #elif(PVMEM_INST_LEVEL>0)
450 #define OSCL_REALLOC(ptr,new_size) _oscl_default_audit_realloc(ptr,new_size,__FILE__,__LINE__)
451 #else
452 #define OSCL_REALLOC(ptr,new_size) _oscl_default_audit_realloc(ptr,new_size)
453 #endif
454 
455 /*
456 ** The public oscl_realloc call has been deprecated.
457 ** PV code should call OSCL_REALLOC.  This macro is
458 ** defined for back-compatibility.
459 */
460 #define oscl_realloc(a,b) OSCL_REALLOC(a,b)
461 
462 /**
463 * Re-Allocates a memory block using the specified
464 * audit object.
465 *
466 * @param auditCB input memory management audit object
467 * @param ptr  original memory block
468 * @param new_size  New size of the block
469 *
470 * @return a void pointer to the allocated space, or NULL if there is insufficient
471 *         memory available.
472 *
473 * @exception none
474 */
475 #if(OSCL_BYPASS_MEMMGT)
476 #define OSCL_AUDIT_REALLOC(auditCB, ptr,new_size) _oscl_realloc(ptr,new_size)
477 #elif(PVMEM_INST_LEVEL>0)
478 #define OSCL_AUDIT_REALLOC(auditCB, ptr,new_size) _oscl_audit_realloc(ptr,new_size, auditCB, __FILE__, __LINE__)
479 #else
480 #define OSCL_AUDIT_REALLOC(auditCB, ptr,new_size) _oscl_audit_realloc(ptr,new_size, auditCB)
481 #endif
482 
483 /**
484  * Deallocates or frees a memory block.
485  *
486  * @param ptr    pointer to previously allocated memory block using the given audit object
487  */
488 #if(OSCL_BYPASS_MEMMGT)
489 #define OSCL_FREE(ptr) _oscl_free(ptr)
490 #else
491 #define OSCL_FREE(ptr) _oscl_audit_free(ptr)
492 #endif
493 
494 /*
495 ** The public oscl_free call has been deprecated.
496 ** PV code should call OSCL_FREE.
497 ** This macro is defined for back-compatibility.
498 */
499 #define oscl_free(x) OSCL_FREE(x)
500 
501 /**
502  * Another back-compatibility definition.
503  */
504 #define OSCL_DEFAULT_FREE(x) OSCL_FREE(x)
505 
506 /** *******************************************************
507  * Macros for new/delete with memory management.
508  */
509 
510 /**
511  * Oscl "new" operator.  This uses the global memory
512  * audit object.
513  *
514  * @param T       data type for 'new' operation
515  * @param params  object initialization parameters
516  *
517  * @return pointer to the newly created object of type T
518  *
519  * @exception may leave with code = bad alloc
520  *
521  *
522  */
523 #if(OSCL_BYPASS_MEMMGT)
524 #define OSCL_NEW( T, params) new T params
525 #elif!(OSCL_HAS_GLOBAL_NEW_DELETE)
526 #define OSCL_NEW( T, params) new T params
527 #elif(PVMEM_INST_LEVEL>0)
528 #define OSCL_NEW( T, params) new(__FILE__,__LINE__) T params
529 #else
530 #define OSCL_NEW( T, params) new T params
531 #endif
532 
533 /********************************************************
534  * Macro for placement new
535  *
536  * @param ptr          pointer to an object
537  *
538  * @param constructor  constructor of the class for the object
539  *
540 ********************************************************/
541 #define OSCL_PLACEMENT_NEW(ptr, constructor) new(ptr) constructor
542 
543 /**
544  * Oscl "new" operator.  This uses the global memory
545  * audit object.  This operator is similar to OSCL_NEW
546  * except that it will handle constructors that leave.
547  * If the constructor leaves, the destructor will be called,
548  * and allocated memory will be freed before allowing the
549  * leave to propagate to the next level.
550  *
551  * @param T_ptr   variable to hold return value-- pointer to
552  *                new object of type T.
553  * @param T       data type for 'new' operation
554  * @param params  object initialization parameters
555  *
556  * @return pointer to the newly created object of type T
557  *
558  * @exception may leave with code = bad alloc
559  *
560  *
561  */
562 #if(OSCL_BYPASS_MEMMGT)
563 #define OSCL_TRAP_NEW(T_ptr,T,params) _OSCL_TRAP_NEW(_oscl_default_new(sizeof(T)),_oscl_free,T_ptr,T,params)
564 #elif!(OSCL_HAS_GLOBAL_NEW_DELETE)
565 #define OSCL_TRAP_NEW(T_ptr,T,params) _OSCL_TRAP_NEW(_oscl_default_audit_new(sizeof(T)),_oscl_audit_free,T_ptr,T,params)
566 #elif(PVMEM_INST_LEVEL>0)
567 #define OSCL_TRAP_NEW(T_ptr,T,params) _OSCL_TRAP_NEW(_oscl_default_audit_new(sizeof(T),__FILE__,__LINE__),_oscl_audit_free,T_ptr,T,params)
568 #else
569 #define OSCL_TRAP_NEW(T_ptr,T,params) _OSCL_TRAP_NEW(_oscl_default_audit_new(sizeof(T)),_oscl_audit_free,T_ptr,T,params)
570 #endif
571 
572 
573 /**
574  * Oscl "new" operator.  This uses the specified memory
575  * audit object.
576  *
577  * @param auditCB input memory management audit object
578  * @param T       data type for 'new' operation
579  * @param params  object initialization parameters
580  *
581  * @return pointer to the newly created object of type T
582  *
583  * @exception may leave with code = bad alloc
584  *
585  */
586 #if(OSCL_BYPASS_MEMMGT)
587 #define OSCL_AUDIT_NEW(auditCB, T, params) new(_oscl_default_new(sizeof(T))) T params
588 #elif!(OSCL_HAS_GLOBAL_NEW_DELETE)
589 #define OSCL_AUDIT_NEW(auditCB, T, params) new(_oscl_audit_new(sizeof(T),auditCB)) T params
590 #elif(PVMEM_INST_LEVEL>0)
591 #define OSCL_AUDIT_NEW(auditCB, T, params) new(_oscl_audit_new(sizeof(T),auditCB,__FILE__,__LINE__)) T params
592 #else
593 #define OSCL_AUDIT_NEW(auditCB, T, params) new(_oscl_audit_new(sizeof(T),auditCB)) T params
594 #endif
595 
596 /**
597  * Oscl "new" operator.  This uses the specified memory
598  * audit object.  This macro is similar to OSCL_AUDIT_NEW
599  * except that it will handle constructors that leave.
600  * If the constructor leaves, the destructor will be called,
601  * and allocated memory will be freed before allowing the
602  * leave to propagate to the next level.
603  *
604  * @param T_ptr   variable to hold return value-- pointer to
605  *                new object of type T.
606  * @param auditCB input memory management audit object
607  * @param T       data type for 'new' operation
608  * @param params  object initialization parameters
609  *
610  * @return pointer to the newly created object of type T
611  *
612  * @exception may leave with code = bad alloc
613  *
614  */
615 #if(OSCL_BYPASS_MEMMGT)
616 #define OSCL_TRAP_AUDIT_NEW(T_ptr,auditCB,T,params) _OSCL_TRAP_NEW(_oscl_default_new(sizeof(T)),_oscl_free,T_ptr,T,params)
617 #elif!(OSCL_HAS_GLOBAL_NEW_DELETE)
618 #define OSCL_TRAP_AUDIT_NEW(T_ptr,auditCB,T,params) _OSCL_TRAP_NEW(_oscl_audit_new(sizeof(T),auditCB),_oscl_audit_free,T_ptr,T,params)
619 #elif(PVMEM_INST_LEVEL>0)
620 #define OSCL_TRAP_AUDIT_NEW(T_ptr,auditCB,T,params) _OSCL_TRAP_NEW(_oscl_audit_new(sizeof(T),auditCB,__FILE__,__LINE__),_oscl_audit_free,T_ptr,T,params)
621 #else
622 #define OSCL_TRAP_AUDIT_NEW(T_ptr,auditCB,T,params) _OSCL_TRAP_NEW(_oscl_audit_new(sizeof(T),auditCB),_oscl_audit_free,T_ptr,T,params)
623 #endif
624 
625 /**
626  * Oscl "delete" operator.
627  *
628  * @param ptr pointer to memory block previously allocated with OSCL_NEW
629  *
630  * @return void
631  */
632 #define OSCL_DELETE(ptr) {\
633     if(ptr){delete(ptr);}\
634 }
635 
636 
637 /** *******************************************************
638  * Macros for array new/delete with memory management.
639  * These only work for simple array types and cannot
640  * be used for class types with constructor/destructors.
641  *
642  * Note: some compilers do not support placement array
643  * new operator, so these macros don't use it.
644  */
645 
646 /**
647  * Oscl array "new" operator.  This uses the input memory
648  * audit object.
649  *
650  * @param auditCB input memory management audit object
651  * @param T       data type for 'new' operation
652  * @param count   number of elements to create
653  *
654  * @return pointer to the newly created object array of type T
655  *
656  * @exception may leave with code = bad alloc
657  *
658  *
659  */
660 #if(OSCL_BYPASS_MEMMGT)
661 #define OSCL_AUDIT_ARRAY_NEW(auditCB, T, count) new(_oscl_default_new(sizeof(T)*(count))) T
662 #elif!(OSCL_HAS_GLOBAL_NEW_DELETE)
663 #define OSCL_AUDIT_ARRAY_NEW(auditCB, T, count) new(_oscl_audit_new(sizeof(T)*(count),auditCB)) T
664 #elif(PVMEM_INST_LEVEL>0)
665 #define OSCL_AUDIT_ARRAY_NEW(auditCB, T, count) new(_oscl_audit_new(sizeof(T)*(count),auditCB,__FILE__,__LINE__)) T
666 #else
667 #define OSCL_AUDIT_ARRAY_NEW(auditCB, T, count) new(_oscl_audit_new(sizeof(T)*(count),auditCB)) T
668 #endif
669 
670 /**
671  * Oscl array "new" operator.  This uses the global memory
672  * audit object.
673  *
674  * @param T       data type for 'new' operation
675  * @param count   number of elements to create
676  *
677  * @return pointer to the newly created object array of type T
678  *
679  * @exception may leave with code = bad alloc
680  *
681  *
682  */
683 #if(OSCL_BYPASS_MEMMGT)
684 #define OSCL_ARRAY_NEW(T, count) new T[count]
685 #elif!(OSCL_HAS_GLOBAL_NEW_DELETE)
686 #define OSCL_ARRAY_NEW(T, count) new T[count]
687 #elif(PVMEM_INST_LEVEL>0)
688 #define OSCL_ARRAY_NEW(T, count) new(__FILE__,__LINE__) T[count]
689 #else
690 #define OSCL_ARRAY_NEW(T, count) new T[count]
691 #endif
692 
693 /**
694  * Oscl array delete operator..
695  *
696  * @param ptr pointer to memory block previously allocated with OSCL_ARRAY_NEW
697  *
698  * @return void
699  */
700 #define OSCL_ARRAY_DELETE(ptr) delete [] ptr
701 
702 
703 /**
704 * Previously this was in oscl_mem_imp.h
705 */
706 
707 #ifndef OSCL_DEFALLOC_H_INCLUDED
708 #include "oscl_defalloc.h"
709 #endif
710 
711 #ifndef OSCL_REFCOUNTER_H_INCLUDED
712 #include "oscl_refcounter.h"
713 #endif
714 
715 #ifndef OSCL_MEM_BASIC_FUNCTIONS_H_INCLUDED
716 #include "oscl_mem_basic_functions.h"
717 #endif
718 
719 #ifndef OSCL_ERROR_H_INCLUDED
720 #include "oscl_error.h"
721 #endif
722 
723 #ifndef OSCL_EXCEPTION_H_INCLUDED
724 #include "oscl_exception.h"
725 #endif
726 
727 #define OSCL_DISABLE_WARNING_TRUNCATE_DEBUG_MESSAGE
728 #include "osclconfig_compiler_warnings.h"
729 
730 /** \class OsclMemAllocator
731 ** A simple allocator class.  Configurable as to whether
732 ** this goes through the memory manager or not.
733 **
734 */
735 class OsclMemAllocator : public Oscl_DefAlloc
736 {
737     public:
738         /** This API throws an exception when malloc returns NULL.
739           * n must be greater than 0.
740           *
741           * @return pointer (or Leave with OsclErrNoMemory )
742           *
743           */
allocate(const uint32 n)744         OsclAny* allocate(const uint32 n)
745         {
746 #if(OSCL_BYPASS_MEMMGT)
747             OsclAny* p = _oscl_malloc(n);
748             if (!p)
749                 OsclError::LeaveIfNull(p);
750 #if OSCL_MEM_FILL_WITH_PATTERN
751             oscl_memset(p, 0x55, n);
752 #endif
753             return (p);
754 #elif (PVMEM_INST_LEVEL>0)
755             //this is really a usage error-- caller should provide file & line.
756             //set a debug breakpoint here...
757             return allocate_fl(n, __FILE__, __LINE__);
758 #else
759             return allocate_fl(n, NULL, 0);
760 #endif
761         }
762 
763 #if(!OSCL_BYPASS_MEMMGT)
allocate_fl(const uint32 n,const char * file_name,const int line_num)764         OsclAny* allocate_fl(const uint32 n, const char * file_name, const int line_num)
765         {
766             OsclAny* p = _oscl_default_audit_malloc(n, file_name, line_num);
767             if (!p)
768                 OsclError::LeaveIfNull(p);
769 #if OSCL_MEM_FILL_WITH_PATTERN
770             oscl_memset(p, 0x55, n);
771 #endif
772             return (p);
773         }
774 #endif
775 
deallocate(OsclAny * p)776         void deallocate(OsclAny* p)
777         {
778             if (p)
779                 OSCL_FREE(p);
780         }
781 };
782 
783 
784 /** \class OsclMemBasicAllocator
785 ** A simple allocator class that does not use the memory management.
786 **
787 ** Note: this allocator is for internal use by Oscl only.  Higher
788 ** level code should use OsclMemAllocator.
789 **
790 */
791 class OsclMemBasicAllocator : public Oscl_DefAlloc
792 {
793     public:
794         /** This API throws an exception when malloc returns NULL.
795           * n must be greater than 0.
796           *
797           * @return pointer (or Leave with OsclErrNoMemory )
798           *
799           */
allocate(const uint32 n)800         OsclAny* allocate(const uint32 n)
801         {
802             OsclAny* p = _oscl_malloc(n);
803             OsclError::LeaveIfNull(p);
804 #if OSCL_MEM_FILL_WITH_PATTERN
805             oscl_memset(p, 0x55, n);
806 #endif
807             return (p);
808         }
809 
deallocate(OsclAny * p)810         void deallocate(OsclAny* p)
811         {
812             if (p)
813                 _oscl_free(p);
814         }
815 };
816 
817 /** \class OsclMemAllocDestructDealloc
818 ** An OsclAllocDestructDealloc class that uses
819 ** OsclMemAllocator.
820 */
821 template <class T> class OsclMemAllocDestructDealloc : public OsclAllocDestructDealloc
822 {
823     public:
824 #if !(OSCL_BYPASS_MEMMGT)
allocate_fl(const uint32 size,const char * file_name,const int line_num)825         OsclAny* allocate_fl(const uint32 size, const char * file_name, const int line_num)
826         {
827             return alloc.allocate_fl(size, file_name, line_num);
828         }
829 #endif
allocate(const uint32 size)830         OsclAny* allocate(const uint32 size)
831         {
832 #if(OSCL_BYPASS_MEMMGT)
833             return alloc.allocate(size);
834 #elif(PVMEM_INST_LEVEL>0)
835             //this is really a usage error-- caller should provide file & line.
836             //set a debug breakpoint here...
837             return allocate_fl(size, __FILE__, __LINE__);
838 #else
839             return allocate_fl(size, NULL, 0);
840 #endif
841         }
deallocate(OsclAny * p)842         void deallocate(OsclAny* p)
843         {
844             alloc.deallocate(p);
845         }
destruct_and_dealloc(OsclAny * p)846         void destruct_and_dealloc(OsclAny* p)
847         {
848             T* ptr = reinterpret_cast<T*>(p);
849             ptr->~T();
850             deallocate(p);
851             OSCL_UNUSED_ARG(ptr); // removes warning on some compilers
852         }
853     private:
854         OsclMemAllocator alloc;
855 };
856 
857 /** \class OsclMemBasicAllocDestructDealloc
858 ** An OsclAllocDestructDealloc class that uses
859 ** OsclMemBasicAllocator.
860 */
861 template <class T> class OsclMemBasicAllocDestructDealloc : public OsclAllocDestructDealloc
862 {
863     public:
allocate(const uint32 size)864         OsclAny* allocate(const uint32 size)
865         {
866 #if(OSCL_BYPASS_MEMMGT)
867             return alloc.allocate(size);
868 #else
869             return alloc.allocate_fl(size, NULL, 0);
870 #endif
871         }
deallocate(OsclAny * p)872         void deallocate(OsclAny* p)
873         {
874             alloc.deallocate(p);
875         }
destruct_and_dealloc(OsclAny * p)876         void destruct_and_dealloc(OsclAny* p)
877         {
878             T* ptr = reinterpret_cast<T*>(p);
879             ptr->~T();
880             deallocate(p);
881             OSCL_UNUSED_ARG(ptr); // removes warning on some compilers
882         }
883     private:
884         OsclMemBasicAllocator alloc;
885 };
886 
887 /**
888  * This class is used to get a pointer to the global audit object.
889  */
890 
891 class OsclMemAudit;
892 class OsclMemGlobalAuditObject
893 {
894     public:
895         typedef OsclMemAudit audit_type;
896         /**
897          * returns the global audit object.  For use
898          * in macros only-- not a public API.
899          */
900         OSCL_IMPORT_REF static audit_type* getGlobalMemAuditObject();
901 
902     private:
903         /**
904          * creates the global audit object
905          */
906         static void createGlobalMemAuditObject();
907 
908         /**
909          * deletes the global audit object
910          */
911         static void deleteGlobalMemAuditObject();
912 
913         friend class OsclMem;
914 };
915 
916 /**
917 * HeapBase is the base class for all classes that allocates memory.
918 *
919 * HeapBase has overloaded new and delete operators.
920 *
921 * Derived from _OsclHeapBase providing CBase* alike pointer and virtual destructor for cleanupstack
922 * to Push and Pop for cleanup when leave occurs.
923 *
924 * HeapBase has a virtual destructor which calls the destructor of all the derived classes.
925 */
926 
927 class HeapBase : public _OsclHeapBase
928 {
929     public:
930 #if (OSCL_HAS_HEAP_BASE_SUPPORT)
931 
932 #if(!OSCL_BYPASS_MEMMGT)
933         static void* operator new(size_t aSize, const char *aFile = NULL, const int aLine = 0)
934         {
935 #if(PVMEM_INST_LEVEL>0)
936             //in case NULL is passed in, record this file & line #
937             if (!aFile)
938                 return _oscl_default_audit_new(aSize, __FILE__, __LINE__);
939 #endif
940             return _oscl_default_audit_new(aSize, aFile, aLine);
941         }
942 #else
943         static void* operator new(size_t aSize)
944         {
945             return _oscl_default_new(aSize);
946         }
947 #endif
948 
949         static void* operator new[](size_t aSize)
950         {
951 #if(!OSCL_BYPASS_MEMMGT)
952             return _oscl_default_audit_new(aSize);
953 #else
954             return _oscl_default_new(aSize);
955 #endif
956         }
957 
958         static void* operator new[](size_t aSize, const char *aFile = NULL, const int aLine = 0)
959         {
960 #if(!OSCL_BYPASS_MEMMGT)
961 #if(PVMEM_INST_LEVEL>0)
962             //in case NULL is passed in, record this file & line #
963             if (!aFile)
964                 return _oscl_default_audit_new(aSize, __FILE__, __LINE__);
965 #endif
966             return _oscl_default_audit_new(aSize, aFile, aLine);
967 #else
968             OSCL_UNUSED_ARG(aFile);
969             OSCL_UNUSED_ARG(aLine);
970             return _oscl_default_new(aSize);
971 #endif
972         }
973 
new(size_t aSize,void * aPtr)974         static void* operator new(size_t aSize, void *aPtr)
975         {
976             return aPtr;
977         }
978 
delete(void * aPtr)979         static void operator delete(void* aPtr)
980         {
981 #if(!OSCL_BYPASS_MEMMGT)
982             _oscl_audit_free(aPtr);
983 #else
984             _oscl_free(aPtr);
985 #endif
986         }
987 
988         static void operator delete[](void* aPtr)
989         {
990 #if(!OSCL_BYPASS_MEMMGT)
991             _oscl_audit_free(aPtr);
992 #else
993             _oscl_free(aPtr);
994 #endif
995         }
996 #endif //OSCL_HAS_HEAP_BASE_SUPPORT
HeapBase()997         HeapBase() {};
~HeapBase()998         virtual ~HeapBase() {};
999 };
1000 
1001 /** Internal-use macro to catch leaves
1002 *in constructors.  If the constructor leaves,
1003 *this will free the memory
1004 *before allowing the leave to propagate to the next
1005 *level.  It is the constructor's responsibility to
1006 *cleanup any memory in the partially constructed
1007 *object before leaving.  This cleanup may include
1008 *cleaning up the base class using the OSCL_CLEANUP_BASE_CLASS
1009 *macro.
1010 *
1011 * @param exp: expression to allocate memory.
1012 * @param Tptr:variable to hold result.
1013 * @param T: type
1014 * @param params: constructor arg list
1015 * @param freeFunc: delete or free function.
1016 */
1017 #define _OSCL_TRAP_NEW(exp,freeFunc,T_ptr,T,params)\
1018 {\
1019     int32 __err;\
1020     OsclAny*__ptr=exp;\
1021     OSCL_TRY(__err,T_ptr=new(__ptr) T params;);\
1022     if(__err){\
1023         freeFunc(__ptr);\
1024         T_ptr=NULL;\
1025         OsclError::Leave(__err);\
1026     }\
1027 }
1028 
1029 /**
1030  * This macro is used to cleanup the
1031  * base class in a derived-class constructor
1032  * just before a leave occurs.
1033  *
1034  * @param T: base class name.
1035  */
1036 #ifdef PVERROR_IMP_CPP_EXCEPTIONS
1037 //when using C++ exceptions, base class cleanup is automatic
1038 #define _OSCL_CLEANUP_BASE_CLASS(T)
1039 #else
1040 //otherwise the destructor needs to be called explicitly.
1041 #define _OSCL_CLEANUP_BASE_CLASS(T) this->T::~T()
1042 #endif
1043 
1044 
1045 /*! @} */
1046 
1047 
1048 #if (!OSCL_DISABLE_INLINES)
1049 #include "oscl_mem.inl"
1050 #endif
1051 
1052 #endif // OSCL_MEM_H_INCLUDED
1053 
1054 
1055 
1056 /*! @} */
1057 
1058