From 374e28f51854d5d2a4b98dbc015f3aad450ebf7d Mon Sep 17 00:00:00 2001 From: Ezhik452 Date: Sat, 30 Jan 2021 20:34:45 +0300 Subject: [PATCH] =?UTF-8?q?+=20=D0=97=D0=B0=D0=B1=D1=8B=D1=82=D1=8B=D0=B5?= =?UTF-8?q?=20=D0=B1=D0=BB=D0=BE=D0=BA=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=B8=20=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++- lib/include/SimpleStaticAllocator.h | 41 ++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) 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(); + } } }; }