Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
0002 /*
0003  * Copyright (c) Facebook, Inc.
0004  * All rights reserved.
0005  *
0006  * This source code is licensed under both the BSD-style license (found in the
0007  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
0008  * in the COPYING file in the root directory of this source tree).
0009  * You may select, at your option, one of the above-listed licenses.
0010  */
0011 
0012 /*
0013  * This file provides common libc dependencies that zstd requires.
0014  * The purpose is to allow replacing this file with a custom implementation
0015  * to compile zstd without libc support.
0016  */
0017 
0018 /* Need:
0019  * NULL
0020  * INT_MAX
0021  * UINT_MAX
0022  * ZSTD_memcpy()
0023  * ZSTD_memset()
0024  * ZSTD_memmove()
0025  */
0026 #ifndef ZSTD_DEPS_COMMON
0027 #define ZSTD_DEPS_COMMON
0028 
0029 #include <linux/limits.h>
0030 #include <linux/stddef.h>
0031 
0032 #define ZSTD_memcpy(d,s,n) __builtin_memcpy((d),(s),(n))
0033 #define ZSTD_memmove(d,s,n) __builtin_memmove((d),(s),(n))
0034 #define ZSTD_memset(d,s,n) __builtin_memset((d),(s),(n))
0035 
0036 #endif /* ZSTD_DEPS_COMMON */
0037 
0038 /*
0039  * Define malloc as always failing. That means the user must
0040  * either use ZSTD_customMem or statically allocate memory.
0041  * Need:
0042  * ZSTD_malloc()
0043  * ZSTD_free()
0044  * ZSTD_calloc()
0045  */
0046 #ifdef ZSTD_DEPS_NEED_MALLOC
0047 #ifndef ZSTD_DEPS_MALLOC
0048 #define ZSTD_DEPS_MALLOC
0049 
0050 #define ZSTD_malloc(s) ({ (void)(s); NULL; })
0051 #define ZSTD_free(p) ((void)(p))
0052 #define ZSTD_calloc(n,s) ({ (void)(n); (void)(s); NULL; })
0053 
0054 #endif /* ZSTD_DEPS_MALLOC */
0055 #endif /* ZSTD_DEPS_NEED_MALLOC */
0056 
0057 /*
0058  * Provides 64-bit math support.
0059  * Need:
0060  * U64 ZSTD_div64(U64 dividend, U32 divisor)
0061  */
0062 #ifdef ZSTD_DEPS_NEED_MATH64
0063 #ifndef ZSTD_DEPS_MATH64
0064 #define ZSTD_DEPS_MATH64
0065 
0066 #include <linux/math64.h>
0067 
0068 static uint64_t ZSTD_div64(uint64_t dividend, uint32_t divisor) {
0069   return div_u64(dividend, divisor);
0070 }
0071 
0072 #endif /* ZSTD_DEPS_MATH64 */
0073 #endif /* ZSTD_DEPS_NEED_MATH64 */
0074 
0075 /*
0076  * This is only requested when DEBUGLEVEL >= 1, meaning
0077  * it is disabled in production.
0078  * Need:
0079  * assert()
0080  */
0081 #ifdef ZSTD_DEPS_NEED_ASSERT
0082 #ifndef ZSTD_DEPS_ASSERT
0083 #define ZSTD_DEPS_ASSERT
0084 
0085 #include <linux/kernel.h>
0086 
0087 #define assert(x) WARN_ON((x))
0088 
0089 #endif /* ZSTD_DEPS_ASSERT */
0090 #endif /* ZSTD_DEPS_NEED_ASSERT */
0091 
0092 /*
0093  * This is only requested when DEBUGLEVEL >= 2, meaning
0094  * it is disabled in production.
0095  * Need:
0096  * ZSTD_DEBUG_PRINT()
0097  */
0098 #ifdef ZSTD_DEPS_NEED_IO
0099 #ifndef ZSTD_DEPS_IO
0100 #define ZSTD_DEPS_IO
0101 
0102 #include <linux/printk.h>
0103 
0104 #define ZSTD_DEBUG_PRINT(...) pr_debug(__VA_ARGS__)
0105 
0106 #endif /* ZSTD_DEPS_IO */
0107 #endif /* ZSTD_DEPS_NEED_IO */
0108 
0109 /*
0110  * Only requested when MSAN is enabled.
0111  * Need:
0112  * intptr_t
0113  */
0114 #ifdef ZSTD_DEPS_NEED_STDINT
0115 #ifndef ZSTD_DEPS_STDINT
0116 #define ZSTD_DEPS_STDINT
0117 
0118 /*
0119  * The Linux Kernel doesn't provide intptr_t, only uintptr_t, which
0120  * is an unsigned long.
0121  */
0122 typedef long intptr_t;
0123 
0124 #endif /* ZSTD_DEPS_STDINT */
0125 #endif /* ZSTD_DEPS_NEED_STDINT */