Commit | Line | Data |
---|---|---|
2463b787 JR |
1 | /* |
2 | * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <assert.h> | |
9 | #include <inttypes.h> | |
10 | ||
11 | #include <urcu.h> | |
12 | #include <urcu/list.h> | |
13 | ||
14 | #include "macros.h" | |
15 | #include "error.h" | |
16 | ||
17 | #include "index-allocator.h" | |
18 | ||
19 | struct lttng_index_allocator { | |
20 | struct cds_list_head unused_list; | |
21 | uint64_t size; | |
22 | uint64_t position; | |
23 | }; | |
24 | ||
25 | struct lttng_index { | |
26 | uint64_t index; | |
27 | struct cds_list_head head; | |
28 | }; | |
29 | ||
30 | struct lttng_index_allocator *lttng_index_allocator_create( | |
31 | uint64_t index_count) | |
32 | { | |
33 | struct lttng_index_allocator *allocator = NULL; | |
34 | ||
35 | allocator = zmalloc(sizeof(*allocator)); | |
36 | if (!allocator) { | |
37 | PERROR("Failed to allocate free index queue"); | |
38 | goto end; | |
39 | } | |
40 | allocator->size = index_count; | |
41 | allocator->position = 0; | |
42 | ||
43 | CDS_INIT_LIST_HEAD(&allocator->unused_list); | |
44 | ||
45 | end: | |
46 | return allocator; | |
47 | } | |
48 | ||
49 | uint64_t lttng_index_allocator_get_index_count(struct lttng_index_allocator *allocator) | |
50 | { | |
51 | return allocator->size; | |
52 | } | |
53 | ||
54 | enum lttng_index_allocator_status lttng_index_allocator_alloc( | |
55 | struct lttng_index_allocator *allocator, | |
56 | uint64_t *allocated_index) | |
57 | { | |
58 | enum lttng_index_allocator_status status = | |
59 | LTTNG_INDEX_ALLOCATOR_STATUS_OK; | |
60 | ||
61 | if (cds_list_empty(&allocator->unused_list)) { | |
62 | if (allocator->position >= allocator->size) { | |
63 | /* No indices left. */ | |
64 | status = LTTNG_INDEX_ALLOCATOR_STATUS_EMPTY; | |
65 | goto end; | |
66 | } | |
67 | ||
68 | *allocated_index = allocator->position++; | |
69 | } else { | |
70 | struct lttng_index *index; | |
71 | ||
72 | index = cds_list_first_entry(&allocator->unused_list, | |
73 | typeof(*index), head); | |
74 | cds_list_del(&index->head); | |
75 | *allocated_index = index->index; | |
76 | free(index); | |
77 | } | |
78 | ||
79 | end: | |
80 | return status; | |
81 | } | |
82 | ||
83 | enum lttng_index_allocator_status lttng_index_allocator_release( | |
84 | struct lttng_index_allocator *allocator, uint64_t idx) | |
85 | { | |
86 | struct lttng_index *index = NULL; | |
87 | enum lttng_index_allocator_status status = | |
88 | LTTNG_INDEX_ALLOCATOR_STATUS_OK; | |
89 | ||
90 | assert(idx < allocator->size); | |
91 | ||
92 | index = zmalloc(sizeof(*index)); | |
93 | if (!index) { | |
94 | PERROR("Failed to allocate free index queue"); | |
95 | status = LTTNG_INDEX_ALLOCATOR_STATUS_ERROR; | |
96 | goto end; | |
97 | } | |
98 | ||
99 | index->index = idx; | |
100 | cds_list_add_tail(&index->head, &allocator->unused_list); | |
101 | ||
102 | end: | |
103 | return status; | |
104 | } | |
105 | ||
106 | void lttng_index_allocator_destroy(struct lttng_index_allocator *allocator) | |
107 | { | |
108 | struct lttng_index *index = NULL, *tmp_index = NULL; | |
109 | ||
110 | if (!allocator) { | |
111 | return; | |
112 | } | |
113 | ||
114 | cds_list_for_each_entry_safe(index, tmp_index, | |
115 | &allocator->unused_list, head) { | |
116 | cds_list_del(&index->head); | |
117 | free(index); | |
118 | } | |
119 | ||
120 | free(allocator); | |
121 | } |