17 #ifndef INC_KY_Kernel_Allocator_H
18 #define INC_KY_Kernel_Allocator_H
26 #if defined(KY_CC_MSVC)
28 #pragma warning(disable : 4503)
29 #pragma warning(disable : 4786)
31 #pragma warning(disable : 4345)
38 #ifndef __PLACEMENT_NEW_INLINE
39 #define __PLACEMENT_NEW_INLINE
41 #if defined(KY_CC_MWERKS) || defined(KY_CC_BORLAND)
46 KY_INLINE
void*
operator new (UPInt n,
void *ptr) {
return ptr; }
47 KY_INLINE
void operator delete (
void *ptr,
void *ptr2) { }
52 #endif // KY_CC_MWERKS | KY_CC_BORLAND
54 #endif // __PLACEMENT_NEW_INLINE
64 KY_INLINE T* Construct(
void *p)
70 KY_INLINE T* Construct(
void *p, const T& source)
72 return ::new(p) T(source);
76 template <class T, class S>
77 KY_INLINE T* ConstructAlt(
void *p, const S& source)
79 return ::new(p) T(source);
82 template <class T, class S1, class S2>
83 KY_INLINE T* ConstructAlt(
void *p, const S1& src1, const S2& src2)
85 return ::new(p) T(src1, src2);
89 KY_INLINE
void ConstructArray(
void *p, UPInt count)
91 UByte *pdata = (UByte*)p;
92 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
99 KY_INLINE
void ConstructArray(
void *p, UPInt count,
const T& source)
101 UByte *pdata = (UByte*)p;
102 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
104 Construct<T>(pdata, source);
109 KY_INLINE
void Destruct(T *pobj)
115 KY_INLINE
void DestructArray(T *pobj, UPInt count)
117 for (UPInt i=0; i<count; ++i, ++pobj)
131 template<
int SID = Stat_Default_Mem>
132 class AllocatorBaseLH
135 enum { StatId = SID };
137 #ifdef KY_BUILD_DEBUG
138 static void* Alloc(
const void* pheapAddr, UPInt size,
const char* pfile,
unsigned line)
140 return Memory::AllocAutoHeap(pheapAddr, size, AllocInfo(StatId, pfile, line));
142 static void* Realloc(
const void*,
void* p, UPInt newSize,
const char* pfile,
unsigned line)
144 KY_UNUSED2(pfile, line);
145 return Memory::ReallocAutoHeap(p, newSize);
150 static void* Alloc(
const void* pheapAddr, UPInt size,
const char*,
unsigned)
152 return Memory::AllocAutoHeap(pheapAddr, size, AllocInfo(StatId, 0, 0));
154 static void* Realloc(
const void*,
void* p, UPInt newSize,
const char*,
unsigned)
156 return Memory::ReallocAutoHeap(p, newSize);
161 static void Free(
void *p)
177 template<
int SID = Stat_Default_Mem>
178 class AllocatorBaseDH
181 enum { StatId = SID };
183 #ifdef KY_BUILD_DEBUG
184 static void* Alloc(
const void* pheap, UPInt size,
const char* pfile,
unsigned line)
186 return Memory::AllocInHeap((MemoryHeap*)pheap, size, AllocInfo(StatId, pfile, line));
188 static void* Realloc(
const void* pheap,
void* p, UPInt newSize,
const char* pfile,
unsigned line)
190 KY_UNUSED2(pfile, line);
191 return Memory::ReallocInHeap((MemoryHeap*)pheap, p, newSize);
196 static void* Alloc(
const void* pheap, UPInt size,
const char*,
unsigned)
198 return Memory::AllocInHeap((MemoryHeap*)pheap, size, AllocInfo(StatId, 0, 0));
200 static void* Realloc(
const void* pheap,
void* p, UPInt newSize,
const char*,
unsigned)
202 return Memory::ReallocInHeap((MemoryHeap*)pheap, p, newSize);
207 static void Free(
void *p)
219 template<
int SID = Stat_Default_Mem>
220 class AllocatorBaseGH
223 enum { StatId = SID };
225 #ifdef KY_BUILD_DEBUG
226 static void* Alloc(
const void*, UPInt size,
const char* pfile,
unsigned line)
228 return Memory::Alloc(size, AllocInfo(StatId, pfile, line));
230 static void* Realloc(
const void*,
void *p, UPInt newSize,
const char* pfile,
unsigned line)
232 KY_UNUSED2(pfile, line);
233 return Memory::Realloc(p, newSize);
238 static void* Alloc(
const void*, UPInt size,
const char*,
unsigned)
240 return Memory::Alloc(size, AllocInfo(StatId, 0, 0));
242 static void* Realloc(
const void*,
void *p, UPInt newSize,
const char*,
unsigned)
244 return Memory::Realloc(p, newSize);
249 static void Free(
void *p)
265 static void Construct(
void *) {}
266 static void Construct(
void *p,
const T& source)
273 static void ConstructAlt(
void *p,
const S& source)
278 static void ConstructArray(
void*, UPInt) {}
280 static void ConstructArray(
void* p, UPInt count,
const T& source)
282 UByte *pdata = (UByte*)p;
283 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
287 static void ConstructArray(
void* p, UPInt count,
const T* psource)
289 memcpy(p, psource,
sizeof(T) * count);
292 static void Destruct(T*) {}
293 static void DestructArray(T*, UPInt) {}
295 static void CopyArrayForward(T* dst,
const T* src, UPInt count)
297 memmove(dst, src, count *
sizeof(T));
300 static void CopyArrayBackward(T* dst,
const T* src, UPInt count)
302 memmove(dst, src, count *
sizeof(T));
305 static bool IsMovable() {
return true; }
317 static void Construct(
void* p)
322 static
void Construct(
void* p, const T& source)
329 static
void ConstructAlt(
void* p, const S& source)
334 static
void ConstructArray(
void* p, UPInt count)
336 UByte* pdata = (UByte*)p;
337 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
341 static void ConstructArray(
void* p, UPInt count,
const T& source)
343 UByte* pdata = (UByte*)p;
344 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
345 Construct(pdata, source);
348 static void ConstructArray(
void* p, UPInt count,
const T* psource)
350 UByte* pdata = (UByte*)p;
351 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
352 Construct(pdata, *psource++);
355 static void Destruct(T* p)
361 static void DestructArray(T* p, UPInt count)
364 for (UPInt i=0; i<count; ++i, --p)
368 static void CopyArrayForward(T* dst,
const T* src, UPInt count)
370 memmove(dst, src, count *
sizeof(T));
373 static void CopyArrayBackward(T* dst,
const T* src, UPInt count)
375 memmove(dst, src, count *
sizeof(T));
378 static bool IsMovable() {
return true; }
390 static void Construct(
void* p)
395 static
void Construct(
void* p, const T& source)
402 static
void ConstructAlt(
void* p, const S& source)
407 static
void ConstructArray(
void* p, UPInt count)
409 UByte* pdata = (UByte*)p;
410 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
414 static void ConstructArray(
void* p, UPInt count,
const T& source)
416 UByte* pdata = (UByte*)p;
417 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
418 Construct(pdata, source);
421 static void ConstructArray(
void* p, UPInt count,
const T* psource)
423 UByte* pdata = (UByte*)p;
424 for (UPInt i=0; i< count; ++i, pdata +=
sizeof(T))
425 Construct(pdata, *psource++);
428 static void Destruct(T* p)
434 static void DestructArray(T* p, UPInt count)
437 for (UPInt i=0; i<count; ++i, --p)
441 static void CopyArrayForward(T* dst,
const T* src, UPInt count)
443 for(UPInt i = 0; i < count; ++i)
447 static void CopyArrayBackward(T* dst,
const T* src, UPInt count)
449 for(UPInt i = count; i; --i)
453 static bool IsMovable() {
return false; }
462 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorGH_POD : AllocatorBaseGH<SID>, ConstructorPOD<T> {};
463 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorGH : AllocatorBaseGH<SID>, ConstructorMov<T> {};
464 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorGH_CPP : AllocatorBaseGH<SID>, ConstructorCPP<T> {};
466 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorLH_POD : AllocatorBaseLH<SID>, ConstructorPOD<T> {};
467 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorLH : AllocatorBaseLH<SID>, ConstructorMov<T> {};
468 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorLH_CPP : AllocatorBaseLH<SID>, ConstructorCPP<T> {};
470 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorDH_POD : AllocatorBaseDH<SID>, ConstructorPOD<T> {};
471 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorDH : AllocatorBaseDH<SID>, ConstructorMov<T> {};
472 template<
class T,
int SID = Stat_Default_Mem>
struct AllocatorDH_CPP : AllocatorBaseDH<SID>, ConstructorCPP<T> {};
477 #if defined(KY_DEFINE_NEW)
478 #define new KY_DEFINE_NEW
Definition: gamekitcrowddispersion.h:20