Commit | Line | Data |
---|---|---|
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 | 43 | typedef void *(*bt_object_pool_new_object_func)(void *data); |
9c7f2f85 | 44 | typedef void (*bt_object_pool_destroy_object_func)(void *obj, void *data); |
312c056a PP |
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 | ||
98b15851 | 98 | BT_ASSERT_DBG(pool); |
ef267d12 | 99 | BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u", |
312c056a | 100 | pool, pool->size, pool->objects->len); |
312c056a PP |
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; | |
312c056a PP |
107 | goto end; |
108 | } | |
109 | ||
110 | /* Pool is empty: create a brand new object */ | |
3f7d4d90 | 111 | BT_LOGD("Pool is empty: allocating new object: pool-addr=%p", |
312c056a | 112 | pool); |
312c056a PP |
113 | obj = pool->funcs.new_object(pool->data); |
114 | ||
115 | end: | |
ef267d12 | 116 | BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p", |
312c056a | 117 | pool, obj); |
312c056a PP |
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 | { | |
cdc4693d PP |
129 | struct bt_object *bt_obj = obj; |
130 | ||
98b15851 PP |
131 | BT_ASSERT_DBG(pool); |
132 | BT_ASSERT_DBG(obj); | |
ef267d12 | 133 | BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p", |
312c056a | 134 | pool, pool->size, pool->objects->len, obj); |
312c056a PP |
135 | |
136 | if (pool->size == pool->objects->len) { | |
137 | /* Backing array is full: make place for recycled object */ | |
3f7d4d90 | 138 | BT_LOGD("Object pool is full: increasing object pool capacity: " |
312c056a PP |
139 | "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u", |
140 | pool, pool->objects->len, pool->objects->len + 1); | |
312c056a PP |
141 | g_ptr_array_set_size(pool->objects, pool->size + 1); |
142 | } | |
143 | ||
cdc4693d | 144 | /* Reset reference count to 1 since it could be 0 now */ |
3fea54f6 | 145 | bt_obj->ref_count = 1; |
cdc4693d PP |
146 | |
147 | /* Back to the pool */ | |
312c056a PP |
148 | pool->objects->pdata[pool->size] = obj; |
149 | pool->size++; | |
ef267d12 | 150 | BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p", |
312c056a | 151 | pool, pool->size, pool->objects->len, obj); |
312c056a PP |
152 | } |
153 | ||
154 | #endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */ |