diff options
author | Jeff Mahoney <jeffm@suse.com> | 2006-06-26 03:27:19 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-06-26 12:58:34 -0400 |
commit | 5806f07cd2c32920d5105e0f9ff3117338f34eec (patch) | |
tree | 65242ea629a19308656a3090fee6b513dfa0f723 /lib | |
parent | c51c2752491e5e771de6c8861a85ba46752d7888 (diff) |
[PATCH] lib: add idr_replace
This patch adds idr_replace() to replace an existing pointer in a single
operation.
Device-mapper will use this to update the pointer it stored against a given
id.
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/idr.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -29,6 +29,7 @@ | |||
29 | #include <linux/init.h> | 29 | #include <linux/init.h> |
30 | #include <linux/module.h> | 30 | #include <linux/module.h> |
31 | #endif | 31 | #endif |
32 | #include <linux/err.h> | ||
32 | #include <linux/string.h> | 33 | #include <linux/string.h> |
33 | #include <linux/idr.h> | 34 | #include <linux/idr.h> |
34 | 35 | ||
@@ -398,6 +399,48 @@ void *idr_find(struct idr *idp, int id) | |||
398 | } | 399 | } |
399 | EXPORT_SYMBOL(idr_find); | 400 | EXPORT_SYMBOL(idr_find); |
400 | 401 | ||
402 | /** | ||
403 | * idr_replace - replace pointer for given id | ||
404 | * @idp: idr handle | ||
405 | * @ptr: pointer you want associated with the id | ||
406 | * @id: lookup key | ||
407 | * | ||
408 | * Replace the pointer registered with an id and return the old value. | ||
409 | * A -ENOENT return indicates that @id was not found. | ||
410 | * A -EINVAL return indicates that @id was not within valid constraints. | ||
411 | * | ||
412 | * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove(). | ||
413 | */ | ||
414 | void *idr_replace(struct idr *idp, void *ptr, int id) | ||
415 | { | ||
416 | int n; | ||
417 | struct idr_layer *p, *old_p; | ||
418 | |||
419 | n = idp->layers * IDR_BITS; | ||
420 | p = idp->top; | ||
421 | |||
422 | id &= MAX_ID_MASK; | ||
423 | |||
424 | if (id >= (1 << n)) | ||
425 | return ERR_PTR(-EINVAL); | ||
426 | |||
427 | n -= IDR_BITS; | ||
428 | while ((n > 0) && p) { | ||
429 | p = p->ary[(id >> n) & IDR_MASK]; | ||
430 | n -= IDR_BITS; | ||
431 | } | ||
432 | |||
433 | n = id & IDR_MASK; | ||
434 | if (unlikely(p == NULL || !test_bit(n, &p->bitmap))) | ||
435 | return ERR_PTR(-ENOENT); | ||
436 | |||
437 | old_p = p->ary[n]; | ||
438 | p->ary[n] = ptr; | ||
439 | |||
440 | return old_p; | ||
441 | } | ||
442 | EXPORT_SYMBOL(idr_replace); | ||
443 | |||
401 | static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache, | 444 | static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache, |
402 | unsigned long flags) | 445 | unsigned long flags) |
403 | { | 446 | { |