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