Move to kernel style SPDX license identifiers
[babeltrace.git] / src / ctf-writer / object-pool.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_LOG_TAG "CTF-WRITER/OBJECT-POOL"
9 #include "logging.h"
10
11 #include <stdint.h>
12
13 #include "common/assert.h"
14
15 #include "object-pool.h"
16
17 int bt_ctf_object_pool_initialize(struct bt_ctf_object_pool *pool,
18 bt_ctf_object_pool_new_object_func new_object_func,
19 bt_ctf_object_pool_destroy_object_func destroy_object_func,
20 void *data)
21 {
22 int ret = 0;
23
24 BT_ASSERT_DBG(new_object_func);
25 BT_ASSERT_DBG(destroy_object_func);
26 BT_LOGD("Initializing object pool: addr=%p, data-addr=%p",
27 pool, data);
28 pool->objects = g_ptr_array_new();
29 if (!pool->objects) {
30 BT_LOGE_STR("Failed to allocate a GPtrArray.");
31 goto error;
32 }
33
34 pool->funcs.new_object = new_object_func;
35 pool->funcs.destroy_object = destroy_object_func;
36 pool->data = data;
37 pool->size = 0;
38 BT_LOGD("Initialized object pool.");
39 goto end;
40
41 error:
42 if (pool) {
43 bt_ctf_object_pool_finalize(pool);
44 }
45
46 ret = -1;
47
48 end:
49 return ret;
50 }
51
52 void bt_ctf_object_pool_finalize(struct bt_ctf_object_pool *pool)
53 {
54 uint64_t i;
55
56 BT_ASSERT_DBG(pool);
57 BT_LOGD("Finalizing object pool.");
58
59 if (pool->objects) {
60 for (i = 0; i < pool->size; i++) {
61 void *obj = pool->objects->pdata[i];
62
63 if (obj) {
64 pool->funcs.destroy_object(obj, pool->data);
65 }
66 }
67
68 g_ptr_array_free(pool->objects, TRUE);
69 pool->objects = NULL;
70 }
71 }
This page took 0.030456 seconds and 4 git commands to generate.