-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*experimental*
- Loading branch information
Showing
15 changed files
with
645 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Bert Melis | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Memory Pool | ||
|
||
EARLY VERSION. USE AT OWN RISK. | ||
|
||
### Description | ||
|
||
This is a simple memory pool that doesn't solve the fragmentation problem but contains it. Inside the pool you will still suffer memory fragmentation. The upside is that you're not restricted on memory size. As long as it fits in the pool, you can request any size! | ||
|
||
For applications where the (maximum) size to allocate is known, a simple fixed block size memory pool is available. There is no memory fragmentation happening in this case. The downside is wastage of memory if you need less then the specified blocksize. | ||
|
||
#### Features | ||
|
||
- pool memory is statically allocated | ||
- pool size adjusts on architecture | ||
- no size calculation required: input number of blocks and size of block | ||
- header-only library | ||
- Variable size pool: no restriction on allocated size | ||
- Variable size pool: malloc and free are O(n); The number of allocated blocks affects lookup. | ||
- Fixed size pool: malloc and free are O(1). | ||
|
||
[![Test with Platformio](https://github.com/bertmelis/MemoryPool/actions/workflows/test-platformio.yml/badge.svg)](https://github.com/bertmelis/MemoryPool/actions/workflows/test-platformio.yml) | ||
[![cpplint](https://github.com/bertmelis/MemoryPool/actions/workflows/cpplint.yml/badge.svg)](https://github.com/bertmelis/MemoryPool/actions/workflows/cpplint.yml) | ||
<!---[![cppcheck](https://github.com/bertmelis/MemoryPool/actions/workflows/cppcheck.yml/badge.svg)](https://github.com/bertmelis/MemoryPool/actions/workflows/cppcheck.yml)---> | ||
|
||
### Usage | ||
|
||
#### Variable size pool | ||
|
||
```cpp | ||
#include <MemoryPool.h> | ||
|
||
Struct MyStruct { | ||
unsigned int id; | ||
std::size_t size; | ||
unsigned char data[256]; | ||
}; | ||
|
||
// pool will be able to hold 10 blocks the size of MyStruct | ||
MemoryPool::Variable<10, sizeof(MyStruct)> pool; | ||
|
||
// you can allocate the specified blocksize | ||
// allocation is done in number of 'unsigned char' | ||
MyStruct* s = reinterpret_cast<MyStruct*>(pool.malloc(sizeof(MyStruct))); | ||
|
||
// you can allocate less than the specified blocksize | ||
int* i = reinterpret_cast<int*>(pool.malloc(sizeof(int))); | ||
|
||
// you can allocate more than the specified blocksize | ||
unsigned char* m = reinterpret_cast<unsigned char*>(pool.malloc(400)); | ||
|
||
pool.free(s); | ||
pool.free(i); | ||
pool.free(m); | ||
``` | ||
|
||
#### Fixed size pool | ||
|
||
```cpp | ||
#include <MemoryPool.h> | ||
|
||
Struct MyStruct { | ||
unsigned int id; | ||
std::size_t size; | ||
unsigned char data[256]; | ||
}; | ||
|
||
// pool will be able to hold 10 blocks the size of MyStruct | ||
MemoryPool::Fixed<10, sizeof(MyStruct)> pool; | ||
|
||
// there is no size argument in the malloc function! | ||
MyStruct* s = reinterpret_cast<MyStruct*>(pool.malloc()); | ||
|
||
// you can allocate less than the specified blocksize | ||
int* i = reinterpret_cast<int*>(pool.malloc()); | ||
|
||
pool.free(s); | ||
pool.free(i); | ||
``` | ||
|
||
#### How it works | ||
|
||
##### Variable size pool | ||
|
||
Free blocks are organized as a linked list with their header (contains pointer to next and size). An allocated block also has this header with it's pointer set to `nullptr`. Therefore, each allocation wastes memory the size of the header (`sizeof(void*) + sizeof(std::size_t)`). On creation, the pool calculations the needed space to store the number of blocks wich each their header. | ||
|
||
However, memory allocation isn't restricted the the specified blocksize. So in reality, you can allocate more if you allocate larger chunks because less memory blocks means less headers. After all, memory needs to be contiguous. | ||
|
||
If you inspect the pool you'll see that a free pool only has one big block. | ||
|
||
Allocation is linear: the pool is iterated until a suitable spot is found. | ||
Freeing is also linear as the pool is traversed to insert the chunk in the linked list of free blocks | ||
|
||
When freeing, free blocks which are adjacent are combined into one. | ||
|
||
##### Fixed size pool | ||
|
||
The fixed size pool is implemented as an array. Free blocks are saved as a linked list in this array. | ||
|
||
### Bugs and feature requests | ||
|
||
Please use Github's facilities to get in touch. | ||
|
||
### License | ||
|
||
This library is released under the MIT Licence. A copy is included in the repo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Datatypes (KEYWORD1) | ||
Fixed KEYWORD1 | ||
Variable KEYWORD1 | ||
|
||
# Methods and Functions (KEYWORD2) | ||
malloc KEYWORD2 | ||
free KEYWORD2 | ||
freeMemory KEYWORD2 | ||
maxBlockSize KEYWORD2 | ||
print KEYWORD2 | ||
|
||
# Structures (KEYWORD3) | ||
# structure KEYWORD3 | ||
|
||
# Constants (LITERAL1) | ||
MemoryPool LITERAL1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "MemoryPool", | ||
"keywords": "memory", | ||
"description": "A simple memory pool for fixed and variable sizes", | ||
"authors": | ||
{ | ||
"name": "Bert Melis", | ||
"url": "https://github.com/bertmelis" | ||
}, | ||
"license": "MIT", | ||
"homepage": "https://github.com/bertmelis/MemoryPool", | ||
"repository": | ||
{ | ||
"type": "git", | ||
"url": "https://github.com/bertmelis/MemoryPool.git" | ||
}, | ||
"version": "0.1.0", | ||
"frameworks": "*", | ||
"platforms": "*", | ||
"headers": ["MemoryPool.h"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=MemoryPool | ||
version=0.1.0 | ||
author=Bert Melis | ||
maintainer=Bert Melis | ||
sentence=A simple memory pool for fixed and variable sizes | ||
paragraph= | ||
category=Other | ||
url=https://github.com/bertmelis/MemoryPool | ||
architectures=* | ||
includes=MemoryPool.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright (c) 2024 Bert Melis. All rights reserved. | ||
This work is licensed under the terms of the MIT license. | ||
For a copy, see <https://opensource.org/licenses/MIT> or | ||
the LICENSE file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstddef> // std::size_t | ||
#include <cassert> // assert | ||
#if _GLIBCXX_HAS_GTHREADS | ||
#include <mutex> // NOLINT [build/c++11] std::mutex, std::lock_guard | ||
#else | ||
#warning "The memory pool is not thread safe" | ||
#endif | ||
|
||
#ifdef MEMPOL_DEBUG | ||
#include <iostream> | ||
#endif | ||
|
||
namespace MemoryPool { | ||
|
||
template <std::size_t nrBlocks, std::size_t blocksize> | ||
class Fixed { | ||
public: | ||
Fixed() // cppcheck-suppress uninitMemberVar | ||
: _buffer{0} | ||
, _head(_buffer) { | ||
unsigned char* b = _head; | ||
std::size_t adjustedBlocksize = sizeof(std::size_t) > blocksize ? sizeof(std::size_t) : blocksize; | ||
for (std::size_t i = 0; i < nrBlocks - 1; ++i) { | ||
*reinterpret_cast<unsigned char**>(b) = b + adjustedBlocksize; | ||
b += adjustedBlocksize; | ||
} | ||
*reinterpret_cast<unsigned char**>(b) = nullptr; | ||
} | ||
|
||
// no copy nor move | ||
Fixed (const Fixed&) = delete; | ||
Fixed& operator= (const Fixed&) = delete; | ||
|
||
void* malloc() { | ||
#if _GLIBCXX_HAS_GTHREADS | ||
const std::lock_guard<std::mutex> lockGuard(_mutex); | ||
#endif | ||
if (_head) { | ||
void* retVal = _head; | ||
_head = *reinterpret_cast<unsigned char**>(_head); | ||
return retVal; | ||
} | ||
return nullptr; | ||
} | ||
|
||
void free(void* ptr) { | ||
if (!ptr) return; | ||
#if _GLIBCXX_HAS_GTHREADS | ||
const std::lock_guard<std::mutex> lockGuard(_mutex); | ||
#endif | ||
*reinterpret_cast<unsigned char**>(ptr) = _head; | ||
_head = reinterpret_cast<unsigned char*>(ptr); | ||
} | ||
|
||
std::size_t freeMemory() { | ||
#if _GLIBCXX_HAS_GTHREADS | ||
const std::lock_guard<std::mutex> lockGuard(_mutex); | ||
#endif | ||
unsigned char* i = _head; | ||
std::size_t retVal = 0; | ||
while (i) { | ||
retVal += blocksize; | ||
i = reinterpret_cast<unsigned char**>(i)[0]; | ||
} | ||
return retVal; | ||
} | ||
|
||
#ifdef MEMPOL_DEBUG | ||
void print() { | ||
std::size_t adjustedBlocksize = sizeof(std::size_t) > blocksize ? sizeof(std::size_t) : blocksize; | ||
std::cout << "+--------------------" << std::endl; | ||
std::cout << "|start:" << reinterpret_cast<void*>(_buffer) << std::endl; | ||
std::cout << "|blocks:" << nrBlocks << std::endl; | ||
std::cout << "|blocksize:" << adjustedBlocksize << std::endl; | ||
std::cout << "|head: " << reinterpret_cast<void*>(_head) << std::endl; | ||
unsigned char* currentBlock = _buffer; | ||
|
||
for (std::size_t i = 0; i < nrBlocks; ++i) { | ||
std::cout << "|" << i + 1 << ": " << reinterpret_cast<void*>(currentBlock) << std::endl; | ||
if (_isFree(currentBlock)) { | ||
std::cout << "| free" << std::endl; | ||
std::cout << "| next: " << reinterpret_cast<void*>(*reinterpret_cast<unsigned char**>(currentBlock)) << std::endl; | ||
} else { | ||
std::cout << "| allocated" << std::endl; | ||
} | ||
currentBlock += adjustedBlocksize; | ||
} | ||
std::cout << "+--------------------" << std::endl; | ||
} | ||
|
||
bool _isFree(const unsigned char* ptr) { | ||
unsigned char* b = _head; | ||
while (b) { | ||
if (b == ptr) return true; | ||
b = *reinterpret_cast<unsigned char**>(b); | ||
} | ||
return false; | ||
} | ||
#endif | ||
|
||
private: | ||
unsigned char _buffer[nrBlocks * (sizeof(std::size_t) > blocksize ? sizeof(std::size_t) : blocksize)]; | ||
unsigned char* _head; | ||
#if _GLIBCXX_HAS_GTHREADS | ||
std::mutex _mutex; | ||
#endif | ||
}; | ||
|
||
} // end namespace MemoryPool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
Copyright (c) 2024 Bert Melis. All rights reserved. | ||
This work is licensed under the terms of the MIT license. | ||
For a copy, see <https://opensource.org/licenses/MIT> or | ||
the LICENSE file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Variable.h" | ||
#include "Fixed.h" |
Oops, something went wrong.