summaryrefslogtreecommitdiff
path: root/malloc.c
diff options
context:
space:
mode:
authorHans Boehm <Hans.Boehm@hp.com>2012-03-15 21:09:05 +0400
committerIvan Maidanski <ivmai@mail.ru>2012-03-15 21:09:05 +0400
commit83231d0ab5ed60015797c3d1ad9056295ac3b2bb (patch)
tree7038b9ba4c272b4f87d65068aaae8e3012fc3d22 /malloc.c
parent6a93f8e5bcad22137f41b6c60a1c7384baaec2b3 (diff)
downloadbdwgc-83231d0ab5ed60015797c3d1ad9056295ac3b2bb.tar.gz
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 cb49a5cf..c9b9eb6a 100644
--- a/malloc.c
+++ b/malloc.c
@@ -381,9 +381,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 */