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 // This macro is defined is osclconfig_compiler_warnings.h
    00049 // This GCC #pragma turns off compiler warning for the rest of this header file
    00050 // This needs to be done because with the GCC 4.1 toolchain, many compiler warnings
    00051 // are generated because Oscl_Alloc and Oscl_Dealloc have virtual functions, but
    00052 // no virtual destructor.
    00053 // An attempt has been made to add the virtual destructors, however, it resulted
    00054 // in run time crashes indicative of double freeing of memory.
    00055 // This is a temporary fix, until the crashes are resolved.
    00056 //
    00057 #ifdef OSCL_DISABLE_GCC_WARNING_SYSTEM_HEADER
    00058 #pragma GCC system_header
    00059 #endif
    00060
    00061 class Oscl_Alloc
    00062 {
    00063     public:
    00064         virtual OsclAny* allocate(const uint32 size) = 0;
    00065
    00066         //Allocator with file name and line number inputs to aid memory auditing.
    00067         //This call should be used in cases where the allocation will invoke
    00068         //the Oscl memory manager.
    00069         //A default implementation is provided for use with allocators that don't
    00070         //invoke Oscl memory manager.
    00071         virtual OsclAny* allocate_fl(const uint32 size, const char * file_name, const int line_num)
    00072         {
    00073             OSCL_UNUSED_ARG(file_name);
    00074             OSCL_UNUSED_ARG(line_num);
    00075             return allocate(size);
    00076         }
    00077 };
    00078
    00079 class Oscl_Dealloc
    00080 {
    00081     public:
    00082         virtual void deallocate(OsclAny* p) = 0;
    00083 };
    00084
    00085
    00086 class Oscl_DefAlloc : public Oscl_Alloc, public Oscl_Dealloc
    00087 {
    00088     public:
    00089         virtual OsclAny* allocate(const uint32 size) = 0;
    00090
    00091         //Allocator with file name and line number inputs to aid memory auditing.
    00092         //This call should be used in cases where the allocation will invoke
    00093         //the Oscl memory manager.
    00094         //A default implementation is provided for use with allocators that don't
    00095         //invoke Oscl memory manager.
    00096         virtual OsclAny* allocate_fl(const uint32 size, const char * file_name, const int line_num)
    00097         {
    00098             OSCL_UNUSED_ARG(file_name);
    00099             OSCL_UNUSED_ARG(line_num);
    00100             return allocate(size);
    00101         }
    00102         virtual void deallocate(OsclAny* p) = 0;
    00103 };
    00104
    00105
    00106 class OsclDestructDealloc
    00107 {
    00108     public:
    00109         virtual void destruct_and_dealloc(OsclAny* ptr) = 0;
    00110 };
    00111
    00112 class OsclAllocDestructDealloc
    00113         : public OsclDestructDealloc, public Oscl_DefAlloc
    00114 {
    00115
    00116     public:
    00117         virtual ~OsclAllocDestructDealloc() {};
    00118 };
    00119
    00120 template<class T, class Alloc>
    00121 class Oscl_TAlloc : public OsclDestructDealloc
    00122 {
    00123     public:
    00124         typedef T           value_type;
    00125         typedef T           * pointer;
    00126         typedef const T     * const_pointer;
    00127         typedef uint32      size_type;
    00128         typedef T&          reference;
    00129         typedef const T&    const_reference;
    00130
    00131         virtual ~Oscl_TAlloc() {};
    00132
    00133         //this is the preferred call-- with file and line number recorded by
    00134         //the caller.  It can be invoked with the ALLOCATE macro.
    00135         pointer allocate_fl(uint32 size , const char * file_name, const int line_num)
    00136         {
    00137             OsclAny* tmp = alloc.allocate_fl(size * sizeof(value_type), file_name, line_num);
    00138             return OSCL_STATIC_CAST(pointer, tmp);
    00139         }
    00140
    00141         pointer allocate(uint32 size)
    00142         {
    00143             OsclAny* tmp = alloc.allocate_fl(size * sizeof(value_type), NULL, 0);
    00144             return OSCL_STATIC_CAST(pointer, tmp);
    00145         }
    00146
    00147         //this is the preferred call-- with file and line number recorded by
    00148         //the caller.  It can be invoked by the ALLOC_AND_CONSTRUCT macro.
    00149         pointer alloc_and_construct_fl(const_reference val, const char * file_name, const int line_num)
    00150         {
    00151             OsclAny* tmp = alloc.allocate_fl(sizeof(value_type), file_name, line_num);
    00152             construct(OSCL_STATIC_CAST(pointer, tmp), val);
    00153             return OSCL_STATIC_CAST(pointer, tmp);
    00154         }
    00155
    00156         pointer alloc_and_construct(const_reference val)
    00157         {
    00158             //note: recording file & line # here is not useful-- the caller
    00159             //should provide them.  Just pass zero to aid debugging.
    00160             OsclAny* tmp = alloc.allocate_fl(sizeof(value_type), NULL, 0);
    00161             construct(OSCL_STATIC_CAST(pointer, tmp), val);
    00162             return OSCL_STATIC_CAST(pointer, tmp);
    00163         }
    00164
    00165         void deallocate(OsclAny* p)
    00166         {
    00167             alloc.deallocate(p);
    00168         }
    00169
    00170         void deallocate(OsclAny* p, size_type n)
    00171         {
    00172             OSCL_UNUSED_ARG(n);
    00173             alloc.deallocate(p);
    00174         }
    00175
    00176         void destruct_and_dealloc(OsclAny* p)
    00177         {
    00178             destroy(OSCL_STATIC_CAST(pointer, p));
    00179             deallocate(p);
    00180         }
    00181
    00182         pointer address(reference r)
    00183         {
    00184             return &r;
    00185         }
    00186         const_pointer address(const_reference r) const
    00187         {
    00188             return &r;
    00189         }
    00190
    00191         void construct(pointer p, const_reference val)
    00192         {
    00193             new(p) T(val);
    00194         }
    00195         void destroy(pointer p)
    00196         {
    00197             OSCL_UNUSED_ARG(p);
    00198             p->~T();
    00199         }
    00200
    00201         template <class U, class V>
    00202         struct rebind
    00203         {
    00204             typedef Oscl_TAlloc<U, V> other;
    00205         };
    00206
    00207     private:
    00208         Alloc alloc;
    00209 };
    00210
    00211
    00214 #endif