Skip to content

Commit 0e0df18

Browse files
Merge pull request #14 from DevShiftTeam/development
Merge MemoryPoolData header into MemoryPool header, speration is not needed
2 parents 3e17e78 + 0ee1aa1 commit 0e0df18

5 files changed

Lines changed: 43 additions & 67 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/.vs*
44
*/CMakeSettings.json
55
CMakeSettings.json
6-
/build
6+
/build
7+
.idea/

MemoryPool.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,51 @@
1818
* @author Sapir Shemer
1919
*/
2020
#pragma once
21+
#define MEMORYPOOL_DEFAULT_BLOCK_SIZE 1024 * 1024
2122

22-
#include "MemoryPoolData.h"
23+
#include <stdlib.h>
2324
#include <cstring>
2425
#include <cstddef>
2526
#include <memory>
2627

2728
namespace CPPShift::Memory {
29+
// Simple error collection for memory pool
30+
enum class EMemoryErrors {
31+
CANNOT_CREATE_MEMORY_POOL,
32+
CANNOT_CREATE_BLOCK,
33+
OUT_OF_POOL,
34+
EXCEEDS_MAX_SIZE,
35+
CANNOT_CREATE_BLOCK_CHAIN
36+
};
37+
38+
// Header for a single memory block
39+
struct SMemoryBlockHeader {
40+
// Block data
41+
size_t blockSize;
42+
size_t offset;
43+
44+
// Movement to other blocks
45+
SMemoryBlockHeader* next;
46+
SMemoryBlockHeader* prev;
47+
48+
// Garbage management data
49+
size_t numberOfAllocated;
50+
size_t numberOfDeleted;
51+
};
52+
53+
// Header of a memory unit in the pool holding important metadata
54+
struct SMemoryUnitHeader {
55+
size_t length;
56+
SMemoryBlockHeader* container;
57+
};
58+
59+
// Header for a scope in memory
60+
struct SMemoryScopeHeader {
61+
size_t scopeOffset;
62+
SMemoryBlockHeader* firstScopeBlock;
63+
SMemoryScopeHeader* prevScope;
64+
};
65+
2866
class MemoryPool {
2967
public:
3068
/**

MemoryPoolData.h

Lines changed: 0 additions & 63 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ I hope this simple feature will help you increase your software's performance -
2323

2424

2525
# Usage
26-
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***
26+
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***
2727

2828
* _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.
2929
* _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.

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_BUILD_TYPE Release)
77
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
88

9-
add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp")
9+
add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp")

0 commit comments

Comments
 (0)