Move to kernel style SPDX license identifiers
[babeltrace.git] / src / ctf-writer / object-pool.h
CommitLineData
e1e02a22 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e1e02a22
PP
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
e1e02a22
PP
6 */
7
0235b0db
MJ
8#ifndef BABELTRACE_CTF_WRITER_OBJECT_POOL_INTERNAL_H
9#define BABELTRACE_CTF_WRITER_OBJECT_POOL_INTERNAL_H
10
e1e02a22
PP
11/*
12 * This is a generic object pool to avoid memory allocation/deallocation
13 * for objects of which the lifespan is typically short, but which are
14 * created a lot.
15 *
16 * The object pool, thanks to two user functions, knows how to allocate
17 * a brand new object in memory when the pool is empty and how to
18 * destroy an object when we destroy the pool.
19 *
20 * The object pool's user is responsible for:
21 *
22 * * Setting whatever references the object needs to keep and reset some
23 * properties _after_ calling bt_ctf_object_pool_create_object(). This is
24 * typically done in the bt_*_create() function which calls
25 * bt_ctf_object_pool_create_object() (which could call the user-provided
26 * allocation function if the pool is empty) and then sets the
27 * appropriate properties on the possibly recycled object.
28 *
29 * * Releasing whatever references the object keeps _before_ calling
30 * bt_ctf_object_pool_recycle_object(). This is typically done in a custom
31 * bt_*_recycle() function which does the necessary before calling
32 * bt_ctf_object_pool_recycle_object() with an object ready to be reused
33 * at any time.
34 */
35
36#include <glib.h>
578e048b
MJ
37
38#include "object.h"
e1e02a22
PP
39
40typedef void *(*bt_ctf_object_pool_new_object_func)(void *data);
41typedef void *(*bt_ctf_object_pool_destroy_object_func)(void *obj, void *data);
42
43struct bt_ctf_object_pool {
44 /*
45 * Container of recycled objects, owned by this. The array's size
46 * is the pool's capacity.
47 */
48 GPtrArray *objects;
49
50 /*
51 * Pool's size, that is, number of elements in the array above,
52 * starting at index 0, which exist as recycled objects.
53 */
54 size_t size;
55
56 /* User functions */
57 struct {
58 /* Allocate a new object in memory */
59 bt_ctf_object_pool_new_object_func new_object;
60
61 /* Free direct and indirect memory occupied by object */
62 bt_ctf_object_pool_destroy_object_func destroy_object;
63 } funcs;
64
65 /* User data passed to user functions */
66 void *data;
67};
68
69/*
70 * Initializes an object pool which is already allocated.
71 */
72int bt_ctf_object_pool_initialize(struct bt_ctf_object_pool *pool,
73 bt_ctf_object_pool_new_object_func new_object_func,
74 bt_ctf_object_pool_destroy_object_func destroy_object_func,
75 void *data);
76
77/*
78 * Finalizes an object pool without deallocating it.
79 */
80void bt_ctf_object_pool_finalize(struct bt_ctf_object_pool *pool);
81
82/*
83 * Creates an object from an object pool. If the pool is empty, this
84 * function calls the "new" user function to allocate a new object
85 * before returning it. Otherwise this function returns a recycled
86 * object, removing it from the pool.
87 *
88 * The returned object is owned by the caller.
89 */
90static inline
91void *bt_ctf_object_pool_create_object(struct bt_ctf_object_pool *pool)
92{
93 struct bt_ctf_object *obj;
94
98b15851 95 BT_ASSERT_DBG(pool);
e1e02a22 96
ef267d12
PP
97#ifdef BT_LOGT
98 BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u",
e1e02a22
PP
99 pool, pool->size, pool->objects->len);
100#endif
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 */
ef267d12
PP
111#ifdef BT_LOGT
112 BT_LOGT("Pool is empty: allocating new object: pool-addr=%p",
e1e02a22
PP
113 pool);
114#endif
115
116 obj = pool->funcs.new_object(pool->data);
117
118end:
ef267d12
PP
119#ifdef BT_LOGT
120 BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p",
e1e02a22
PP
121 pool, obj);
122#endif
123
124 return obj;
125}
126
127/*
128 * Recycles an object, that is, puts it back into the pool.
129 *
130 * The pool becomes the sole owner of the object to recycle.
131 */
132static inline
133void bt_ctf_object_pool_recycle_object(struct bt_ctf_object_pool *pool, void *obj)
134{
135 struct bt_ctf_object *bt_obj = obj;
136
98b15851
PP
137 BT_ASSERT_DBG(pool);
138 BT_ASSERT_DBG(obj);
e1e02a22 139
ef267d12
PP
140#ifdef BT_LOGT
141 BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
e1e02a22
PP
142 pool, pool->size, pool->objects->len, obj);
143#endif
144
145 if (pool->size == pool->objects->len) {
146 /* Backing array is full: make place for recycled object */
ef267d12
PP
147#ifdef BT_LOGT
148 BT_LOGT("Object pool is full: increasing object pool capacity: "
e1e02a22
PP
149 "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u",
150 pool, pool->objects->len, pool->objects->len + 1);
151#endif
152 g_ptr_array_set_size(pool->objects, pool->size + 1);
153 }
154
155 /* Reset reference count to 1 since it could be 0 now */
156 bt_obj->ref_count = 1;
157
158 /* Back to the pool */
159 pool->objects->pdata[pool->size] = obj;
160 pool->size++;
161
ef267d12
PP
162#ifdef BT_LOGT
163 BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
e1e02a22
PP
164 pool, pool->size, pool->objects->len, obj);
165#endif
166}
167
168#endif /* BABELTRACE_CTF_WRITER_OBJECT_POOL_INTERNAL_H */
This page took 0.06758 seconds and 4 git commands to generate.