Visibility hidden by default
[babeltrace.git] / src / lib / object-pool.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #ifndef BABELTRACE_OBJECT_POOL_INTERNAL_H
8 #define BABELTRACE_OBJECT_POOL_INTERNAL_H
9
10 /*
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
13 * created a lot.
14 *
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.
18 *
19 * The object pool's user is responsible for:
20 *
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.
27 *
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
32 * at any time.
33 */
34
35 #include <glib.h>
36 #include "lib/object.h"
37
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.
41 #endif
42
43 typedef void *(*bt_object_pool_new_object_func)(void *data);
44 typedef void (*bt_object_pool_destroy_object_func)(void *obj, void *data);
45
46 struct bt_object_pool {
47 /*
48 * Container of recycled objects, owned by this. The array's size
49 * is the pool's capacity.
50 */
51 GPtrArray *objects;
52
53 /*
54 * Pool's size, that is, number of elements in the array above,
55 * starting at index 0, which exist as recycled objects.
56 */
57 size_t size;
58
59 /* User functions */
60 struct {
61 /* Allocate a new object in memory */
62 bt_object_pool_new_object_func new_object;
63
64 /* Free direct and indirect memory occupied by object */
65 bt_object_pool_destroy_object_func destroy_object;
66 } funcs;
67
68 /* User data passed to user functions */
69 void *data;
70 };
71
72 /*
73 * Initializes an object pool which is already allocated.
74 */
75 int bt_object_pool_initialize(struct bt_object_pool *pool,
76 bt_object_pool_new_object_func new_object_func,
77 bt_object_pool_destroy_object_func destroy_object_func,
78 void *data);
79
80 /*
81 * Finalizes an object pool without deallocating it.
82 */
83 void bt_object_pool_finalize(struct bt_object_pool *pool);
84
85 /*
86 * Creates an object from an object pool. If the pool is empty, this
87 * function calls the "new" user function to allocate a new object
88 * before returning it. Otherwise this function returns a recycled
89 * object, removing it from the pool.
90 *
91 * The returned object is owned by the caller.
92 */
93 static inline
94 void *bt_object_pool_create_object(struct bt_object_pool *pool)
95 {
96 struct bt_object *obj;
97
98 BT_ASSERT_DBG(pool);
99 BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u",
100 pool, pool->size, pool->objects->len);
101
102 if (pool->size > 0) {
103 /* Pick one from the pool */
104 pool->size--;
105 obj = pool->objects->pdata[pool->size];
106 pool->objects->pdata[pool->size] = NULL;
107 goto end;
108 }
109
110 /* Pool is empty: create a brand new object */
111 BT_LOGD("Pool is empty: allocating new object: pool-addr=%p",
112 pool);
113 obj = pool->funcs.new_object(pool->data);
114
115 end:
116 BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p",
117 pool, obj);
118 return obj;
119 }
120
121 /*
122 * Recycles an object, that is, puts it back into the pool.
123 *
124 * The pool becomes the sole owner of the object to recycle.
125 */
126 static inline
127 void bt_object_pool_recycle_object(struct bt_object_pool *pool, void *obj)
128 {
129 struct bt_object *bt_obj = obj;
130
131 BT_ASSERT_DBG(pool);
132 BT_ASSERT_DBG(obj);
133 BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
134 pool, pool->size, pool->objects->len, obj);
135
136 if (pool->size == pool->objects->len) {
137 /* Backing array is full: make place for recycled object */
138 BT_LOGD("Object pool is full: increasing object pool capacity: "
139 "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u",
140 pool, pool->objects->len, pool->objects->len + 1);
141 g_ptr_array_set_size(pool->objects, pool->size + 1);
142 }
143
144 /* Reset reference count to 1 since it could be 0 now */
145 bt_obj->ref_count = 1;
146
147 /* Back to the pool */
148 pool->objects->pdata[pool->size] = obj;
149 pool->size++;
150 BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
151 pool, pool->size, pool->objects->len, obj);
152 }
153
154 #endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */
This page took 0.032495 seconds and 4 git commands to generate.