Added talloc.

This commit is contained in:
Nikos Mavrogiannopoulos
2014-05-06 15:30:44 +02:00
parent 89ddd81c0e
commit 6f694915c7
6 changed files with 3071 additions and 1 deletions

View File

@@ -92,6 +92,32 @@ fi
AM_CONDITIONAL(LOCAL_PROTOBUF_C, test "x$with_local_protobuf_c" != xno)
AC_ARG_WITH(talloc,
AS_HELP_STRING([--without-talloc], [use the included talloc library]),
test_for_talloc=$withval,
test_for_talloc=yes)
with_local_talloc=no
if test "$test_for_talloc" = yes;then
PKG_CHECK_MODULES([LIBPROTOBUF_C], [libtalloc-c],,
[AC_LIB_HAVE_LINKFLAGS(talloc,, [#include <talloc.h>], [talloc_size(0,0);])
if test x$ac_cv_libtalloc = xyes; then
AC_SUBST([LIBTALLOC_LIBS], [$LIBTALLOC])
else
AC_MSG_WARN([[***
*** libtalloc-c was not found.
***]])
AC_SUBST([LIBTALLOC_CFLAGS], [-Iccan/talloc/ -Iccan/typedef_cb/ -Iccan/compiler/])
with_local_talloc=yes
fi
]
)
fi
AM_CONDITIONAL(LOCAL_TALLOC, test "x$with_local_talloc" != xno)
AC_ARG_WITH(libnl,
AS_HELP_STRING([--without-libnl], [do not try to use the libnl library]),
test_for_libnl=$withval,
@@ -349,6 +375,7 @@ Summary of build options:
dbus: ${dbus_enabled}
readline: ${have_readline}
libnl3: ${have_libnl3}
local talloc: ${with_local_talloc}
local protobuf-c: ${with_local_protobuf_c}
local PCL library: ${with_local_pcl}
local libopts: ${enable_local_libopts}

View File

@@ -4,7 +4,8 @@ AM_CPPFLAGS = -I$(srcdir)/../gl/ -I$(builddir)/../gl/ \
-I$(srcdir)/ -I$(builddir)/../ $(LIBOPTS_CFLAGS) \
$(LIBGNUTLS_CFLAGS) \
$(LIBPROTOBUF_C_CFLAGS) $(LIBDBUS_CFLAGS) \
$(LIBNL3_CFLAGS) $(LIBREADLINE_CFLAGS)
$(LIBNL3_CFLAGS) $(LIBREADLINE_CFLAGS) \
$(LIBTALLOC_CFLAGS)
BUILT_SOURCES = ocpasswd-args.c ocpasswd-args.h \
ocserv-args.c ocserv-args.h ipc.pb-c.c ipc.pb-c.h
@@ -39,6 +40,12 @@ CCAN_SOURCES = ccan/build_assert/build_assert.h ccan/check_type/check_type.h \
ccan/htable/htable.c ccan/htable/htable.h ccan/htable/htable_type.h \
ccan/list/list.c ccan/list/list.h
if LOCAL_TALLOC
CCAN_SOURCES += ccan/talloc/talloc.c ccan/talloc/talloc.h ccan/compiler/compiler.h \
ccan/typesafe_cb/typesafe_cb.h
endif
noinst_LIBRARIES = libcmd-ocserv.a
libcmd_ocserv_a_SOURCES = ocserv-args.def ocserv-args.c ocserv-args.h

View File

@@ -0,0 +1,217 @@
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_COMPILER_H
#define CCAN_COMPILER_H
#include "config.h"
#ifndef COLD
#if HAVE_ATTRIBUTE_COLD
/**
* COLD - a function is unlikely to be called.
*
* Used to mark an unlikely code path and optimize appropriately.
* It is usually used on logging or error routines.
*
* Example:
* static void COLD moan(const char *reason)
* {
* fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
* }
*/
#define COLD __attribute__((cold))
#else
#define COLD
#endif
#endif
#ifndef NORETURN
#if HAVE_ATTRIBUTE_NORETURN
/**
* NORETURN - a function does not return
*
* Used to mark a function which exits; useful for suppressing warnings.
*
* Example:
* static void NORETURN fail(const char *reason)
* {
* fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
* exit(1);
* }
*/
#define NORETURN __attribute__((noreturn))
#else
#define NORETURN
#endif
#endif
#ifndef PRINTF_FMT
#if HAVE_ATTRIBUTE_PRINTF
/**
* PRINTF_FMT - a function takes printf-style arguments
* @nfmt: the 1-based number of the function's format argument.
* @narg: the 1-based number of the function's first variable argument.
*
* This allows the compiler to check your parameters as it does for printf().
*
* Example:
* void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
*/
#define PRINTF_FMT(nfmt, narg) \
__attribute__((format(__printf__, nfmt, narg)))
#else
#define PRINTF_FMT(nfmt, narg)
#endif
#endif
#ifndef CONST_FUNCTION
#if HAVE_ATTRIBUTE_CONST
/**
* CONST_FUNCTION - a function's return depends only on its argument
*
* This allows the compiler to assume that the function will return the exact
* same value for the exact same arguments. This implies that the function
* must not use global variables, or dereference pointer arguments.
*/
#define CONST_FUNCTION __attribute__((const))
#else
#define CONST_FUNCTION
#endif
#endif
#if HAVE_ATTRIBUTE_UNUSED
#ifndef UNNEEDED
/**
* UNNEEDED - a variable/function may not be needed
*
* This suppresses warnings about unused variables or functions, but tells
* the compiler that if it is unused it need not emit it into the source code.
*
* Example:
* // With some preprocessor options, this is unnecessary.
* static UNNEEDED int counter;
*
* // With some preprocessor options, this is unnecessary.
* static UNNEEDED void add_to_counter(int add)
* {
* counter += add;
* }
*/
#define UNNEEDED __attribute__((unused))
#endif
#ifndef NEEDED
#if HAVE_ATTRIBUTE_USED
/**
* NEEDED - a variable/function is needed
*
* This suppresses warnings about unused variables or functions, but tells
* the compiler that it must exist even if it (seems) unused.
*
* Example:
* // Even if this is unused, these are vital for debugging.
* static NEEDED int counter;
* static NEEDED void dump_counter(void)
* {
* printf("Counter is %i\n", counter);
* }
*/
#define NEEDED __attribute__((used))
#else
/* Before used, unused functions and vars were always emitted. */
#define NEEDED __attribute__((unused))
#endif
#endif
#ifndef UNUSED
/**
* UNUSED - a parameter is unused
*
* Some compilers (eg. gcc with -W or -Wunused) warn about unused
* function parameters. This suppresses such warnings and indicates
* to the reader that it's deliberate.
*
* Example:
* // This is used as a callback, so needs to have this prototype.
* static int some_callback(void *unused UNUSED)
* {
* return 0;
* }
*/
#define UNUSED __attribute__((unused))
#endif
#else
#ifndef UNNEEDED
#define UNNEEDED
#endif
#ifndef NEEDED
#define NEEDED
#endif
#ifndef UNUSED
#define UNUSED
#endif
#endif
#ifndef IS_COMPILE_CONSTANT
#if HAVE_BUILTIN_CONSTANT_P
/**
* IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
* @expr: the expression to evaluate
*
* When an expression manipulation is complicated, it is usually better to
* implement it in a function. However, if the expression being manipulated is
* known at compile time, it is better to have the compiler see the entire
* expression so it can simply substitute the result.
*
* This can be done using the IS_COMPILE_CONSTANT() macro.
*
* Example:
* enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
*
* // Out-of-line version.
* const char *greek_name(enum greek greek);
*
* // Inline version.
* static inline const char *_greek_name(enum greek greek)
* {
* switch (greek) {
* case ALPHA: return "alpha";
* case BETA: return "beta";
* case GAMMA: return "gamma";
* case DELTA: return "delta";
* case EPSILON: return "epsilon";
* default: return "**INVALID**";
* }
* }
*
* // Use inline if compiler knows answer. Otherwise call function
* // to avoid copies of the same code everywhere.
* #define greek_name(g) \
* (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
*/
#define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
#else
/* If we don't know, assume it's not. */
#define IS_COMPILE_CONSTANT(expr) 0
#endif
#endif
#ifndef WARN_UNUSED_RESULT
#if HAVE_WARN_UNUSED_RESULT
/**
* WARN_UNUSED_RESULT - warn if a function return value is unused.
*
* Used to mark a function where it is extremely unlikely that the caller
* can ignore the result, eg realloc().
*
* Example:
* // buf param may be freed by this; need return value!
* static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
* {
* return realloc(buf, (*size) *= 2);
* }
*/
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define WARN_UNUSED_RESULT
#endif
#endif
#endif /* CCAN_COMPILER_H */

1653
src/ccan/talloc/talloc.c Normal file

File diff suppressed because it is too large Load Diff

1032
src/ccan/talloc/talloc.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,134 @@
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_TYPESAFE_CB_H
#define CCAN_TYPESAFE_CB_H
#include "config.h"
#if HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P
/**
* typesafe_cb_cast - only cast an expression if it matches a given type
* @desttype: the type to cast to
* @oktype: the type we allow
* @expr: the expression to cast
*
* This macro is used to create functions which allow multiple types.
* The result of this macro is used somewhere that a @desttype type is
* expected: if @expr is exactly of type @oktype, then it will be
* cast to @desttype type, otherwise left alone.
*
* This macro can be used in static initializers.
*
* This is merely useful for warnings: if the compiler does not
* support the primitives required for typesafe_cb_cast(), it becomes an
* unconditional cast, and the @oktype argument is not used. In
* particular, this means that @oktype can be a type which uses the
* "typeof": it will not be evaluated if typeof is not supported.
*
* Example:
* // We can take either an unsigned long or a void *.
* void _set_some_value(void *val);
* #define set_some_value(e) \
* _set_some_value(typesafe_cb_cast(void *, (e), unsigned long))
*/
#define typesafe_cb_cast(desttype, oktype, expr) \
__builtin_choose_expr( \
__builtin_types_compatible_p(__typeof__(0?(expr):(expr)), \
oktype), \
(desttype)(expr), (expr))
#else
#define typesafe_cb_cast(desttype, oktype, expr) ((desttype)(expr))
#endif
/**
* typesafe_cb_cast3 - only cast an expression if it matches given types
* @desttype: the type to cast to
* @ok1: the first type we allow
* @ok2: the second type we allow
* @ok3: the third type we allow
* @expr: the expression to cast
*
* This is a convenient wrapper for multiple typesafe_cb_cast() calls.
* You can chain them inside each other (ie. use typesafe_cb_cast()
* for expr) if you need more than 3 arguments.
*
* Example:
* // We can take either a long, unsigned long, void * or a const void *.
* void _set_some_value(void *val);
* #define set_some_value(expr) \
* _set_some_value(typesafe_cb_cast3(void *,, \
* long, unsigned long, const void *,\
* (expr)))
*/
#define typesafe_cb_cast3(desttype, ok1, ok2, ok3, expr) \
typesafe_cb_cast(desttype, ok1, \
typesafe_cb_cast(desttype, ok2, \
typesafe_cb_cast(desttype, ok3, \
(expr))))
/**
* typesafe_cb - cast a callback function if it matches the arg
* @rtype: the return type of the callback function
* @atype: the (pointer) type which the callback function expects.
* @fn: the callback function to cast
* @arg: the (pointer) argument to hand to the callback function.
*
* If a callback function takes a single argument, this macro does
* appropriate casts to a function which takes a single atype argument if the
* callback provided matches the @arg.
*
* It is assumed that @arg is of pointer type: usually @arg is passed
* or assigned to a void * elsewhere anyway.
*
* Example:
* void _register_callback(void (*fn)(void *arg), void *arg);
* #define register_callback(fn, arg) \
* _register_callback(typesafe_cb(void, (fn), void*, (arg)), (arg))
*/
#define typesafe_cb(rtype, atype, fn, arg) \
typesafe_cb_cast(rtype (*)(atype), \
rtype (*)(__typeof__(arg)), \
(fn))
/**
* typesafe_cb_preargs - cast a callback function if it matches the arg
* @rtype: the return type of the callback function
* @atype: the (pointer) type which the callback function expects.
* @fn: the callback function to cast
* @arg: the (pointer) argument to hand to the callback function.
*
* This is a version of typesafe_cb() for callbacks that take other arguments
* before the @arg.
*
* Example:
* void _register_callback(void (*fn)(int, void *arg), void *arg);
* #define register_callback(fn, arg) \
* _register_callback(typesafe_cb_preargs(void, void *, \
* (fn), (arg), int), \
* (arg))
*/
#define typesafe_cb_preargs(rtype, atype, fn, arg, ...) \
typesafe_cb_cast(rtype (*)(__VA_ARGS__, atype), \
rtype (*)(__VA_ARGS__, __typeof__(arg)), \
(fn))
/**
* typesafe_cb_postargs - cast a callback function if it matches the arg
* @rtype: the return type of the callback function
* @atype: the (pointer) type which the callback function expects.
* @fn: the callback function to cast
* @arg: the (pointer) argument to hand to the callback function.
*
* This is a version of typesafe_cb() for callbacks that take other arguments
* after the @arg.
*
* Example:
* void _register_callback(void (*fn)(void *arg, int), void *arg);
* #define register_callback(fn, arg) \
* _register_callback(typesafe_cb_postargs(void, (fn), void *, \
* (arg), int), \
* (arg))
*/
#define typesafe_cb_postargs(rtype, atype, fn, arg, ...) \
typesafe_cb_cast(rtype (*)(atype, __VA_ARGS__), \
rtype (*)(__typeof__(arg), __VA_ARGS__), \
(fn))
#endif /* CCAN_CAST_IF_TYPE_H */