mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] flex_array: add helpers to get and put to make pointers easy to use
@ 2010-08-03 19:36 Eric Paris
  2010-08-03 19:53 ` Joe Perches
  2010-08-03 20:55 ` David Rientjes
  0 siblings, 2 replies; 5+ messages in thread
From: Eric Paris @ 2010-08-03 19:36 UTC (permalink / raw)
  To: linux-kernel, selinux; +Cc: dave, akpm, jmorris, sds

Getting and putting arrays of pointers with flex arrays is a PITA.  You
have to remember to pass &ptr to the _put and you have to do weird and
wacky casting to get the ptr back from the _get.  Add two functions
flex_array_get_ptr() and flex_array_put_ptr() to handle all of the magic.

Signed-off-by: Eric Paris <eparis@redhat.com>
---

 include/linux/flex_array.h |    6 ++++++
 lib/flex_array.c           |   25 ++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletions(-)

diff --git a/include/linux/flex_array.h b/include/linux/flex_array.h
index 1d747f7..f4e4eb2 100644
--- a/include/linux/flex_array.h
+++ b/include/linux/flex_array.h
@@ -70,4 +70,10 @@ int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
 void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
 int flex_array_shrink(struct flex_array *fa);
 
+#define flex_array_put_ptr(fa, nr, src, gfp)	({						\
+						 void *_src = (src);				\
+						 flex_array_put((fa), (nr), &_src, (gfp));	\
+						})
+void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr);
+
 #endif /* _FLEX_ARRAY_H */
diff --git a/lib/flex_array.c b/lib/flex_array.c
index 41b1804..77a6fea 100644
--- a/lib/flex_array.c
+++ b/lib/flex_array.c
@@ -171,6 +171,8 @@ __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
  * Note that this *copies* the contents of @src into
  * the array.  If you are trying to store an array of
  * pointers, make sure to pass in &ptr instead of ptr.
+ * You may instead wish to use the flex_array_put_ptr()
+ * helper function.
  *
  * Locking must be provided by the caller.
  */
@@ -265,7 +267,8 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
  *
  * Returns a pointer to the data at index @element_nr.  Note
  * that this is a copy of the data that was passed in.  If you
- * are using this to store pointers, you'll get back &ptr.
+ * are using this to store pointers, you'll get back &ptr.  You
+ * may instead wish to use the flex_array_get_ptr helper.
  *
  * Locking must be provided by the caller.
  */
@@ -286,6 +289,26 @@ void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
 	return &part->elements[index_inside_part(fa, element_nr)];
 }
 
+/**
+ * flex_array_get_ptr - pull a ptr back out of the array
+ * @fa:		the flex array from which to extract data
+ * @element_nr:	index of the element to fetch from the array
+ *
+ * Returns the pointer placed in the flex array at element_nr using
+ * flex_array_put_ptr().  This function should not be called if the
+ * element in question was not set using the _put_ptr() helper.
+ */
+void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
+{
+	void **tmp;
+
+	tmp = flex_array_get(fa, element_nr);
+	if (!tmp)
+		return NULL;
+
+	return *tmp;
+}
+
 static int part_is_free(struct flex_array_part *part)
 {
 	int i;


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] flex_array: add helpers to get and put to make pointers easy to use
  2010-08-03 19:36 [PATCH] flex_array: add helpers to get and put to make pointers easy to use Eric Paris
@ 2010-08-03 19:53 ` Joe Perches
  2010-08-03 20:55 ` David Rientjes
  1 sibling, 0 replies; 5+ messages in thread
From: Joe Perches @ 2010-08-03 19:53 UTC (permalink / raw)
  To: Eric Paris; +Cc: linux-kernel, selinux, dave, akpm, jmorris, sds

On Tue, 2010-08-03 at 15:36 -0400, Eric Paris wrote:
> Getting and putting arrays of pointers with flex arrays is a PITA.  You
> have to remember to pass &ptr to the _put and you have to do weird and
> wacky casting to get the ptr back from the _get.  Add two functions
> flex_array_get_ptr() and flex_array_put_ptr() to handle all of the magic.
> 
> Signed-off-by: Eric Paris <eparis@redhat.com>
> ---
> 
>  include/linux/flex_array.h |    6 ++++++
>  lib/flex_array.c           |   25 ++++++++++++++++++++++++-
>  2 files changed, 30 insertions(+), 1 deletions(-)
> 
> diff --git a/include/linux/flex_array.h b/include/linux/flex_array.h
> index 1d747f7..f4e4eb2 100644
> --- a/include/linux/flex_array.h
> +++ b/include/linux/flex_array.h
> @@ -70,4 +70,10 @@ int flex_array_clear(struct flex_array *fa, unsigned int element_nr);
>  void *flex_array_get(struct flex_array *fa, unsigned int element_nr);
>  int flex_array_shrink(struct flex_array *fa);
>  
> +#define flex_array_put_ptr(fa, nr, src, gfp)	({						\
> +						 void *_src = (src);				\
> +						 flex_array_put((fa), (nr), &_src, (gfp));	\
> +						})

