Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MemoryArea.h
- #pragma once
- #include "Preprocessor.h"
- namespace comet
- {
- class HeapArea
- {
- public:
- CM_EXPLICIT HeapArea(size_t size);
- ~HeapArea(void);
- CM_INLINE char* GetStart(void) { return m_start; }
- CM_INLINE char* GetEnd(void) { return m_end; }
- CM_INLINE size_t GetMemory(void) { return m_memory; }
- private:
- char* m_start;
- char* m_end;
- size_t m_memory;
- };
- class StackArea
- {
- public:
- CM_EXPLICIT StackArea(size_t size);
- CM_INLINE ~StackArea(void) {}
- CM_INLINE char* GetStart(void) { return m_start; }
- CM_INLINE char* GetEnd(void) { return m_end; }
- CM_INLINE size_t GetMemory(void) { return m_memory; }
- private:
- char* m_start;
- char* m_end;
- size_t m_memory;
- };
- }
- // MemoryArea.cpp
- #include <malloc.h>
- #include "MemoryArea.h"
- using namespace comet;
- // ------ Heap Area ------ //
- comet::HeapArea::HeapArea(size_t size)
- {
- m_start = (char*)malloc(size * sizeof(char));
- m_end = m_start + size;
- m_memory = size;
- }
- comet::HeapArea::~HeapArea(void)
- {
- free(m_start);
- }
- // ------ Stack Area ------ //
- #if defined(WIN32) || defined(_MSC_VER)
- #define alloca _alloca
- #endif
- comet::StackArea::StackArea(size_t size)
- {
- m_start = (char*)alloca(size * sizeof(char));
- m_end = m_start + size;
- m_memory = size;
- }
- // BasicAllocators.h
- // Version: v1.00
- // Created: 28/05/2017
- #pragma once
- #include <Platform/Types.h>
- #include "Preprocessor.h"
- namespace comet
- {
- class LinearAllocator
- {
- public:
- CM_EXPLICIT LinearAllocator(size_t size);
- LinearAllocator(char* start, char* end);
- void* Allocate(size_t size, size_t alignement, size_t offset);
- void Reset(void);
- CM_INLINE void Free(void* ptr)
- {
- CM_UNUSED(ptr);
- Reset();
- }
- size_t GetAllocationSize(void* allocation) const;
- private:
- char* m_start;
- char* m_end;
- char* m_current;
- };
- class StackAllocator
- {
- public:
- StackAllocator(char* start, char* end);
- void* Allocate(size_t size, size_t alignment, size_t offset);
- void Free(void *ptr);
- private:
- char* m_start;
- char* m_end;
- char* m_current;
- };
- class AllocationFreeList
- {
- public:
- AllocationFreeList(void* start, void* end, size_t elementSize, size_t alignement, size_t offset);
- CM_INLINE void* Obtain(void);
- CM_INLINE void Return(void* ptr);
- private:
- AllocationFreeList* m_next;
- };
- }
- // BasicAllocators.cpp
- #include <Platform/Types.h>
- #include "BasicAllocators.h"
- #include "PointerUtil.h"
- #include "SafeCast.h"
- using namespace comet;
- // ------- Linear Allocator ------- //
- // comet::LinearAllocator::LinearAllocator(size_t size) {}
- comet::LinearAllocator::LinearAllocator(char* start, char* end)
- :
- m_start(start),
- m_current(start),
- m_end(end)
- {}
- void* comet::LinearAllocator::Allocate(size_t size, size_t alignement, size_t offset)
- {
- m_current = ptr_util::AlignTop(m_current + offset, alignement) - offset;
- void* userPtr = m_current;
- m_current += size;
- if (m_current >= m_end)
- {
- // Ran out of memory
- // Assert?
- return nullptr;
- }
- return userPtr;
- }
- void comet::LinearAllocator::Reset(void)
- {
- m_current = m_start;
- }
- size_t comet::LinearAllocator::GetAllocationSize(void * allocation) const
- {
- union
- {
- void* as_void;
- char* as_char;
- size_t* as_size_t;
- };
- as_void = allocation;
- as_char -= sizeof(size_t);
- return (*as_size_t);
- }
- // ----- Stack allocator ----- //
- namespace
- {
- CM_CONSTEXPR_VAL size_t SIZE_OF_ALLOCATION_OFFSET = sizeof(U32);
- static_assert(SIZE_OF_ALLOCATION_OFFSET == 4, "Allocation offset has wrong size.");
- }
- comet::StackAllocator::StackAllocator::StackAllocator(char* start, char* end)
- :
- m_start(start),
- m_current(start),
- m_end(end)
- {}
- void* StackAllocator::Allocate(size_t size, size_t alignment, size_t offset)
- {
- size += SIZE_OF_ALLOCATION_OFFSET;
- offset += SIZE_OF_ALLOCATION_OFFSET;
- const U32 allocationOffset = safe_static_cast<U32>(m_current - m_start);
- m_current = comet::ptr_util::AlignTop(m_current + offset, alignment) - offset;
- if (m_current + size > m_end)
- {
- // out of memory
- return nullptr;
- }
- union
- {
- void* as_void;
- char* as_char;
- U32* as_U32;
- };
- as_char = m_current;
- // store allocation offset in the first 4 bytes
- *as_U32 = allocationOffset;
- as_char += SIZE_OF_ALLOCATION_OFFSET;
- void* userPtr = as_void;
- m_current += size;
- return userPtr;
- }
- void comet::StackAllocator::Free(void* ptr)
- {
- union
- {
- void* as_void;
- char* as_char;
- U32* as_U32;
- };
- as_void = ptr;
- as_char -= SIZE_OF_ALLOCATION_OFFSET;
- const U32 allocationOffset = *as_U32;
- m_current = m_start + allocationOffset;
- }
- // ----- Pool allocator ----- //
- comet::AllocationFreeList::AllocationFreeList(void* start, void* end, size_t elementSize, size_t alignement, size_t offset)
- {
- // TODO(mattmatt): implement
- }
- CM_INLINE void * comet::AllocationFreeList::Obtain(void)
- {
- if (m_next == nullptr)
- {
- return nullptr;
- }
- AllocationFreeList* head = m_next;
- m_next = head->m_next;
- return head;
- }
- CM_INLINE void comet::AllocationFreeList::Return(void * ptr)
- {
- AllocationFreeList* head = static_cast<AllocationFreeList*>(ptr);
- head->m_next = m_next;
- m_next = head;
- }
- // Main.cpp
- #include <Core/Allocator.h>
- #include <stdio.h>
- using namespace comet;
- int main(int argc, char** argv)
- {
- HeapArea heapArea(256);
- MemoryArena<LinearAllocator,
- policies::SingleTheadPolicy,
- policies::NoBoundsChecking,
- policies::NoMemoryTracking,
- policies::NoMemoryTagging> arena(heapArea);
- int* data = CM_NEW(int, arena);
- *data = 5;
- printf("%d\n", *data);
- CM_DELETE(data, arena);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement