mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christoph Lameter <clameter@sgi.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	akpm@linux-foundation.org, torvalds@linux-foundation.org
Subject: Re: [RFC PATCH] type safe allocator
Date: Wed, 1 Aug 2007 22:33:07 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0708012223520.3265@schroedinger.engr.sgi.com> (raw)
In-Reply-To: <E1IGAAI-0006K6-00@dorka.pomaz.szeredi.hu>

On Wed, 1 Aug 2007, Miklos Szeredi wrote:

> I wonder why we don't have type safe object allocators a-la new() in
> C++ or g_new() in glib?
> 
>   fooptr = k_new(struct foo, GFP_KERNEL);
> 
> is nicer and more descriptive than
> 
>   fooptr = kmalloc(sizeof(*fooptr), GFP_KERNEL);
> 
> and more safe than
> 
>   fooptr = kmalloc(sizeof(struct foo), GFP_KERNEL);
> 
> And we have zillions of both variants.

Hmmm yes I think that would be good. However, please clean up the naming.
The variant on zeroing on zering get to be too much.

> + * k_new - allocate given type object
> + * @type: the type of the object to allocate
> + * @flags: the type of memory to allocate.
> + */
> +#define k_new(type, flags) ((type *) kmalloc(sizeof(type), flags))

kalloc?

> +
> + * k_new0 - allocate given type object, zero out allocated space
> + * @type: the type of the object to allocate
> + * @flags: the type of memory to allocate.
> + */
> +#define k_new0(type, flags) ((type *) kzalloc(sizeof(type), flags))

A new notation for zeroing! This is equivalent to

kalloc(type, flags | __GFP_ZERO)

maybe define new GFP_xxx instead?

> +/**
> + * k_new_array - allocate array of given type object
> + * @type: the type of the object to allocate
> + * @len: the length of the array
> + * @flags: the type of memory to allocate.
> + */
> +#define k_new_array(type, len, flags) \
> +	((type *) kmalloc(sizeof(type) * (len), flags))

We already have array initializations using kcalloc.

> +#define k_new0_array(type, len, flags) \
> +	((type *) kzalloc(sizeof(type) * (len), flags))

Same as before.


I do not see any _node variants?

How about the following minimal set


kmalloc(size, flags)		kalloc(struct, flags)
kmalloc_node(size, flags, node)	kalloc_node(struct, flags, node)


The array variants translate into kmalloc anyways and are used
in an inconsistent manner. Sometime this way sometimes the other. Leave 
them?

	kcalloc(n, size, flags) == kmalloc(size, flags)

Then kzalloc is equivalent to adding the __GFP_ZERO flag. Thus

	kzalloc(size, flags) == kmalloc(size, flags | __GFPZERO)

If you define a new flag like GFP_ZERO_ATOMIC and GFP_ZERO_KERNEL you 
could do

	kalloc(struct, GFP_ZERO_KERNEL)

instead of adding new variants?


  parent reply	other threads:[~2007-08-02  5:33 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-01  9:06 Miklos Szeredi
2007-08-01  9:29 ` Adrian Bunk
2007-08-01  9:41   ` Miklos Szeredi
2007-08-01 10:44 ` Andi Kleen
2007-08-01  9:57   ` Miklos Szeredi
2007-08-01 11:34     ` Andi Kleen
2007-08-01 10:45       ` Miklos Szeredi
2007-08-01 11:44         ` Jan Engelhardt
2007-08-01 11:56           ` Miklos Szeredi
2007-08-02  3:56 ` Linus Torvalds
2007-08-02  7:27   ` Miklos Szeredi
2007-08-02 12:05     ` Al Viro
2007-08-02 13:05       ` Miklos Szeredi
2007-08-02 17:23     ` Linus Torvalds
2007-08-02  5:33 ` Christoph Lameter [this message]
2007-08-02  7:38   ` Miklos Szeredi
2007-08-02 19:16     ` Christoph Lameter
2007-08-02  7:37 ` Satyam Sharma
2007-08-02  7:40   ` Miklos Szeredi
2007-08-02 11:31 ` [PATCH] " Miklos Szeredi
2007-08-02 12:04   ` Alexey Dobriyan
2007-08-02 12:24     ` Jan Engelhardt
2007-08-02 13:06       ` Miklos Szeredi
2007-08-02 13:35         ` Jan Engelhardt
2007-08-02 13:49           ` Miklos Szeredi
2007-08-02 13:47     ` Peter Zijlstra
2007-08-02 14:06       ` Bernd Petrovitsch
2007-08-02 14:08         ` Jan Engelhardt
2007-08-02 18:36   ` Andrew Morton
2007-08-02 18:48     ` Miklos Szeredi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0708012223520.3265@schroedinger.engr.sgi.com \
    --to=clameter@sgi.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=miklos@szeredi.hu \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®