view libmlk-core/mlk/core/alloc.c @ 580:459ff09c09b3

core: animation can be looped
author David Demelier <markand@malikania.fr>
date Sat, 11 Mar 2023 21:20:33 +0100
parents cba66f7d8a53
children 4349b591c3ac
line wrap: on
line source

/*
 * alloc.c -- memory allocators
 *
 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
 *
 * 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 <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <SDL.h>

#include "alloc.h"
#include "panic.h"

#define OOM_MSG "out of memory"
#define BLKSIZE (offsetof (struct block, data))

struct block {
	size_t n;
	size_t w;
	unsigned char data[];
};

static void *
panic_alloc(size_t size)
{
	void *mem;

	if (!(mem = malloc(size)))
		mlk_panicf(OOM_MSG);

	return mem;
}

static void *
panic_realloc(void *ptr, size_t size)
{
	void *mem;

	if (!(mem = realloc(ptr, size)) && size != 0)
		mlk_panicf(OOM_MSG);

	return mem;
}

static const struct mlk_alloc_funcs *funcs = &(const struct mlk_alloc_funcs) {
	.alloc = panic_alloc,
	.realloc = panic_realloc,
	.free = free
};

static void *
wrap_sdl_malloc(size_t s)
{
	return funcs->alloc(s);
}

static void *
wrap_sdl_calloc(size_t n, size_t w)
{
	const size_t s = n * w;

	return memset(funcs->alloc(s), 0, s);
}

static void *
wrap_sdl_realloc(void *mem, size_t s)
{
	return funcs->realloc(mem, s);
}

static void
wrap_sdl_free(void *mem)
{
	funcs->free(mem);
}

void
mlk_alloc_set(const struct mlk_alloc_funcs *newfuncs)
{
	assert(funcs);
	assert(funcs->alloc);
	assert(funcs->realloc);
	assert(funcs->free);

	funcs = newfuncs;

	/* Change SDL allocators as well. */
	SDL_SetMemoryFunctions(wrap_sdl_malloc, wrap_sdl_calloc,
	    wrap_sdl_realloc, wrap_sdl_free);
}

static inline struct block *
blockat(const void *ptr)
{
	const unsigned char *addr = (const unsigned char *)ptr;

	return (struct block *)&addr[-BLKSIZE];
}

static inline void *
allocate(size_t n, size_t w, int zero)
{
	struct block *b;
	size_t s = n * w;

	// TODO: overflow check.

	b = funcs->alloc(BLKSIZE + s);
	b->n = n;
	b->w = w;

	if (zero)
		memset(b->data, 0, s);

	return b->data;
}

static inline void *
reallocate(void *ptr, size_t n, int zero)
{
	assert(ptr);
	assert(n);

	struct block *b = blockat(ptr);
	size_t osize = b->n * b->w;
	size_t nsize = (b->n + n) * b->w;

#if !defined(NDEBUG)
	assert(SIZE_MAX - osize >= nsize);
#endif

	b = funcs->realloc(b, BLKSIZE + nsize);
	b->n = n;

	if (zero && nsize > osize)
		memset(b->data + osize, 0, nsize - osize);

	return b->data;
}

static inline void *
expand(void *ptr, size_t n, int zero)
{
	struct block *b = blockat(ptr);

	if (n <= b->n)
		return ptr;

	return reallocate(ptr, b->n + n, zero);
}

void *
mlk_alloc_new(size_t n, size_t w)
{
	assert(n);
	assert(w);

	return allocate(n, w, 0);
}

void *
mlk_alloc_new0(size_t n, size_t w)
{
	assert(n);
	assert(w);

	return allocate(n, w, 1);
}

void *
mlk_alloc_resize(void *ptr, size_t n)
{
	return reallocate(ptr, n, 0);
}

void *
mlk_alloc_resize0(void *ptr, size_t n)
{
	return reallocate(ptr, n, 1);
}

void *
mlk_alloc_expand(void *ptr, size_t n)
{
	return reallocate(ptr, n, 0);
}

void *
mlk_alloc_expand0(void *ptr, size_t n)
{
	return expand(ptr, n, 1);
}

void *
mlk_alloc_dup(const void *ptr, size_t n, size_t w)
{
	assert(ptr);
	assert(n);
	assert(w);

	return memcpy(mlk_alloc_new(n, w), ptr, n * w);
}

char *
mlk_alloc_sdup(const char *src)
{
	assert(src);

	size_t len = strlen(src) + 1;

	return memcpy(mlk_alloc_new(len, 1), src, len);
}

char *
mlk_alloc_sdupf(const char *fmt, ...)
{
	va_list ap;
	char *str;
	int size;

	va_start(ap, fmt);
	size = vsnprintf(NULL, 0, fmt, ap);
	va_end(ap);

	if (size <= 0)
		return NULL;

	str = mlk_alloc_new(size + 1, 1);
	va_start(ap, fmt);
	vsnprintf(str, size + 1, fmt, ap);
	va_end(ap);

	return str;
}

size_t
mlk_alloc_getn(const void *ptr)
{
	assert(ptr);

	return blockat(ptr)->n;
}

size_t
mlk_alloc_getw(const void *ptr)
{
	assert(ptr);

	return blockat(ptr)->w;
}

void
mlk_alloc_free(void *ptr)
{
	if (ptr)
		funcs->free(blockat(ptr));
}