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#include <limits.h>
28#include <stddef.h>
29#include <string.h>
30
31#if defined(__GNUC__) && __GNUC__ >= 4
32# define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
33# define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
34# define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
35#else
36# define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
37# define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
38# define ZSTD_memset(p,v,l) memset((p),(v),(l))
39#endif
40
41#endif /* ZSTD_DEPS_COMMON */
42
43/* Need:
44 * ZSTD_malloc()
45 * ZSTD_free()
46 * ZSTD_calloc()
47 */
48#ifdef ZSTD_DEPS_NEED_MALLOC
49#ifndef ZSTD_DEPS_MALLOC
50#define ZSTD_DEPS_MALLOC
51
52#include <stdlib.h>
53
54#define ZSTD_malloc(s) malloc(s)
55#define ZSTD_calloc(n,s) calloc((n), (s))
56#define ZSTD_free(p) free((p))
57
58#endif /* ZSTD_DEPS_MALLOC */
59#endif /* ZSTD_DEPS_NEED_MALLOC */
60
61/*
62 * Provides 64-bit math support.
63 * Need:
64 * U64 ZSTD_div64(U64 dividend, U32 divisor)
65 */
66#ifdef ZSTD_DEPS_NEED_MATH64
67#ifndef ZSTD_DEPS_MATH64
68#define ZSTD_DEPS_MATH64
69
70#define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
71
72#endif /* ZSTD_DEPS_MATH64 */
73#endif /* ZSTD_DEPS_NEED_MATH64 */
74
75/* Need:
76 * assert()
77 */
78#ifdef ZSTD_DEPS_NEED_ASSERT
79#ifndef ZSTD_DEPS_ASSERT
80#define ZSTD_DEPS_ASSERT
81
82#include <assert.h>
83
84#endif /* ZSTD_DEPS_ASSERT */
85#endif /* ZSTD_DEPS_NEED_ASSERT */
86
87/* Need:
88 * ZSTD_DEBUG_PRINT()
89 */
90#ifdef ZSTD_DEPS_NEED_IO
91#ifndef ZSTD_DEPS_IO
92#define ZSTD_DEPS_IO
93
94#include <stdio.h>
95#define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
96
97#endif /* ZSTD_DEPS_IO */
98#endif /* ZSTD_DEPS_NEED_IO */
99
100/* Only requested when <stdint.h> is known to be present.
101 * Need:
102 * intptr_t
103 */
104#ifdef ZSTD_DEPS_NEED_STDINT
105#ifndef ZSTD_DEPS_STDINT
106#define ZSTD_DEPS_STDINT
107
108#include <stdint.h>
109
110#endif /* ZSTD_DEPS_STDINT */
111#endif /* ZSTD_DEPS_NEED_STDINT */
112