From 0df8ca8621715af71afbfc255f3ebe462898288e Mon Sep 17 00:00:00 2001 From: William Cohen Date: Thu, 24 Mar 2011 11:22:49 -0400 Subject: Reduce the overhead of the memcpy check This is based on patch from John Reiser . Rather than use _buildin_abs (which also has issues with 64-bit values) do the test as two conditional tests. Also add the unlikely() notation to give the compiler some hint on how to organize the code to make common case straight-line code. --- memstomp.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/memstomp.c b/memstomp.c index 69faeaa..fdf8853 100644 --- a/memstomp.c +++ b/memstomp.c @@ -44,6 +44,9 @@ #include #define ABRT_TRAP raise(SIGSEGV) +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + static bool abrt_trap = false; #ifndef SCHED_RESET_ON_FORK @@ -294,10 +297,10 @@ static void warn_memcpy(void * dest, const void * src, size_t count) void * memcpy(void * dest, const void * src, size_t count) { - size_t distance = __builtin_abs(((char *)dest - (char *)src)); + size_t d = (char *)dest - (char *)src; /* Check for overlap. */ - if (distance < count) { + if (unlikely(d < count || -d < count)) { if (abrt_trap) ABRT_TRAP; /* report the overlap */ warn_memcpy(dest, src, count); -- cgit