The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/uzip/g_uzip_zstd.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/errno.h>
   34 #include <sys/kernel.h>
   35 #include <sys/malloc.h>
   36 #include <sys/sysctl.h>
   37 
   38 #include <geom/uzip/g_uzip.h>
   39 #include <geom/uzip/g_uzip_dapi.h>
   40 #include <geom/uzip/g_uzip_zstd.h>
   41 
   42 /*
   43  * We don't actually need any static-link ABI, just want to use "experimental"
   44  * custom malloc/free APIs.
   45  */
   46 #define ZSTD_STATIC_LINKING_ONLY
   47 #include <contrib/zstd/lib/zstd.h>
   48 
   49 FEATURE(geom_uzip_zstd, "g_uzip Zstd support");
   50 
   51 struct g_uzip_zstd {
   52         struct g_uzip_dapi      guz_pub;
   53         uint32_t                guz_blksz;
   54         ZSTD_DCtx               *guz_dctx;
   55 };
   56 
   57 #ifndef container_of
   58 #define container_of(ptr, type, member)                         \
   59 ({                                                              \
   60         const __typeof(((type *)0)->member) *__p = (ptr);       \
   61         (type *)((uintptr_t)__p - offsetof(type, member));      \
   62 })
   63 #endif
   64 #define to_zstd_softc(zpp)      container_of(zpp, struct g_uzip_zstd, guz_pub)
   65 
   66 static int
   67 guz_zstd_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *input,
   68     size_t ilen, void *outputbuf)
   69 {
   70         struct g_uzip_zstd *sc;
   71         size_t rc;
   72 
   73         sc = to_zstd_softc(zpp);
   74         rc = ZSTD_decompressDCtx(sc->guz_dctx, outputbuf, sc->guz_blksz, input,
   75             ilen);
   76         if (ZSTD_isError(rc)) {
   77                 printf("%s: UZIP(zstd) decompress failed: %s\n", gp_name,
   78                     ZSTD_getErrorName(rc));
   79                 return (EIO);
   80         }
   81         KASSERT(rc == sc->guz_blksz, ("%s: Expected %u bytes, got %zu",
   82             __func__, sc->guz_blksz, rc));
   83         return (0);
   84 }
   85 
   86 static void
   87 guz_zstd_free(struct g_uzip_dapi *zpp)
   88 {
   89         struct g_uzip_zstd *sc;
   90         size_t rc;
   91 
   92         sc = to_zstd_softc(zpp);
   93         rc = ZSTD_freeDCtx(sc->guz_dctx);
   94         if (ZSTD_isError(rc))
   95                 printf("%s: UZIP(zstd) free failed: %s\n", __func__,
   96                     ZSTD_getErrorName(rc));
   97 
   98         free(sc, M_GEOM_UZIP);
   99 }
  100 
  101 static int
  102 guz_zstd_rewind(struct g_uzip_dapi *zpp, const char *gp_name)
  103 {
  104         struct g_uzip_zstd *sc;
  105         size_t rc;
  106 
  107         sc = to_zstd_softc(zpp);
  108         rc = ZSTD_DCtx_reset(sc->guz_dctx, ZSTD_reset_session_and_parameters);
  109         if (ZSTD_isError(rc)) {
  110                 printf("%s: UZIP(zstd) rewind failed: %s\n", gp_name,
  111                     ZSTD_getErrorName(rc));
  112                 return (EIO);
  113         }
  114         return (0);
  115 }
  116 
  117 static void *
  118 zstd_alloc(void *opaque, size_t size)
  119 {
  120         return (malloc(size, opaque, M_WAITOK));
  121 }
  122 
  123 static void
  124 zstd_free(void *opaque, void *address)
  125 {
  126         free(address, opaque);
  127 }
  128 
  129 static const ZSTD_customMem zstd_guz_alloc = {
  130         .customAlloc = zstd_alloc,
  131         .customFree = zstd_free,
  132         .opaque = M_GEOM_UZIP,
  133 };
  134 
  135 struct g_uzip_dapi *
  136 g_uzip_zstd_ctor(uint32_t blksz)
  137 {
  138         struct g_uzip_zstd *sc;
  139 
  140         sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
  141 
  142         sc->guz_dctx = ZSTD_createDCtx_advanced(zstd_guz_alloc);
  143         if (sc->guz_dctx == NULL) {
  144                 printf("%s: ZSTD_createDCtx_advanced failed\n", __func__);
  145                 free(sc, M_GEOM_UZIP);
  146                 return (NULL);
  147         }
  148 
  149         sc->guz_blksz = blksz;
  150         sc->guz_pub.max_blen = ZSTD_compressBound(blksz);
  151         sc->guz_pub.decompress = guz_zstd_decompress;
  152         sc->guz_pub.free = guz_zstd_free;
  153         sc->guz_pub.rewind = guz_zstd_rewind;
  154         sc->guz_pub.pvt = NULL;
  155 
  156         return (&sc->guz_pub);
  157 }

Cache object: 8a0f0709799f0724b8977156c2f9520c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.