2021-10-02 15:17:04 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2021-09-04 07:58:21 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-10-02 15:17:04 +00:00
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/fmt/leb128.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-08-11 07:15:29 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2021-10-02 15:17:04 +00:00
|
|
|
#include "libc/nexgen32e/crc32.h"
|
2022-06-10 01:48:15 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2021-10-02 15:17:04 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/x/x.h"
|
|
|
|
#include "third_party/zlib/zlib.h"
|
2021-09-04 07:58:21 +00:00
|
|
|
|
2021-10-02 15:17:04 +00:00
|
|
|
/**
|
|
|
|
* Loads δzd encoded data once atomically.
|
|
|
|
*
|
2022-11-02 05:36:03 +00:00
|
|
|
* @param a points to your static pointer holder
|
2021-10-02 15:17:04 +00:00
|
|
|
* @param p is read-only data compressed using raw deflate
|
|
|
|
* @param n is byte length of deflated data
|
|
|
|
* @param m is byte length of inflated data
|
|
|
|
* @param c is number of items in array
|
|
|
|
* @param z is byte length of items
|
|
|
|
* @param s is crc32 checksum
|
|
|
|
* @return pointer to decoded data
|
|
|
|
* @threadsafe
|
|
|
|
*/
|
2023-04-27 03:45:01 +00:00
|
|
|
void *xloadzd(_Atomic(void *) *a, const void *p, size_t n, size_t m, size_t c,
|
2021-10-02 15:17:04 +00:00
|
|
|
size_t z, uint32_t s) {
|
|
|
|
size_t i;
|
|
|
|
char *q, *b;
|
2022-11-02 05:36:03 +00:00
|
|
|
void *r, *g;
|
2021-10-02 15:17:04 +00:00
|
|
|
int64_t x, y;
|
2022-11-02 05:36:03 +00:00
|
|
|
if ((r = atomic_load_explicit(a, memory_order_acquire))) return r;
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(z == 2 || z == 4);
|
2022-11-02 05:36:03 +00:00
|
|
|
if (!(b = q = malloc(m))) return 0;
|
|
|
|
if (__inflate(q, m, p, n)) {
|
|
|
|
free(q);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-09-12 04:34:53 +00:00
|
|
|
if (!(r = malloc(c * z))) {
|
2022-11-02 05:36:03 +00:00
|
|
|
free(q);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-10-02 15:17:04 +00:00
|
|
|
for (x = i = 0; i < c; ++i) {
|
|
|
|
b += unzleb64(b, 10, &y);
|
|
|
|
x += y;
|
|
|
|
if (z == 2) {
|
|
|
|
((uint16_t *)r)[i] = x;
|
|
|
|
} else {
|
|
|
|
((uint32_t *)r)[i] = x;
|
|
|
|
}
|
2021-09-04 07:58:21 +00:00
|
|
|
}
|
2021-10-02 15:17:04 +00:00
|
|
|
free(q);
|
2022-11-02 05:36:03 +00:00
|
|
|
assert(crc32_z(0, r, c * z) == s);
|
|
|
|
g = 0;
|
|
|
|
if (!atomic_compare_exchange_strong_explicit(a, &g, r, memory_order_relaxed,
|
|
|
|
memory_order_relaxed)) {
|
|
|
|
r = g;
|
2021-09-04 07:58:21 +00:00
|
|
|
}
|
2022-11-02 05:36:03 +00:00
|
|
|
return r;
|
2021-09-04 07:58:21 +00:00
|
|
|
}
|