X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Flib%2Fobject-pool.h;h=e0501daf70b47a075695c4939f3e8b28965b4708;hb=HEAD;hp=24d1e540aeaf53132c5090b45baed88c6d3dd5e1;hpb=ef267d12284b855bc52ee429a5dc12da1d1e3b95;p=babeltrace.git diff --git a/src/lib/object-pool.h b/src/lib/object-pool.h index 24d1e540..c1558de1 100644 --- a/src/lib/object-pool.h +++ b/src/lib/object-pool.h @@ -1,28 +1,12 @@ -#ifndef BABELTRACE_OBJECT_POOL_INTERNAL_H -#define BABELTRACE_OBJECT_POOL_INTERNAL_H - /* - * Copyright (c) 2018 Philippe Proulx - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * SPDX-License-Identifier: MIT * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Copyright (c) 2018 Philippe Proulx */ +#ifndef BABELTRACE_LIB_OBJECT_POOL_H +#define BABELTRACE_LIB_OBJECT_POOL_H + /* * This is a generic object pool to avoid memory allocation/deallocation * for objects of which the lifespan is typically short, but which are @@ -51,8 +35,13 @@ #include #include "lib/object.h" +/* Protection: this file uses BT_LIB_LOG*() macros directly */ +#ifndef BT_LIB_LOG_SUPPORTED +# error Please include "lib/logging.h" before including this file. +#endif + typedef void *(*bt_object_pool_new_object_func)(void *data); -typedef void *(*bt_object_pool_destroy_object_func)(void *obj, void *data); +typedef void (*bt_object_pool_destroy_object_func)(void *obj, void *data); struct bt_object_pool { /* @@ -106,12 +95,9 @@ void *bt_object_pool_create_object(struct bt_object_pool *pool) { struct bt_object *obj; - BT_ASSERT(pool); - -#ifdef BT_LOGT + BT_ASSERT_DBG(pool); BT_LOGT("Creating object from pool: pool-addr=%p, pool-size=%zu, pool-cap=%u", pool, pool->size, pool->objects->len); -#endif if (pool->size > 0) { /* Pick one from the pool */ @@ -122,19 +108,13 @@ void *bt_object_pool_create_object(struct bt_object_pool *pool) } /* Pool is empty: create a brand new object */ -#ifdef BT_LOGD BT_LOGD("Pool is empty: allocating new object: pool-addr=%p", pool); -#endif - obj = pool->funcs.new_object(pool->data); end: -#ifdef BT_LOGT BT_LOGT("Created one object from pool: pool-addr=%p, obj-addr=%p", pool, obj); -#endif - return obj; } @@ -148,21 +128,16 @@ void bt_object_pool_recycle_object(struct bt_object_pool *pool, void *obj) { struct bt_object *bt_obj = obj; - BT_ASSERT(pool); - BT_ASSERT(obj); - -#ifdef BT_LOGT + BT_ASSERT_DBG(pool); + BT_ASSERT_DBG(obj); BT_LOGT("Recycling object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p", pool, pool->size, pool->objects->len, obj); -#endif if (pool->size == pool->objects->len) { /* Backing array is full: make place for recycled object */ -#ifdef BT_LOGD BT_LOGD("Object pool is full: increasing object pool capacity: " "pool-addr=%p, old-pool-cap=%u, new-pool-cap=%u", pool, pool->objects->len, pool->objects->len + 1); -#endif g_ptr_array_set_size(pool->objects, pool->size + 1); } @@ -172,11 +147,8 @@ void bt_object_pool_recycle_object(struct bt_object_pool *pool, void *obj) /* Back to the pool */ pool->objects->pdata[pool->size] = obj; pool->size++; - -#ifdef BT_LOGT BT_LOGT("Recycled object: pool-addr=%p, pool-size=%zu, pool-cap=%u, obj-addr=%p", pool, pool->size, pool->objects->len, obj); -#endif } -#endif /* BABELTRACE_OBJECT_POOL_INTERNAL_H */ +#endif /* BABELTRACE_LIB_OBJECT_POOL_H */