2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "LIB/OBJECT-POOL"
8 #include "lib/logging.h"
11 #include "common/assert.h"
12 #include "lib/assert-cond.h"
13 #include "lib/object-pool.h"
15 int bt_object_pool_initialize(struct bt_object_pool
*pool
,
16 bt_object_pool_new_object_func new_object_func
,
17 bt_object_pool_destroy_object_func destroy_object_func
,
23 BT_ASSERT(new_object_func
);
24 BT_ASSERT(destroy_object_func
);
25 BT_LOGD("Initializing object pool: addr=%p, data-addr=%p",
27 pool
->objects
= g_ptr_array_new();
29 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
33 pool
->funcs
.new_object
= new_object_func
;
34 pool
->funcs
.destroy_object
= destroy_object_func
;
37 BT_LIB_LOGD("Initialized object pool: %!+o", pool
);
41 bt_object_pool_finalize(pool
);
48 void bt_object_pool_finalize(struct bt_object_pool
*pool
)
53 BT_LIB_LOGD("Finalizing object pool: %!+o", pool
);
56 for (i
= 0; i
< pool
->size
; i
++) {
57 void *obj
= pool
->objects
->pdata
[i
];
60 pool
->funcs
.destroy_object(obj
, pool
->data
);
64 g_ptr_array_free(pool
->objects
, TRUE
);