1 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
3 * Copyright (c) Facebook, Inc.
6 * This source code is licensed under both the BSD-style license (found in the
7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8 * in the COPYING file in the root directory of this source tree).
9 * You may select, at your option, one of the above-listed licenses.
13 * This file provides common libc dependencies that zstd requires.
14 * The purpose is to allow replacing this file with a custom implementation
15 * to compile zstd without libc support.
26 #ifndef ZSTD_DEPS_COMMON
27 #define ZSTD_DEPS_COMMON
29 #include <linux/limits.h>
30 #include <linux/stddef.h>
32 #define ZSTD_memcpy(d,s,n) __builtin_memcpy((d),(s),(n))
33 #define ZSTD_memmove(d,s,n) __builtin_memmove((d),(s),(n))
34 #define ZSTD_memset(d,s,n) __builtin_memset((d),(s),(n))
36 #endif /* ZSTD_DEPS_COMMON */
39 * Define malloc as always failing. That means the user must
40 * either use ZSTD_customMem or statically allocate memory.
46 #ifdef ZSTD_DEPS_NEED_MALLOC
47 #ifndef ZSTD_DEPS_MALLOC
48 #define ZSTD_DEPS_MALLOC
50 #define ZSTD_malloc(s) ({ (void)(s); NULL; })
51 #define ZSTD_free(p) ((void)(p))
52 #define ZSTD_calloc(n,s) ({ (void)(n); (void)(s); NULL; })
54 #endif /* ZSTD_DEPS_MALLOC */
55 #endif /* ZSTD_DEPS_NEED_MALLOC */
58 * Provides 64-bit math support.
60 * U64 ZSTD_div64(U64 dividend, U32 divisor)
62 #ifdef ZSTD_DEPS_NEED_MATH64
63 #ifndef ZSTD_DEPS_MATH64
64 #define ZSTD_DEPS_MATH64
66 #include <linux/math64.h>
68 static uint64_t ZSTD_div64(uint64_t dividend, uint32_t divisor) {
69 return div_u64(dividend, divisor);
72 #endif /* ZSTD_DEPS_MATH64 */
73 #endif /* ZSTD_DEPS_NEED_MATH64 */
76 * This is only requested when DEBUGLEVEL >= 1, meaning
77 * it is disabled in production.
81 #ifdef ZSTD_DEPS_NEED_ASSERT
82 #ifndef ZSTD_DEPS_ASSERT
83 #define ZSTD_DEPS_ASSERT
85 #include <linux/kernel.h>
87 #define assert(x) WARN_ON((x))
89 #endif /* ZSTD_DEPS_ASSERT */
90 #endif /* ZSTD_DEPS_NEED_ASSERT */
93 * This is only requested when DEBUGLEVEL >= 2, meaning
94 * it is disabled in production.
98 #ifdef ZSTD_DEPS_NEED_IO
102 #include <linux/printk.h>
104 #define ZSTD_DEBUG_PRINT(...) pr_debug(__VA_ARGS__)
106 #endif /* ZSTD_DEPS_IO */
107 #endif /* ZSTD_DEPS_NEED_IO */
110 * Only requested when MSAN is enabled.
114 #ifdef ZSTD_DEPS_NEED_STDINT
115 #ifndef ZSTD_DEPS_STDINT
116 #define ZSTD_DEPS_STDINT
119 * The Linux Kernel doesn't provide intptr_t, only uintptr_t, which
120 * is an unsigned long.
122 typedef long intptr_t;
124 #endif /* ZSTD_DEPS_STDINT */
125 #endif /* ZSTD_DEPS_NEED_STDINT */