From 0ee1aa1aa8b4fb564040c2cbc230114bd63b6143 Mon Sep 17 00:00:00 2001 From: LessComplexity Date: Mon, 20 Dec 2021 11:49:13 +0200 Subject: [PATCH] Merge MemoryPoolData header into MemoryPool header, speration is not needed --- .gitignore | 3 ++- MemoryPool.h | 40 +++++++++++++++++++++++++++- MemoryPoolData.h | 63 --------------------------------------------- README.md | 2 +- test/CMakeLists.txt | 2 +- 5 files changed, 43 insertions(+), 67 deletions(-) delete mode 100644 MemoryPoolData.h diff --git a/.gitignore b/.gitignore index 95cd3a0..c1714ae 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /.vs* */CMakeSettings.json CMakeSettings.json -/build \ No newline at end of file +/build +.idea/ \ No newline at end of file diff --git a/MemoryPool.h b/MemoryPool.h index 98c5e99..778b17f 100644 --- a/MemoryPool.h +++ b/MemoryPool.h @@ -18,13 +18,51 @@ * @author Sapir Shemer */ #pragma once +#define MEMORYPOOL_DEFAULT_BLOCK_SIZE 1024 * 1024 -#include "MemoryPoolData.h" +#include #include #include #include namespace CPPShift::Memory { + // Simple error collection for memory pool + enum class EMemoryErrors { + CANNOT_CREATE_MEMORY_POOL, + CANNOT_CREATE_BLOCK, + OUT_OF_POOL, + EXCEEDS_MAX_SIZE, + CANNOT_CREATE_BLOCK_CHAIN + }; + + // Header for a single memory block + struct SMemoryBlockHeader { + // Block data + size_t blockSize; + size_t offset; + + // Movement to other blocks + SMemoryBlockHeader* next; + SMemoryBlockHeader* prev; + + // Garbage management data + size_t numberOfAllocated; + size_t numberOfDeleted; + }; + + // Header of a memory unit in the pool holding important metadata + struct SMemoryUnitHeader { + size_t length; + SMemoryBlockHeader* container; + }; + + // Header for a scope in memory + struct SMemoryScopeHeader { + size_t scopeOffset; + SMemoryBlockHeader* firstScopeBlock; + SMemoryScopeHeader* prevScope; + }; + class MemoryPool { public: /** diff --git a/MemoryPoolData.h b/MemoryPoolData.h deleted file mode 100644 index 5cc1360..0000000 --- a/MemoryPoolData.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * CPPShift Memory Pool v2.0.0 - * - * Copyright 2020-present Sapir Shemer, DevShift (devshift.biz) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * @author Sapir Shemer - */ - -#pragma once - -#include -#define MEMORYPOOL_DEFAULT_BLOCK_SIZE 1024 * 1024 - -namespace CPPShift::Memory { - // Simple error collection for memory pool - enum class EMemoryErrors { - CANNOT_CREATE_MEMORY_POOL, - CANNOT_CREATE_BLOCK, - OUT_OF_POOL, - EXCEEDS_MAX_SIZE, - CANNOT_CREATE_BLOCK_CHAIN - }; - - // Header for a single memory block - struct SMemoryBlockHeader { - // Block data - size_t blockSize; - size_t offset; - - // Movement to other blocks - SMemoryBlockHeader* next; - SMemoryBlockHeader* prev; - - // Garbage management data - size_t numberOfAllocated; - size_t numberOfDeleted; - }; - - // Header of a memory unit in the pool holding important metadata - struct SMemoryUnitHeader { - size_t length; - SMemoryBlockHeader* container; - }; - - // Header for a scope in memory - struct SMemoryScopeHeader { - size_t scopeOffset; - SMemoryBlockHeader* firstScopeBlock; - SMemoryScopeHeader* prevScope; - }; -} \ No newline at end of file diff --git a/README.md b/README.md index c02fb13..efddb60 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ I hope this simple feature will help you increase your software's performance - # Usage -To use the memory pool features you just need to copy the [MemoryPool.cpp](MemoryPool.cpp), [MemoryPool.h](MemoryPool.h) & [MemoryPoolData.h](MemoryPoolData.h) files to your project. The memory pool structure is `CPPShift::Memory::MemoryPool`. ***The Memory Pool Is Not Thread Safe - In case of threads it is better to create a memory pool for each thread*** +To use the memory pool features you just need to copy the [MemoryPool.cpp](MemoryPool.cpp) & [MemoryPool.h](MemoryPool.h) files to your project. The memory pool structure is `CPPShift::Memory::MemoryPool`. ***The Memory Pool Is Not Thread Safe - In case of threads it is better to create a memory pool for each thread*** * _Create a memory pool_: `CPPShift::Memory::MemoryPool * mp = new CPPShift::Memory::MemoryPool(size);` Create a new memory pool structure and a first memory block. If you don't specify a size then by default it will be the `MEMORYPOOL_DEFAULT_BLOCK_SIZE` macro. * _Allocate space_: `Type* allocated = new (mp) Type[size];` or `Type* allocated = (Type*) mp->allocate(size * sizeof(Type));` or `Type* allocated = mp->allocate(size);` Where `Type` is the object\primitive type to create, `mp` is the memory pool object address, and `size` is a represention of the amount of types to allocate. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 31fa241..3c7812e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,4 +6,4 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_BUILD_TYPE Release) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") -add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp") +add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp") \ No newline at end of file