ref.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * 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.
  20. *
  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.
  32. */
  33. #include "core.h"
  34. #include "ref.h"
  35. #include "port.h"
  36. #include "subscr.h"
  37. #include "name_distr.h"
  38. #include "name_table.h"
  39. #include "config.h"
  40. #include "discover.h"
  41. #include "bearer.h"
  42. #include "node.h"
  43. #include "bcast.h"
  44. /*
  45. * Object reference table consists of 2**N entries.
  46. *
  47. * A used entry has object ptr != 0, reference == XXXX|own index
  48. * (XXXX changes each time entry is acquired)
  49. * A free entry has object ptr == 0, reference == YYYY|next free index
  50. * (YYYY is one more than last used XXXX)
  51. *
  52. * Free list is initially chained from entry (2**N)-1 to entry 1.
  53. * Entry 0 is not used to allow index 0 to indicate the end of the free list.
  54. *
  55. * Note: Any accidental reference of the form XXXX|0--0 won't match entry 0
  56. * because entry 0's reference field has the form XXXX|1--1.
  57. */
  58. struct ref_table ref_table = { 0 };
  59. rwlock_t reftbl_lock = RW_LOCK_UNLOCKED;
  60. /**
  61. * ref_table_init - create reference table for objects
  62. */
  63. int ref_table_init(u32 requested_size, u32 start)
  64. {
  65. struct reference *table;
  66. u32 sz = 1 << 4;
  67. u32 index_mask;
  68. int i;
  69. while (sz < requested_size) {
  70. sz <<= 1;
  71. }
  72. table = (struct reference *)vmalloc(sz * sizeof(struct reference));
  73. if (table == NULL)
  74. return -ENOMEM;
  75. write_lock_bh(&reftbl_lock);
  76. index_mask = sz - 1;
  77. for (i = sz - 1; i >= 0; i--) {
  78. table[i].object = 0;
  79. table[i].lock = SPIN_LOCK_UNLOCKED;
  80. table[i].data.next_plus_upper = (start & ~index_mask) + i - 1;
  81. }
  82. ref_table.entries = table;
  83. ref_table.index_mask = index_mask;
  84. ref_table.first_free = sz - 1;
  85. ref_table.last_free = 1;
  86. write_unlock_bh(&reftbl_lock);
  87. return TIPC_OK;
  88. }
  89. /**
  90. * ref_table_stop - destroy reference table for objects
  91. */
  92. void ref_table_stop(void)
  93. {
  94. if (!ref_table.entries)
  95. return;
  96. vfree(ref_table.entries);
  97. ref_table.entries = 0;
  98. }
  99. /**
  100. * ref_acquire - create reference to an object
  101. *
  102. * Return a unique reference value which can be translated back to the pointer
  103. * 'object' at a later time. Also, pass back a pointer to the lock protecting
  104. * the object, but without locking it.
  105. */
  106. u32 ref_acquire(void *object, spinlock_t **lock)
  107. {
  108. struct reference *entry;
  109. u32 index;
  110. u32 index_mask;
  111. u32 next_plus_upper;
  112. u32 reference = 0;
  113. assert(ref_table.entries && object);
  114. write_lock_bh(&reftbl_lock);
  115. if (ref_table.first_free) {
  116. index = ref_table.first_free;
  117. entry = &(ref_table.entries[index]);
  118. index_mask = ref_table.index_mask;
  119. /* take lock in case a previous user of entry still holds it */
  120. spin_lock_bh(&entry->lock);
  121. next_plus_upper = entry->data.next_plus_upper;
  122. ref_table.first_free = next_plus_upper & index_mask;
  123. reference = (next_plus_upper & ~index_mask) + index;
  124. entry->data.reference = reference;
  125. entry->object = object;
  126. if (lock != 0)
  127. *lock = &entry->lock;
  128. spin_unlock_bh(&entry->lock);
  129. }
  130. write_unlock_bh(&reftbl_lock);
  131. return reference;
  132. }
  133. /**
  134. * ref_discard - invalidate references to an object
  135. *
  136. * Disallow future references to an object and free up the entry for re-use.
  137. * Note: The entry's spin_lock may still be busy after discard
  138. */
  139. void ref_discard(u32 ref)
  140. {
  141. struct reference *entry;
  142. u32 index;
  143. u32 index_mask;
  144. assert(ref_table.entries);
  145. assert(ref != 0);
  146. write_lock_bh(&reftbl_lock);
  147. index_mask = ref_table.index_mask;
  148. index = ref & index_mask;
  149. entry = &(ref_table.entries[index]);
  150. assert(entry->object != 0);
  151. assert(entry->data.reference == ref);
  152. /* mark entry as unused */
  153. entry->object = 0;
  154. if (ref_table.first_free == 0)
  155. ref_table.first_free = index;
  156. else
  157. /* next_plus_upper is always XXXX|0--0 for last free entry */
  158. ref_table.entries[ref_table.last_free].data.next_plus_upper
  159. |= index;
  160. ref_table.last_free = index;
  161. /* increment upper bits of entry to invalidate subsequent references */
  162. entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
  163. write_unlock_bh(&reftbl_lock);
  164. }