ref.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * net/tipc/ref.c: TIPC object registry code
  3. *
  4. * Copyright (c) 2003-2005, Ericsson Research Canada
  5. * Copyright (c) 2004-2005, Wind River Systems
  6. * Copyright (c) 2005-2006, Ericsson AB
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "core.h"
  38. #include "ref.h"
  39. #include "port.h"
  40. #include "subscr.h"
  41. #include "name_distr.h"
  42. #include "name_table.h"
  43. #include "config.h"
  44. #include "discover.h"
  45. #include "bearer.h"
  46. #include "node.h"
  47. #include "bcast.h"
  48. /*
  49. * Object reference table consists of 2**N entries.
  50. *
  51. * A used entry has object ptr != 0, reference == XXXX|own index
  52. * (XXXX changes each time entry is acquired)
  53. * A free entry has object ptr == 0, reference == YYYY|next free index
  54. * (YYYY is one more than last used XXXX)
  55. *
  56. * Free list is initially chained from entry (2**N)-1 to entry 1.
  57. * Entry 0 is not used to allow index 0 to indicate the end of the free list.
  58. *
  59. * Note: Any accidental reference of the form XXXX|0--0 won't match entry 0
  60. * because entry 0's reference field has the form XXXX|1--1.
  61. */
  62. struct ref_table ref_table = { 0 };
  63. rwlock_t reftbl_lock = RW_LOCK_UNLOCKED;
  64. /**
  65. * ref_table_init - create reference table for objects
  66. */
  67. int ref_table_init(u32 requested_size, u32 start)
  68. {
  69. struct reference *table;
  70. u32 sz = 1 << 4;
  71. u32 index_mask;
  72. int i;
  73. while (sz < requested_size) {
  74. sz <<= 1;
  75. }
  76. table = (struct reference *)vmalloc(sz * sizeof(struct reference));
  77. if (table == NULL)
  78. return -ENOMEM;
  79. write_lock_bh(&reftbl_lock);
  80. index_mask = sz - 1;
  81. for (i = sz - 1; i >= 0; i--) {
  82. table[i].object = 0;
  83. table[i].lock = SPIN_LOCK_UNLOCKED;
  84. table[i].data.next_plus_upper = (start & ~index_mask) + i - 1;
  85. }
  86. ref_table.entries = table;
  87. ref_table.index_mask = index_mask;
  88. ref_table.first_free = sz - 1;
  89. ref_table.last_free = 1;
  90. write_unlock_bh(&reftbl_lock);
  91. return TIPC_OK;
  92. }
  93. /**
  94. * ref_table_stop - destroy reference table for objects
  95. */
  96. void ref_table_stop(void)
  97. {
  98. if (!ref_table.entries)
  99. return;
  100. vfree(ref_table.entries);
  101. ref_table.entries = 0;
  102. }
  103. /**
  104. * ref_acquire - create reference to an object
  105. *
  106. * Return a unique reference value which can be translated back to the pointer
  107. * 'object' at a later time. Also, pass back a pointer to the lock protecting
  108. * the object, but without locking it.
  109. */
  110. u32 ref_acquire(void *object, spinlock_t **lock)
  111. {
  112. struct reference *entry;
  113. u32 index;
  114. u32 index_mask;
  115. u32 next_plus_upper;
  116. u32 reference = 0;
  117. assert(ref_table.entries && object);
  118. write_lock_bh(&reftbl_lock);
  119. if (ref_table.first_free) {
  120. index = ref_table.first_free;
  121. entry = &(ref_table.entries[index]);
  122. index_mask = ref_table.index_mask;
  123. /* take lock in case a previous user of entry still holds it */
  124. spin_lock_bh(&entry->lock);
  125. next_plus_upper = entry->data.next_plus_upper;
  126. ref_table.first_free = next_plus_upper & index_mask;
  127. reference = (next_plus_upper & ~index_mask) + index;
  128. entry->data.reference = reference;
  129. entry->object = object;
  130. if (lock != 0)
  131. *lock = &entry->lock;
  132. spin_unlock_bh(&entry->lock);
  133. }
  134. write_unlock_bh(&reftbl_lock);
  135. return reference;
  136. }
  137. /**
  138. * ref_discard - invalidate references to an object
  139. *
  140. * Disallow future references to an object and free up the entry for re-use.
  141. * Note: The entry's spin_lock may still be busy after discard
  142. */
  143. void ref_discard(u32 ref)
  144. {
  145. struct reference *entry;
  146. u32 index;
  147. u32 index_mask;
  148. assert(ref_table.entries);
  149. assert(ref != 0);
  150. write_lock_bh(&reftbl_lock);
  151. index_mask = ref_table.index_mask;
  152. index = ref & index_mask;
  153. entry = &(ref_table.entries[index]);
  154. assert(entry->object != 0);
  155. assert(entry->data.reference == ref);
  156. /* mark entry as unused */
  157. entry->object = 0;
  158. if (ref_table.first_free == 0)
  159. ref_table.first_free = index;
  160. else
  161. /* next_plus_upper is always XXXX|0--0 for last free entry */
  162. ref_table.entries[ref_table.last_free].data.next_plus_upper
  163. |= index;
  164. ref_table.last_free = index;
  165. /* increment upper bits of entry to invalidate subsequent references */
  166. entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
  167. write_unlock_bh(&reftbl_lock);
  168. }