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