Move to kernel style SPDX license identifiers
[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 BT_HIDDEN
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,
79 void *data);
80
81 /*
82 * Finalizes an object pool without deallocating it.
83 */
84 BT_HIDDEN
85 void bt_object_pool_finalize(struct bt_object_pool *pool);
86
87 /*
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.
92 *
93 * The returned object is owned by the caller.
94 */
95 static inline
96 void *bt_object_pool_create_object(struct bt_object_pool *pool)
97 {
98 struct bt_object *obj;
99
100 BT_ASSERT_DBG(pool);
101 BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u",
102 pool, pool->size, pool->objects->len);
103
104 if (pool->size > 0) {
105 /* Pick one from the pool */
106 pool->size--;
107 obj = pool->objects->pdata[pool->size];
108 pool->objects->pdata[pool->size] = NULL;
109 goto end;
110 }
111
112 /* Pool is empty: create a brand new object */
113 BT_LOGD("Pool is empty: allocating new object: pool-addr=%p",
114 pool);
115 obj = pool->funcs.new_object(pool->data);
116
117 end:
118 BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p",
119 pool, obj);
120 return obj;
121 }
122
123 /*
124 * Recycles an object, that is, puts it back into the pool.
125 *
126 * The pool becomes the sole owner of the object to recycle.
127 */
128 static inline
129 void bt_object_pool_recycle_object(struct bt_object_pool *pool, void *obj)
130 {
131 struct bt_object *bt_obj = obj;
132
133 BT_ASSERT_DBG(pool);
134 BT_ASSERT_DBG(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);
137
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);
144 }
145
146 /* Reset reference count to 1 since it could be 0 now */
147 bt_obj->ref_count = 1;
148
149 /* Back to the pool */
150 pool->objects->pdata[pool->size] = obj;
151 pool->size++;
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);
154 }
155
156 #endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */
This page took 0.031441 seconds and 4 git commands to generate.