1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014-2020, Linaro Limited 4 */ 5 6 #ifndef PKCS11_TA_HANDLE_H 7 #define PKCS11_TA_HANDLE_H 8 9 #include <stddef.h> 10 11 struct handle_db { 12 void **ptrs; 13 uint32_t max_ptrs; 14 }; 15 16 /* 17 * Initialize the handle database 18 */ 19 void handle_db_init(struct handle_db *db); 20 21 /* 22 * Free all internal data structures of the database, but does not free 23 * the db pointer. The database is safe to reuse after it's destroyed, it 24 * just be empty again. 25 */ 26 void handle_db_destroy(struct handle_db *db); 27 28 /* 29 * Allocate a new handle ID and assigns the supplied pointer to it, 30 * The function returns > 0 on success and 0 on failure. 31 */ 32 uint32_t handle_get(struct handle_db *db, void *ptr); 33 34 /* 35 * Deallocate a handle. Returns the associated 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, uint32_t handle); 39 40 /* 41 * Return the associated 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, uint32_t handle); 46 47 /* Return the handle associated to a pointer if found, else return 0 */ 48 uint32_t handle_lookup_handle(struct handle_db *db, void *ptr); 49 50 #endif /*PKCS11_TA_HANDLE_H*/ 51