Fix: lib: pass down API function name to some helpers
[babeltrace.git] / src / lib / object-pool.h
CommitLineData
312c056a 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
312c056a 3 *
0235b0db 4 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
312c056a
PP
5 */
6
0235b0db
MJ
7#ifndef BABELTRACE_OBJECT_POOL_INTERNAL_H
8#define BABELTRACE_OBJECT_POOL_INTERNAL_H
9
312c056a
PP
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>
578e048b 36#include "lib/object.h"
312c056a 37
130f527c
PP
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
312c056a 43typedef void *(*bt_object_pool_new_object_func)(void *data);
9c7f2f85 44typedef void (*bt_object_pool_destroy_object_func)(void *obj, void *data);
312c056a
PP
45
46struct 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 */
13f9fa40 75BT_HIDDEN
312c056a
PP
76int 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 */
13f9fa40 84BT_HIDDEN
312c056a
PP
85void 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 */
95static inline
96void *bt_object_pool_create_object(struct bt_object_pool *pool)
97{
98 struct bt_object *obj;
99
98b15851 100 BT_ASSERT_DBG(pool);
ef267d12 101 BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u",
312c056a 102 pool, pool->size, pool->objects->len);
312c056a
PP
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;
312c056a
PP
109 goto end;
110 }
111
112 /* Pool is empty: create a brand new object */
3f7d4d90 113 BT_LOGD("Pool is empty: allocating new object: pool-addr=%p",
312c056a 114 pool);
312c056a
PP
115 obj = pool->funcs.new_object(pool->data);
116
117end:
ef267d12 118 BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p",
312c056a 119 pool, obj);
312c056a
PP
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 */
128static inline
129void bt_object_pool_recycle_object(struct bt_object_pool *pool, void *obj)
130{
cdc4693d
PP
131 struct bt_object *bt_obj = obj;
132
98b15851
PP
133 BT_ASSERT_DBG(pool);
134 BT_ASSERT_DBG(obj);
ef267d12 135 BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
312c056a 136 pool, pool->size, pool->objects->len, obj);
312c056a
PP
137
138 if (pool->size == pool->objects->len) {
139 /* Backing array is full: make place for recycled object */
3f7d4d90 140 BT_LOGD("Object pool is full: increasing object pool capacity: "
312c056a
PP
141 "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u",
142 pool, pool->objects->len, pool->objects->len + 1);
312c056a
PP
143 g_ptr_array_set_size(pool->objects, pool->size + 1);
144 }
145
cdc4693d 146 /* Reset reference count to 1 since it could be 0 now */
3fea54f6 147 bt_obj->ref_count = 1;
cdc4693d
PP
148
149 /* Back to the pool */
312c056a
PP
150 pool->objects->pdata[pool->size] = obj;
151 pool->size++;
ef267d12 152 BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
312c056a 153 pool, pool->size, pool->objects->len, obj);
312c056a
PP
154}
155
156#endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */
This page took 0.077903 seconds and 4 git commands to generate.