public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dave Jones <davej@redhat.com>
To: dean gaudet <dean-list-linux-kernel@arctic.org>
Cc: Pekka Enberg <penberg@cs.helsinki.fi>,
	arjanv@redhat.com, Chris Wright <chrisw@osdl.org>,
	linux-kernel@vger.kernel.org, tiwai@suse.de
Subject: Re: [PATCH] ALSA: Remove subsystem-specific malloc (1/8)
Date: Thu, 10 Jun 2004 13:32:35 +0100	[thread overview]
Message-ID: <20040610123235.GA28923@redhat.com> (raw)
In-Reply-To: <Pine.LNX.4.58.0406092133300.19296@twinlark.arctic.org>

On Wed, Jun 09, 2004 at 10:08:34PM -0700, dean gaudet wrote:

 > > +void *kcalloc(size_t n, size_t size, int flags)
 > > +{
 > > +	if (n != 0 && size > INT_MAX / n)
 > > +		return NULL;
 > 
 > division isn't very efficient :)  it's typically a 40+ cycle operation.
 >
 > there's almost certainly more efficient ways to do this with per-target
 > assembly... but you should probably just crib the code from glibc which
 > avoids division for small allocations:
 > 
 >   /* size_t is unsigned so the behavior on overflow is defined.  */
 >   bytes = n * elem_size;
 > #define HALF_INTERNAL_SIZE_T \
 >   (((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2))
 >   if (__builtin_expect ((n | elem_size) >= HALF_INTERNAL_SIZE_T, 0)) {
 >     if (elem_size != 0 && bytes / elem_size != n) {
 >       MALLOC_FAILURE_ACTION;
 >       return 0;
 >     }
 >   }

Considering this isn't exactly a cycle-critical piece of code,
I'd choose readability over saving 40 cycles which is probably
lost in the noise of every other operation we do in that allocation.

		Dave


      reply	other threads:[~2004-06-10 12:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-08 21:24 [PATCH] ALSA: Remove subsystem-specific malloc (1/8) Pekka J Enberg
2004-06-09 18:34 ` Chris Wright
2004-06-09 20:13   ` Pekka Enberg
2004-06-09 20:21     ` Arjan van de Ven
2004-06-09 20:40       ` Valdis.Kletnieks
2004-06-09 20:57       ` Chris Wright
2004-06-09 20:57       ` Pekka Enberg
2004-06-09 20:59         ` Arjan van de Ven
2004-06-09 21:07           ` Pekka Enberg
2004-06-11 10:09             ` Takashi Iwai
2004-06-11 10:43               ` Pekka J Enberg
2004-06-11 10:53               ` Pekka J Enberg
2004-06-09 21:00         ` [PATCH] " Chris Wright
2004-06-09 21:18           ` Pekka Enberg
2004-06-10  5:08         ` dean gaudet
2004-06-10 12:32           ` Dave Jones [this message]

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=20040610123235.GA28923@redhat.com \
    --to=davej@redhat.com \
    --cc=arjanv@redhat.com \
    --cc=chrisw@osdl.org \
    --cc=dean-list-linux-kernel@arctic.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penberg@cs.helsinki.fi \
    --cc=tiwai@suse.de \
    /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