/*
  Copyright 2011-2021 David Robillard <d@drobilla.net>

  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.

  THIS 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.
*/

#ifndef SERD_MEMORY_H
#define SERD_MEMORY_H

#include "serd/serd.h"

#include <stddef.h>
#include <string.h>

// Allocator convenience wrappers that fall back to the default for NULL

/// Convenience wrapper that defers to malloc() if allocator is null
static inline void*
serd_amalloc(SerdAllocator* const allocator, const size_t size)
{
  SerdAllocator* const actual =
    allocator ? allocator : serd_default_allocator();

  return actual->malloc(actual, size);
}

/// Convenience wrapper that defers to calloc() if allocator is null
static inline void*
serd_acalloc(SerdAllocator* const allocator,
             const size_t         nmemb,
             const size_t         size)
{
  SerdAllocator* const actual =
    allocator ? allocator : serd_default_allocator();

  return actual->calloc(actual, nmemb, size);
}

/// Convenience wrapper that defers to realloc() if allocator is null
static inline void*
serd_arealloc(SerdAllocator* const allocator,
              void* const          ptr,
              const size_t         size)
{
  SerdAllocator* const actual =
    allocator ? allocator : serd_default_allocator();

  return actual->realloc(actual, ptr, size);
}

/// Convenience wrapper that defers to free() if allocator is null
static inline void
serd_afree(SerdAllocator* const allocator, void* const ptr)
{
  SerdAllocator* const actual =
    allocator ? allocator : serd_default_allocator();

  actual->free(actual, ptr);
}

/// Convenience wrapper that defers to the system allocator if allocator is null
static inline void*
serd_aaligned_alloc(SerdAllocator* const allocator,
                    const size_t         alignment,
                    const size_t         size)
{
  SerdAllocator* const actual =
    allocator ? allocator : serd_default_allocator();

  return actual->aligned_alloc(actual, alignment, size);
}

/// Convenience wrapper for serd_aaligned_alloc that zeros memory
static inline void*
serd_aaligned_calloc(SerdAllocator* const allocator,
                     const size_t         alignment,
                     const size_t         size)
{
  void* const ptr = serd_aaligned_alloc(allocator, alignment, size);
  if (ptr) {
    memset(ptr, 0, size);
  }
  return ptr;
}

/// Convenience wrapper that defers to the system allocator if allocator is null
static inline void
serd_aaligned_free(SerdAllocator* const allocator, void* const ptr)
{
  SerdAllocator* const actual =
    allocator ? allocator : serd_default_allocator();

  actual->aligned_free(actual, ptr);
}

// World convenience wrappers

static inline void*
serd_wmalloc(const SerdWorld* const world, const size_t size)
{
  return serd_amalloc(serd_world_allocator(world), size);
}

static inline void*
serd_wcalloc(const SerdWorld* const world,
             const size_t           nmemb,
             const size_t           size)
{
  return serd_acalloc(serd_world_allocator(world), nmemb, size);
}

static inline void*
serd_wrealloc(const SerdWorld* const world, void* const ptr, const size_t size)
{
  return serd_arealloc(serd_world_allocator(world), ptr, size);
}

static inline void
serd_wfree(const SerdWorld* const world, void* const ptr)
{
  serd_afree(serd_world_allocator(world), ptr);
}

static inline void*
serd_waligned_alloc(const SerdWorld* const world,
                    const size_t           alignment,
                    const size_t           size)
{
  return serd_aaligned_alloc(serd_world_allocator(world), alignment, size);
}

static inline void
serd_waligned_free(const SerdWorld* const world, void* const ptr)
{
  serd_aaligned_free(serd_world_allocator(world), ptr);
}

#endif // SERD_MEMORY_H