summaryrefslogtreecommitdiff
path: root/malloc.c
diff options
context:
space:
mode:
authorHans Boehm <Hans.Boehm@hp.com>2012-03-15 18:09:05 +0100
committerIvan Maidanski <ivmai@mail.ru>2012-03-15 18:19:23 +0100
commit61efdf6905c09a12e1a62fad44b2ab5e20fd9c4d (patch)
tree1cdc31990df29844ff11c1e3e7d99ce0a26b3e9a /malloc.c
parent34088e4d72b2ee83fceaa1688b0972b3884b63d3 (diff)
Speedup calloc size overflow check by preventing division if small values
* malloc.c (GC_SQRT_SIZE_MAX): New macro. * malloc.c (calloc): Add fast initial size overflow check to avoid integer division for reasonably small values passed.
Diffstat (limited to 'malloc.c')
-rw-r--r--malloc.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/malloc.c b/malloc.c
index e99e306..2389fd0 100644
--- a/malloc.c
+++ b/malloc.c
@@ -378,9 +378,12 @@ void * malloc(size_t lb)
# define GC_SIZE_MAX (~(size_t)0)
#endif
+#define GC_SQRT_SIZE_MAX ((1U << (WORDSZ / 2)) - 1)
+
void * calloc(size_t n, size_t lb)
{
- if (lb && n > GC_SIZE_MAX / lb)
+ if ((lb | n) > GC_SQRT_SIZE_MAX /* fast initial test */
+ && lb && n > GC_SIZE_MAX / lb)
return NULL;
# if defined(GC_LINUX_THREADS) /* && !defined(USE_PROC_FOR_LIBRARIES) */
/* libpthread allocated some memory that is only pointed to by */