Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.35 KB | None | 0 0
  1. // MemoryArea.h
  2.  
  3. #pragma once
  4. #include "Preprocessor.h"
  5.  
  6. namespace comet
  7. {
  8.     class HeapArea
  9.     {
  10.     public:
  11.         CM_EXPLICIT HeapArea(size_t size);
  12.         ~HeapArea(void);
  13.  
  14.         CM_INLINE char* GetStart(void) { return m_start; }
  15.         CM_INLINE char* GetEnd(void) { return m_end; }
  16.         CM_INLINE size_t GetMemory(void) { return m_memory; }
  17.  
  18.     private:
  19.         char* m_start;
  20.         char* m_end;
  21.         size_t m_memory;
  22.     };
  23.  
  24.     class StackArea
  25.     {
  26.     public:
  27.         CM_EXPLICIT StackArea(size_t size);
  28.         CM_INLINE ~StackArea(void) {}
  29.  
  30.         CM_INLINE char* GetStart(void) { return m_start; }
  31.         CM_INLINE char* GetEnd(void) { return m_end; }
  32.         CM_INLINE size_t GetMemory(void) { return m_memory; }
  33.  
  34.     private:
  35.         char* m_start;
  36.         char* m_end;
  37.         size_t m_memory;
  38.     };
  39.  
  40. }
  41.  
  42. // MemoryArea.cpp
  43.  
  44. #include <malloc.h>
  45. #include "MemoryArea.h"
  46.  
  47. using namespace comet;
  48.  
  49. // ------ Heap Area ------ //
  50.  
  51. comet::HeapArea::HeapArea(size_t size)
  52. {
  53.     m_start = (char*)malloc(size * sizeof(char));
  54.     m_end = m_start + size;
  55.     m_memory = size;
  56. }
  57.  
  58. comet::HeapArea::~HeapArea(void)
  59. {
  60.     free(m_start);
  61. }
  62.  
  63. // ------ Stack Area ------ //
  64.  
  65. #if defined(WIN32) || defined(_MSC_VER)
  66. #define alloca _alloca
  67.  
  68. #endif
  69.  
  70. comet::StackArea::StackArea(size_t size)
  71. {
  72.     m_start = (char*)alloca(size * sizeof(char));
  73.     m_end = m_start + size;
  74.     m_memory = size;
  75. }
  76.  
  77. // BasicAllocators.h
  78.  
  79. // Version: v1.00
  80. // Created: 28/05/2017
  81. #pragma once
  82. #include <Platform/Types.h>
  83. #include "Preprocessor.h"
  84.  
  85. namespace comet
  86. {
  87.     class LinearAllocator
  88.     {
  89.     public:
  90.         CM_EXPLICIT LinearAllocator(size_t size);
  91.         LinearAllocator(char* start, char* end);
  92.  
  93.         void* Allocate(size_t size, size_t alignement, size_t offset);
  94.         void Reset(void);
  95.      
  96.         CM_INLINE void Free(void* ptr)
  97.         {
  98.             CM_UNUSED(ptr);
  99.             Reset();
  100.         }
  101.  
  102.         size_t GetAllocationSize(void* allocation) const;
  103.  
  104.     private:
  105.         char* m_start;
  106.         char* m_end;
  107.         char* m_current;
  108.     };
  109.  
  110.     class StackAllocator
  111.     {
  112.     public:
  113.         StackAllocator(char* start, char* end);
  114.        
  115.         void* Allocate(size_t size, size_t alignment, size_t offset);
  116.         void Free(void *ptr);
  117.    
  118.     private:
  119.         char* m_start;
  120.         char* m_end;
  121.         char* m_current;
  122.     };
  123.  
  124.     class AllocationFreeList
  125.     {
  126.     public:
  127.         AllocationFreeList(void* start, void* end, size_t elementSize, size_t alignement, size_t offset);
  128.  
  129.         CM_INLINE void* Obtain(void);
  130.         CM_INLINE void Return(void* ptr);
  131.  
  132.     private:
  133.         AllocationFreeList* m_next;
  134.     };
  135.  
  136. }
  137. // BasicAllocators.cpp
  138.  
  139. #include <Platform/Types.h>
  140. #include "BasicAllocators.h"
  141. #include "PointerUtil.h"
  142. #include "SafeCast.h"
  143.  
  144. using namespace comet;
  145.  
  146. // ------- Linear Allocator ------- //
  147.  
  148. // comet::LinearAllocator::LinearAllocator(size_t size) {}
  149. comet::LinearAllocator::LinearAllocator(char* start, char* end)
  150.     :
  151.         m_start(start),
  152.         m_current(start),
  153.         m_end(end)
  154. {}
  155.  
  156. void* comet::LinearAllocator::Allocate(size_t size, size_t alignement, size_t offset)
  157. {
  158.     m_current = ptr_util::AlignTop(m_current + offset, alignement) - offset;
  159.  
  160.     void* userPtr = m_current;
  161.     m_current += size;
  162.  
  163.     if (m_current >= m_end)
  164.     {
  165.         // Ran out of memory
  166.         // Assert?
  167.         return nullptr;
  168.     }
  169.  
  170.     return userPtr;
  171. }
  172.  
  173. void comet::LinearAllocator::Reset(void)
  174. {
  175.     m_current = m_start;
  176. }
  177.  
  178.  
  179. size_t comet::LinearAllocator::GetAllocationSize(void * allocation) const
  180. {
  181.     union
  182.     {
  183.         void* as_void;
  184.         char* as_char;
  185.         size_t* as_size_t;
  186.     };
  187.  
  188.     as_void = allocation;
  189.     as_char -= sizeof(size_t);
  190.     return (*as_size_t);
  191. }
  192.  
  193. // ----- Stack allocator ----- //
  194.  
  195. namespace
  196. {
  197.     CM_CONSTEXPR_VAL size_t SIZE_OF_ALLOCATION_OFFSET = sizeof(U32);
  198.     static_assert(SIZE_OF_ALLOCATION_OFFSET == 4, "Allocation offset has wrong size.");
  199. }
  200.  
  201. comet::StackAllocator::StackAllocator::StackAllocator(char* start, char* end)
  202.     :
  203.         m_start(start),
  204.         m_current(start),
  205.         m_end(end)
  206. {}
  207.  
  208. void* StackAllocator::Allocate(size_t size, size_t alignment, size_t offset)
  209. {
  210.     size += SIZE_OF_ALLOCATION_OFFSET;
  211.     offset += SIZE_OF_ALLOCATION_OFFSET;
  212.    
  213.     const U32 allocationOffset = safe_static_cast<U32>(m_current - m_start);
  214.     m_current = comet::ptr_util::AlignTop(m_current + offset, alignment) - offset;
  215.  
  216.     if (m_current + size > m_end)
  217.     {
  218.         // out of memory
  219.         return nullptr;
  220.     }
  221.  
  222.     union
  223.     {
  224.         void* as_void;
  225.         char* as_char;
  226.         U32* as_U32;
  227.     };
  228.  
  229.     as_char = m_current;
  230.  
  231.     // store allocation offset in the first 4 bytes
  232.     *as_U32 = allocationOffset;
  233.     as_char += SIZE_OF_ALLOCATION_OFFSET;
  234.  
  235.     void* userPtr = as_void;
  236.     m_current += size;
  237.     return userPtr;
  238. }
  239.  
  240. void comet::StackAllocator::Free(void* ptr)
  241. {
  242.     union
  243.     {
  244.         void* as_void;
  245.         char* as_char;
  246.         U32* as_U32;
  247.     };
  248.  
  249.     as_void = ptr;
  250.  
  251.     as_char -= SIZE_OF_ALLOCATION_OFFSET;
  252.     const U32 allocationOffset = *as_U32;
  253.  
  254.     m_current = m_start + allocationOffset;
  255. }
  256.  
  257. // ----- Pool allocator ----- //
  258. comet::AllocationFreeList::AllocationFreeList(void* start, void* end, size_t elementSize, size_t alignement, size_t offset)
  259. {
  260.     // TODO(mattmatt): implement
  261. }
  262.  
  263. CM_INLINE void * comet::AllocationFreeList::Obtain(void)
  264. {
  265.     if (m_next == nullptr)
  266.     {
  267.         return nullptr;
  268.     }
  269.  
  270.     AllocationFreeList* head = m_next;
  271.     m_next = head->m_next;
  272.     return head;
  273. }
  274.  
  275. CM_INLINE void comet::AllocationFreeList::Return(void * ptr)
  276. {
  277.     AllocationFreeList* head = static_cast<AllocationFreeList*>(ptr);
  278.     head->m_next = m_next;
  279.     m_next = head;
  280. }
  281.  
  282. // Main.cpp
  283.  
  284. #include <Core/Allocator.h>
  285. #include <stdio.h>
  286. using namespace comet;
  287.  
  288. int main(int argc, char** argv)
  289. {
  290.     HeapArea heapArea(256);
  291.     MemoryArena<LinearAllocator,
  292.         policies::SingleTheadPolicy,
  293.         policies::NoBoundsChecking,
  294.         policies::NoMemoryTracking,
  295.         policies::NoMemoryTagging> arena(heapArea);
  296.        
  297.     int* data = CM_NEW(int, arena);
  298.  
  299.     *data = 5;
  300.     printf("%d\n", *data);
  301.  
  302.     CM_DELETE(data, arena);
  303.  
  304.  
  305.     return 0;
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement