-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ added copyright information and licensce file
+ added example + added README.md file * fixed bug in free_memory_list::allocate(n), allows n to be 1 now * improved memory_pool allocation for arrays if multiple elements can fit into one node
- Loading branch information
Showing
20 changed files
with
262 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (C) 2015 Jonathan Müller <jonathanmueller.dev@gmail.com> | ||
|
||
This software is provided 'as-is', without any express or | ||
implied warranty. In no event will the authors be held | ||
liable for any damages arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute | ||
it freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; | ||
you must not claim that you wrote the original software. | ||
If you use this software in a product, an acknowledgment | ||
in the product documentation would be appreciated but | ||
is not required. | ||
|
||
2. Altered source versions must be plainly marked as such, | ||
and must not be misrepresented as being the original software. | ||
|
||
3. This notice may not be removed or altered from any | ||
source distribution. |
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,43 @@ | ||
memory | ||
====== | ||
This library provides various memory allocators for high-performance allocation and deallocation. These allocators are provided in the form of a new allocator concept: RawAllocator. A RawAllocator is an improved version over the classical STL-Allocator. There are various wrapper classes and traits to convert between the two types. Each RawAllocator has the following interface or an appropriate specialization of the raw_allocator_traits: | ||
|
||
// A raw allocator, only supports raw memory allocation. | ||
// Similar to ::operator new/malloc. This avoids the need to be templated and to use one allocator for multiple types. | ||
// The raw_allocator_traits can be specialized to adopt to another interface. | ||
class raw_allocator | ||
{ | ||
public: | ||
// Whether or not the allocator is stateful. | ||
// Non-stateful allocators don't need to be stored and can be default constructed on the fly. | ||
using is_stateful = std::true_type/std::false_type; | ||
// The allocator is required to be moveable | ||
raw_allocator(raw_allocator&&); | ||
raw_allocator& operator=(raw_allocator&&); | ||
// Allocates memory for a node. A node is a single object of given size and alignment. | ||
// Precondition: size <= max_node_size() && alignment <= max_alignment() | ||
// Throws an exception derived from std::bad_alloc in case of failure. | ||
void* allocate_node(std::size_t size, std::size_t alignment); | ||
// Allocates memory for an array of multiple nodes. | ||
// Precondition: count * size <= max_array_size() && alignment <= max_alignment() | ||
// Throws an exception derived from std::bad_alloc in case of failure. | ||
void* allocate_array(std::size_t count, std::size_t size, std::size_t alignment); | ||
// Deallocates memory for a node. Must not throw. | ||
void deallocate_node(void *node, std::size_t size, std::size_t alignment) noexcept; | ||
// Deallocates memory for an array of nodes. Must not throw. | ||
void deallocate_array(void *array, std::size_t count, std::size_t size, std::size_t alignment) noexcept; | ||
// Returns the maximum size of a node, inclusive. Should not throw. | ||
std::size_t max_node_size() const; | ||
// Returns the maximum size for an array (total size, no_elements * object_size), inclusive. Should not throw. | ||
std::size_t max_array_size() const; | ||
// Returns the maximum supported alignment, inclusive. Should not throw. | ||
std::size_t max_alignment() const; | ||
}; |
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
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 |
---|---|---|
@@ -1,9 +1,121 @@ | ||
#include "../pool.hpp" | ||
// Copyright (C) 2015 Jonathan Müller <jonathanmueller.dev@gmail.com> | ||
// This file is subject to the license terms in the LICENSE file | ||
// found in the top-level directory of this distribution. | ||
|
||
using namespace foonathan::memory; | ||
#include <iostream> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
int main() | ||
#include "../allocator_adapter.hpp" // for raw_allocator_allocator | ||
#include "../pool_allocator.hpp" // for memory_pool | ||
#include "../tracking.hpp" // for make_tracked_allocator | ||
|
||
using namespace foonathan; | ||
|
||
// uses a RawAllocator or a class that has specialized the raw_allocator_traits | ||
template <class RawAllocator> | ||
void use_allocator(RawAllocator &alloc) | ||
{ | ||
// raw_allocator_allocator is the bridge between RawAllocators and STL-Allocator | ||
using std_allocator = memory::raw_allocator_allocator<int, RawAllocator>; | ||
|
||
// create a vector that uses the pool | ||
// it is passed via reference to the vector | ||
std::vector<int, std_allocator> a(alloc); | ||
// add some elements | ||
std::clog << "vector creation\n"; | ||
for (auto i = 0u; i != 10u; ++i) | ||
a.push_back(i); | ||
|
||
// remove the third one | ||
std::clog << "vector erase\n"; | ||
a.erase(a.begin() + 2); | ||
|
||
// make a copy of the vector | ||
// they share the same allocator | ||
std::clog << "vector copy\n"; | ||
auto b = a; | ||
// insert an element into the copy | ||
std::clog << "vector insert\n"; | ||
b.push_back(4); | ||
|
||
// move b into a new vector | ||
// the allocator is transferred two to make it fast | ||
std::clog << "vector move\n"; | ||
auto c = std::move(b); | ||
|
||
// swap a and c, this also swaps the allocator | ||
std::clog << "vector swap\n"; | ||
swap(a, c); | ||
|
||
// create a set that uses the allocator | ||
std::clog << "create set\n"; | ||
std::unordered_set<int, std::hash<int>, std::equal_to<int>, std_allocator> | ||
set(5, {}, {}, alloc); // set bucket counter to trigger rehash | ||
|
||
// insert and erase values | ||
std::clog << "insert/erase set\n"; | ||
for (auto i = 0u; i != 10u; ++i) | ||
set.insert(i); | ||
set.erase(2); | ||
set.erase(10); | ||
} | ||
|
||
int main() | ||
{ | ||
memory_pool<> pool(16, 1024); | ||
std::clog << std::unitbuf; | ||
// a memory pool that supports arrays, each node is 32 bytes big, initially 4KB long | ||
memory::memory_pool<memory::array_pool> pool(32, 4096); | ||
{ | ||
// allocate one such node | ||
auto mem = pool.allocate_node(); | ||
std::clog << "Allocated a node from pool " << mem << '\n'; | ||
|
||
// deallocate it | ||
pool.deallocate_node(mem); | ||
|
||
// allocate an array, that is, multiple nodes one after the other | ||
mem = pool.allocate_array(16); | ||
std::clog << "Allocated array from pool " << mem << '\n'; | ||
// deallocate it | ||
pool.deallocate_array(mem, 16); | ||
} | ||
// use the allocator inside STL containers | ||
std::clog << "\npool test\n\n"; | ||
use_allocator(pool); | ||
|
||
// tracker class that logs internal behavior of the allocator | ||
struct tracker | ||
{ | ||
void on_node_allocation(void *mem, std::size_t size, std::size_t) noexcept | ||
{ | ||
std::clog << this << " node allocated: "; | ||
std::clog << mem << " (" << size << ") " << '\n'; | ||
} | ||
|
||
void on_array_allocation(void *mem, std::size_t count, std::size_t size, std::size_t) noexcept | ||
{ | ||
std::clog << this << " array allocated: "; | ||
std::clog << mem << " (" << count << " * " << size << ") " << '\n'; | ||
} | ||
|
||
void on_node_deallocation(void *ptr, std::size_t, std::size_t) noexcept | ||
{ | ||
std::clog << this << " node deallocated: " << ptr << " \n"; | ||
} | ||
|
||
void on_array_deallocation(void *ptr, std::size_t, std::size_t, std::size_t) noexcept | ||
{ | ||
std::clog << this << " array deallocated: " << ptr << " \n"; | ||
} | ||
}; | ||
|
||
// create a tracked memory_pool to see what kind of allocations are made | ||
// necessary to move the pool into the new allocator, since it can't be copied | ||
auto tracked_pool = memory::make_tracked_allocator(tracker{}, std::move(pool)); | ||
// tracked_pool does not provide the pool specific interface anymore, only the general one | ||
|
||
// use it | ||
std::clog << "\ntracked pool test\n\n"; | ||
use_allocator(tracked_pool); | ||
} |
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
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
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
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