| 1 |
/* |
| 2 |
* lib_common.h - internal header included by all library code |
| 3 |
*/ |
| 4 |
|
| 5 |
#ifndef LIB_LIB_COMMON_H |
| 6 |
#define LIB_LIB_COMMON_H |
| 7 |
|
| 8 |
#ifdef LIBDEFLATE_H |
| 9 |
/* |
| 10 |
* When building the library, LIBDEFLATEAPI needs to be defined properly before |
| 11 |
* including libdeflate.h. |
| 12 |
*/ |
| 13 |
# error "lib_common.h must always be included before libdeflate.h" |
| 14 |
#endif |
| 15 |
|
| 16 |
#if defined(LIBDEFLATE_DLL) && (defined(_WIN32) || defined(__CYGWIN__)) |
| 17 |
# define LIBDEFLATE_EXPORT_SYM __declspec(dllexport) |
| 18 |
#elif defined(__GNUC__) |
| 19 |
# define LIBDEFLATE_EXPORT_SYM __attribute__((visibility("default"))) |
| 20 |
#else |
| 21 |
# define LIBDEFLATE_EXPORT_SYM |
| 22 |
#endif |
| 23 |
|
| 24 |
/* |
| 25 |
* On i386, gcc assumes that the stack is 16-byte aligned at function entry. |
| 26 |
* However, some compilers (e.g. MSVC) and programming languages (e.g. Delphi) |
| 27 |
* only guarantee 4-byte alignment when calling functions. This is mainly an |
| 28 |
* issue on Windows, but it has been seen on Linux too. Work around this ABI |
| 29 |
* incompatibility by realigning the stack pointer when entering libdeflate. |
| 30 |
* This prevents crashes in SSE/AVX code. |
| 31 |
*/ |
| 32 |
#if defined(__GNUC__) && defined(__i386__) |
| 33 |
# define LIBDEFLATE_ALIGN_STACK __attribute__((force_align_arg_pointer)) |
| 34 |
#else |
| 35 |
# define LIBDEFLATE_ALIGN_STACK |
| 36 |
#endif |
| 37 |
|
| 38 |
#define LIBDEFLATEAPI LIBDEFLATE_EXPORT_SYM LIBDEFLATE_ALIGN_STACK |
| 39 |
|
| 40 |
#include "../common_defs.h" |
| 41 |
|
| 42 |
typedef void *(*malloc_func_t)(size_t); |
| 43 |
typedef void (*free_func_t)(void *); |
| 44 |
|
| 45 |
extern malloc_func_t libdeflate_default_malloc_func; |
| 46 |
extern free_func_t libdeflate_default_free_func; |
| 47 |
|
| 48 |
void *libdeflate_aligned_malloc(malloc_func_t malloc_func, |
| 49 |
size_t alignment, size_t size); |
| 50 |
void libdeflate_aligned_free(free_func_t free_func, void *ptr); |
| 51 |
|
| 52 |
#ifdef FREESTANDING |
| 53 |
/* |
| 54 |
* With -ffreestanding, <string.h> may be missing, and we must provide |
| 55 |
* implementations of memset(), memcpy(), memmove(), and memcmp(). |
| 56 |
* See https://gcc.gnu.org/onlinedocs/gcc/Standards.html |
| 57 |
* |
| 58 |
* Also, -ffreestanding disables interpreting calls to these functions as |
| 59 |
* built-ins. E.g., calling memcpy(&v, p, WORDBYTES) will make a function call, |
| 60 |
* not be optimized to a single load instruction. For performance reasons we |
| 61 |
* don't want that. So, declare these functions as macros that expand to the |
| 62 |
* corresponding built-ins. This approach is recommended in the gcc man page. |
| 63 |
* We still need the actual function definitions in case gcc calls them. |
| 64 |
*/ |
| 65 |
void *memset(void *s, int c, size_t n); |
| 66 |
#define memset(s, c, n) __builtin_memset((s), (c), (n)) |
| 67 |
|
| 68 |
void *memcpy(void *dest, const void *src, size_t n); |
| 69 |
#define memcpy(dest, src, n) __builtin_memcpy((dest), (src), (n)) |
| 70 |
|
| 71 |
void *memmove(void *dest, const void *src, size_t n); |
| 72 |
#define memmove(dest, src, n) __builtin_memmove((dest), (src), (n)) |
| 73 |
|
| 74 |
int memcmp(const void *s1, const void *s2, size_t n); |
| 75 |
#define memcmp(s1, s2, n) __builtin_memcmp((s1), (s2), (n)) |
| 76 |
|
| 77 |
#undef LIBDEFLATE_ENABLE_ASSERTIONS |
| 78 |
#else |
| 79 |
# include <string.h> |
| 80 |
/* |
| 81 |
* To prevent false positive static analyzer warnings, ensure that assertions |
| 82 |
* are visible to the static analyzer. |
| 83 |
*/ |
| 84 |
# ifdef __clang_analyzer__ |
| 85 |
# define LIBDEFLATE_ENABLE_ASSERTIONS |
| 86 |
# endif |
| 87 |
#endif |
| 88 |
|
| 89 |
/* |
| 90 |
* Runtime assertion support. Don't enable this in production builds; it may |
| 91 |
* hurt performance significantly. |
| 92 |
*/ |
| 93 |
#ifdef LIBDEFLATE_ENABLE_ASSERTIONS |
| 94 |
NORETURN void |
| 95 |
libdeflate_assertion_failed(const char *expr, const char *file, int line); |
| 96 |
#define ASSERT(expr) { if (unlikely(!(expr))) \ |
| 97 |
libdeflate_assertion_failed(#expr, __FILE__, __LINE__); } |
| 98 |
#else |
| 99 |
#define ASSERT(expr) (void)(expr) |
| 100 |
#endif |
| 101 |
|
| 102 |
#define CONCAT_IMPL(a, b) a##b |
| 103 |
#define CONCAT(a, b) CONCAT_IMPL(a, b) |
| 104 |
#define ADD_SUFFIX(name) CONCAT(name, SUFFIX) |
| 105 |
|
| 106 |
#endif /* LIB_LIB_COMMON_H */ |