Main Page   Modules   Class Hierarchy   Data Structures   File List   Data Fields   Globals   Related Pages  

oscl_defalloc.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00003 
00004 //                     O S C L _ D E F A L L O C
00005 
00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
00007 
00018 #ifndef OSCL_DEFALLOC_H_INCLUDED
00019 #define OSCL_DEFALLOC_H_INCLUDED
00020 
00021 #ifndef OSCL_BASE_H_INCLUDED
00022 #include "oscl_base.h"
00023 #endif
00024 
00025 #define OSCL_DISABLE_WARNING_TRUNCATE_DEBUG_MESSAGE
00026 #include "osclconfig_compiler_warnings.h"
00027 
00028 #ifndef OSCL_MEM_INST_H_INCLUDED
00029 #include "oscl_mem_inst.h"
00030 #endif
00031 
00032 //A macro for using the Oscl_Alloc or Oscl_DefAlloc call with file name and
00033 //line number inputs to aid memory auditing.
00034 #if(PVMEM_INST_LEVEL>0)
00035 #define ALLOCATE(n) allocate_fl(n,__FILE__,__LINE__)
00036 #else
00037 #define ALLOCATE(n) allocate(n)
00038 #endif
00039 
00040 //A macro for using the Oscl_TAlloc call with file name and line number inputs
00041 //to aid memory auditing.
00042 #if(PVMEM_INST_LEVEL>0)
00043 #define ALLOC_AND_CONSTRUCT(n) alloc_and_construct_fl(n,__FILE__,__LINE__)
00044 #else
00045 #define ALLOC_AND_CONSTRUCT(n) alloc_and_construct(n)
00046 #endif
00047 
00048 class Oscl_Alloc
00049 {
00050     public:
00051         virtual ~Oscl_Alloc() {}
00052         virtual OsclAny* allocate(const uint32 size) = 0;
00053 
00054         //Allocator with file name and line number inputs to aid memory auditing.
00055         //This call should be used in cases where the allocation will invoke
00056         //the Oscl memory manager.
00057         //A default implementation is provided for use with allocators that don't
00058         //invoke Oscl memory manager.
00059         virtual OsclAny* allocate_fl(const uint32 size, const char * file_name, const int line_num)
00060         {
00061             OSCL_UNUSED_ARG(file_name);
00062             OSCL_UNUSED_ARG(line_num);
00063             return allocate(size);
00064         }
00065 };
00066 
00067 class Oscl_Dealloc
00068 {
00069     public:
00070         virtual void deallocate(OsclAny* p) = 0;
00071         virtual ~Oscl_Dealloc() {}
00072 };
00073 
00074 
00075 class Oscl_DefAlloc : public Oscl_Alloc, public Oscl_Dealloc
00076 {
00077     public:
00078         virtual OsclAny* allocate(const uint32 size) = 0;
00079 
00080         //Allocator with file name and line number inputs to aid memory auditing.
00081         //This call should be used in cases where the allocation will invoke
00082         //the Oscl memory manager.
00083         //A default implementation is provided for use with allocators that don't
00084         //invoke Oscl memory manager.
00085         virtual OsclAny* allocate_fl(const uint32 size, const char * file_name, const int line_num)
00086         {
00087             OSCL_UNUSED_ARG(file_name);
00088             OSCL_UNUSED_ARG(line_num);
00089             return allocate(size);
00090         }
00091         virtual void deallocate(OsclAny* p) = 0;
00092 };
00093 
00094 
00095 class OsclDestructDealloc
00096 {
00097     public:
00098         virtual ~OsclDestructDealloc() {}
00099         virtual void destruct_and_dealloc(OsclAny* ptr) = 0;
00100 };
00101 
00102 class OsclAllocDestructDealloc
00103         : public OsclDestructDealloc, public Oscl_DefAlloc
00104 {
00105 
00106     public:
00107         virtual ~OsclAllocDestructDealloc() {};
00108 };
00109 
00110 template<class T, class Alloc>
00111 class Oscl_TAlloc : public OsclDestructDealloc
00112 {
00113     public:
00114         typedef T           value_type;
00115         typedef T           * pointer;
00116         typedef const T     * const_pointer;
00117         typedef uint32      size_type;
00118         typedef T&          reference;
00119         typedef const T&    const_reference;
00120 
00121         virtual ~Oscl_TAlloc() {};
00122 
00123         //this is the preferred call-- with file and line number recorded by
00124         //the caller.  It can be invoked with the ALLOCATE macro.
00125         pointer allocate_fl(uint32 size , const char * file_name, const int line_num)
00126         {
00127             OsclAny* tmp = alloc.allocate_fl(size * sizeof(value_type), file_name, line_num);
00128             return OSCL_STATIC_CAST(pointer, tmp);
00129         }
00130 
00131         pointer allocate(uint32 size)
00132         {
00133             OsclAny* tmp = alloc.allocate_fl(size * sizeof(value_type), NULL, 0);
00134             return OSCL_STATIC_CAST(pointer, tmp);
00135         }
00136 
00137         //this is the preferred call-- with file and line number recorded by
00138         //the caller.  It can be invoked by the ALLOC_AND_CONSTRUCT macro.
00139         pointer alloc_and_construct_fl(const_reference val, const char * file_name, const int line_num)
00140         {
00141             OsclAny* tmp = alloc.allocate_fl(sizeof(value_type), file_name, line_num);
00142             construct(OSCL_STATIC_CAST(pointer, tmp), val);
00143             return OSCL_STATIC_CAST(pointer, tmp);
00144         }
00145 
00146         pointer alloc_and_construct(const_reference val)
00147         {
00148             //note: recording file & line # here is not useful-- the caller
00149             //should provide them.  Just pass zero to aid debugging.
00150             OsclAny* tmp = alloc.allocate_fl(sizeof(value_type), NULL, 0);
00151             construct(OSCL_STATIC_CAST(pointer, tmp), val);
00152             return OSCL_STATIC_CAST(pointer, tmp);
00153         }
00154 
00155         void deallocate(OsclAny* p)
00156         {
00157             alloc.deallocate(p);
00158         }
00159 
00160         void deallocate(OsclAny* p, size_type n)
00161         {
00162             OSCL_UNUSED_ARG(n);
00163             alloc.deallocate(p);
00164         }
00165 
00166         void destruct_and_dealloc(OsclAny* p)
00167         {
00168             destroy(OSCL_STATIC_CAST(pointer, p));
00169             deallocate(p);
00170         }
00171 
00172         pointer address(reference r)
00173         {
00174             return &r;
00175         }
00176         const_pointer address(const_reference r) const
00177         {
00178             return &r;
00179         }
00180 
00181         void construct(pointer p, const_reference val)
00182         {
00183             new(p) T(val);
00184         }
00185         void destroy(pointer p)
00186         {
00187             OSCL_UNUSED_ARG(p);
00188             p->~T();
00189         }
00190 
00191         template <class U, class V>
00192         struct rebind
00193         {
00194             typedef Oscl_TAlloc<U, V> other;
00195         };
00196 
00197     private:
00198         Alloc alloc;
00199 };
00200 
00201 
00204 #endif

OSCL API
Posting Version: CORE_8.508.1.1