ref.c 6.2 KB
Newer Older
P
Per Liden 已提交
1 2 3
/*
 * net/tipc/ref.c: TIPC object registry code
 * 
P
Per Liden 已提交
4
 * Copyright (c) 1991-2006, Ericsson AB
P
Per Liden 已提交
5 6 7 8 9
 * Copyright (c) 2004-2005, Wind River Systems
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
P
Per Liden 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
P
Per Liden 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "ref.h"
#include "port.h"
#include "subscr.h"
#include "name_distr.h"
#include "name_table.h"
#include "config.h"
#include "discover.h"
#include "bearer.h"
#include "node.h"
#include "bcast.h"

/*
 * Object reference table consists of 2**N entries.
 *
 * A used entry has object ptr != 0, reference == XXXX|own index
 *				     (XXXX changes each time entry is acquired) 
 * A free entry has object ptr == 0, reference == YYYY|next free index
 *				     (YYYY is one more than last used XXXX)
 *
 * Free list is initially chained from entry (2**N)-1 to entry 1. 
 * Entry 0 is not used to allow index 0 to indicate the end of the free list.
 *
 * Note: Any accidental reference of the form XXXX|0--0 won't match entry 0
 * because entry 0's reference field has the form XXXX|1--1.
 */

64
struct ref_table tipc_ref_table = { NULL };
P
Per Liden 已提交
65

66
static rwlock_t ref_table_lock = RW_LOCK_UNLOCKED;
P
Per Liden 已提交
67 68

/**
69
 * tipc_ref_table_init - create reference table for objects
P
Per Liden 已提交
70 71
 */

72
int tipc_ref_table_init(u32 requested_size, u32 start)
P
Per Liden 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
{
	struct reference *table;
	u32 sz = 1 << 4;
	u32 index_mask;
	int i;

	while (sz < requested_size) {
		sz <<= 1;
	}
	table = (struct reference *)vmalloc(sz * sizeof(struct reference));
	if (table == NULL)
		return -ENOMEM;

86
	write_lock_bh(&ref_table_lock);
P
Per Liden 已提交
87 88
	index_mask = sz - 1;
	for (i = sz - 1; i >= 0; i--) {
89
		table[i].object = NULL;
P
Per Liden 已提交
90 91 92
		table[i].lock = SPIN_LOCK_UNLOCKED;
		table[i].data.next_plus_upper = (start & ~index_mask) + i - 1;
	}
93 94 95 96 97
	tipc_ref_table.entries = table;
	tipc_ref_table.index_mask = index_mask;
	tipc_ref_table.first_free = sz - 1;
	tipc_ref_table.last_free = 1;
	write_unlock_bh(&ref_table_lock);
P
Per Liden 已提交
98 99 100 101
	return TIPC_OK;
}

/**
102
 * tipc_ref_table_stop - destroy reference table for objects
P
Per Liden 已提交
103 104
 */

105
void tipc_ref_table_stop(void)
P
Per Liden 已提交
106
{
107
	if (!tipc_ref_table.entries)
P
Per Liden 已提交
108 109
		return;

110
	vfree(tipc_ref_table.entries);
111
	tipc_ref_table.entries = NULL;
P
Per Liden 已提交
112 113 114
}

/**
115
 * tipc_ref_acquire - create reference to an object
P
Per Liden 已提交
116 117 118 119 120 121
 * 
 * Return a unique reference value which can be translated back to the pointer
 * 'object' at a later time.  Also, pass back a pointer to the lock protecting 
 * the object, but without locking it.
 */

122
u32 tipc_ref_acquire(void *object, spinlock_t **lock)
P
Per Liden 已提交
123 124 125 126 127 128 129
{
	struct reference *entry;
	u32 index;
	u32 index_mask;
	u32 next_plus_upper;
	u32 reference = 0;

130 131 132 133 134 135 136 137
	if (!object) {
		err("Attempt to acquire reference to non-existent object\n");
		return 0;
	}
	if (!tipc_ref_table.entries) {
		err("Reference table not found during acquisition attempt\n");
		return 0;
	}
P
Per Liden 已提交
138

139 140 141 142 143
	write_lock_bh(&ref_table_lock);
	if (tipc_ref_table.first_free) {
		index = tipc_ref_table.first_free;
		entry = &(tipc_ref_table.entries[index]);
		index_mask = tipc_ref_table.index_mask;
P
Per Liden 已提交
144 145 146
		/* take lock in case a previous user of entry still holds it */ 
		spin_lock_bh(&entry->lock);  
		next_plus_upper = entry->data.next_plus_upper;
147
		tipc_ref_table.first_free = next_plus_upper & index_mask;
P
Per Liden 已提交
148 149 150 151 152 153 154
		reference = (next_plus_upper & ~index_mask) + index;
		entry->data.reference = reference;
		entry->object = object;
                if (lock != 0)
                        *lock = &entry->lock;
		spin_unlock_bh(&entry->lock);
	}
155
	write_unlock_bh(&ref_table_lock);
P
Per Liden 已提交
156 157 158 159
	return reference;
}

/**
160
 * tipc_ref_discard - invalidate references to an object
P
Per Liden 已提交
161 162 163 164 165
 * 
 * Disallow future references to an object and free up the entry for re-use.
 * Note: The entry's spin_lock may still be busy after discard
 */

166
void tipc_ref_discard(u32 ref)
P
Per Liden 已提交
167 168 169 170 171
{
	struct reference *entry;
	u32 index; 
	u32 index_mask;

172 173 174 175 176 177 178 179
	if (!ref) {
		err("Attempt to discard reference 0\n");
		return;
	}
	if (!tipc_ref_table.entries) {
		err("Reference table not found during discard attempt\n");
		return;
	}
P
Per Liden 已提交
180

181 182
	write_lock_bh(&ref_table_lock);
	index_mask = tipc_ref_table.index_mask;
P
Per Liden 已提交
183
	index = ref & index_mask;
184
	entry = &(tipc_ref_table.entries[index]);
185 186 187 188 189 190 191 192 193

	if (!entry->object) {
		err("Attempt to discard reference to non-existent object\n");
		goto exit;
	}
	if (entry->data.reference != ref) {
		err("Attempt to discard non-existent reference\n");
		goto exit;
	}
P
Per Liden 已提交
194 195

	/* mark entry as unused */
196
	entry->object = NULL;
197 198
	if (tipc_ref_table.first_free == 0)
		tipc_ref_table.first_free = index;
P
Per Liden 已提交
199 200
	else
		/* next_plus_upper is always XXXX|0--0 for last free entry */
201
		tipc_ref_table.entries[tipc_ref_table.last_free].data.next_plus_upper 
P
Per Liden 已提交
202
			|= index;
203
	tipc_ref_table.last_free = index;
P
Per Liden 已提交
204 205 206

	/* increment upper bits of entry to invalidate subsequent references */
	entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
207
exit:
208
	write_unlock_bh(&ref_table_lock);
P
Per Liden 已提交
209 210
}