Skip to content
Permalink
Browse files

Import ZStandard build compatibility headers from FreeBSD

These files make ZStandard compile as a kernel module without modifying
the ZStandard source files.

Removed unexpanded SVN version tracking tags from FreeBSD

Signed-off-by: Allan Jude <allanjude@freebsd.org>
  • Loading branch information...
allanjude committed Jul 12, 2019
1 parent 65c2b99 commit 95c878eddba41998bc4812ee7231e19516b5946e
@@ -0,0 +1,6 @@
/* This file is in the public domain */
#if defined(__FreeBSD__)
#include <sys/limits.h>
#elif defined(__linux__)
#include <linux/kernel.h>
#endif
@@ -0,0 +1,2 @@
/* This file is in the public domain */
#include "zstd_kfreebsd.h"
@@ -0,0 +1,2 @@
/* This file is in the public domain */
#include "zstd_kfreebsd.h"
@@ -0,0 +1,2 @@
/* This file is in the public domain */
#include "zstd_kfreebsd.h"
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2014-2019 Allan Jude <allanjude@freebsd.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

#ifndef _ZSTD_FREEBSD_STDLIB_H_
#define _ZSTD_FREEBSD_STDLIB_H_

#ifdef _KERNEL
#include <sys/param.h> /* size_t */
#endif

#if defined(__FreeBSD__) && defined(_KERNEL)

#include <sys/malloc.h>
MALLOC_DECLARE(M_ZSTD);
#undef malloc
#define malloc(x) (malloc)((x), M_ZSTD, M_WAITOK)
#define free(x) (free)((x), M_ZSTD)
#define calloc(a, b) (mallocarray)((a), (b), M_ZSTD, M_WAITOK | M_ZERO)

#elif defined(__linux__) && defined(_KERNEL)

#undef GCC_VERSION
extern void *spl_kmem_alloc(size_t sz, int fl, const char *func, int line);
extern void *spl_kmem_zalloc(size_t sz, int fl, const char *func, int line);
extern void spl_kmem_free(const void *ptr, size_t sz);
#define KM_SLEEP 0x0000 /* can block for memory; success guaranteed */
#define KM_NOSLEEP 0x0001 /* cannot block for memory; may fail */
#define KM_ZERO 0x1000 /* zero the allocation */
#undef malloc
#define malloc(sz) spl_kmem_alloc((sz), KM_SLEEP, __func__, __LINE__)
#define free(ptr) spl_kmem_free((ptr), 0)
#define calloc(n, sz) \
spl_kmem_zalloc((n) * (sz), KM_SLEEP, __func__, __LINE__)

#endif /* _KERNEL */

#endif /* _ZSTD_FREEBSD_STDLIB_H_ */
@@ -0,0 +1,2 @@
/* This file is in the public domain */
#include "zstd_kfreebsd.h"
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2018 Conrad Meyer <cem@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/

#include "zstd_kfreebsd.h"
#include <sys/param.h>
#include <sys/endian.h>

#ifndef _KERNEL
#include <strings.h>
#endif

/*
* The kernel as a standalone target does not link against libgcc or
* libcompiler-rt. On platforms (e.g., MIPS and RISCV) that do not have a
* direct assembly implementation of the relevant builtin functions that zstd
* references, the compiler converts them into calls to the runtime library
* intrinsics. Since the kernel does not link against the runtime libraries,
* this results in a failure to link the kernel.
*
* The goal of the following definitions is to use supported kernel constructs
* to implement the same functionality, without adding diff to the Zstd contrib
* code.
*
* A subsequent enhancement might create a mini compiler-rt library for kernel
* use and move these over there instead.
*/

/* Swap endianness */
int
__bswapsi2(int x)
{
return (bswap32(x));
}

long long
__bswapdi2(long long x)
{
return (bswap64(x));
}

/* Count trailing zeros */
int
__ctzsi2(int x)
{
if (x == 0)
return (sizeof (x) * NBBY);
return (ffs(x) - 1);
}

long long
__ctzdi2(long long x)
{
if (x == 0)
return (sizeof (x) * NBBY);
return (ffsll(x) - 1);
}

/* Count leading zeros */
int
__clzsi2(int x)
{
return (sizeof (x) * NBBY - fls(x));
}

long long
__clzdi2(long long x)
{
return (sizeof (x) * NBBY - flsll(x));
}
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2014-2019 Allan Jude <allanjude@freebsd.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

#ifndef ZSTD_KFREEBSD_H
#define ZSTD_KFREEBSD_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _KERNEL
#include <sys/param.h> /* size_t */
#if defined(__FreeBSD__)
#include <sys/systm.h> /* memcpy, memset */
#elif defined(__linux__)
#include <linux/string.h> /* memcpy, memset */
#endif /* __FreeBSD__ */
#else /* !_KERNEL */
#include <stddef.h> /* size_t, ptrdiff_t */
#include <stdlib.h>
#include <string.h>
#endif /* _KERNEL */

#ifdef __cplusplus
}
#endif

#endif /* ZSTD_KFREEBSD_H */
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016-2019 Allan Jude <allanjude@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>

MALLOC_DEFINE(M_ZSTD, "zstd", "ZSTD Compressor");

/*
* Build zstd lib/common/debug.c (single extern variable) while avoiding
* conflict with Xen's debug.c in objdir.
*/
#include "debug.c"

0 comments on commit 95c878e

Please sign in to comment.
You can’t perform that action at this time.