xref: /optee_os/core/kernel/handle.c (revision 9403c583381528e7fb391e3769644cc9653cfbb6)
1 /*
2  * Copyright (c) 2014, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <kernel/handle.h>
30 
31 /*
32  * Define the initial capacity of the database. It should be a low number
33  * multiple of 2 since some databases a likely to only use a few handles.
34  * Since the algorithm is to doubles up when growing it shouldn't cause a
35  * noticable overhead on large databases.
36  */
37 #define HANDLE_DB_INITIAL_MAX_PTRS	4
38 
39 void handle_db_destroy(struct handle_db *db)
40 {
41 	if (db) {
42 		free(db->ptrs);
43 		db->ptrs = NULL;
44 		db->max_ptrs = 0;
45 	}
46 }
47 
48 int handle_get(struct handle_db *db, void *ptr)
49 {
50 	size_t n;
51 	void *p;
52 	size_t new_max_ptrs;
53 
54 	if (!db || !ptr)
55 		return -1;
56 
57 	/* Try to find an empty location */
58 	for (n = 0; n < db->max_ptrs; n++) {
59 		if (!db->ptrs[n]) {
60 			db->ptrs[n] = ptr;
61 			return n;
62 		}
63 	}
64 
65 	/* No location available, grow the ptrs array */
66 	if (db->max_ptrs)
67 		new_max_ptrs = db->max_ptrs * 2;
68 	else
69 		new_max_ptrs = HANDLE_DB_INITIAL_MAX_PTRS;
70 	p = realloc(db->ptrs, new_max_ptrs * sizeof(void *));
71 	if (!p)
72 		return -1;
73 	db->ptrs = p;
74 	memset(db->ptrs + db->max_ptrs, 0,
75 	       (new_max_ptrs - db->max_ptrs) * sizeof(void *));
76 	db->max_ptrs = new_max_ptrs;
77 
78 	/* Since n stopped at db->max_ptrs there is an empty location there */
79 	db->ptrs[n] = ptr;
80 	return n;
81 }
82 
83 void *handle_put(struct handle_db *db, int handle)
84 {
85 	void *p;
86 
87 	if (!db || handle < 0 || (size_t)handle >= db->max_ptrs)
88 		return NULL;
89 
90 	p = db->ptrs[handle];
91 	db->ptrs[handle] = NULL;
92 	return p;
93 }
94 
95 void *handle_lookup(struct handle_db *db, int handle)
96 {
97 	if (!db || handle < 0 || (size_t)handle >= db->max_ptrs)
98 		return NULL;
99 
100 	return db->ptrs[handle];
101 }
102