| 1 | /* @title: ID Allocator (id_space) */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <stdint.h> |
| 4 | #include <structures/rbt.h> |
| 5 | #include <sync/spinlock.h> |
| 6 | |
| 7 | #define ID_RANGE_RESERVE_COUNT 128 |
| 8 | |
| 9 | struct id_range { |
| 10 | struct rbt_node node; |
| 11 | uint64_t start; |
| 12 | uint64_t length; |
| 13 | struct id_range *next; |
| 14 | }; |
| 15 | |
| 16 | struct id_space { |
| 17 | struct rbt tree; |
| 18 | struct spinlock lock; |
| 19 | struct id_range reserve_pool[ID_RANGE_RESERVE_COUNT]; |
| 20 | struct id_range *reserve_free; |
| 21 | }; |
| 22 | |
| 23 | #define ID_SPACE_INIT \ |
| 24 | (struct id_space) { \ |
| 25 | .reserve_free = NULL \ |
| 26 | } |
| 27 | |
| 28 | struct id_space *id_space_init(uint64_t max_id); |
| 29 | |
| 30 | void id_space_destroy(struct id_space *is); |
| 31 | |
| 32 | uint64_t id_space_alloc(struct id_space *is); |
| 33 | |
| 34 | void id_space_free(struct id_space *is, uint64_t id); |
| 35 | |
| 36 | uint64_t id_space_alloc_range(struct id_space *is, uint64_t count); |
| 37 | |
| 38 | void id_space_free_range(struct id_space *is, uint64_t start, uint64_t count); |
| 39 |