Commit da512b70 authored by Jens Gustedt's avatar Jens Gustedt
Browse files

mv the callback functions to a file of their own

parent b8c03bca
/* This may look like nonsense, but it really is -*- mode: C -*- */
/* */
/* Except for parts copied from previous work and as explicitly stated below, */
/* the author and copyright holder for this work is */
/* (C) copyright 2012 Jens Gustedt, INRIA, France */
/* */
/* This file is free software; it is part of the P99 project. */
/* You can redistribute it and/or modify it under the terms of the QPL as */
/* given in the file LICENSE. It is distributed without any warranty; */
/* without even the implied warranty of merchantability or fitness for a */
/* particular purpose. */
/* */
#ifndef P99_CALLBACK_H
#define P99_CALLBACK_H 1
/* Additions by C11 */
# if __STDC_VERSION__ < 201100L
# include "p99_atomic.h"
# endif
/**
** @addtogroup callbacks simple callbacks with or without @c void* argument
**
** @{
**/
#if defined(P99_LIFO_POP) || defined(P00_DOXYGEN)
# define p00_has_feature_callback_thread_safe 1
# define p00_has_extension_callback_thread_safe 1
#endif
typedef struct p00_cb_el p00_cb_el;
P99_POINTER_TYPE(p00_cb_el);
#ifndef P00_DOXYGEN
# if p99_has_feature(callback_thread_safe)
P99_DECLARE_ATOMIC(p00_cb_el_ptr);
typedef _Atomic(p00_cb_el_ptr) p99_callback_stack;
# else
typedef p00_cb_el_ptr p99_callback_stack;
# endif
#endif
typedef void p99_callback_void_func(void);
typedef void p99_callback_voidptr_func(void*);
struct p00_cb_el {
p00_cb_el_ptr p99_lifo;
p99_callback_voidptr_func * p00_voidptr_func;
union {
p99_callback_void_func * p00_void_func;
void* p00_arg;
};
};
p99_inline
p00_cb_el* p00_cb_el_init(p00_cb_el * p00_obj,
p99_callback_voidptr_func* p00_voidptr_func,
p99_callback_void_func* p00_void_func,
void* p00_arg
) {
if (p00_obj) {
if (p00_voidptr_func)
*p00_obj = (p00_cb_el) {
.p00_voidptr_func = p00_voidptr_func,
.p00_arg = p00_arg,
};
else
*p00_obj = (p00_cb_el) { .p00_void_func = p00_void_func };
}
return p00_obj;
}
#define p00_cb_el_init_1(FUNC) \
P99_GENERIC((&*FUNC), \
, \
(p99_callback_voidptr_func*, (p99_callback_voidptr_func*)(FUNC)), \
(p99_callback_void_func*, (p99_callback_voidptr_func*)0) \
)
#define p00_cb_el_init_2(FUNC) \
P99_GENERIC((&*FUNC), \
, \
(p99_callback_void_func*, (p99_callback_void_func*)(FUNC)), \
(p99_callback_voidptr_func*, (p99_callback_void_func*)0) \
)
#define p00_cb_el_init_(OBJ, FUNC, ARG) \
p00_cb_el_init((OBJ), p00_cb_el_init_1(FUNC), p00_cb_el_init_2(FUNC), ARG)
#define p00_cb_el_init(...) \
P99_IF_LT(P99_NARG(__VA_ARGS__), 3) \
(p00_cb_el_init_(__VA_ARGS__, 0)) \
(p00_cb_el_init_(__VA_ARGS__))
p99_inline
p00_cb_el* p99_callback_top(p99_callback_stack* p00_l) {
#if p99_has_feature(callback_thread_safe)
return P99_LIFO_TOP(p00_l);
#else
return *p00_l;
#endif
}
p99_inline
void p99_callback_push(p99_callback_stack* p00_l, p00_cb_el* p00_el) {
#if p99_has_feature(callback_thread_safe)
P99_LIFO_PUSH(p00_l, p00_el);
#else
p00_el->p99_lifo = *p00_l;
*p00_l = p00_el;
#endif
}
p99_inline
p00_cb_el* p99_callback_pop(p99_callback_stack* p00_l) {
#if p99_has_feature(callback_thread_safe)
return P99_LIFO_POP(p00_l);
#else
p00_cb_el *p00_el = p99_callback_top(p00_l);
if (p00_el) {
*p00_l = p00_el->p99_lifo;
p00_el->p99_lifo = 0;
}
return p00_el;
#endif
}
p99_inline
void p99_callback_stack_call(p99_callback_stack* stck) {
for (;;) {
p00_cb_el *el = p99_callback_pop(stck);
if (P99_UNLIKELY(!el)) break;
p99_callback_voidptr_func * p00_voidptr_func = el->p00_voidptr_func;
p99_callback_void_func * p00_void_func = el->p00_void_func;
void * p00_arg = el->p00_arg;
free(el);
if (p00_voidptr_func) p00_voidptr_func(p00_arg);
else p00_void_func();
}
}
/**
** @}
**/
#endif
......@@ -15,6 +15,7 @@
#define P99_CLIB_H 1
#include "p99_new.h"
#include "p99_callback.h"
/* Additions by C11 */
# if __STDC_VERSION__ < 201100L
......@@ -35,10 +36,6 @@
#define p00_has_extension_aligned_alloc 1
#define p00_has_feature_quick_exit 1
#define p00_has_extension_quick_exit 1
#if defined(P99_LIFO_POP) || defined(P00_DOXYGEN)
# define p00_has_feature_callback_thread_safe 1
# define p00_has_extension_callback_thread_safe 1
#endif
/**
** @brief allocation with a chosen alignment
......@@ -59,119 +56,6 @@ void *aligned_alloc(size_t p00_alignment, size_t p00_size) {
return p00_ret;
}
typedef struct p00_cb_el p00_cb_el;
P99_POINTER_TYPE(p00_cb_el);
#ifndef P00_DOXYGEN
# if p99_has_feature(callback_thread_safe)
P99_DECLARE_ATOMIC(p00_cb_el_ptr);
typedef _Atomic(p00_cb_el_ptr) p99_callback_stack;
# else
typedef p00_cb_el_ptr p99_callback_stack;
# endif
#endif
typedef void p99_callback_void_func(void);
typedef void p99_callback_voidptr_func(void*);
struct p00_cb_el {
p00_cb_el_ptr p99_lifo;
p99_callback_voidptr_func * p00_voidptr_func;
union {
p99_callback_void_func * p00_void_func;
void* p00_arg;
};
};
p99_inline
p00_cb_el* p00_cb_el_init(p00_cb_el * p00_obj,
p99_callback_voidptr_func* p00_voidptr_func,
p99_callback_void_func* p00_void_func,
void* p00_arg
) {
if (p00_obj) {
if (p00_voidptr_func)
*p00_obj = (p00_cb_el) {
.p00_voidptr_func = p00_voidptr_func,
.p00_arg = p00_arg,
};
else
*p00_obj = (p00_cb_el) { .p00_void_func = p00_void_func };
}
return p00_obj;
}
#define p00_cb_el_init_1(FUNC) \
P99_GENERIC((&*FUNC), \
, \
(p99_callback_voidptr_func*, (p99_callback_voidptr_func*)(FUNC)), \
(p99_callback_void_func*, (p99_callback_voidptr_func*)0) \
)
#define p00_cb_el_init_2(FUNC) \
P99_GENERIC((&*FUNC), \
, \
(p99_callback_void_func*, (p99_callback_void_func*)(FUNC)), \
(p99_callback_voidptr_func*, (p99_callback_void_func*)0) \
)
#define p00_cb_el_init_(OBJ, FUNC, ARG) \
p00_cb_el_init((OBJ), p00_cb_el_init_1(FUNC), p00_cb_el_init_2(FUNC), ARG)
#define p00_cb_el_init(...) \
P99_IF_LT(P99_NARG(__VA_ARGS__), 3) \
(p00_cb_el_init_(__VA_ARGS__, 0)) \
(p00_cb_el_init_(__VA_ARGS__))
p99_inline
p00_cb_el* p99_callback_top(p99_callback_stack* p00_l) {
#if p99_has_feature(callback_thread_safe)
return P99_LIFO_TOP(p00_l);
#else
return *p00_l;
#endif
}
p99_inline
void p99_callback_push(p99_callback_stack* p00_l, p00_cb_el* p00_el) {
#if p99_has_feature(callback_thread_safe)
P99_LIFO_PUSH(p00_l, p00_el);
#else
p00_el->p99_lifo = *p00_l;
*p00_l = p00_el;
#endif
}
p99_inline
p00_cb_el* p99_callback_pop(p99_callback_stack* p00_l) {
#if p99_has_feature(callback_thread_safe)
return P99_LIFO_POP(p00_l);
#else
p00_cb_el *p00_el = p99_callback_top(p00_l);
if (p00_el) {
*p00_l = p00_el->p99_lifo;
p00_el->p99_lifo = 0;
}
return p00_el;
#endif
}
p99_inline
void p99_callback_stack_call(p99_callback_stack* stck) {
for (;;) {
p00_cb_el *el = p99_callback_pop(stck);
if (P99_UNLIKELY(!el)) break;
p99_callback_voidptr_func * p00_voidptr_func = el->p00_voidptr_func;
p99_callback_void_func * p00_void_func = el->p00_void_func;
void * p00_arg = el->p00_arg;
free(el);
if (p00_voidptr_func) p00_voidptr_func(p00_arg);
else p00_void_func();
}
}
/* In both cases this is guaranteed to do the correct
initialization. */
P99_WEAK(p00_cb)
......
......@@ -41,7 +41,7 @@ automatically generated, do not change manually.
**/
#define P99_VERSION_ID "$Format:%H$"
#define P00_VERSION_DATE "Thu, 06 Dec 2012 21:22:58 +0100"
#define P00_VERSION_DATE "Mon, 24 Dec 2012 11:18:47 +0100"
#define P00_VERSION_ID ""
/** @brief The maximal number of arguments the P99 macros can handle.
......@@ -9240,6 +9240,14 @@ automatically generated, do not change manually.
//! @}
//! @addtogroup callbacks
//! @{
#define P00_callbacks
//! @}
//! @}
//! @addtogroup preprocessor_macros
......
......@@ -1252,6 +1252,7 @@ my @groups =
["futex"],
["library_wrappers"],
["posix_default"],
["callbacks"],
],
[ "preprocessor_macros",
[ "meta_programming",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment