memory pool

group turbo_memory_pool

Functions

template<typename T>
inline T *get_object()

get_object is used to get an object typed |T|.

The object should be cleared before usage. NOTE: T must have a default constructor. Example:

struct MyObject {
    int a;
    int b;
    MyObject() : a(0), b(0) {}
    void clear() {
        a = 0;
        b = 0;
        // other clear operation
        // ...
     }
 };

 MyObject *obj = get_object<MyObject>();
 obj->clear();
The above code will get an object typed MyObject, and the object should be cleared before usage.

Note

MyObject must have a default constructor.

Returns:

T* the object typed T

template<typename T, typename ...Args>
inline T *get_object(const Args&... args)

get_object is used to get an object typed |T| with arguments.

Example:

struct MyObject {
    int a;
    int b;
    MyObject(int a, int b) : a(a), b(b) {}
    void clear() {
        a = 0;
        b = 0;
        // other clear operation
        // ...
     }
 };

 MyObject *obj = get_object<MyObject>(1, 2);
 obj->clear();
The above code will get an object typed MyObject with arguments 1 and 2.

Returns:

T* the object typed T

template<typename T, typename ...Args>
inline T *get_object(Args&&... args)

get_object is used to get an object typed |T| with arguments.

Example:

struct MyObject {
    std::string a;
    int b;
    MyObject(std::string&& a, int b) : a(std::move(a)), b(b) {}

    void clear() {
        a.clear();
        b = 0;
        // other clear operation
    }
};

std::string a = "hello";
MyObject *obj = get_object<MyObject>(std::move(a), 2);
// a is empty now
The above code will get an object typed MyObject with arguments lvalue a and 2.

Note

the arguments will be moved to the object

Returns:

T* the object typed T

template<typename T>
inline int return_object(T *ptr)

return_object is used to return the object |ptr| back.

The object is NOT destructed and will be returned by later get_object<T>. Similar with free/delete, validity of the object is not checked, user shall not return a not-yet-allocated or already-returned object otherwise behavior is undefined.

Parameters:

ptr – the object to be returned

Returns:

int 0 when successful, -1 otherwise.

template<typename T>
inline void clear_objects()

reclaim_objects is used to reclaim all allocated objects typed T if caller is the last thread called this function, otherwise do nothing.

You rarely need to call this function manually because it’s called automatically when each thread quits.

template<typename T>
ObjectPoolInfo describe_objects()

Get description of objects typed T.

This function is possibly slow because it iterates internal structures. Don’t use it frequently like a “getter” function.

template<typename T>
inline T *get_resource(ResourceId<T> *id)

Get an object typed |T| and write its identifier into |id|.

The object should be cleared before usage. NOTE: T must be default-constructible.

Template Parameters:

T – the type of object

Parameters:

id – [output]the identifier of object

Returns:

the object

template<typename T, typename ...Args>
inline T *get_resource(ResourceId<T> *id, const Args&... args)

Get an object typed |T| and write its identifier into |id|.

The object should be cleared before usage. Example:

 struct MyStruct{
    int a;
    int b;
    MyStruct(int a, int b) : a(a), b(b) {}
};
ResourceId<MyStruct> id;
MyStruct *ptr = get_resource(&id, 1, 2);

Note

T must have a constructor with the same number of parameters as Args.

Template Parameters:
  • T – the type of object

  • Args – the type of parameters

Parameters:

id – [output]the identifier of object

Returns:

the object

template<typename T, typename ...Args>
inline T *get_resource(ResourceId<T> *id, Args&&... arg)

Get an object typed |T| and write its identifier into |id|.

The object should be cleared before usage. Example:

struct MyStruct{
   std::string a;
   std::string b;
};
ResourceId<MyStruct> id;
std::string a = "hello";
std::string b = "world";
MyStruct *ptr = get_resource(&id, std::move(a), std::move(b));
The above code will get an object typed MyStruct with arguments a and b.

Note

the constructor of T must have a parameter of type Args&&. after the call, a and b will be empty.

Template Parameters:
  • T – the type of object

  • Args – the type of parameters

Parameters:

