diff --git a/.gitignore b/.gitignore index 259148f..59c6b5b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,32 +1,3 @@ -# Prerequisites -*.d +.idea -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app +cmake-build-* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..72cf7b0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.14.0) +project(SimpleStaticAllocator) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(TestApp main.cpp) +target_link_libraries(TestApp SimpleStaticAllocator) + +add_subdirectory(lib) \ No newline at end of file diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..fe2a079 --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,5 @@ +set(target "SimpleStaticAllocator") + +add_library(${target} INTERFACE) + +target_include_directories(${target} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/lib/include/SimpleStack.h b/lib/include/SimpleStack.h new file mode 100644 index 0000000..47315af --- /dev/null +++ b/lib/include/SimpleStack.h @@ -0,0 +1,40 @@ +#ifndef _SIMPLE_STACK_H_ +#define _SIMPLE_STACK_H_ + +#include +#include +#include + +template +class SimpleStack { + static_assert(size > 0, "Stack size must be greater than 0"); +private: + uint32_t count = 0; + std::array stack; + +public: + void init() { + count = 0; + } + + bool isEmpty() { + return count == 0; + } + + bool isFull() { + return count == size; + } + + void push(ElementType element) { + stack[count++] = element; + } + + ElementType pop() { + ElementType res = stack[count - 1]; + count--; + + return res; + } +}; + +#endif diff --git a/lib/include/SimpleStaticAllocator.h b/lib/include/SimpleStaticAllocator.h new file mode 100644 index 0000000..a43642c --- /dev/null +++ b/lib/include/SimpleStaticAllocator.h @@ -0,0 +1,45 @@ +#ifndef _SIMPLE_STATIC_ALLOCATOR_H_ +#define _SIMPLE_STATIC_ALLOCATOR_H_ + +#include + +#include "SimpleStack.h" + +namespace TestTask { + template + class SimpleStaticAllocator { + using ElementTypePointer = ElementType *; + + private: + static constexpr uint32_t POOL_ELEMENTS_COUNT = poolSize / blockSize; + + private: + ElementType pool[POOL_ELEMENTS_COUNT]; + SimpleStack freeStack; + + public: + void init() { + freeStack.init(); + + for (uint32_t i = 0; i < POOL_ELEMENTS_COUNT; i++) { + freeStack.push(&pool[i]); + } + } + + ElementTypePointer allocate() { + if (!freeStack.isEmpty()) { + return freeStack.pop(); + } + + return nullptr; + } + + void free(ElementTypePointer element) { + if (!freeStack.isFull()) { + freeStack.push(element); + } + } + }; +} + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..75fd97a --- /dev/null +++ b/main.cpp @@ -0,0 +1,17 @@ +#include + +#include "lib/include/SimpleStaticAllocator.h" + +int main() +{ + TestTask::SimpleStaticAllocator allocator; + allocator.init(); + + auto e0 = allocator.allocate(); + std::cout << e0 << std::endl; + + auto e1 = allocator.allocate(); + std::cout << e1 << std::endl; + + allocator.free(e0); +}