Subversion Repositories Kolibri OS

Rev

Rev 1967 | Rev 4065 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1408 serge 1
/*
2
 * include/linux/idr.h
3
 *
4
 * 2002-10-18  written by Jim Houston jim.houston@ccur.com
5
 *	Copyright (C) 2002 by Concurrent Computer Corporation
6
 *	Distributed under the GNU GPL license version 2.
7
 *
8
 * Small id to pointer translation service avoiding fixed sized
9
 * tables.
10
 */
11
 
12
#ifndef __IDR_H__
13
#define __IDR_H__
14
 
3391 Serge 15
#include 
1964 serge 16
#include 
1967 serge 17
#include 
1964 serge 18
#include 
1408 serge 19
//#include 
20
//#include 
3391 Serge 21
#include 
22
#include 
23
#include 
1408 serge 24
 
25
 
3391 Serge 26
/*
27
 * We want shallower trees and thus more bits covered at each layer.  8
28
 * bits gives us large enough first layer for most use cases and maximum
29
 * tree depth of 4.  Each idr_layer is slightly larger than 2k on 64bit and
30
 * 1k on 32bit.
31
 */
32
#define IDR_BITS 8
1408 serge 33
#define IDR_SIZE (1 << IDR_BITS)
34
#define IDR_MASK ((1 << IDR_BITS)-1)
35
 
36
struct idr_layer {
3391 Serge 37
	int			prefix;	/* the ID prefix of this idr_layer */
38
	DECLARE_BITMAP(bitmap, IDR_SIZE); /* A zero bit means "space here" */
1964 serge 39
	struct idr_layer __rcu	*ary[1<
1408 serge 40
	int			 count;	 /* When zero, we can release it */
41
	int			 layer;	 /* distance from leaf */
42
    struct rcu_head      rcu_head;
43
};
44
 
45
struct idr {
3391 Serge 46
	struct idr_layer __rcu	*hint;	/* the last layer allocated from */
1964 serge 47
	struct idr_layer __rcu *top;
1408 serge 48
	struct idr_layer *id_free;
3391 Serge 49
	int			layers;	/* only valid w/o concurrent changes */
1408 serge 50
	int		  id_free_cnt;
3391 Serge 51
	spinlock_t		lock;
1408 serge 52
};
53
 
54
#define IDR_INIT(name)						\
55
{								\
3391 Serge 56
	.lock			= __SPIN_LOCK_UNLOCKED(name.lock),	\
1408 serge 57
}
58
#define DEFINE_IDR(name)	struct idr name = IDR_INIT(name)
59
 
60
/**
1964 serge 61
 * DOC: idr sync
1408 serge 62
 * idr synchronization (stolen from radix-tree.h)
63
 *
64
 * idr_find() is able to be called locklessly, using RCU. The caller must
65
 * ensure calls to this function are made within rcu_read_lock() regions.
66
 * Other readers (lock-free or otherwise) and modifications may be running
67
 * concurrently.
68
 *
69
 * It is still required that the caller manage the synchronization and
70
 * lifetimes of the items. So if RCU lock-free lookups are used, typically
71
 * this would mean that the items have their own locks, or are amenable to
72
 * lock-free access; and that the items are freed by RCU (or only freed after
73
 * having been deleted from the idr tree *and* a synchronize_rcu() grace
74
 * period).
75
 */
76
 
77
/*
78
 * This is what we export.
79
 */
80
 
3391 Serge 81
void *idr_find_slowpath(struct idr *idp, int id);
1964 serge 82
int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
1408 serge 83
int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
3391 Serge 84
void idr_preload(gfp_t gfp_mask);
85
int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask);
1408 serge 86
int idr_for_each(struct idr *idp,
87
		 int (*fn)(int id, void *p, void *data), void *data);
88
void *idr_get_next(struct idr *idp, int *nextid);
89
void *idr_replace(struct idr *idp, void *ptr, int id);
90
void idr_remove(struct idr *idp, int id);
3391 Serge 91
void idr_free(struct idr *idp, int id);
1408 serge 92
void idr_destroy(struct idr *idp);
93
void idr_init(struct idr *idp);
94
 
3391 Serge 95
/**
96
 * idr_preload_end - end preload section started with idr_preload()
97
 *
98
 * Each idr_preload() should be matched with an invocation of this
99
 * function.  See idr_preload() for details.
100
 */
101
static inline void idr_preload_end(void)
102
{
103
//	preempt_enable();
104
}
1408 serge 105
 
3391 Serge 106
/**
107
 * idr_find - return pointer for given id
108
 * @idp: idr handle
109
 * @id: lookup key
110
 *
111
 * Return the pointer given the id it has been registered with.  A %NULL
112
 * return indicates that @id is not valid or you passed %NULL in
113
 * idr_get_new().
114
 *
115
 * This function can be called under rcu_read_lock(), given that the leaf
116
 * pointers lifetimes are correctly managed.
117
 */
118
static inline void *idr_find(struct idr *idr, int id)
119
{
120
	struct idr_layer *hint = rcu_dereference_raw(idr->hint);
121
 
122
	if (hint && (id & ~IDR_MASK) == hint->prefix)
123
		return rcu_dereference_raw(hint->ary[id & IDR_MASK]);
124
 
125
	return idr_find_slowpath(idr, id);
126
}
127
 
128
/**
129
 * idr_get_new - allocate new idr entry
130
 * @idp: idr handle
131
 * @ptr: pointer you want associated with the id
132
 * @id: pointer to the allocated handle
133
 *
134
 * Simple wrapper around idr_get_new_above() w/ @starting_id of zero.
135
 */
136
static inline int idr_get_new(struct idr *idp, void *ptr, int *id)
137
{
138
	return idr_get_new_above(idp, ptr, 0, id);
139
}
140
 
141
/**
142
 * idr_for_each_entry - iterate over an idr's elements of a given type
143
 * @idp:     idr handle
144
 * @entry:   the type * to use as cursor
145
 * @id:      id entry's key
146
 */
147
#define idr_for_each_entry(idp, entry, id)				\
148
	for (id = 0, entry = (typeof(entry))idr_get_next((idp), &(id)); \
149
	     entry != NULL;                                             \
150
	     ++id, entry = (typeof(entry))idr_get_next((idp), &(id)))
151
 
152
void __idr_remove_all(struct idr *idp);	/* don't use */
153
 
154
/**
155
 * idr_remove_all - remove all ids from the given idr tree
156
 * @idp: idr handle
157
 *
158
 * If you're trying to destroy @idp, calling idr_destroy() is enough.
159
 * This is going away.  Don't use.
160
 */
161
static inline void __deprecated idr_remove_all(struct idr *idp)
162
{
163
	__idr_remove_all(idp);
164
}
165
 
1408 serge 166
/*
167
 * IDA - IDR based id allocator, use when translation from id to
168
 * pointer isn't necessary.
1964 serge 169
 *
170
 * IDA_BITMAP_LONGS is calculated to be one less to accommodate
171
 * ida_bitmap->nr_busy so that the whole struct fits in 128 bytes.
1408 serge 172
 */
173
#define IDA_CHUNK_SIZE		128	/* 128 bytes per chunk */
1964 serge 174
#define IDA_BITMAP_LONGS	(IDA_CHUNK_SIZE / sizeof(long) - 1)
1408 serge 175
#define IDA_BITMAP_BITS		(IDA_BITMAP_LONGS * sizeof(long) * 8)
176
 
177
struct ida_bitmap {
178
	long			nr_busy;
179
	unsigned long		bitmap[IDA_BITMAP_LONGS];
180
};
181
 
182
struct ida {
183
	struct idr		idr;
184
	struct ida_bitmap	*free_bitmap;
185
};
186
 
3391 Serge 187
#define IDA_INIT(name)		{ .idr = IDR_INIT((name).idr), .free_bitmap = NULL, }
1408 serge 188
#define DEFINE_IDA(name)	struct ida name = IDA_INIT(name)
189
 
1964 serge 190
int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
1408 serge 191
int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
192
void ida_remove(struct ida *ida, int id);
193
void ida_destroy(struct ida *ida);
194
void ida_init(struct ida *ida);
195
 
3391 Serge 196
void __init idr_init_cache(void);
1408 serge 197
 
3391 Serge 198
 
199
 
1408 serge 200
#endif /* __IDR_H__ */