| 1 | /* |
| 2 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _COMMON_INDEX_ALLOCATOR_H |
| 9 | #define _COMMON_INDEX_ALLOCATOR_H |
| 10 | |
| 11 | #include <inttypes.h> |
| 12 | #include <lttng/lttng-export.h> |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | struct lttng_index_allocator; |
| 19 | |
| 20 | enum lttng_index_allocator_status { |
| 21 | LTTNG_INDEX_ALLOCATOR_STATUS_OK, |
| 22 | LTTNG_INDEX_ALLOCATOR_STATUS_EMPTY, |
| 23 | LTTNG_INDEX_ALLOCATOR_STATUS_ERROR, |
| 24 | }; |
| 25 | |
| 26 | /* |
| 27 | * Create an index allocator of `index_count` slots. |
| 28 | */ |
| 29 | LTTNG_EXPORT struct lttng_index_allocator *lttng_index_allocator_create( |
| 30 | uint64_t index_count); |
| 31 | |
| 32 | /* |
| 33 | * Get the number of indexes currently in use. |
| 34 | */ |
| 35 | LTTNG_EXPORT uint64_t lttng_index_allocator_get_index_count( |
| 36 | struct lttng_index_allocator *allocator); |
| 37 | |
| 38 | /* |
| 39 | * Allocate (i.e. reserve) a slot. |
| 40 | */ |
| 41 | LTTNG_EXPORT enum lttng_index_allocator_status lttng_index_allocator_alloc( |
| 42 | struct lttng_index_allocator *allocator, |
| 43 | uint64_t *index); |
| 44 | |
| 45 | /* |
| 46 | * Release a slot by index. The slot will be re-used by the index allocator |
| 47 | * in future 'alloc' calls. |
| 48 | */ |
| 49 | LTTNG_EXPORT enum lttng_index_allocator_status lttng_index_allocator_release( |
| 50 | struct lttng_index_allocator *allocator, uint64_t index); |
| 51 | |
| 52 | /* |
| 53 | * Destroy an index allocator. |
| 54 | */ |
| 55 | LTTNG_EXPORT void lttng_index_allocator_destroy(struct lttng_index_allocator *allocator); |
| 56 | |
| 57 | #ifdef __cplusplus |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | #endif /* _COMMON_INDEX_ALLOCATOR_H */ |