From 6adf5b8a078f2b37f2d3d91cd060b891c2a7efaa Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 1 Mar 2010 16:41:12 -0500 Subject: Improve safe alignment buffer handling macros Make the counter optional so that alignment safe macros can be used also where there is no counter to update. Change arguments names so that they are not deceiving (ptr normlly identify a pointer) Turn the memcpy substitute into an inline function so that passing a pointer to rp and checking for it doesn't make the compiler spit lots of warnings. --- src/util/util.h | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'src/util') diff --git a/src/util/util.h b/src/util/util.h index 5d2dff28..1f5573d4 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -163,26 +163,35 @@ errno_t set_debug_file_from_fd(const int fd); #define OUT_OF_ID_RANGE(id, min, max) \ (id == 0 || (min && (id < min)) || (max && (id > max))) -#define COPY_MEM(to, from, ptr, size) do { \ - memcpy(to, from, size); \ - ptr += size; \ +static inline void +safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter) +{ + memcpy(dest, src, n); + if (counter) { + *counter += n; + } +} + +#define SAFEALIGN_SET_VALUE(dest, value, type, pctr) do { \ + type CV_MACRO_val = (type)(value); \ + safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \ } while(0) -#define COPY_TYPE(to, from, ptr, type) COPY_MEM(to, from, ptr, sizeof(type)) +#define SAFEALIGN_COPY_UINT32(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(uint32_t), pctr) -#define COPY_VALUE(to, value, ptr, type) do { \ - type CV_MACRO_val = (type) value; \ - COPY_TYPE(to, &CV_MACRO_val, ptr, type); \ -} while(0) +#define SAFEALIGN_SET_UINT32(dest, value, pctr) \ + SAFEALIGN_SET_VALUE(dest, value, uint32_t, pctr) + +#define SAFEALIGN_COPY_INT32(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(int32_t), pctr) -#define COPY_UINT32(to, from, ptr) COPY_TYPE(to, from, ptr, uint32_t) -#define COPY_UINT32_VALUE(to, value, ptr) COPY_VALUE(to, value, ptr, uint32_t) -#define COPY_INT32(to, from, ptr) COPY_TYPE(to, from, ptr, int32_t) -#define COPY_INT32_VALUE(to, value, ptr) COPY_VALUE(to, value, ptr, int32_t) +#define SAFEALIGN_SET_INT32(dest, value, pctr) \ + SAFEALIGN_SET_VALUE(dest, value, int32_t, pctr) -#define COPY_UINT32_CHECK(to, from, ptr, size) do { \ - if ((ptr + sizeof(uint32_t)) > size) return EINVAL; \ - COPY_UINT32(to, from, ptr); \ +#define SAFEALIGN_COPY_UINT32_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(uint32_t)) > (len)) return EINVAL; \ + safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \ } while(0) #include "util/dlinklist.h" -- cgit