Skip to content

Commit

Permalink
+ Файлы проекта
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezhik452 committed Jan 27, 2021
1 parent 052d281 commit 3e0e3b9
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 31 deletions.
33 changes: 2 additions & 31 deletions .gitignore
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-*
9 changes: 9 additions & 0 deletions CMakeLists.txt
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)
5 changes: 5 additions & 0 deletions lib/CMakeLists.txt
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)
40 changes: 40 additions & 0 deletions lib/include/SimpleStack.h
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
45 changes: 45 additions & 0 deletions lib/include/SimpleStaticAllocator.h
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
17 changes: 17 additions & 0 deletions main.cpp
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);
}

0 comments on commit 3e0e3b9

Please sign in to comment.