Code Monkey home page Code Monkey logo

multitype-pool's Introduction

multitype-pool

Pool implementation that is able to support multiple fixed types, and will be more efficient because of this information.

This pool optionally uses a statically allocated data store, so that the custom pointer type can use an automagically-sized index for referring to the object, thus saving memory

examples

Create a single type pool with static memory allocation:

#include <iostream>

#include "pool/policy/static_storage.hpp"
#include "pool/policy/intrusive_linked_stack.hpp"
#include "pool/policy/freelist_data_store.hpp"
#include "pool/pool.hpp"

// the stored type
struct foo {
	char bar[42];
}

constexpr int pool_size = 10;

// create a global pool data variable to store the elements in
// the template variables are the stored type, and the maximum number of allocations of that type
// we are using the freelist node from intrusive_linked_stack
using freelist_t = static_storage_array_data<
	freelist_stack_node< // freelist free block tracking
		foo // the data object
	>::node, // using ::node, as it is a partial applicaton that requires a pointer type 
	pool_size>; // the size of the storage
freelist_t data_storage{};

// create the actual pool object, with the reference to the data
 
inline void oom_exit() {
	std::cerr << "Out of memory!" << std::endl;
	std::exit(1);
}
 
using pool_t = pool<
	freelist_data_store< // freelist data adapter
		atomic_freelist_stack< // the actual stack to use (includes the stack head)
			static_storage< // type adapter for the data storage object
				freelist_t, // the type of the storage, all information about the data gets 
				            // pulled from this type
				data_storage>>>, // a reference to the actual data storage object
	oom_exit>; // the function that gets called when the pool is out of objects
pool_t pool{}; // the actual pool object

int main() {
	// allocate a single element, use auto for convinience (using the custom pool pointer type)
	auto ptr = pool.allocate();
	// the pointer type acts like a normal pointer, so you can print it and do pointer arithmetic 
	std::cout << ptr << ": " << *ptr << std::endl;
	// the pointer is only one byte
	std::cout << sizeof(ptr) << std::endl;
	pool.free(ptr); // free the pointer again
	
	// allocate can also take a custom type if the pool supports it
	// the default type is taken from the data storage type
	ptr = pool.allocate<foo>();
	pool.free(ptr);
}

multitype-pool's People

Contributors

chieltbest avatar

Stargazers

Edwin Barczyński avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.