2010-08-17 05:33:07 +04:00
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2009-11-18 02:42:52 +03:00
2012-07-18 21:07:51 +04:00
# pragma once
2009-11-18 02:42:52 +03:00
2010-02-03 15:03:47 +03:00
/***
This file is part of systemd .
Copyright 2010 Lennart Poettering
systemd is free software ; you can redistribute it and / or modify it
2012-04-12 02:20:58 +04:00
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
2010-02-03 15:03:47 +03:00
( at your option ) any later version .
systemd 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
2012-04-12 02:20:58 +04:00
Lesser General Public License for more details .
2010-02-03 15:03:47 +03:00
2012-04-12 02:20:58 +04:00
You should have received a copy of the GNU Lesser General Public License
2010-02-03 15:03:47 +03:00
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
2009-11-18 02:42:52 +03:00
# include <assert.h>
2012-02-29 17:42:49 +04:00
# include <sys/param.h>
2009-11-18 02:42:52 +03:00
# include <sys/types.h>
2010-09-08 05:07:44 +04:00
# include <sys/uio.h>
# include <inttypes.h>
2009-11-18 02:42:52 +03:00
2010-06-02 23:34:03 +04:00
# define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
# define _sentinel_ __attribute__ ((sentinel))
# define _noreturn_ __attribute__((noreturn))
# define _unused_ __attribute__ ((unused))
# define _destructor_ __attribute__ ((destructor))
# define _pure_ __attribute__ ((pure))
# define _const_ __attribute__ ((const))
# define _deprecated_ __attribute__ ((deprecated))
# define _packed_ __attribute__ ((packed))
# define _malloc_ __attribute__ ((malloc))
# define _weak_ __attribute__ ((weak))
# define _likely_(x) (__builtin_expect(!!(x),1))
# define _unlikely_(x) (__builtin_expect(!!(x),0))
2010-06-21 21:17:47 +04:00
# define _public_ __attribute__ ((visibility("default")))
# define _hidden_ __attribute__ ((visibility("hidden")))
2010-07-16 05:07:53 +04:00
# define _weakref_(x) __attribute__((weakref(#x)))
2010-11-23 23:12:11 +03:00
# define _introspect_(x) __attribute__((section("introspect." x)))
2012-07-15 16:58:29 +04:00
# define _alignas_(x) __attribute__((aligned(__alignof(x))))
2009-11-18 02:42:52 +03:00
2011-01-04 03:58:38 +03:00
# define XSTRINGIFY(x) #x
# define STRINGIFY(x) XSTRINGIFY(x)
2009-11-18 02:42:52 +03:00
/* Rounds up */
2011-03-18 05:03:41 +03:00
# define ALIGN(l) ALIGN_TO((l), sizeof(void*))
static inline size_t ALIGN_TO ( size_t l , size_t ali ) {
return ( ( l + ali - 1 ) & ~ ( ali - 1 ) ) ;
2010-09-23 17:01:41 +04:00
}
2009-11-18 02:42:52 +03:00
# define ELEMENTSOF(x) (sizeof(x) / sizeof((x)[0]))
2012-05-07 23:06:55 +04:00
/*
* container_of - cast a member of a structure out to the containing structure
* @ ptr : the pointer to the member .
* @ type : the type of the container struct this is embedded in .
* @ member : the name of the member within the struct .
*
*/
# define container_of(ptr, type, member) ({ \
const typeof ( ( ( type * ) 0 ) - > member ) * __mptr = ( ptr ) ; \
( type * ) ( ( char * ) __mptr - offsetof ( type , member ) ) ; } )
2010-10-19 00:38:41 +04:00
# ifndef MAX
2009-11-18 02:42:52 +03:00
# define MAX(a,b) \
__extension__ ( { \
typeof ( a ) _a = ( a ) ; \
typeof ( b ) _b = ( b ) ; \
_a > _b ? _a : _b ; \
} )
2010-10-19 00:38:41 +04:00
# endif
2009-11-18 02:42:52 +03:00
2010-10-19 00:38:41 +04:00
# define MAX3(a,b,c) \
MAX ( MAX ( a , b ) , c )
# ifndef MIN
2009-11-18 02:42:52 +03:00
# define MIN(a,b) \
__extension__ ( { \
typeof ( a ) _a = ( a ) ; \
typeof ( b ) _b = ( b ) ; \
_a < _b ? _a : _b ; \
} )
2010-10-19 00:38:41 +04:00
# endif
# define MIN3(a,b,c) \
MIN ( MIN ( a , b ) , c )
2009-11-18 02:42:52 +03:00
# define CLAMP(x, low, high) \
__extension__ ( { \
typeof ( x ) _x = ( x ) ; \
typeof ( low ) _low = ( low ) ; \
typeof ( high ) _high = ( high ) ; \
( ( _x > _high ) ? _high : ( ( _x < _low ) ? _low : _x ) ) ; \
} )
2010-04-13 05:59:39 +04:00
# define assert_se(expr) \
do { \
2010-06-02 23:34:03 +04:00
if ( _unlikely_ ( ! ( expr ) ) ) \
2012-01-17 15:05:33 +04:00
log_assert_failed ( # expr , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ ) ; \
2010-04-13 05:59:39 +04:00
} while ( false ) \
/* We override the glibc assert() here. */
# undef assert
# ifdef NDEBUG
# define assert(expr) do {} while(false)
# else
# define assert(expr) assert_se(expr)
# endif
2009-11-18 02:42:52 +03:00
2010-04-13 05:59:39 +04:00
# define assert_not_reached(t) \
do { \
2012-01-17 15:05:33 +04:00
log_assert_failed_unreachable ( t , __FILE__ , __LINE__ , __PRETTY_FUNCTION__ ) ; \
2010-04-13 05:59:39 +04:00
} while ( false )
2009-11-18 02:42:52 +03:00
# define assert_cc(expr) \
do { \
switch ( 0 ) { \
case 0 : \
case ! ! ( expr ) : \
; \
} \
} while ( false )
# define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
# define UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
# define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
# define UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
2010-07-11 02:50:49 +04:00
# define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p)))
# define ULONG_TO_PTR(u) ((void*) ((uintptr_t) (u)))
2009-11-18 02:42:52 +03:00
# define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
# define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
# define TO_INT32(p) ((int32_t) ((intptr_t) (p)))
# define INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
2010-07-11 02:50:49 +04:00
# define PTR_TO_LONG(p) ((long) ((intptr_t) (p)))
# define LONG_TO_PTR(u) ((void*) ((intptr_t) (u)))
2010-01-24 02:38:51 +03:00
# define memzero(x,l) (memset((x), 0, (l)))
# define zero(x) (memzero(&(x), sizeof(x)))
2010-01-26 06:18:44 +03:00
# define char_array_0(x) x[sizeof(x)-1] = 0;
2010-05-15 19:22:58 +04:00
# define IOVEC_SET_STRING(i, s) \
2010-04-06 23:54:19 +04:00
do { \
2010-05-15 19:22:58 +04:00
struct iovec * _i = & ( i ) ; \
char * _s = ( char * ) ( s ) ; \
_i - > iov_base = _s ; \
_i - > iov_len = strlen ( _s ) ; \
2011-11-08 21:18:48 +04:00
} while ( false )
2010-04-06 23:54:19 +04:00
2010-09-08 05:07:44 +04:00
static inline size_t IOVEC_TOTAL_SIZE ( const struct iovec * i , unsigned n ) {
unsigned j ;
size_t r = 0 ;
for ( j = 0 ; j < n ; j + + )
r + = i [ j ] . iov_len ;
return r ;
}
static inline size_t IOVEC_INCREMENT ( struct iovec * i , unsigned n , size_t k ) {
unsigned j ;
for ( j = 0 ; j < n ; j + + ) {
size_t sub ;
if ( _unlikely_ ( k < = 0 ) )
break ;
sub = MIN ( i [ j ] . iov_len , k ) ;
i [ j ] . iov_len - = sub ;
i [ j ] . iov_base = ( uint8_t * ) i [ j ] . iov_base + sub ;
k - = sub ;
}
return k ;
}
2010-04-13 05:59:39 +04:00
# include "log.h"