...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::interprocess::rbtree_best_fit
// In header: <boost/interprocess/mem_algo/rbtree_best_fit.hpp> template<typename MutexFamily, typename VoidPointer, std::size_t MemAlignment> class rbtree_best_fit { public: // types typedef MutexFamily mutex_family; // Shared mutex family used for the rest of the Interprocess framework. typedef VoidPointer void_pointer; // Pointer type to be used with the rest of the Interprocess framework. typedef unspecified multiallocation_chain; typedef boost::intrusive::pointer_traits< char_ptr >::difference_type difference_type; typedef unspecified size_type; // construct/copy/destruct rbtree_best_fit(size_type, size_type); ~rbtree_best_fit(); // public member functions void * allocate(size_type); void deallocate(void *); size_type get_size() const; size_type get_free_memory() const; void zero_free_memory(); void grow(size_type); void shrink_to_fit(); bool all_memory_deallocated(); bool check_sanity(); template<typename T> T * allocation_command(boost::interprocess::allocation_type, size_type, size_type &, T *&); void * raw_allocation_command(boost::interprocess::allocation_type, size_type, size_type &, void *&, size_type = 1); size_type size(const void *) const; void * allocate_aligned(size_type, size_type); // public static functions static size_type get_min_size(size_type); // public data members static const size_type PayloadPerAllocation; };
This class implements an algorithm that stores the free nodes in a red-black tree to have logarithmic search/insert times.
rbtree_best_fit
public member functionsvoid * allocate(size_type nbytes);Allocates bytes, returns 0 if there is not more memory.
void deallocate(void * addr);Deallocates previously allocated bytes.
size_type get_size() const;Returns the size of the memory segment.
size_type get_free_memory() const;Returns the number of free bytes of the segment.
void zero_free_memory();
Initializes to zero all the memory that's not in use. This function is normally used for security reasons.
void grow(size_type extra_size);
Increases managed memory in extra_size bytes more
void shrink_to_fit();Decreases managed memory as much as possible.
bool all_memory_deallocated();Returns true if all allocated memory has been deallocated.
bool check_sanity();
Makes an internal sanity check and returns true if success
template<typename T> T * allocation_command(boost::interprocess::allocation_type command, size_type limit_size, size_type & prefer_in_recvd_out_size, T *& reuse);
void * raw_allocation_command(boost::interprocess::allocation_type command, size_type limit_object, size_type & prefer_in_recvd_out_size, void *& reuse_ptr, size_type sizeof_object = 1);
size_type size(const void * ptr) const;Returns the size of the buffer previously allocated pointed by ptr.
void * allocate_aligned(size_type nbytes, size_type alignment);
Allocates aligned bytes, returns 0 if there is not more memory. Alignment must be power of 2