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 #  error "lib_common.h must always be included before libdeflate.h"
10    /* because BUILDING_LIBDEFLATE must be set first */
11 #endif
12 
13 #define BUILDING_LIBDEFLATE
14 
15 #include "../common/common_defs.h"
16 
17 /*
18  * Prefix with "_libdeflate_" all global symbols which are not part of the API
19  * and don't already have a "libdeflate" prefix.  This avoids exposing overly
20  * generic names when libdeflate is built as a static library.
21  *
22  * Note that the chosen prefix is not really important and can be changed
23  * without breaking library users.  It was just chosen so that the resulting
24  * symbol names are unlikely to conflict with those from any other software.
25  * Also note that this fixup has no useful effect when libdeflate is built as a
26  * shared library, since these symbols are not exported.
27  */
28 #define SYM_FIXUP(sym)			_libdeflate_##sym
29 #define deflate_get_compression_level	SYM_FIXUP(deflate_get_compression_level)
30 #define _cpu_features			SYM_FIXUP(_cpu_features)
31 #define setup_cpu_features		SYM_FIXUP(setup_cpu_features)
32 
33 void *libdeflate_malloc(size_t size);
34 void libdeflate_free(void *ptr);
35 
36 void *libdeflate_aligned_malloc(size_t alignment, size_t size);
37 void libdeflate_aligned_free(void *ptr);
38 
39 #ifdef FREESTANDING
40 /*
41  * With -ffreestanding, <string.h> may be missing, and we must provide
42  * implementations of memset(), memcpy(), memmove(), and memcmp().
43  * See https://gcc.gnu.org/onlinedocs/gcc/Standards.html
44  *
45  * Also, -ffreestanding disables interpreting calls to these functions as
46  * built-ins.  E.g., calling memcpy(&v, p, WORDBYTES) will make a function call,
47  * not be optimized to a single load instruction.  For performance reasons we
48  * don't want that.  So, declare these functions as macros that expand to the
49  * corresponding built-ins.  This approach is recommended in the gcc man page.
50  * We still need the actual function definitions in case gcc calls them.
51  */
52 void *memset(void *s, int c, size_t n);
53 #define memset(s, c, n)		__builtin_memset((s), (c), (n))
54 
55 void *memcpy(void *dest, const void *src, size_t n);
56 #define memcpy(dest, src, n)	__builtin_memcpy((dest), (src), (n))
57 
58 void *memmove(void *dest, const void *src, size_t n);
59 #define memmove(dest, src, n)	__builtin_memmove((dest), (src), (n))
60 
61 int memcmp(const void *s1, const void *s2, size_t n);
62 #define memcmp(s1, s2, n)	__builtin_memcmp((s1), (s2), (n))
63 #else
64 #include <string.h>
65 #endif
66 
67 #endif /* LIB_LIB_COMMON_H */
68