Skip to content

Commit

Permalink
+ Забытые блокировки и описание.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezhik452 committed Jan 30, 2021
1 parent c7435a4 commit 374e28f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# embedded-test
# embedded-test

* lib - библиотека с простым аллокатором статической памяти.
* tests - тесты аллокатора. Тесты реализованы с помощью библиотеки [Catch2](https://github.com/catchorg/Catch2)
* app - пример обертки над аллокатором для использования в приложении с операционной системой
41 changes: 38 additions & 3 deletions lib/include/SimpleStaticAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ namespace AllocatorLib {
ElementType pool[POOL_ELEMENTS_COUNT];
SimpleStack<ElementTypePointer, POOL_ELEMENTS_COUNT> freeStack;

MutexLockFuntionType lock = nullptr;
MutexReleaseFuntionType release = nullptr;

public:
SimpleStaticAllocator(MutexLockFuntionType lockFunction, MutexReleaseFuntionType releaseFunction) {

lock = lockFunction;
release = releaseFunction;

freeStack.init();

for (uint32_t i = 0; i < POOL_ELEMENTS_COUNT; i++) {
Expand All @@ -35,23 +42,51 @@ namespace AllocatorLib {
}

size_t count() const noexcept {
return POOL_ELEMENTS_COUNT - freeStack.count();
if (lock != nullptr) {
lock();
}

size_t res = POOL_ELEMENTS_COUNT - freeStack.count();

if (release != nullptr) {
release();
}

return res;
}


ElementTypePointer allocate() {
if (lock != nullptr) {
lock();
}

ElementTypePointer res = nullptr;

if (!freeStack.isEmpty()) {
return freeStack.pop();
res = freeStack.pop();
}

if (release != nullptr) {
release();
}

return nullptr;
return res;
}

void free(ElementTypePointer &element) {
if (lock != nullptr) {
lock();
}

if (!freeStack.isFull() && element != nullptr) {
freeStack.push(element);
element = nullptr;
}

if (release != nullptr) {
release();
}
}
};
}
Expand Down

0 comments on commit 374e28f

Please sign in to comment.