develooper Front page | perl.perl5.porters | Postings from March 2007

[PATCH] stop the cargo cult of (MEM_SIZE)~0

Thread Next
From:
Jarkko Hietaniemi
Date:
March 27, 2007 05:08
Subject:
[PATCH] stop the cargo cult of (MEM_SIZE)~0
Message ID:
200703271207.l2RC7qOC443040@kosh.hut.fi
--- handy.h.dist	2007-03-27 14:39:42.000000000 +0300
+++ handy.h	2007-03-27 15:02:53.000000000 +0300
@@ -685,16 +685,18 @@
 #define NEWSV(x,len)	newSV(len)
 #endif
 
+#define MEM_SIZE_MAX ((MEM_SIZE)~0)
+
 /* The +0.0 in MEM_WRAP_CHECK_ is an attempt to foil
  * overly eager compilers that will bleat about e.g.
  * (U16)n > (size_t)~0/sizeof(U16) always being false. */
 #ifdef PERL_MALLOC_WRAP
 #define MEM_WRAP_CHECK(n,t) MEM_WRAP_CHECK_1(n,t,PL_memory_wrap)
 #define MEM_WRAP_CHECK_1(n,t,a) \
-	(void)(sizeof(t) > 1 && ((MEM_SIZE)(n)+0.0) > ((MEM_SIZE)~0)/sizeof(t) && (Perl_croak_nocontext(a),0))
+	(void)(sizeof(t) > 1 && ((MEM_SIZE)(n)+0.0) > MEM_SIZE_MAX/sizeof(t) && (Perl_croak_nocontext(a),0))
 #define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
 
-#define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > (MEM_SIZE)~0 - 2 * PERL_STRLEN_ROUNDUP_QUANTUM) ? (Perl_croak_nocontext(PL_memory_wrap),0):0),((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
+#define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > MEM_SIZE_MAX - 2 * PERL_STRLEN_ROUNDUP_QUANTUM) ? (Perl_croak_nocontext(PL_memory_wrap),0):0),((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
 
 #else
 
--- pp.c.dist	2007-03-27 14:39:46.000000000 +0300
+++ pp.c	2007-03-27 14:41:43.000000000 +0300
@@ -1537,7 +1537,7 @@
 		SvCUR_set(TARG, 0);
 	    else {
 		const STRLEN max = (UV)count * len;
-		if (len > ((MEM_SIZE)~0)/count)
+		if (len > MEM_SIZE_MAX / count)
 		     Perl_croak(aTHX_ oom_string_extend);
 	        MEM_WRAP_CHECK_1(max, char, oom_string_extend);
 		SvGROW(TARG, max + 1);
--- util.c.dist	2007-03-27 14:39:52.000000000 +0300
+++ util.c	2007-03-27 14:42:10.000000000 +0300
@@ -261,12 +261,12 @@
     MEM_SIZE total_size = 0;
 
     /* Even though calloc() for zero bytes is strange, be robust. */
-    if (size && (count <= (MEM_SIZE)~0 / size))
+    if (size && (count <= MEM_SIZE_MAX / size))
 	total_size = size * count;
     else
 	Perl_croak_nocontext(PL_memory_wrap);
 #ifdef PERL_TRACK_MEMPOOL
-    if (sTHX <= (MEM_SIZE)~0 - (MEM_SIZE)total_size)
+    if (sTHX <= MEM_SIZE_MAX - (MEM_SIZE)total_size)
 	total_size += sTHX;
     else
 	Perl_croak_nocontext(PL_memory_wrap);

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About