3 * (C) COPYRIGHT 2008-2011 ARM Limited. All rights reserved.
5 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
8 * A copy of the licence is included with the program, and can also be obtained from Free Software
9 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * @file ump_kernel_descriptor_mapping.h
19 #ifndef _UMP_KERNEL_DESCRIPTOR_MAPPING_H_
20 #define _UMP_KERNEL_DESCRIPTOR_MAPPING_H_
22 #include <linux/rwsem.h>
23 #include <linux/slab.h>
25 * The actual descriptor mapping table, never directly accessed by clients
27 typedef struct umpp_descriptor_table
29 /* keep as a unsigned long to rely on the OS's bitops support */
30 unsigned long * usage; /**< Pointer to bitpattern indicating if a descriptor is valid/used(1) or not(0) */
31 void** mappings; /**< Array of the pointers the descriptors map to */
32 } umpp_descriptor_table;
35 * The descriptor mapping object
36 * Provides a separate namespace where we can map an integer to a pointer
38 typedef struct umpp_descriptor_mapping
40 struct rw_semaphore lock; /**< Lock protecting access to the mapping object */
41 unsigned int max_nr_mappings_allowed; /**< Max number of mappings to support in this namespace */
42 unsigned int current_nr_mappings; /**< Current number of possible mappings */
43 umpp_descriptor_table * table; /**< Pointer to the current mapping table */
44 } umpp_descriptor_mapping;
47 * Create a descriptor mapping object.
48 * Create a descriptor mapping capable of holding init_entries growable to max_entries.
49 * ID 0 is reserved so the number of available entries will be max - 1.
50 * @param init_entries Number of entries to preallocate memory for
51 * @param max_entries Number of entries to max support
52 * @return Pointer to a descriptor mapping object, NULL on failure
54 umpp_descriptor_mapping * umpp_descriptor_mapping_create(unsigned int init_entries, unsigned int max_entries);
57 * Destroy a descriptor mapping object
58 * @param[in] map The map to free
60 void umpp_descriptor_mapping_destroy(umpp_descriptor_mapping * map);
63 * Allocate a new mapping entry (descriptor ID)
64 * Allocates a new entry in the map.
65 * @param[in] map The map to allocate a new entry in
66 * @param[in] target The value to map to
67 * @return The descriptor allocated, ID 0 on failure.
69 unsigned int umpp_descriptor_mapping_allocate(umpp_descriptor_mapping * map, void * target);
72 * Get the value mapped to by a descriptor ID
73 * @param[in] map The map to lookup the descriptor id in
74 * @param[in] descriptor The descriptor ID to lookup
75 * @param[out] target Pointer to a pointer which will receive the stored value
77 * @return 0 on success lookup, -EINVAL on lookup failure.
79 int umpp_descriptor_mapping_lookup(umpp_descriptor_mapping * map, unsigned int descriptor, void** target);
82 * Free the descriptor ID
83 * For the descriptor to be reused it has to be freed
84 * @param[in] map The map to free the descriptor from
85 * @param descriptor The descriptor ID to free
87 void umpp_descriptor_mapping_remove(umpp_descriptor_mapping * map, unsigned int descriptor);
89 #endif /* _UMP_KERNEL_DESCRIPTOR_MAPPING_H_ */