From: Trent Piepho <xyzzy@speakeasy.org>
To: "Bruno Prémont" <bonbons@linux-vserver.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Arjan van de Ven <arjan@infradead.org>,
JosephChan@via.com.tw, linux-fbdev-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Fix crash in viafb due to 4k stack overflow
Date: Sun, 9 Nov 2008 14:57:59 -0800 (PST) [thread overview]
Message-ID: <Pine.LNX.4.64.0811091439310.22998@t2.domain.actdsltmp> (raw)
In-Reply-To: <20081109223748.3182e31d@neptune.home>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: TEXT/PLAIN; charset=X-UNKNOWN, Size: 1678 bytes --]
On Sun, 9 Nov 2008, Bruno Prémont wrote:
> What is preferred, allocating a big block of memory and point various
> variables inside that block or do multiple individual allocations?
>
> e.g.
> u8 data[CURSOR_SIZE/8]
> u32 data_bak[CURSOR_SIZE/32]
> =>
> u8 *data = kzalloc(...)
> u32 *data_bak = kzalloc(...)
> or
> u8 *data = kzalloc(CURSOR_SIZE/8 + CURSOR_SIZE/32, ...)
> u32 *data_bak = (u32*)(data+CURSOR_SIZE/8);
>
> First option is more readable, second should be more efficient...
I like this method:
foo()
{
/* anonymous struct, you don't need to think of a name */
struct {
u8 data[CURSOR_SIZE/8];
u32 data_bak[CURSOR_SIZE/32];
} *cursor;
cursor = kzalloc(sizeof(*cursor), ...);
/* for remaining code:
* s/data/cursor->data/
* s/data_bak/cursor->data_bak/
*/
free(cursor);
}
A number of advantages over multiple allocations:
The alloc code and free code only has to run once.
Likely less memory will be allocated, due to allocation granularity.
Only one pointer is needed to keep track of the allocations instead of two.
This frees up a pointer's worth of stack space and/or another register for
the compiler to use.
More readable than u32 *data_bak = (u32*)(data+CURSOR_SIZE/8) and so on.
If you check for kzalloc failure, you only need code to check once. And
you can avoid the need for rolling back the first allocation if the second
fails.
The disadvantage is that you can't free just one of the allocations and big
allocations are harder to satisfy than small ones. When you get up to the
megabyte range, combining allocations into bigger ones becomes a bad idea.
next prev parent reply other threads:[~2008-11-09 22:57 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-11-09 19:25 [PATCH] Fix crash in viafb due to 4k stack overflow Bruno Prémont
2008-11-09 19:36 ` Andrew Morton
2008-11-09 20:25 ` Arjan van de Ven
2008-11-09 20:38 ` Bruno Prémont
2008-11-09 20:55 ` Andrew Morton
2008-11-09 21:37 ` Bruno Prémont
2008-11-09 22:57 ` Trent Piepho [this message]
2008-11-09 22:59 ` Andrew Morton
2008-11-10 21:00 ` Bruno Prémont
2008-11-12 23:01 ` Andrew Morton
2008-11-13 0:58 ` JosephChan
-- strict thread matches above, loose matches on Subject: below --
2008-11-14 8:41 JosephChan
2008-11-14 9:01 ` Bruno Prémont
2008-11-14 10:20 ` JosephChan
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.0811091439310.22998@t2.domain.actdsltmp \
--to=xyzzy@speakeasy.org \
--cc=JosephChan@via.com.tw \
--cc=akpm@linux-foundation.org \
--cc=arjan@infradead.org \
--cc=bonbons@linux-vserver.org \
--cc=linux-fbdev-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.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