diff options
author | Andrea Canciani <ranma42@gmail.com> | 2011-10-01 16:35:16 -0700 |
---|---|---|
committer | Andrea Canciani <ranma42@gmail.com> | 2012-07-31 16:57:45 +0200 |
commit | 3e4f6534b02de1405bbaa2b64cf11b815fa3747f (patch) | |
tree | 59078b7213735b5619081ae41cfa72a144308963 /compiler | |
parent | 4ef59230222be54b3e3051cefc66cf0a8014e24d (diff) |
First version of the simpleops library
The library provides atomic operations, mutexes, TLS and init/fini
callbacks in a cross-platform fashion.
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/simpleops-compiler-config.h | 41 | ||||
-rw-r--r-- | compiler/simpleops-compiler-types.h | 180 | ||||
-rw-r--r-- | compiler/simpleops-compiler-utils.h | 98 | ||||
-rw-r--r-- | compiler/simpleops-compiler.h | 35 |
4 files changed, 354 insertions, 0 deletions
diff --git a/compiler/simpleops-compiler-config.h b/compiler/simpleops-compiler-config.h new file mode 100644 index 0000000..adb54c8 --- /dev/null +++ b/compiler/simpleops-compiler-config.h @@ -0,0 +1,41 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + */ + +#ifndef SIMPLEOPS_COMPILER_CONFIG_H +#define SIMPLEOPS_COMPILER_CONFIG_H + +#ifndef SIMPLEOPS_COMPILER_H +#error Private header used directly +#endif + +#ifdef _MSC_VER +/* TODO */ +#else +#include "simpleops/simpleops-config.h" +#endif + +#endif diff --git a/compiler/simpleops-compiler-types.h b/compiler/simpleops-compiler-types.h new file mode 100644 index 0000000..15248da --- /dev/null +++ b/compiler/simpleops-compiler-types.h @@ -0,0 +1,180 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + */ + +#ifndef SIMPLEOPS_COMPILER_TYPES_H +#define SIMPLEOPS_COMPILER_TYPES_H + +#ifndef SIMPLEOPS_COMPILER_H +#error Private header used directly +#endif + +/** + * The type of boolean values returned by simpleops functions. + */ +typedef int simpleops_bool_t; + +#ifndef FALSE +/** + * The false boolean value. + */ +#define FALSE 0 +#endif + +#ifndef TRUE +/** + * The true boolean value. + */ +#define TRUE (! FALSE) +#endif + +#if defined(_MSC_VER) +#define simpleops_inline __inline +#else +/** + * An attribute or type qualifier that hints to the compiler that the + * function might be appropriate for inlining. + * + * This is only a hint, the compiler can choose to ignore it. + */ +#define simpleops_inline inline +#endif + +#if defined(_MSC_VER) +#define simpleops_always_inline simpleops_inline __forceinline +#elif SIMPLEOPS_HAVE_ATTRIBUTE_ALWAYS_INLINE +#define simpleops_always_inline simpleops_inline __attribute__((always_inline)) +#else +/** + * An attribute or type qualifier that requires to the compiler to + * inline the function. + * + * This is stronger than simpleops_inline, as it asks to the compiler + * to inline the function even if this worsen the performance of the + * code. + */ +#define simpleops_always_inline simpleops_inline +#endif + +#if defined(_MSC_VER) +#define simpleops_noinline __declspec(noinline) +#elif SIMPLEOPS_HAVE_ATTRIBUTE_NOINLINE +#define simpleops_noinline __attribute__((noinline)) +#else +/** + * An attribute or type qualifier that requests to the compiler to + * svoid inlining the function. + * + * This is conflicting with simpleops_always_inline and + * simpleops_inline. + */ +#define simpleops_noinline +#endif + +#if SIMPLEOPS_HAVE_ATTRIBUTE_CONST +#define simpleops_const __attribute__((const)) +#else +/** + * An attribute that informs the compiler that the function does not + * examine any value except those of its arguments. + * + * This is stronger than simpleops_pure, as const functions are not + * allowed to read global state (for example, const functions cannot + * dereference pointers). + */ +#define simpleops_const +#endif + +#if SIMPLEOPS_HAVE_ATTRIBUTE_PURE +#define simpleops_pure __attribute__((pure)) +#else +/** + * An attribute that informs the compiler that the function has no + * side effects and that the return value only depends on its + * arguments and global state. + * + * Pure functions are not allowed to read global state, but their + * behavior should not change depending on system resources and they + * should not diverge. For example, memcmp() is a pure function, + * time() is not. + */ +#define simpleops_pure +#endif + +#endif + +/* The following definitions are outside of the + * SIMPLEOPS_COMPILER_TYPES_H #if to allow libraries using simpleops + * to import symbols from other libraries using simpleops without + * conflicts. + */ + +#undef _simpleops_import +#undef _simpleops_export +#undef simpleops_private +#undef simpleops_public + +#if defined(_MSC_VER) && defined(SIMPLEOPS_DLL) +#define _simpleops_import __declspec(dllimport) +#define _simpleops_export __declspec(dllexport) +#define simpleops_private +#elif defined(__GNUC__) && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)) +#define _simpleops_import extern +#define _simpleops_export __attribute__((__visibility__("default"))) +#define simpleops_private __attribute__((__visibility__("hidden"))) +#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550) +#define _simpleops_import extern +#define _simpleops_export __global +#define simpleops_private __hidden +#else +#define _simpleops_import extern +#define _simpleops_export +/** + * An attribute that informs the compiler that the function is + * private, i.e. it should not be visible outside the library. + */ +#define simpleops_private +#endif + +#ifdef SIMPLEOPS_EXPORT_PUBLIC +#define simpleops_public _simpleops_export +#else +/** + * An attribute that informs the compiler that the function is public. + * + * When declaring symbols in the library, the + * SIMPLEOPS_EXPORT_PUBLIC macro should be defined, so that the + * public symbols are exported. + * + * When declaring symbols of an external library (usually by + * including its headers), SIMPLEOPS_EXPORT_PUBLIC should NOT be + * defined, so that the functions are imported. + * + * If the symbols being declared belong to a DLL, the SIMPLEOPS_DLL + * macro should be defined, too. + */ +#define simpleops_public _simpleops_import +#endif diff --git a/compiler/simpleops-compiler-utils.h b/compiler/simpleops-compiler-utils.h new file mode 100644 index 0000000..187386d --- /dev/null +++ b/compiler/simpleops-compiler-utils.h @@ -0,0 +1,98 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + */ + +#ifndef SIMPLEOPS_COMPILER_UTILS_H +#define SIMPLEOPS_COMPILER_UTILS_H + +#ifndef SIMPLEOPS_COMPILER_H +#error Private header used directly +#endif + +/** + * Computes the number of elements of an array + */ +#define SIMPLEOPS_ARRAY_LENGTH(x) (sizeof (x) / sizeof (x[0])) + +#define _SIMPLEOPS_CONCAT(x,y) x ## y +#define _SIMPLEOPS_RESOLVE_CONCAT(x,y) _SIMPLEOPS_CONCAT(x,y) + +/** + * Prepends the SIMPLEOPS_PREFIX to the argument. + * + * This macro is used to generate non-static identifiers that do not + * collide across multiple libraries. In order to use it, + * SIMPLEOPS_PREFIX should be defined to an appropriate identifier. + */ +#define SIMPLEOPS_ADD_PREFIX(x) _SIMPLEOPS_RESOLVE_CONCAT(SIMPLEOPS_PREFIX, x) + +#define _SIMPLEOPS_COMPILE_TIME_RAW_ASSERT(condition, line) \ + typedef int compile_time_assertion_at_line_##line##_failed [(condition)?1:-1] +#define _SIMPLEOPS_COMPILE_TIME_RESOLVE_ASSERT(condition, line) \ + _SIMPLEOPS_COMPILE_TIME_RAW_ASSERT(condition, line) + +/** + * Asserts that the condition is TRUE at compile time. + * + * Conditions that cannot be evaluated at compile time cannot be used + * in these assertions. + */ +#define SIMPLEOPS_COMPILE_TIME_ASSERT(condition) \ + _SIMPLEOPS_COMPILE_TIME_RESOLVE_ASSERT(condition, __LINE__) + + +#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) + +#define _SIMPLEOPS_BOOLEAN_EXPR(expr) ((expr) ? TRUE : FALSE) + +#define SIMPLEOPS_LIKELY(expr) \ + (__builtin_expect (_SIMPLEOPS_BOOLEAN_EXPR (expr), TRUE)) +#define SIMPLEOPS_UNLIKELY(expr) \ + (__builtin_expect (_SIMPLEOPS_BOOLEAN_EXPR (expr), FALSE)) + +#else + +/** + * Provides an hint to the compiler that the expression is probably + * going to be TRUE and evaluates to the value of the expression. + */ +#define SIMPLEOPS_LIKELY(expr) (expr) + +/** + * Provides an hint to the compiler that the expression is probably + * going to be FALSE and evaluates to the value of the expression. + */ +#define SIMPLEOPS_UNLIKELY(expr) (expr) +#endif + +/** + * Computes the address of a structure given its type and the address + * and name of one of its fields. + */ +#define SIMPLEOPS_CONTAINER_OF(ptr, type, member) \ + ((type*)(((char *) ptr) - offsetof (type, member))) + +#endif diff --git a/compiler/simpleops-compiler.h b/compiler/simpleops-compiler.h new file mode 100644 index 0000000..d6fe97a --- /dev/null +++ b/compiler/simpleops-compiler.h @@ -0,0 +1,35 @@ +/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */ +/* + * Copyright 2010-2011 Andrea Canciani + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Author(s): Andrea Canciani <ranma42@gmail.com> + */ + +#ifndef SIMPLEOPS_COMPILER_H +#define SIMPLEOPS_COMPILER_H + +#include "simpleops/compiler/simpleops-compiler-config.h" +#include "simpleops/compiler/simpleops-compiler-types.h" +#include "simpleops/compiler/simpleops-compiler-utils.h" + +#endif |