diff --git a/Worksheets/Worksheet2/Task 2/allocator.tpp b/Worksheets/Worksheet2/Task 2/allocator.tpp new file mode 100644 index 0000000000000000000000000000000000000000..ea42a743522d8850583f3e5a20a5dd4a4594bb31 --- /dev/null +++ b/Worksheets/Worksheet2/Task 2/allocator.tpp @@ -0,0 +1,27 @@ + +#ifndef ALLOCATOR_TPP +#define ALLOCATOR_TPP + +#include "allocator.hpp" + + +template <typename T> +T* bAllocator::alloc(size_t count){ + + // finds required memory space needed + size_t total_size = count * sizeof(T); + + // check there is enough space in the stack + if (reinterpret_cast<char*>(next) + total_size > + reinterpret_cast<char*>(heap_start) + heap_size){ + return nullptr; // pointer representing false + } + + T* result = static_cast<T*>(next); + next = reinterpret_cast<char*>(next) + total_size; + a_count++; + return result; + +} + +#endif \ No newline at end of file