00001
00002
00003
00004
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
00033
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
00041
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
00049
00050
00051
00052
00053
00054
00055
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
00067
00068
00069
00070
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
00092
00093
00094
00095
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
00134
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
00148
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
00159
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