/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â 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.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/intrin/atomic.h"
#include "libc/intrin/kmalloc.h"
#include "libc/runtime/internal.h"
#include "libc/x/x.h"
#include "third_party/zlib/zlib.h"

/**
 * Inflates data once atomically, e.g.
 *
 *     void *GetData(void) {
 *       static _Atomic(void *) ptr;
 *       static const unsigned char rodata[] = {...};
 *       return xload(&ptr, rodata, 112, 1024);
 *     }
 *
 * The above is an example of how this helper may be used to have lazy
 * loading of big infrequently accessed image data.
 *
 * @param a points to your static pointer holder
 * @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
 * @return pointer to inflated data
 * @threadsafe
 */
void *xload(_Atomic(void *) *a, const void *p, size_t n, size_t m) {
  void *r, *z;
  if ((r = atomic_load_explicit(a, memory_order_acquire))) return r;
  if (!(r = kmalloc(m))) return 0;
  if (__inflate(r, m, p, n)) return 0;
  z = 0;
  if (!atomic_compare_exchange_strong_explicit(a, &z, r, memory_order_release,
                                               memory_order_relaxed)) {
    r = z;
  }
  return r;
}