Might be nicer to use fewer indents.
Isn't this equivalent?

#define flex_array_put_ptr(fa, nr, src, gfp)			\
	flex_array_put(fa, nr, &(void *)(src), gfp)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] flex_array: add helpers to get and put to make pointers easy to use
  2010-08-03 19:36 [PATCH] flex_array: add helpers to get and put to make pointers easy to use Eric Paris
  2010-08-03 19:53 ` Joe Perches
@ 2010-08-03 20:55 ` David Rientjes
  2010-08-03 22:05   ` Eric Paris
  1 sibling, 1 reply; 5+ messages in thread
From: David Rientjes @ 2010-08-03 20:55 UTC (permalink / raw)
  To: Eric Paris; +Cc: linux-kernel, selinux, dave, Andrew Morton, jmorris, sds

On Tue, 3 Aug 2010, Eric Paris wrote:

> Getting and putting arrays of pointers with flex arrays is a PITA.  You
> have to remember to pass &ptr to the _put and you have to do weird and
> wacky casting to get the ptr back from the _get.  Add two functions
> flex_array_get_ptr() and flex_array_put_ptr() to handle all of the magic.
> 

There's no code currently in the tree that uses the flex array interface, 
so is this something that you've encountered in practice or only by 
inspection?  If it's the former, is that code on its way for 2.6.36?

I'm hoping distros aren't creating internal dependencies on this interface 
that could practically be removed from upstream at any time given its lack 
of popularity.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] flex_array: add helpers to get and put to make pointers easy to use
  2010-08-03 20:55 ` David Rientjes
@ 2010-08-03 22:05   ` Eric Paris
  2010-08-03 22:43     ` David Rientjes
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Paris @ 2010-08-03 22:05 UTC (permalink / raw)
  To: David Rientjes; +Cc: linux-kernel, selinux, dave, Andrew Morton, jmorris, sds

On Tue, 2010-08-03 at 13:55 -0700, David Rientjes wrote:
> On Tue, 3 Aug 2010, Eric Paris wrote:
> 
> > Getting and putting arrays of pointers with flex arrays is a PITA.  You
> > have to remember to pass &ptr to the _put and you have to do weird and
> > wacky casting to get the ptr back from the _get.  Add two functions
> > flex_array_get_ptr() and flex_array_put_ptr() to handle all of the magic.
> > 
> 
> There's no code currently in the tree that uses the flex array interface, 
> so is this something that you've encountered in practice or only by 
> inspection?  If it's the former, is that code on its way for 2.6.36?

I did decide to use flex_arrays on code intended for .36
http://git.kernel.org/?p=linux/kernel/git/jmorris/security-testing-2.6.git;a=commitdiff;h=6371dcd36f649d9d07823f31400618155a20dde1

> I'm hoping distros aren't creating internal dependencies on this interface 
> that could practically be removed from upstream at any time given its lack 
> of popularity.

I've got some more patches that I'm looking towards .37 which would use
the _ptr helpers I suggested:

http://marc.info/?l=selinux&m=128086475312322&w=2
http://marc.info/?l=selinux&m=128086558513723&w=2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] flex_array: add helpers to get and put to make pointers easy to use
  2010-08-03 22:05   ` Eric Paris
@ 2010-08-03 22:43     ` David Rientjes
  0 siblings, 0 replies; 5+ messages in thread
From: David Rientjes @ 2010-08-03 22:43 UTC (permalink / raw)
  To: Eric Paris; +Cc: linux-kernel, selinux, dave, Andrew Morton, jmorris, sds

On Tue, 3 Aug 2010, Eric Paris wrote:

> > > Getting and putting arrays of pointers with flex arrays is a PITA.  You
> > > have to remember to pass &ptr to the _put and you have to do weird and
> > > wacky casting to get the ptr back from the _get.  Add two functions
> > > flex_array_get_ptr() and flex_array_put_ptr() to handle all of the magic.
> > > 
> > 
> > There's no code currently in the tree that uses the flex array interface, 
> > so is this something that you've encountered in practice or only by 
> > inspection?  If it's the former, is that code on its way for 2.6.36?
> 
> I did decide to use flex_arrays on code intended for .36
> http://git.kernel.org/?p=linux/kernel/git/jmorris/security-testing-2.6.git;a=commitdiff;h=6371dcd36f649d9d07823f31400618155a20dde1
> 

Great, a user!  I'm hoping that more people will adopt this interface but 
I can't think of anything that could currently be converted.  Maybe we can 
find opportunities in patches as they're proposed to the list.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-08-03 22:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-03 19:36 [PATCH] flex_array: add helpers to get and put to make pointers easy to use Eric Paris
2010-08-03 19:53 ` Joe Perches
2010-08-03 20:55 ` David Rientjes
2010-08-03 22:05   ` Eric Paris
2010-08-03 22:43     ` David Rientjes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®