diff options
author | William Cohen <wcohen@redhat.com> | 2011-03-24 11:22:49 -0400 |
---|---|---|
committer | William Cohen <wcohen@redhat.com> | 2011-03-24 11:22:49 -0400 |
commit | 0df8ca8621715af71afbfc255f3ebe462898288e (patch) | |
tree | d2288544113cdfe257f7db81f5aef6482f64bb73 | |
parent | d1f97aa76432df9bb63fa254712bce5074628e37 (diff) | |
download | memstomp-0df8ca8621715af71afbfc255f3ebe462898288e.tar.gz memstomp-0df8ca8621715af71afbfc255f3ebe462898288e.tar.xz memstomp-0df8ca8621715af71afbfc255f3ebe462898288e.zip |
Reduce the overhead of the memcpy check
This is based on patch from John Reiser <jreiser@bitwagon.com>. 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.
-rw-r--r-- | memstomp.c | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -44,6 +44,9 @@ #include <signal.h> #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); |