Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/antares0982/memorypool
memory pool for multi-threading
https://github.com/antares0982/memorypool
Last synced: 8 days ago
JSON representation
memory pool for multi-threading
- Host: GitHub
- URL: https://github.com/antares0982/memorypool
- Owner: Antares0982
- License: mit
- Created: 2023-03-15T06:27:23.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-30T12:05:13.000Z (about 1 year ago)
- Last Synced: 2023-09-30T13:24:11.814Z (about 1 year ago)
- Language: C++
- Size: 72.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MemoryPool
A simple memory pool using `std::pmr::monotonic_buffer_resource` to allocate memory, providing safe asynchronous destruction. Supports at least C++17 only.## Why use it
Generally, directly calling `operator new` or `malloc()` to allocate a block of memory on the heap takes much time. If memory needs to be frequently allocated, the use of `operator new` or `malloc()` can greatly affect performance. Memory resources reduce the time of allocation by allocating a big block of memory at one time and then using an algorithm to provide the required size of memory to the program. However, if multiple threads request or release memory in parallel from a lock-free memory resource, it may cause memory conflicts; if a mutex is used, it will greatly affect the speed of memory allocation. We can solve the problem of allocating in parallel by using memory resources in thread local storage, which means the memory will only be allocated using a memory resource in current thread. However, the deallocation of memory may happen in a completely different thread. Conflicts may still happens when multiple threads request deallocations.
The purpose of this memory pool is to avoid the conflict when memory is allocated in one thread but deallocated in another. If you don't worry about this problem, this memory pool maybe not for you; you can use thread local `std::pmr::unsynchronized_pool_resource` or `std::pmr::synchronized_pool_resource` directly. But if you have the problem, then this pool maybe the simplest and easiest pool to use.
## How to use it
You can create an object using `New`:
```c++
using MemoryPool = Antares::MemoryPool;
MemoryPool pool;
T* object = pool.New(arg1, arg2); // T* object = new T(arg1, arg2);
```Create array:
```c++
T* arr = pool.NewArray(size);
// T* arr = new T[size];
T* arr2 = pool.NewArray(size, arr[0]); // without calling the default constructor
// T* arr2 = new T[size]; for(size_t i = 0;i