├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── README.md ├── examples └── integer_pool.cpp ├── mempool.h └── test ├── catch.hpp └── tests.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: cpp 3 | 4 | compiler: 5 | - gcc 6 | - clang 7 | 8 | script: 9 | - mkdir build 10 | - cd build 11 | - cmake -DCMAKE_CXX_COMPILER=$COMPILER .. && make 12 | - ./run_tests 13 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.2) 2 | project(cpp-mempool VERSION 1.0) 3 | 4 | # Do the release build by default 5 | # For debug builds, please use: 'cmake .. -DCMAKE_BUILD_TYPE=Debug' 6 | if (NOT CMAKE_BUILD_TYPE) 7 | set(CMAKE_BUILD_TYPE Release) 8 | endif() 9 | 10 | # Warnings and debug/release modes 11 | set(CMAKE_CXX_FLAGS "-Wall") 12 | set(CMAKE_CXX_FLAGS_DEBUG "-g") 13 | set(CMAKE_CXX_FLAGS_RELEASE "-O3") 14 | 15 | # C++14 standard 16 | set(CMAKE_CXX_STANDARD 14) 17 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 18 | 19 | # Add the current directory to the include list 20 | include_directories("${PROJECT_SOURCE_DIR}") 21 | 22 | add_executable(run_tests test/tests.cpp) 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Hardik Patel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/hardikp/cpp-mempool.svg?branch=master)](https://travis-ci.org/hardikp/cpp-mempool) 2 | 3 | # cpp-mempool 4 | C++ header-only mempool library 5 | 6 | ## Examples 7 | ```C++ 8 | #include 9 | 10 | int main(int argc, char** argv) { 11 | unsigned capacity = 1024; 12 | MemPool pool(capacity); 13 | 14 | for (auto i = 0u; i < capacity * 2; i++) { 15 | int *location = pool.allocate(); 16 | } 17 | } 18 | ``` 19 | 20 | ## Notes 21 | * `MemPool` is not thread-safe. If you're trying to use a single memory pool from multiple threads, it can create race conditions. 22 | -------------------------------------------------------------------------------- /examples/integer_pool.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv) { 4 | unsigned capacity = 1024; 5 | MemPool pool(capacity); 6 | std::vector locations; 7 | 8 | for (auto i = 0u; i < capacity * 2; i++) { 9 | int *location = pool.allocate(); 10 | locations.push_back(location); 11 | } 12 | 13 | for (auto i = 0u; i < capacity * 2; i++) { 14 | pool.deallocate(locations[i]); 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /mempool.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define DEFAULT_MEMPOOL_SIZE 16 6 | 7 | template class MemPool { 8 | private: 9 | unsigned size_; 10 | unsigned capacity_; 11 | 12 | // Contains large memory blocks 13 | // This is where the actual memory sits. 14 | std::vector markers_; 15 | 16 | // Contains the available memory addresses 17 | // When we need a new memory address, we will 18 | // select the address available at the top of this stack. 19 | T **memstack_; 20 | 21 | public: 22 | MemPool(unsigned size = DEFAULT_MEMPOOL_SIZE) 23 | : size_(0), capacity_(std::max(size, (unsigned)DEFAULT_MEMPOOL_SIZE)) { 24 | 25 | T *new_block = (T *)calloc(capacity_, sizeof(T)); 26 | markers_.push_back(new_block); 27 | 28 | memstack_ = (T **)calloc(capacity_, sizeof(T *)); 29 | // Fill the stack with the available memory addresses. 30 | for (unsigned i = 0; i < capacity_; i++) { 31 | memstack_[i] = new_block + i; 32 | } 33 | } 34 | 35 | ~MemPool() { 36 | for (unsigned i = 0; i < markers_.size(); i++) { 37 | free(markers_[i]); 38 | } 39 | 40 | free(memstack_); 41 | } 42 | 43 | T *allocate() { 44 | if (size_ == capacity_) { 45 | // Free the old stack of addresses 46 | free(memstack_); 47 | 48 | // Allocated memory has filled, reallocate memory 49 | memstack_ = (T **)calloc(2 * capacity_, sizeof(T *)); 50 | 51 | T *new_block = (T *)calloc(capacity_, sizeof(T)); 52 | 53 | // Keep track of the large memory blocks for destructor 54 | markers_.push_back(new_block); 55 | 56 | // Record the newly available addresses in the stack 57 | // Note that we don't care about the older addresses 58 | // since they are already allocated and given out. 59 | for (unsigned i = 0; i < capacity_; i++) { 60 | memstack_[capacity_ + i] = new_block + i; 61 | } 62 | 63 | capacity_ *= 2; 64 | } 65 | 66 | T *next = memstack_[size_++]; 67 | bzero(next, sizeof(T)); 68 | return next; 69 | } 70 | 71 | void deallocate(T *mem) { 72 | // mem location is now available 73 | // Add that at the top of the stack 74 | memstack_[--size_] = mem; 75 | } 76 | 77 | int size() { return size_; } 78 | int capacity() { return capacity_; } 79 | }; -------------------------------------------------------------------------------- /test/tests.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CATCH_CONFIG_MAIN 3 | #include "catch.hpp" 4 | 5 | TEST_CASE("test_my_header") { 6 | unsigned capacity = 1024; 7 | MemPool pool(capacity); 8 | std::vector locations; 9 | 10 | for (auto i = 0u; i < capacity * 2; i++) { 11 | int *location = pool.allocate(); 12 | locations.push_back(location); 13 | } 14 | 15 | for (auto i = 0u; i < capacity * 2; i++) { 16 | pool.deallocate(locations[i]); 17 | } 18 | } --------------------------------------------------------------------------------