xref: /linux/lib/zstd/common/zstd_deps.h (revision 3db55767)
1 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
2 /*
3  * Copyright (c) Facebook, Inc.
4  * All rights reserved.
5  *
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.
10  */
11 
12 /*
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.
16  */
17 
18 /* Need:
19  * NULL
20  * INT_MAX
21  * UINT_MAX
22  * ZSTD_memcpy()
23  * ZSTD_memset()
24  * ZSTD_memmove()
25  */
26 #ifndef ZSTD_DEPS_COMMON
27 #define ZSTD_DEPS_COMMON
28 
29 #include <linux/limits.h>
30 #include <linux/stddef.h>
31 
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))
35 
36 #endif /* ZSTD_DEPS_COMMON */
37 
38 /*
39  * Define malloc as always failing. That means the user must
40  * either use ZSTD_customMem or statically allocate memory.
41  * Need:
42  * ZSTD_malloc()
43  * ZSTD_free()
44  * ZSTD_calloc()
45  */
46 #ifdef ZSTD_DEPS_NEED_MALLOC
47 #ifndef ZSTD_DEPS_MALLOC
48 #define ZSTD_DEPS_MALLOC
49 
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; })
53 
54 #endif /* ZSTD_DEPS_MALLOC */
55 #endif /* ZSTD_DEPS_NEED_MALLOC */
56 
57 /*
58  * Provides 64-bit math support.
59  * Need:
60  * U64 ZSTD_div64(U64 dividend, U32 divisor)
61  */
62 #ifdef ZSTD_DEPS_NEED_MATH64
63 #ifndef ZSTD_DEPS_MATH64
64 #define ZSTD_DEPS_MATH64
65 
66 #include <linux/math64.h>
67 
ZSTD_div64(uint64_t dividend,uint32_t divisor)68 static uint64_t ZSTD_div64(uint64_t dividend, uint32_t divisor) {
69   return div_u64(dividend, divisor);
70 }
71 
72 #endif /* ZSTD_DEPS_MATH64 */
73 #endif /* ZSTD_DEPS_NEED_MATH64 */
74 
75 /*
76  * This is only requested when DEBUGLEVEL >= 1, meaning
77  * it is disabled in production.
78  * Need:
79  * assert()
80  */
81 #ifdef ZSTD_DEPS_NEED_ASSERT
82 #ifndef ZSTD_DEPS_ASSERT
83 #define ZSTD_DEPS_ASSERT
84 
85 #include <linux/kernel.h>
86 
87 #define assert(x) WARN_ON(!(x))
88 
89 #endif /* ZSTD_DEPS_ASSERT */
90 #endif /* ZSTD_DEPS_NEED_ASSERT */
91 
92 /*
93  * This is only requested when DEBUGLEVEL >= 2, meaning
94  * it is disabled in production.
95  * Need:
96  * ZSTD_DEBUG_PRINT()
97  */
98 #ifdef ZSTD_DEPS_NEED_IO
99 #ifndef ZSTD_DEPS_IO
100 #define ZSTD_DEPS_IO
101 
102 #include <linux/printk.h>
103 
104 #define ZSTD_DEBUG_PRINT(...) pr_debug(__VA_ARGS__)
105 
106 #endif /* ZSTD_DEPS_IO */
107 #endif /* ZSTD_DEPS_NEED_IO */
108