Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #pragma once
00015
00040 #include "Types.h"
00041
00042 namespace kNet
00043 {
00044
00045 typedef u32 AllocFlags;
00046 enum
00047 {
00048 AFAllocLow = 0x1,
00049 AFAllocHigh = 0x2
00050 };
00051
00059 template<typename Object, typename Allocator>
00060 inline Object *New(Allocator *allocator, size_t alignment, const char *nameTag = 0, AllocFlags flags = AFAllocLow)
00061 {
00062 void *mem = Allocator::Alloc(allocator, sizeof(Object), alignment, nameTag);
00063 return new(mem) Object;
00064 }
00065
00070 template<typename Object, typename Allocator>
00071 inline Object *NewArray(size_t numObjects, Allocator *allocator, size_t alignment, const char *nameTag = 0,
00072 AllocFlags flags = AFAllocLow)
00073 {
00074 unsigned long extraSize = sizeof(size_t);
00075
00077 void *ptr = Allocator::Alloc(allocator, sizeof(Object)*numObjects + extraSize, alignment, nameTag, flags);
00078 *reinterpret_cast<size_t*>(ptr) = numObjects;
00079
00080 Object *mem = reinterpret_cast<Object*>(reinterpret_cast<size_t*>(ptr)+1);
00081 for(size_t i = 0; i < numObjects; ++i)
00082 new (&mem[i]) Object;
00083
00084 return mem;
00085 }
00086
00091 template<typename Object, typename Allocator>
00092 inline void Delete(Object *ptr, Allocator *allocator)
00093 {
00094 if (!ptr)
00095 return;
00096
00097 ptr->~Object();
00098 Allocator::Free(allocator, ptr);
00099 }
00100
00105 template<typename Object, typename Allocator>
00106 inline void DeleteArray(Object *ptr, Allocator *allocator)
00107 {
00108 if (!ptr)
00109 return;
00110
00111 size_t numObjects = *(reinterpret_cast<size_t*>(ptr)-1);
00112 if (numObjects == 0)
00113 return;
00114
00115
00116 for(size_t i = numObjects; i > 0; --i)
00117 ptr[i-1].~Object();
00118
00119 Allocator::Free(allocator, reinterpret_cast<size_t*>(ptr)-1);
00120 }
00121
00122 }