2 * net/tipc/ref.c: TIPC object registry code
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2004-2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
38 #include "name_distr.h"
39 #include "name_table.h"
47 * Object reference table consists of 2**N entries.
49 * A used entry has object ptr != 0, reference == XXXX|own index
50 * (XXXX changes each time entry is acquired)
51 * A free entry has object ptr == 0, reference == YYYY|next free index
52 * (YYYY is one more than last used XXXX)
54 * Free list is initially chained from entry (2**N)-1 to entry 1.
55 * Entry 0 is not used to allow index 0 to indicate the end of the free list.
57 * Note: Any accidental reference of the form XXXX|0--0 won't match entry 0
58 * because entry 0's reference field has the form XXXX|1--1.
61 struct ref_table ref_table = { 0 };
63 rwlock_t reftbl_lock = RW_LOCK_UNLOCKED;
66 * ref_table_init - create reference table for objects
69 int ref_table_init(u32 requested_size, u32 start)
71 struct reference *table;
76 while (sz < requested_size) {
79 table = (struct reference *)vmalloc(sz * sizeof(struct reference));
83 write_lock_bh(&reftbl_lock);
85 for (i = sz - 1; i >= 0; i--) {
87 table[i].lock = SPIN_LOCK_UNLOCKED;
88 table[i].data.next_plus_upper = (start & ~index_mask) + i - 1;
90 ref_table.entries = table;
91 ref_table.index_mask = index_mask;
92 ref_table.first_free = sz - 1;
93 ref_table.last_free = 1;
94 write_unlock_bh(&reftbl_lock);
99 * ref_table_stop - destroy reference table for objects
102 void ref_table_stop(void)
104 if (!ref_table.entries)
107 vfree(ref_table.entries);
108 ref_table.entries = 0;
112 * ref_acquire - create reference to an object
114 * Return a unique reference value which can be translated back to the pointer
115 * 'object' at a later time. Also, pass back a pointer to the lock protecting
116 * the object, but without locking it.
119 u32 ref_acquire(void *object, spinlock_t **lock)
121 struct reference *entry;
127 assert(ref_table.entries && object);
129 write_lock_bh(&reftbl_lock);
130 if (ref_table.first_free) {
131 index = ref_table.first_free;
132 entry = &(ref_table.entries[index]);
133 index_mask = ref_table.index_mask;
134 /* take lock in case a previous user of entry still holds it */
135 spin_lock_bh(&entry->lock);
136 next_plus_upper = entry->data.next_plus_upper;
137 ref_table.first_free = next_plus_upper & index_mask;
138 reference = (next_plus_upper & ~index_mask) + index;
139 entry->data.reference = reference;
140 entry->object = object;
142 *lock = &entry->lock;
143 spin_unlock_bh(&entry->lock);
145 write_unlock_bh(&reftbl_lock);
150 * ref_discard - invalidate references to an object
152 * Disallow future references to an object and free up the entry for re-use.
153 * Note: The entry's spin_lock may still be busy after discard
156 void ref_discard(u32 ref)
158 struct reference *entry;
162 assert(ref_table.entries);
165 write_lock_bh(&reftbl_lock);
166 index_mask = ref_table.index_mask;
167 index = ref & index_mask;
168 entry = &(ref_table.entries[index]);
169 assert(entry->object != 0);
170 assert(entry->data.reference == ref);
172 /* mark entry as unused */
174 if (ref_table.first_free == 0)
175 ref_table.first_free = index;
177 /* next_plus_upper is always XXXX|0--0 for last free entry */
178 ref_table.entries[ref_table.last_free].data.next_plus_upper
180 ref_table.last_free = index;
182 /* increment upper bits of entry to invalidate subsequent references */
183 entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
184 write_unlock_bh(&reftbl_lock);