id – [output]the identifier of object

Returns:

the object

template<typename T>
inline int return_resource(ResourceId<T> id)

Return the object associated with identifier |id| back.

The object is NOT destructed and will be returned by later get_resource<T>. Similar with free/delete, validity of the id is not checked, user shall not return a not-yet-allocated or already-returned id otherwise behavior is undefined.

Template Parameters:

T – the type of object

Parameters:

id – the identifier of object

Returns:

int 0 when successful, -1 otherwise.

template<typename T>
inline T *address_resource(ResourceId<T> id)

Get the object associated with the identifier |id|.

Returns NULL if |id| was not allocated by get_resource<T> or ResourcePool<T>::get_resource() of a variant before. Addressing a free(returned to pool) identifier does not return NULL. NOTE: Calling this function before any other get_resource<T>/ return_resource<T>/address<T>, even if the identifier is valid, may race with another thread calling clear_resources<T>.

Template Parameters:

T – the type of object

Parameters:

id – the identifier of object

Returns:

the object

template<typename T>
inline void clear_resources()

Reclaim all allocated resources typed T if caller is the last thread called this function, otherwise do nothing.

You rarely need to call this function manually because it’s called automatically when each thread quits.

Template Parameters:

T – the type of object

template<typename T>
ResourcePoolInfo describe_resources()

Get description of resources typed T.

This function is possibly slow because it iterates internal structures. Don’t use it frequently like a “getter” function.

Template Parameters:

T – the type of object

Returns:

ResourcePoolInfo

template<typename T>
struct ObjectPoolTraitsBase
#include <object_pool.h>

ObjectPoolTraitsBase is the base class of ObjectPoolTraits ObjectPoolTraitsBase is used to define the default value of ObjectPoolTraits.

Template Parameters:

T – the type of object

Subclassed by turbo::ObjectPoolTraits< T >

template<typename T>
struct ObjectPoolTraits : public turbo::ObjectPoolTraitsBase<T>
#include <object_pool.h>

ObjectPoolTraits is used to define redefine the default value of ObjectPoolTraitsBase Example:

struct MyObject {
  int a;
  int b;
};

struct ObjectPoolTraits<MyObject> : public ObjectPoolTraitsBase<MyObject> {
    typedef ObjectPoolTraitsBase<MyObject> Base;
    using Base::kBlockMaxSize;
    using Base::kBlockMaxItems;
    KFreeChunkMaxItem = 2048;
    using Base::validate;
};
The above code will redefine the default value of ObjectPoolTraitsBase kFreeChunkMaxItem = 2048

Template Parameters:

T – the type of object

template<typename T>
struct ResourcePoolTraitsBase
#include <resource_pool.h>

ResourcePoolTraitsBase is the base class of ResourcePoolTraits, ResourcePoolTraitsBase is used to define the default value of ResourcePoolTraits Example:

struct MyStruct{
    int a;
    int b;
};

template<>
struct ResourcePoolTraits<MyStruct> : public ResourcePoolTraitsBase<T>{
      static constexpr size_t kBlockMaxSize = 1024 * 64;
      static constexpr size_t kBlockMaxItems = 256;
      static constexpr size_t kFreeChunkMaxItem = 256;
      static bool validate(const T *ptr) {
          return ptr->a == 1;
      }
};
Template Parameters:

T – the type of object

Subclassed by turbo::ResourcePoolTraits< T >

template<typename T>
struct ResourcePoolTraits : public turbo::ResourcePoolTraitsBase<T>
#include <resource_pool.h>

ResourcePoolTraits is used to define redefine the default value of ResourcePoolTraitsBase Example:

struct MyStruct{
    int a;
    int b;
};

template<>
struct ResourcePoolTraits<MyStruct> : public ResourcePoolTraitsBase<T>{
      static constexpr size_t kBlockMaxSize = 1024 * 64;
      static constexpr size_t kBlockMaxItems = 256;
      static constexpr size_t kFreeChunkMaxItem = 256;
      static bool validate(const T *ptr) {
          return ptr->a == 1;
      }
};
Template Parameters:

T – the type of object