Skip to content

Commit

Permalink
Merge pull request #14 from DevShiftTeam/development
Browse files Browse the repository at this point in the history
Merge MemoryPoolData header into MemoryPool header, speration is not needed
  • Loading branch information
LessComplexity authored Dec 20, 2021
2 parents 3e17e78 + 0ee1aa1 commit 0e0df18
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/.vs*
*/CMakeSettings.json
CMakeSettings.json
/build
/build
.idea/
40 changes: 39 additions & 1 deletion MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,51 @@
* @author Sapir Shemer
*/
#pragma once
#define MEMORYPOOL_DEFAULT_BLOCK_SIZE 1024 * 1024

#include "MemoryPoolData.h"
#include <stdlib.h>
#include <cstring>
#include <cstddef>
#include <memory>

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:
/**
Expand Down
63 changes: 0 additions & 63 deletions MemoryPoolData.h

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>(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.
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 0e0df18

Please sign in to comment.