Fix: lib: add NULL check for destroy_listeners in destroy_component_class
[babeltrace.git] / src / ctf-writer / object-pool.c
CommitLineData
e1e02a22 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e1e02a22
PP
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
e1e02a22
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "CTF-WRITER/OBJECT-POOL"
67d2ce02 9#include "logging.h"
e1e02a22
PP
10
11#include <stdint.h>
578e048b
MJ
12
13#include "common/assert.h"
14
15#include "object-pool.h"
e1e02a22
PP
16
17int 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
98b15851
PP
24 BT_ASSERT_DBG(new_object_func);
25 BT_ASSERT_DBG(destroy_object_func);
e1e02a22
PP
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;
67d2ce02 38 BT_LOGD("Initialized object pool.");
e1e02a22
PP
39 goto end;
40
41error:
42 if (pool) {
43 bt_ctf_object_pool_finalize(pool);
44 }
45
46 ret = -1;
47
48end:
49 return ret;
50}
51
52void bt_ctf_object_pool_finalize(struct bt_ctf_object_pool *pool)
53{
54 uint64_t i;
55
98b15851 56 BT_ASSERT_DBG(pool);
67d2ce02 57 BT_LOGD("Finalizing object pool.");
e1e02a22
PP
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.086603 seconds and 4 git commands to generate.