Skip to content

Commit 586d632

Browse files
committed
Adjust MemSet macro to use size_t rather than long
Likewise for MemSetAligned. "long" wasn't the most suitable type for these macros as with MSVC in 64-bit builds, sizeof(long) == 4, which is narrower than the processor's word size, therefore these macros had to perform twice as many loops as they otherwise might. Author: David Rowley <dgrowleyml@gmail.com> Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Discussion: https://postgr.es/m/CAApHDvoGFjSA3aNyVQ3ivbyc4ST=CC5L-_VjEUQ92HbE2Cxovg@mail.gmail.com
1 parent 9c047da commit 586d632

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/include/c.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,29 +1007,29 @@ pg_noreturn extern void ExceptionalCondition(const char *conditionName,
10071007
#define Min(x, y) ((x) < (y) ? (x) : (y))
10081008

10091009

1010-
/* Get a bit mask of the bits set in non-long aligned addresses */
1011-
#define LONG_ALIGN_MASK (sizeof(long) - 1)
1010+
/* Get a bit mask of the bits set in non-size_t aligned addresses */
1011+
#define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
10121012

10131013
/*
10141014
* MemSet
10151015
* Exactly the same as standard library function memset(), but considerably
1016-
* faster for zeroing small word-aligned structures (such as parsetree nodes).
1017-
* This has to be a macro because the main point is to avoid function-call
1018-
* overhead. However, we have also found that the loop is faster than
1019-
* native libc memset() on some platforms, even those with assembler
1020-
* memset() functions. More research needs to be done, perhaps with
1021-
* MEMSET_LOOP_LIMIT tests in configure.
1016+
* faster for zeroing small size_t-aligned structures (such as parsetree
1017+
* nodes). This has to be a macro because the main point is to avoid
1018+
* function-call overhead. However, we have also found that the loop is
1019+
* faster than native libc memset() on some platforms, even those with
1020+
* assembler memset() functions. More research needs to be done, perhaps
1021+
* with MEMSET_LOOP_LIMIT tests in configure.
10221022
*/
10231023
#define MemSet(start, val, len) \
10241024
do \
10251025
{ \
1026-
/* must be void* because we don't know if it is integer aligned yet */ \
1026+
/* must be void* because we don't know if it is size_t aligned yet */ \
10271027
void *_vstart = (void *) (start); \
10281028
int _val = (val); \
10291029
Size _len = (len); \
10301030
\
1031-
if ((((uintptr_t) _vstart) & LONG_ALIGN_MASK) == 0 && \
1032-
(_len & LONG_ALIGN_MASK) == 0 && \
1031+
if ((((uintptr_t) _vstart) & SIZE_T_ALIGN_MASK) == 0 && \
1032+
(_len & SIZE_T_ALIGN_MASK) == 0 && \
10331033
_val == 0 && \
10341034
_len <= MEMSET_LOOP_LIMIT && \
10351035
/* \
@@ -1038,8 +1038,8 @@ pg_noreturn extern void ExceptionalCondition(const char *conditionName,
10381038
*/ \
10391039
MEMSET_LOOP_LIMIT != 0) \
10401040
{ \
1041-
long *_start = (long *) _vstart; \
1042-
long *_stop = (long *) ((char *) _start + _len); \
1041+
size_t *_start = (size_t *) _vstart; \
1042+
size_t *_stop = (size_t *) ((char *) _start + _len); \
10431043
while (_start < _stop) \
10441044
*_start++ = 0; \
10451045
} \
@@ -1049,23 +1049,23 @@ pg_noreturn extern void ExceptionalCondition(const char *conditionName,
10491049

10501050
/*
10511051
* MemSetAligned is the same as MemSet except it omits the test to see if
1052-
* "start" is word-aligned. This is okay to use if the caller knows a-priori
1053-
* that the pointer is suitably aligned (typically, because he just got it
1054-
* from palloc(), which always delivers a max-aligned pointer).
1052+
* "start" is size_t-aligned. This is okay to use if the caller knows
1053+
* a-priori that the pointer is suitably aligned (typically, because he just
1054+
* got it from palloc(), which always delivers a max-aligned pointer).
10551055
*/
10561056
#define MemSetAligned(start, val, len) \
10571057
do \
10581058
{ \
1059-
long *_start = (long *) (start); \
1059+
size_t *_start = (size_t *) (start); \
10601060
int _val = (val); \
10611061
Size _len = (len); \
10621062
\
1063-
if ((_len & LONG_ALIGN_MASK) == 0 && \
1063+
if ((_len & SIZE_T_ALIGN_MASK) == 0 && \
10641064
_val == 0 && \
10651065
_len <= MEMSET_LOOP_LIMIT && \
10661066
MEMSET_LOOP_LIMIT != 0) \
10671067
{ \
1068-
long *_stop = (long *) ((char *) _start + _len); \
1068+
size_t *_stop = (size_t *) ((char *) _start + _len); \
10691069
while (_start < _stop) \
10701070
*_start++ = 0; \
10711071
} \

0 commit comments

Comments
 (0)