1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, Linaro Limited
4  * Copyright (c) 2020, Arm Limited
5  */
6 #ifndef KERNEL_HANDLE_H
7 #define KERNEL_HANDLE_H
8 
9 #include <stdbool.h>
10 #include <stdint.h>
11 
12 struct handle_db {
13 	void **ptrs;
14 	size_t max_ptrs;
15 };
16 
17 #define HANDLE_DB_INITIALIZER { NULL, 0 }
18 
19 /*
20  * Frees all internal data structures of the database, but does not free
21  * the db pointer. The database is safe to reuse after it's destroyed, it
22  * will just be empty again. If ptr_destructor is non-null it will be
23  * called for each registered pointer before the database is cleared.
24  */
25 void handle_db_destroy(struct handle_db *db, void (*ptr_destructor)(void *ptr));
26 
27 /* Checks if the associated pointers of all handles in the database are NULL. */
28 bool handle_db_is_empty(struct handle_db *db);
29 
30 /*
31  * Allocates a new handle and assigns the supplied pointer to it,
32  * ptr must not be NULL.
33  * The function returns
34  * >= 0 on success and
35  * -1 on failure
36  */
37 int handle_get(struct handle_db *db, void *ptr);
38 
39 /*
40  * Deallocates a handle. Returns the assiciated pointer of the handle
41  * if the handle was valid or NULL if it's invalid.
42  */
43 void *handle_put(struct handle_db *db, int handle);
44 
45 /*
46  * Returns the assiciated pointer of the handle if the handle is a valid
47  * handle.
48  * Returns NULL on failure.
49  */
50 void *handle_lookup(struct handle_db *db, int handle);
51 
52 #endif /*KERNEL_HANDLE_H*/
53