| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This source code is licensed under both the BSD-style license (found in the |
| 6 |
* LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 |
* in the COPYING file in the root directory of this source tree). |
| 8 |
* You may select, at your option, one of the above-listed licenses. |
| 9 |
*/ |
| 10 |
|
| 11 |
/* This file provides common libc dependencies that zstd requires. |
| 12 |
* The purpose is to allow replacing this file with a custom implementation |
| 13 |
* to compile zstd without libc support. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* Need: |
| 17 |
* NULL |
| 18 |
* INT_MAX |
| 19 |
* UINT_MAX |
| 20 |
* ZSTD_memcpy() |
| 21 |
* ZSTD_memset() |
| 22 |
* ZSTD_memmove() |
| 23 |
*/ |
| 24 |
#ifndef ZSTD_DEPS_COMMON |
| 25 |
#define ZSTD_DEPS_COMMON |
| 26 |
|
| 27 |
/* Even though we use qsort_r only for the dictionary builder, the macro |
| 28 |
* _GNU_SOURCE has to be declared *before* the inclusion of any standard |
| 29 |
* header and the script 'combine.sh' combines the whole zstd source code |
| 30 |
* in a single file. |
| 31 |
*/ |
| 32 |
#if defined(__linux) || defined(__linux__) || defined(linux) || defined(__gnu_linux__) || \ |
| 33 |
defined(__CYGWIN__) || defined(__MSYS__) |
| 34 |
#if !defined(_GNU_SOURCE) && !defined(__ANDROID__) /* NDK doesn't ship qsort_r(). */ |
| 35 |
#define _GNU_SOURCE |
| 36 |
#endif |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <limits.h> |
| 40 |
#include <stddef.h> |
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
#if defined(__GNUC__) && __GNUC__ >= 4 |
| 44 |
# define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l)) |
| 45 |
# define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l)) |
| 46 |
# define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l)) |
| 47 |
#else |
| 48 |
# define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l)) |
| 49 |
# define ZSTD_memmove(d,s,l) memmove((d),(s),(l)) |
| 50 |
# define ZSTD_memset(p,v,l) memset((p),(v),(l)) |
| 51 |
#endif |
| 52 |
|
| 53 |
#endif /* ZSTD_DEPS_COMMON */ |
| 54 |
|
| 55 |
/* Need: |
| 56 |
* ZSTD_malloc() |
| 57 |
* ZSTD_free() |
| 58 |
* ZSTD_calloc() |
| 59 |
*/ |
| 60 |
#ifdef ZSTD_DEPS_NEED_MALLOC |
| 61 |
#ifndef ZSTD_DEPS_MALLOC |
| 62 |
#define ZSTD_DEPS_MALLOC |
| 63 |
|
| 64 |
#include <stdlib.h> |
| 65 |
|
| 66 |
#define ZSTD_malloc(s) malloc(s) |
| 67 |
#define ZSTD_calloc(n,s) calloc((n), (s)) |
| 68 |
#define ZSTD_free(p) free((p)) |
| 69 |
|
| 70 |
#endif /* ZSTD_DEPS_MALLOC */ |
| 71 |
#endif /* ZSTD_DEPS_NEED_MALLOC */ |
| 72 |
|
| 73 |
/* |
| 74 |
* Provides 64-bit math support. |
| 75 |
* Need: |
| 76 |
* U64 ZSTD_div64(U64 dividend, U32 divisor) |
| 77 |
*/ |
| 78 |
#ifdef ZSTD_DEPS_NEED_MATH64 |
| 79 |
#ifndef ZSTD_DEPS_MATH64 |
| 80 |
#define ZSTD_DEPS_MATH64 |
| 81 |
|
| 82 |
#define ZSTD_div64(dividend, divisor) ((dividend) / (divisor)) |
| 83 |
|
| 84 |
#endif /* ZSTD_DEPS_MATH64 */ |
| 85 |
#endif /* ZSTD_DEPS_NEED_MATH64 */ |
| 86 |
|
| 87 |
/* Need: |
| 88 |
* assert() |
| 89 |
*/ |
| 90 |
#ifdef ZSTD_DEPS_NEED_ASSERT |
| 91 |
#ifndef ZSTD_DEPS_ASSERT |
| 92 |
#define ZSTD_DEPS_ASSERT |
| 93 |
|
| 94 |
#include <assert.h> |
| 95 |
|
| 96 |
#endif /* ZSTD_DEPS_ASSERT */ |
| 97 |
#endif /* ZSTD_DEPS_NEED_ASSERT */ |
| 98 |
|
| 99 |
/* Need: |
| 100 |
* ZSTD_DEBUG_PRINT() |
| 101 |
*/ |
| 102 |
#ifdef ZSTD_DEPS_NEED_IO |
| 103 |
#ifndef ZSTD_DEPS_IO |
| 104 |
#define ZSTD_DEPS_IO |
| 105 |
|
| 106 |
#include <stdio.h> |
| 107 |
#define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__) |
| 108 |
|
| 109 |
#endif /* ZSTD_DEPS_IO */ |
| 110 |
#endif /* ZSTD_DEPS_NEED_IO */ |
| 111 |
|
| 112 |
/* Only requested when <stdint.h> is known to be present. |
| 113 |
* Need: |
| 114 |
* intptr_t |
| 115 |
*/ |
| 116 |
#ifdef ZSTD_DEPS_NEED_STDINT |
| 117 |
#ifndef ZSTD_DEPS_STDINT |
| 118 |
#define ZSTD_DEPS_STDINT |
| 119 |
|
| 120 |
#include <stdint.h> |
| 121 |
|
| 122 |
#endif /* ZSTD_DEPS_STDINT */ |
| 123 |
#endif /* ZSTD_DEPS_NEED_STDINT */ |