Commit | Line | Data |
---|---|---|
c2606e2f PP |
1 | /* |
2 | * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation | |
3 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
24 | #define BT_LOG_TAG "OBJECT-POOL" | |
bf436657 | 25 | #include "logging.h" |
c2606e2f PP |
26 | |
27 | #include <stdint.h> | |
57952005 MJ |
28 | |
29 | #include "common/assert.h" | |
30 | ||
31 | #include "object-pool.h" | |
c2606e2f PP |
32 | |
33 | int bt_ctf_object_pool_initialize(struct bt_ctf_object_pool *pool, | |
34 | bt_ctf_object_pool_new_object_func new_object_func, | |
35 | bt_ctf_object_pool_destroy_object_func destroy_object_func, | |
36 | void *data) | |
37 | { | |
38 | int ret = 0; | |
39 | ||
40 | BT_ASSERT(new_object_func); | |
41 | BT_ASSERT(destroy_object_func); | |
42 | BT_LOGD("Initializing object pool: addr=%p, data-addr=%p", | |
43 | pool, data); | |
44 | pool->objects = g_ptr_array_new(); | |
45 | if (!pool->objects) { | |
46 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
47 | goto error; | |
48 | } | |
49 | ||
50 | pool->funcs.new_object = new_object_func; | |
51 | pool->funcs.destroy_object = destroy_object_func; | |
52 | pool->data = data; | |
53 | pool->size = 0; | |
bf436657 | 54 | BT_LOGD("Initialized object pool."); |
c2606e2f PP |
55 | goto end; |
56 | ||
57 | error: | |
58 | if (pool) { | |
59 | bt_ctf_object_pool_finalize(pool); | |
60 | } | |
61 | ||
62 | ret = -1; | |
63 | ||
64 | end: | |
65 | return ret; | |
66 | } | |
67 | ||
68 | void bt_ctf_object_pool_finalize(struct bt_ctf_object_pool *pool) | |
69 | { | |
70 | uint64_t i; | |
71 | ||
72 | BT_ASSERT(pool); | |
bf436657 | 73 | BT_LOGD("Finalizing object pool."); |
c2606e2f PP |
74 | |
75 | if (pool->objects) { | |
76 | for (i = 0; i < pool->size; i++) { | |
77 | void *obj = pool->objects->pdata[i]; | |
78 | ||
79 | if (obj) { | |
80 | pool->funcs.destroy_object(obj, pool->data); | |
81 | } | |
82 | } | |
83 | ||
84 | g_ptr_array_free(pool->objects, TRUE); | |
85 | pool->objects = NULL; | |
86 | } | |
87 | } |