-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set(target "SimpleStaticAllocator") | ||
|
||
add_library(${target} INTERFACE) | ||
|
||
target_include_directories(${target} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef _SIMPLE_STACK_H_ | ||
#define _SIMPLE_STACK_H_ | ||
|
||
#include <array> | ||
#include <cstdint> | ||
#include <iostream> | ||
|
||
template<typename ElementType, uint32_t size> | ||
class SimpleStack { | ||
static_assert(size > 0, "Stack size must be greater than 0"); | ||
private: | ||
uint32_t count = 0; | ||
std::array<ElementType, size> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef _SIMPLE_STATIC_ALLOCATOR_H_ | ||
#define _SIMPLE_STATIC_ALLOCATOR_H_ | ||
|
||
#include <cstdint> | ||
|
||
#include "SimpleStack.h" | ||
|
||
namespace TestTask { | ||
template<typename ElementType, uint32_t blockSize, uint32_t poolSize> | ||
class SimpleStaticAllocator { | ||
using ElementTypePointer = ElementType *; | ||
|
||
private: | ||
static constexpr uint32_t POOL_ELEMENTS_COUNT = poolSize / blockSize; | ||
|
||
private: | ||
ElementType pool[POOL_ELEMENTS_COUNT]; | ||
SimpleStack<ElementTypePointer, POOL_ELEMENTS_COUNT> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
|
||
#include "lib/include/SimpleStaticAllocator.h" | ||
|
||
int main() | ||
{ | ||
TestTask::SimpleStaticAllocator<uint32_t, 1, 10> allocator; | ||
allocator.init(); | ||
|
||
auto e0 = allocator.allocate(); | ||
std::cout << e0 << std::endl; | ||
|
||
auto e1 = allocator.allocate(); | ||
std::cout << e1 << std::endl; | ||
|
||
allocator.free(e0); | ||
} |