2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_OBJECT_POOL_INTERNAL_H
8 #define BABELTRACE_OBJECT_POOL_INTERNAL_H
11 * This is a generic object pool to avoid memory allocation/deallocation
12 * for objects of which the lifespan is typically short, but which are
15 * The object pool, thanks to two user functions, knows how to allocate
16 * a brand new object in memory when the pool is empty and how to
17 * destroy an object when we destroy the pool.
19 * The object pool's user is responsible for:
21 * * Setting whatever references the object needs to keep and reset some
22 * properties _after_ calling bt_object_pool_create_object(). This is
23 * typically done in the bt_*_create() function which calls
24 * bt_object_pool_create_object() (which could call the user-provided
25 * allocation function if the pool is empty) and then sets the
26 * appropriate properties on the possibly recycled object.
28 * * Releasing whatever references the object keeps _before_ calling
29 * bt_object_pool_recycle_object(). This is typically done in a custom
30 * bt_*_recycle() function which does the necessary before calling
31 * bt_object_pool_recycle_object() with an object ready to be reused
36 #include "lib/object.h"
38 /* Protection: this file uses BT_LIB_LOG*() macros directly */
39 #ifndef BT_LIB_LOG_SUPPORTED
40 # error Please include "lib/logging.h" before including this file.
43 typedef void *(*bt_object_pool_new_object_func
)(void *data
);
44 typedef void (*bt_object_pool_destroy_object_func
)(void *obj
, void *data
);
46 struct bt_object_pool
{
48 * Container of recycled objects, owned by this. The array's size
49 * is the pool's capacity.
54 * Pool's size, that is, number of elements in the array above,
55 * starting at index 0, which exist as recycled objects.
61 /* Allocate a new object in memory */
62 bt_object_pool_new_object_func new_object
;
64 /* Free direct and indirect memory occupied by object */
65 bt_object_pool_destroy_object_func destroy_object
;
68 /* User data passed to user functions */
73 * Initializes an object pool which is already allocated.
76 int bt_object_pool_initialize(struct bt_object_pool
*pool
,
77 bt_object_pool_new_object_func new_object_func
,
78 bt_object_pool_destroy_object_func destroy_object_func
,
82 * Finalizes an object pool without deallocating it.
85 void bt_object_pool_finalize(struct bt_object_pool
*pool
);
88 * Creates an object from an object pool. If the pool is empty, this
89 * function calls the "new" user function to allocate a new object
90 * before returning it. Otherwise this function returns a recycled
91 * object, removing it from the pool.
93 * The returned object is owned by the caller.
96 void *bt_object_pool_create_object(struct bt_object_pool
*pool
)
98 struct bt_object
*obj
;
101 BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u",
102 pool
, pool
->size
, pool
->objects
->len
);
104 if (pool
->size
> 0) {
105 /* Pick one from the pool */
107 obj
= pool
->objects
->pdata
[pool
->size
];
108 pool
->objects
->pdata
[pool
->size
] = NULL
;
112 /* Pool is empty: create a brand new object */
113 BT_LOGD("Pool is empty: allocating new object: pool-addr=%p",
115 obj
= pool
->funcs
.new_object(pool
->data
);
118 BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p",
124 * Recycles an object, that is, puts it back into the pool.
126 * The pool becomes the sole owner of the object to recycle.
129 void bt_object_pool_recycle_object(struct bt_object_pool
*pool
, void *obj
)
131 struct bt_object
*bt_obj
= obj
;
135 BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
136 pool
, pool
->size
, pool
->objects
->len
, obj
);
138 if (pool
->size
== pool
->objects
->len
) {
139 /* Backing array is full: make place for recycled object */
140 BT_LOGD("Object pool is full: increasing object pool capacity: "
141 "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u",
142 pool
, pool
->objects
->len
, pool
->objects
->len
+ 1);
143 g_ptr_array_set_size(pool
->objects
, pool
->size
+ 1);
146 /* Reset reference count to 1 since it could be 0 now */
147 bt_obj
->ref_count
= 1;
149 /* Back to the pool */
150 pool
->objects
->pdata
[pool
->size
] = obj
;
152 BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
153 pool
, pool
->size
, pool
->objects
->len
, obj
);
156 #endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */