Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 262304
b: refs/heads/master
c: 88eca02
h: refs/heads/master
v: v3
  • Loading branch information
Rusty Russell authored and Linus Torvalds committed Aug 4, 2011
1 parent 65f75a7 commit 60baf3c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: a7295898a1d2e501427f557111c2b4bdfc90b1ed
refs/heads/master: 88eca0207cf1574328c3ce8c3be537a9317261bb
4 changes: 4 additions & 0 deletions trunk/include/linux/idr.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ void ida_remove(struct ida *ida, int id);
void ida_destroy(struct ida *ida);
void ida_init(struct ida *ida);

int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
gfp_t gfp_mask);
void ida_simple_remove(struct ida *ida, unsigned int id);

void __init idr_init_cache(void);

#endif /* __IDR_H__ */
67 changes: 67 additions & 0 deletions trunk/lib/idr.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
#include <linux/err.h>
#include <linux/string.h>
#include <linux/idr.h>
#include <linux/spinlock.h>

static struct kmem_cache *idr_layer_cache;
static DEFINE_SPINLOCK(simple_ida_lock);

static struct idr_layer *get_from_free_list(struct idr *idp)
{
Expand Down Expand Up @@ -925,6 +927,71 @@ void ida_destroy(struct ida *ida)
}
EXPORT_SYMBOL(ida_destroy);

/**
* ida_simple_get - get a new id.
* @ida: the (initialized) ida.
* @start: the minimum id (inclusive, < 0x8000000)
* @end: the maximum id (exclusive, < 0x8000000 or 0)
* @gfp_mask: memory allocation flags
*
* Allocates an id in the range start <= id < end, or returns -ENOSPC.
* On memory allocation failure, returns -ENOMEM.
*
* Use ida_simple_remove() to get rid of an id.
*/
int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
gfp_t gfp_mask)
{
int ret, id;
unsigned int max;

BUG_ON((int)start < 0);
BUG_ON((int)end < 0);

if (end == 0)
max = 0x80000000;
else {
BUG_ON(end < start);
max = end - 1;
}

again:
if (!ida_pre_get(ida, gfp_mask))
return -ENOMEM;

spin_lock(&simple_ida_lock);
ret = ida_get_new_above(ida, start, &id);
if (!ret) {
if (id > max) {
ida_remove(ida, id);
ret = -ENOSPC;
} else {
ret = id;
}
}
spin_unlock(&simple_ida_lock);

if (unlikely(ret == -EAGAIN))
goto again;

return ret;
}
EXPORT_SYMBOL(ida_simple_get);

/**
* ida_simple_remove - remove an allocated id.
* @ida: the (initialized) ida.
* @id: the id returned by ida_simple_get.
*/
void ida_simple_remove(struct ida *ida, unsigned int id)
{
BUG_ON((int)id < 0);
spin_lock(&simple_ida_lock);
ida_remove(ida, id);
spin_unlock(&simple_ida_lock);
}
EXPORT_SYMBOL(ida_simple_remove);

/**
* ida_init - initialize ida handle
* @ida: ida handle
Expand Down

0 comments on commit 60baf3c

Please sign in to comment.