diff options
author | Angelos Mouzakitis <a.mouzakitis@virtualopensystems.com> | 2023-10-10 14:33:42 +0000 |
---|---|---|
committer | Angelos Mouzakitis <a.mouzakitis@virtualopensystems.com> | 2023-10-10 14:33:42 +0000 |
commit | af1a266670d040d2f4083ff309d732d648afba2a (patch) | |
tree | 2fc46203448ddcc6f81546d379abfaeb323575e9 /roms/skiboot/core/pool.c | |
parent | e02cda008591317b1625707ff8e115a4841aa889 (diff) |
Change-Id: Iaf8d18082d3991dec7c0ebbea540f092188eb4ec
Diffstat (limited to 'roms/skiboot/core/pool.c')
-rw-r--r-- | roms/skiboot/core/pool.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/roms/skiboot/core/pool.c b/roms/skiboot/core/pool.c new file mode 100644 index 000000000..a0283199a --- /dev/null +++ b/roms/skiboot/core/pool.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +/* + * This file provides some functions to manage a pool of pre-allocated + * objects. It also provides a method to reserve a pre-defined number + * of objects for higher priorty requests. The allocations follow the + * following rules: + * + * 1. An allocation will succeed at any priority if there is more than + * the reserved number of objects free. + * 2. Only high priority allocations will succeed when there are less + * than the reserved number of objects free. + * 3. When an allocation is freed it is always added to the high priority + * pool if there are less than the reserved number of allocations + * available. + * + * Copyright 2013-2014 IBM Corp. + */ + +#include <pool.h> +#include <string.h> +#include <stdlib.h> +#include <ccan/list/list.h> + +void* pool_get(struct pool *pool, enum pool_priority priority) +{ + void *obj; + + if (!pool->free_count || + ((pool->free_count <= pool->reserved) && priority == POOL_NORMAL)) + return NULL; + + pool->free_count--; + obj = (void *) list_pop_(&pool->free_list, 0); + assert(obj); + memset(obj, 0, pool->obj_size); + return obj; +} + +void pool_free_object(struct pool *pool, void *obj) +{ + pool->free_count++; + list_add_tail(&pool->free_list, + (struct list_node *) (obj)); +} + +int pool_init(struct pool *pool, size_t obj_size, int count, int reserved) +{ + int i; + + if (obj_size < sizeof(struct list_node)) + obj_size = sizeof(struct list_node); + + assert(count >= reserved); + pool->buf = malloc(obj_size*count); + if (!pool->buf) + return -1; + + pool->obj_size = obj_size; + pool->free_count = count; + pool->reserved = reserved; + list_head_init(&pool->free_list); + + for(i = 0; i < count; i++) + list_add_tail(&pool->free_list, + (struct list_node *) (pool->buf + obj_size*i)); + + return 0; +} |