diff --git a/README.md b/README.md index c844919..315dd54 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# embedded-test \ No newline at end of file +# embedded-test + +* lib - библиотека с простым аллокатором статической памяти. +* tests - тесты аллокатора. Тесты реализованы с помощью библиотеки [Catch2](https://github.com/catchorg/Catch2) +* app - пример обертки над аллокатором для использования в приложении с операционной системой \ No newline at end of file diff --git a/lib/include/SimpleStaticAllocator.h b/lib/include/SimpleStaticAllocator.h index c0e9557..108d506 100644 --- a/lib/include/SimpleStaticAllocator.h +++ b/lib/include/SimpleStaticAllocator.h @@ -21,8 +21,15 @@ namespace AllocatorLib { ElementType pool[POOL_ELEMENTS_COUNT]; SimpleStack 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++) { @@ -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(); + } } }; }