From: Martin Willi Date: Wed, 15 Apr 2015 14:59:25 +0000 (+0200) Subject: align: Move min/max/padding/alignment functions to separate files X-Git-Tag: 5.3.1dr1~11^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=04f12ecd296287f433405078c520d5cbf073fcd9;p=thirdparty%2Fstrongswan.git align: Move min/max/padding/alignment functions to separate files --- diff --git a/src/libstrongswan/Android.mk b/src/libstrongswan/Android.mk index bb21302fb0..d019d96e12 100644 --- a/src/libstrongswan/Android.mk +++ b/src/libstrongswan/Android.mk @@ -41,7 +41,8 @@ utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \ utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \ utils/parser_helper.c utils/test.c utils/process.c utils/utils/strerror.c \ utils/utils/atomics.c utils/utils/string.c utils/utils/memory.c \ -utils/utils/tty.c utils/utils/path.c utils/utils/status.c utils/utils/time.c +utils/utils/tty.c utils/utils/path.c utils/utils/status.c utils/utils/time.c \ +utils/utils/align.c libstrongswan_la_SOURCES += \ threading/thread.c \ diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 17e3c80c0d..b3636cfb89 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -39,7 +39,8 @@ utils/utils.c utils/chunk.c utils/debug.c utils/enum.c utils/identification.c \ utils/lexparser.c utils/optionsfrom.c utils/capabilities.c utils/backtrace.c \ utils/parser_helper.c utils/test.c utils/process.c utils/utils/strerror.c \ utils/utils/atomics.c utils/utils/string.c utils/utils/memory.c \ -utils/utils/tty.c utils/utils/path.c utils/utils/status.c utils/utils/time.c +utils/utils/tty.c utils/utils/path.c utils/utils/status.c utils/utils/time.c \ +utils/utils/align.c if !USE_WINDOWS libstrongswan_la_SOURCES += \ @@ -111,7 +112,7 @@ utils/parser_helper.h utils/test.h utils/integrity_checker.h utils/process.h \ utils/utils/strerror.h utils/compat/windows.h utils/compat/apple.h \ utils/utils/atomics.h utils/utils/types.h utils/utils/byteorder.h \ utils/utils/string.h utils/utils/memory.h utils/utils/tty.h utils/utils/path.h \ -utils/utils/status.h utils/utils/object.h utils/utils/time.h +utils/utils/status.h utils/utils/object.h utils/utils/time.h utils/utils/align.h endif library.lo : $(top_builddir)/config.status diff --git a/src/libstrongswan/utils/utils.c b/src/libstrongswan/utils/utils.c index 3de663a4a9..e3a16c0f0a 100644 --- a/src/libstrongswan/utils/utils.c +++ b/src/libstrongswan/utils/utils.c @@ -28,56 +28,11 @@ #endif #include -#include #include #include #include #include -/** - * Described in header. - */ -void* malloc_align(size_t size, u_int8_t align) -{ - u_int8_t pad; - void *ptr; - - if (align == 0) - { - align = 1; - } - ptr = malloc(align + sizeof(pad) + size); - if (!ptr) - { - return NULL; - } - /* store padding length just before data, down to the allocation boundary - * to do some verification during free_align() */ - pad = align - ((uintptr_t)ptr % align); - memset(ptr, pad, pad); - return ptr + pad; -} - -/** - * Described in header. - */ -void free_align(void *ptr) -{ - u_int8_t pad, *pos; - - pos = ptr - 1; - /* verify padding to check any corruption */ - for (pad = *pos; (void*)pos >= ptr - pad; pos--) - { - if (*pos != pad) - { - DBG1(DBG_LIB, "!!!! invalid free_align() !!!!"); - return; - } - } - free(ptr - pad); -} - #ifdef WIN32 /** diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h index f74e301b73..87d0695152 100644 --- a/src/libstrongswan/utils/utils.h +++ b/src/libstrongswan/utils/utils.h @@ -77,6 +77,7 @@ #include "utils/types.h" #include "enum.h" #include "utils/atomics.h" +#include "utils/align.h" #include "utils/byteorder.h" #include "utils/string.h" #include "utils/memory.h" @@ -100,22 +101,6 @@ void utils_init(); */ void utils_deinit(); -/** - * Macro gives back larger of two values. - */ -#define max(x,y) ({ \ - typeof(x) _x = (x); \ - typeof(y) _y = (y); \ - _x > _y ? _x : _y; }) - -/** - * Macro gives back smaller of two values. - */ -#define min(x,y) ({ \ - typeof(x) _x = (x); \ - typeof(y) _y = (y); \ - _x < _y ? _x : _y; }) - /** * Debug macro to follow control flow */ @@ -163,24 +148,6 @@ void utils_deinit(); */ #define ignore_result(call) { if(call){}; } -/** - * malloc(), but returns aligned memory. - * - * The returned pointer must be freed using free_align(), not free(). - * - * @param size size of allocated data - * @param align alignment, up to 255 bytes, usually a power of 2 - * @return allocated hunk, aligned to align bytes - */ -void* malloc_align(size_t size, u_int8_t align); - -/** - * Free a hunk allocated by malloc_align(). - * - * @param ptr hunk to free - */ -void free_align(void *ptr); - /** * Portable function to wait for SIGINT/SIGTERM (or equivalent). */ @@ -215,31 +182,4 @@ bool return_true(); */ bool return_false(); -/** - * Get the padding required to make size a multiple of alignment - */ -static inline size_t pad_len(size_t size, size_t alignment) -{ - size_t remainder; - - remainder = size % alignment; - return remainder ? alignment - remainder : 0; -} - -/** - * Round up size to be multiple of alignment - */ -static inline size_t round_up(size_t size, size_t alignment) -{ - return size + pad_len(size, alignment); -} - -/** - * Round down size to be a multiple of alignment - */ -static inline size_t round_down(size_t size, size_t alignment) -{ - return size - (size % alignment); -} - #endif /** UTILS_H_ @}*/ diff --git a/src/libstrongswan/utils/utils/align.c b/src/libstrongswan/utils/utils/align.c new file mode 100644 index 0000000000..29f110ff12 --- /dev/null +++ b/src/libstrongswan/utils/utils/align.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2008-2014 Tobias Brunner + * Copyright (C) 2005-2008 Martin Willi + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include +#include + +/** + * Described in header. + */ +void* malloc_align(size_t size, u_int8_t align) +{ + u_int8_t pad; + void *ptr; + + if (align == 0) + { + align = 1; + } + ptr = malloc(align + sizeof(pad) + size); + if (!ptr) + { + return NULL; + } + /* store padding length just before data, down to the allocation boundary + * to do some verification during free_align() */ + pad = align - ((uintptr_t)ptr % align); + memset(ptr, pad, pad); + return ptr + pad; +} + +/** + * Described in header. + */ +void free_align(void *ptr) +{ + u_int8_t pad, *pos; + + pos = ptr - 1; + /* verify padding to check any corruption */ + for (pad = *pos; (void*)pos >= ptr - pad; pos--) + { + if (*pos != pad) + { + DBG1(DBG_LIB, "!!!! invalid free_align() !!!!"); + return; + } + } + free(ptr - pad); +} diff --git a/src/libstrongswan/utils/utils/align.h b/src/libstrongswan/utils/utils/align.h new file mode 100644 index 0000000000..39cde10c88 --- /dev/null +++ b/src/libstrongswan/utils/utils/align.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2008-2014 Tobias Brunner + * Copyright (C) 2008 Martin Willi + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup align_i align + * @{ @ingroup utils_i + */ + +#ifndef ALIGN_H_ +#define ALIGN_H_ + +/** + * Macro gives back larger of two values. + */ +#define max(x,y) ({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + _x > _y ? _x : _y; }) + +/** + * Macro gives back smaller of two values. + */ +#define min(x,y) ({ \ + typeof(x) _x = (x); \ + typeof(y) _y = (y); \ + _x < _y ? _x : _y; }) + +/** + * Get the padding required to make size a multiple of alignment + */ +static inline size_t pad_len(size_t size, size_t alignment) +{ + size_t remainder; + + remainder = size % alignment; + return remainder ? alignment - remainder : 0; +} + +/** + * Round up size to be multiple of alignment + */ +static inline size_t round_up(size_t size, size_t alignment) +{ + return size + pad_len(size, alignment); +} + +/** + * Round down size to be a multiple of alignment + */ +static inline size_t round_down(size_t size, size_t alignment) +{ + return size - (size % alignment); +} + +/** + * malloc(), but returns aligned memory. + * + * The returned pointer must be freed using free_align(), not free(). + * + * @param size size of allocated data + * @param align alignment, up to 255 bytes, usually a power of 2 + * @return allocated hunk, aligned to align bytes + */ +void* malloc_align(size_t size, u_int8_t align); + +/** + * Free a hunk allocated by malloc_align(). + * + * @param ptr hunk to free + */ +void free_align(void *ptr); + +#endif /** ALIGN_H_ @} */