* [PATCH 01/08] idr: add idr_replace method for replacing pointers
@ 2006-04-13 20:35 Jeff Mahoney
2006-04-13 20:44 ` Jeff Mahoney
0 siblings, 1 reply; 5+ messages in thread
From: Jeff Mahoney @ 2006-04-13 20:35 UTC (permalink / raw)
To: Linux Kernel Mailing List; +Cc: Andrew Morton, Linus Torvalds
This patch adds an idr_replace() method to the IDR library for replacing
a pointer already present in the IDR. Rather than do a remove/add pair, this
function simply updates the pointer for an ID.
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
--
include/linux/idr.h | 1 +
lib/idr.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff -ruNpX ../dontdiff linux-2.6.16-staging1/include/linux/idr.h linux-2.6.16-staging2/include/linux/idr.h
--- linux-2.6.16-staging1/include/linux/idr.h 2006-01-02 22:21:10.000000000 -0500
+++ linux-2.6.16-staging2/include/linux/idr.h 2006-04-13 16:18:14.000000000 -0400
@@ -78,6 +78,7 @@ void *idr_find(struct idr *idp, int id);
int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
int idr_get_new(struct idr *idp, void *ptr, int *id);
int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
+int idr_replace(struct idr *idp, void *ptr, int id);
void idr_remove(struct idr *idp, int id);
void idr_destroy(struct idr *idp);
void idr_init(struct idr *idp);
diff -ruNpX ../dontdiff linux-2.6.16-staging1/lib/idr.c linux-2.6.16-staging2/lib/idr.c
--- linux-2.6.16-staging1/lib/idr.c 2006-01-02 22:21:10.000000000 -0500
+++ linux-2.6.16-staging2/lib/idr.c 2006-04-13 16:18:14.000000000 -0400
@@ -390,6 +390,43 @@ void *idr_find(struct idr *idp, int id)
}
EXPORT_SYMBOL(idr_find);
+/**
+ * idr_replace - replace pointer for given id
+ * @idp: idr handle
+ * @ptr: pointer you want associated with the ide
+ * @id: lookup key
+ *
+ * Replace the pointer registered with the id. A -ENOENT
+ * return indicates that @id is not found.
+ *
+ * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
+ */
+int idr_replace(struct idr *idp, void *ptr, int id)
+{
+ int n;
+ struct idr_layer *p;
+ int shift = (idp->layers - 1) * IDR_BITS;
+
+ n = idp->layers * IDR_BITS;
+ p = idp->top;
+
+ id &= MAX_ID_MASK;
+
+ while ((shift > 0) && p) {
+ n = (id >> shift) & IDR_MASK;
+ p = p->ary[n];
+ shift -= IDR_BITS;
+ }
+
+ n = id & IDR_MASK;
+ if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
+ return -ENOENT;
+
+ p->ary[n] = ptr;
+ return 0;
+}
+EXPORT_SYMBOL(idr_replace);
+
static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache,
unsigned long flags)
{
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 01/08] idr: add idr_replace method for replacing pointers
@ 2006-04-13 20:35 Jeff Mahoney
2006-04-13 22:05 ` Andrew Morton
0 siblings, 1 reply; 5+ messages in thread
From: Jeff Mahoney @ 2006-04-13 20:35 UTC (permalink / raw)
To: Linux Kernel Mailing List; +Cc: Andrew Morton, Linus Torvalds
This patch adds an idr_replace() method to the IDR library for replacing
a pointer already present in the IDR. Rather than do a remove/add pair, this
function simply updates the pointer for an ID.
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
--
include/linux/idr.h | 1 +
lib/idr.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff -ruNpX ../dontdiff linux-2.6.16-staging1/include/linux/idr.h linux-2.6.16-staging2/include/linux/idr.h
--- linux-2.6.16-staging1/include/linux/idr.h 2006-01-02 22:21:10.000000000 -0500
+++ linux-2.6.16-staging2/include/linux/idr.h 2006-04-13 16:18:14.000000000 -0400
@@ -78,6 +78,7 @@ void *idr_find(struct idr *idp, int id);
int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
int idr_get_new(struct idr *idp, void *ptr, int *id);
int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
+int idr_replace(struct idr *idp, void *ptr, int id);
void idr_remove(struct idr *idp, int id);
void idr_destroy(struct idr *idp);
void idr_init(struct idr *idp);
diff -ruNpX ../dontdiff linux-2.6.16-staging1/lib/idr.c linux-2.6.16-staging2/lib/idr.c
--- linux-2.6.16-staging1/lib/idr.c 2006-01-02 22:21:10.000000000 -0500
+++ linux-2.6.16-staging2/lib/idr.c 2006-04-13 16:18:14.000000000 -0400
@@ -390,6 +390,43 @@ void *idr_find(struct idr *idp, int id)
}
EXPORT_SYMBOL(idr_find);
+/**
+ * idr_replace - replace pointer for given id
+ * @idp: idr handle
+ * @ptr: pointer you want associated with the ide
+ * @id: lookup key
+ *
+ * Replace the pointer registered with the id. A -ENOENT
+ * return indicates that @id was not found.
+ *
+ * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
+ */
+int idr_replace(struct idr *idp, void *ptr, int id)
+{
+ int n;
+ struct idr_layer *p;
+ int shift = (idp->layers - 1) * IDR_BITS;
+
+ n = idp->layers * IDR_BITS;
+ p = idp->top;
+
+ id &= MAX_ID_MASK;
+
+ while ((shift > 0) && p) {
+ n = (id >> shift) & IDR_MASK;
+ p = p->ary[n];
+ shift -= IDR_BITS;
+ }
+
+ n = id & IDR_MASK;
+ if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
+ return -ENOENT;
+
+ p->ary[n] = ptr;
+ return 0;
+}
+EXPORT_SYMBOL(idr_replace);
+
static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache,
unsigned long flags)
{
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 01/08] idr: add idr_replace method for replacing pointers
2006-04-13 20:35 [PATCH 01/08] idr: add idr_replace method for replacing pointers Jeff Mahoney
@ 2006-04-13 20:44 ` Jeff Mahoney
0 siblings, 0 replies; 5+ messages in thread
From: Jeff Mahoney @ 2006-04-13 20:44 UTC (permalink / raw)
To: Jeff Mahoney; +Cc: Linux Kernel Mailing List, Andrew Morton, Linus Torvalds
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jeff Mahoney wrote:
> --- linux-2.6.16-staging1/lib/idr.c 2006-01-02 22:21:10.000000000 -0500
> +++ linux-2.6.16-staging2/lib/idr.c 2006-04-13 16:18:14.000000000 -0400
> @@ -390,6 +390,43 @@ void *idr_find(struct idr *idp, int id)
> }
> EXPORT_SYMBOL(idr_find);
>
> +/**
> + * idr_replace - replace pointer for given id
> + * @idp: idr handle
> + * @ptr: pointer you want associated with the ide
> + * @id: lookup key
> + *
> + * Replace the pointer registered with the id. A -ENOENT
> + * return indicates that @id is not found.
This was accidentally sent. It's a vim backup file. Just a grammar
change from the one that I'm not replying to. Sorry for the confusion.
- -Jeff
- --
Jeff Mahoney
SUSE Labs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEPrgpLPWxlyuTD7IRAlp1AJsGHFot4YULH2+wJd02zJ3H1HxWqwCfT/r3
aUvCCekPGHiQfaZXyOTx6oM=
=tFd9
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 01/08] idr: add idr_replace method for replacing pointers
2006-04-13 20:35 Jeff Mahoney
@ 2006-04-13 22:05 ` Andrew Morton
2006-04-14 0:08 ` Jeff Mahoney
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2006-04-13 22:05 UTC (permalink / raw)
To: Jeff Mahoney; +Cc: linux-kernel, torvalds
Jeff Mahoney <jeffm@suse.com> wrote:
>
> +/**
> + * idr_replace - replace pointer for given id
> + * @idp: idr handle
> + * @ptr: pointer you want associated with the ide
> + * @id: lookup key
> + *
> + * Replace the pointer registered with the id. A -ENOENT
> + * return indicates that @id was not found.
> + *
> + * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
> + */
> +int idr_replace(struct idr *idp, void *ptr, int id)
> +{
> + int n;
> + struct idr_layer *p;
> + int shift = (idp->layers - 1) * IDR_BITS;
> +
> + n = idp->layers * IDR_BITS;
> + p = idp->top;
> +
> + id &= MAX_ID_MASK;
> +
> + while ((shift > 0) && p) {
> + n = (id >> shift) & IDR_MASK;
> + p = p->ary[n];
> + shift -= IDR_BITS;
> + }
> +
> + n = id & IDR_MASK;
> + if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
> + return -ENOENT;
> +
> + p->ary[n] = ptr;
> + return 0;
> +}
I'd have thought it would be more flexible were this to return the old
pointer.
If there was no old item, we could return NULL and "succeed". But that gets
a bit ill-defined, because lack of an old pointer can occur if either a)
there was a layer, but the slot was empty or b) there wasn't a layer for
this new item. So perhaps it's best to continue considering lack of an old
pointer as an error, with ERR_PTR(-ENOENT).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 01/08] idr: add idr_replace method for replacing pointers
2006-04-13 22:05 ` Andrew Morton
@ 2006-04-14 0:08 ` Jeff Mahoney
0 siblings, 0 replies; 5+ messages in thread
From: Jeff Mahoney @ 2006-04-14 0:08 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, torvalds
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Andrew Morton wrote:
> Jeff Mahoney <jeffm@suse.com> wrote:
>> +/**
>> + * idr_replace - replace pointer for given id
>> + * @idp: idr handle
>> + * @ptr: pointer you want associated with the ide
>> + * @id: lookup key
>> + *
>> + * Replace the pointer registered with the id. A -ENOENT
>> + * return indicates that @id was not found.
>> + *
>> + * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
>> + */
>> +int idr_replace(struct idr *idp, void *ptr, int id)
>
> I'd have thought it would be more flexible were this to return the old
> pointer.
>
> If there was no old item, we could return NULL and "succeed". But that gets
> a bit ill-defined, because lack of an old pointer can occur if either a)
> there was a layer, but the slot was empty or b) there wasn't a layer for
> this new item. So perhaps it's best to continue considering lack of an old
> pointer as an error, with ERR_PTR(-ENOENT).
Sure, I'll make those changes now.
- -Jeff
- --
Jeff Mahoney
SUSE Labs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEPugXLPWxlyuTD7IRAswwAJ0XVEBu/kRp4RNcW3JNeNRTqCYEowCfR01q
0hPOY5g0V8WEaOU8lWBfG/U=
=iJrK
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-04-14 0:09 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-13 20:35 [PATCH 01/08] idr: add idr_replace method for replacing pointers Jeff Mahoney
2006-04-13 20:44 ` Jeff Mahoney
-- strict thread matches above, loose matches on Subject: below --
2006-04-13 20:35 Jeff Mahoney
2006-04-13 22:05 ` Andrew Morton
2006-04-14 0:08 ` Jeff Mahoney
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox