2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
8 #ifndef BABELTRACE_CTF_WRITER_OBJECT_POOL_INTERNAL_H
9 #define BABELTRACE_CTF_WRITER_OBJECT_POOL_INTERNAL_H
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
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.
20 * The object pool's user is responsible for:
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.
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
40 typedef void *(*bt_ctf_object_pool_new_object_func
)(void *data
);
41 typedef void *(*bt_ctf_object_pool_destroy_object_func
)(void *obj
, void *data
);
43 struct bt_ctf_object_pool
{
45 * Container of recycled objects, owned by this. The array's size
46 * is the pool's capacity.
51 * Pool's size, that is, number of elements in the array above,
52 * starting at index 0, which exist as recycled objects.
58 /* Allocate a new object in memory */
59 bt_ctf_object_pool_new_object_func new_object
;
61 /* Free direct and indirect memory occupied by object */
62 bt_ctf_object_pool_destroy_object_func destroy_object
;
65 /* User data passed to user functions */
70 * Initializes an object pool which is already allocated.
72 int 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
,
78 * Finalizes an object pool without deallocating it.
80 void bt_ctf_object_pool_finalize(struct bt_ctf_object_pool
*pool
);
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.
88 * The returned object is owned by the caller.
91 void *bt_ctf_object_pool_create_object(struct bt_ctf_object_pool
*pool
)
93 struct bt_ctf_object
*obj
;
98 BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u",
99 pool
, pool
->size
, pool
->objects
->len
);
102 if (pool
->size
> 0) {
103 /* Pick one from the pool */
105 obj
= pool
->objects
->pdata
[pool
->size
];
106 pool
->objects
->pdata
[pool
->size
] = NULL
;
110 /* Pool is empty: create a brand new object */
112 BT_LOGT("Pool is empty: allocating new object: pool-addr=%p",
116 obj
= pool
->funcs
.new_object(pool
->data
);
120 BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p",
128 * Recycles an object, that is, puts it back into the pool.
130 * The pool becomes the sole owner of the object to recycle.
133 void bt_ctf_object_pool_recycle_object(struct bt_ctf_object_pool
*pool
, void *obj
)
135 struct bt_ctf_object
*bt_obj
= obj
;
141 BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
142 pool
, pool
->size
, pool
->objects
->len
, obj
);
145 if (pool
->size
== pool
->objects
->len
) {
146 /* Backing array is full: make place for recycled object */
148 BT_LOGT("Object pool is full: increasing object pool capacity: "
149 "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u",
150 pool
, pool
->objects
->len
, pool
->objects
->len
+ 1);
152 g_ptr_array_set_size(pool
->objects
, pool
->size
+ 1);
155 /* Reset reference count to 1 since it could be 0 now */
156 bt_obj
->ref_count
= 1;
158 /* Back to the pool */
159 pool
->objects
->pdata
[pool
->size
] = obj
;
163 BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p",
164 pool
, pool
->size
, pool
->objects
->len
, obj
);
168 #endif /* BABELTRACE_CTF_WRITER_OBJECT_POOL_INTERNAL_H */