Commit | Line | Data |
---|---|---|
312c056a PP |
1 | #ifndef BABELTRACE_OBJECT_POOL_INTERNAL_H |
2 | #define BABELTRACE_OBJECT_POOL_INTERNAL_H | |
3 | ||
4 | /* | |
312c056a PP |
5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
26 | /* | |
27 | * This is a generic object pool to avoid memory allocation/deallocation | |
28 | * for objects of which the lifespan is typically short, but which are | |
29 | * created a lot. | |
30 | * | |
31 | * The object pool, thanks to two user functions, knows how to allocate | |
32 | * a brand new object in memory when the pool is empty and how to | |
33 | * destroy an object when we destroy the pool. | |
34 | * | |
35 | * The object pool's user is responsible for: | |
36 | * | |
37 | * * Setting whatever references the object needs to keep and reset some | |
38 | * properties _after_ calling bt_object_pool_create_object(). This is | |
39 | * typically done in the bt_*_create() function which calls | |
40 | * bt_object_pool_create_object() (which could call the user-provided | |
41 | * allocation function if the pool is empty) and then sets the | |
42 | * appropriate properties on the possibly recycled object. | |
43 | * | |
44 | * * Releasing whatever references the object keeps _before_ calling | |
45 | * bt_object_pool_recycle_object(). This is typically done in a custom | |
46 | * bt_*_recycle() function which does the necessary before calling | |
47 | * bt_object_pool_recycle_object() with an object ready to be reused | |
48 | * at any time. | |
49 | */ | |
50 | ||
51 | #include <glib.h> | |
578e048b | 52 | #include "lib/object.h" |
312c056a | 53 | |
130f527c PP |
54 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
55 | #ifndef BT_LIB_LOG_SUPPORTED | |
56 | # error Please include "lib/logging.h" before including this file. | |
57 | #endif | |
58 | ||
312c056a | 59 | typedef void *(*bt_object_pool_new_object_func)(void *data); |
9c7f2f85 | 60 | typedef void (*bt_object_pool_destroy_object_func)(void *obj, void *data); |
312c056a PP |
61 | |
62 | struct bt_object_pool { | |
63 | /* | |
64 | * Container of recycled objects, owned by this. The array's size | |
65 | * is the pool's capacity. | |
66 | */ | |
67 | GPtrArray *objects; | |
68 | ||
69 | /* | |
70 | * Pool's size, that is, number of elements in the array above, | |
71 | * starting at index 0, which exist as recycled objects. | |
72 | */ | |
73 | size_t size; | |
74 | ||
75 | /* User functions */ | |
76 | struct { | |
77 | /* Allocate a new object in memory */ | |
78 | bt_object_pool_new_object_func new_object; | |
79 | ||
80 | /* Free direct and indirect memory occupied by object */ | |
81 | bt_object_pool_destroy_object_func destroy_object; | |
82 | } funcs; | |
83 | ||
84 | /* User data passed to user functions */ | |
85 | void *data; | |
86 | }; | |
87 | ||
88 | /* | |
89 | * Initializes an object pool which is already allocated. | |
90 | */ | |
13f9fa40 | 91 | BT_HIDDEN |
312c056a PP |
92 | int bt_object_pool_initialize(struct bt_object_pool *pool, |
93 | bt_object_pool_new_object_func new_object_func, | |
94 | bt_object_pool_destroy_object_func destroy_object_func, | |
95 | void *data); | |
96 | ||
97 | /* | |
98 | * Finalizes an object pool without deallocating it. | |
99 | */ | |
13f9fa40 | 100 | BT_HIDDEN |
312c056a PP |
101 | void bt_object_pool_finalize(struct bt_object_pool *pool); |
102 | ||
103 | /* | |
104 | * Creates an object from an object pool. If the pool is empty, this | |
105 | * function calls the "new" user function to allocate a new object | |
106 | * before returning it. Otherwise this function returns a recycled | |
107 | * object, removing it from the pool. | |
108 | * | |
109 | * The returned object is owned by the caller. | |
110 | */ | |
111 | static inline | |
112 | void *bt_object_pool_create_object(struct bt_object_pool *pool) | |
113 | { | |
114 | struct bt_object *obj; | |
115 | ||
98b15851 | 116 | BT_ASSERT_DBG(pool); |
ef267d12 | 117 | BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u", |
312c056a | 118 | pool, pool->size, pool->objects->len); |
312c056a PP |
119 | |
120 | if (pool->size > 0) { | |
121 | /* Pick one from the pool */ | |
122 | pool->size--; | |
123 | obj = pool->objects->pdata[pool->size]; | |
124 | pool->objects->pdata[pool->size] = NULL; | |
312c056a PP |
125 | goto end; |
126 | } | |
127 | ||
128 | /* Pool is empty: create a brand new object */ | |
3f7d4d90 | 129 | BT_LOGD("Pool is empty: allocating new object: pool-addr=%p", |
312c056a | 130 | pool); |
312c056a PP |
131 | obj = pool->funcs.new_object(pool->data); |
132 | ||
133 | end: | |
ef267d12 | 134 | BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p", |
312c056a | 135 | pool, obj); |
312c056a PP |
136 | return obj; |
137 | } | |
138 | ||
139 | /* | |
140 | * Recycles an object, that is, puts it back into the pool. | |
141 | * | |
142 | * The pool becomes the sole owner of the object to recycle. | |
143 | */ | |
144 | static inline | |
145 | void bt_object_pool_recycle_object(struct bt_object_pool *pool, void *obj) | |
146 | { | |
cdc4693d PP |
147 | struct bt_object *bt_obj = obj; |
148 | ||
98b15851 PP |
149 | BT_ASSERT_DBG(pool); |
150 | BT_ASSERT_DBG(obj); | |
ef267d12 | 151 | BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p", |
312c056a | 152 | pool, pool->size, pool->objects->len, obj); |
312c056a PP |
153 | |
154 | if (pool->size == pool->objects->len) { | |
155 | /* Backing array is full: make place for recycled object */ | |
3f7d4d90 | 156 | BT_LOGD("Object pool is full: increasing object pool capacity: " |
312c056a PP |
157 | "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u", |
158 | pool, pool->objects->len, pool->objects->len + 1); | |
312c056a PP |
159 | g_ptr_array_set_size(pool->objects, pool->size + 1); |
160 | } | |
161 | ||
cdc4693d | 162 | /* Reset reference count to 1 since it could be 0 now */ |
3fea54f6 | 163 | bt_obj->ref_count = 1; |
cdc4693d PP |
164 | |
165 | /* Back to the pool */ | |
312c056a PP |
166 | pool->objects->pdata[pool->size] = obj; |
167 | pool->size++; | |
ef267d12 | 168 | BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p", |
312c056a | 169 | pool, pool->size, pool->objects->len, obj); |
312c056a PP |
170 | } |
171 | ||
172 | #endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */ |