align: Move min/max/padding/alignment functions to separate files

This commit is contained in:
Martin Willi
2015-04-16 14:50:40 +02:00
parent eaa02bc925
commit 04f12ecd29
6 changed files with 154 additions and 109 deletions
+2 -1
View File
@@ -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 \
+3 -2
View File
@@ -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
-45
View File
@@ -28,56 +28,11 @@
#endif
#include <library.h>
#include <utils/debug.h>
#include <utils/chunk.h>
#include <collections/enumerator.h>
#include <threading/mutex.h>
#include <threading/condvar.h>
/**
* 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
/**
+1 -61
View File
@@ -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_ @}*/
+62
View File
@@ -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 <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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 <utils/utils.h>
#include <utils/debug.h>
/**
* 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);
}
+86
View File
@@ -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 <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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_ @} */