2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and 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.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "base.h"
|
|
|
|
#include "bmem.h"
|
2015-11-18 12:22:56 -08:00
|
|
|
#include "platform.h"
|
2014-04-07 01:25:38 -07:00
|
|
|
#include "threading.h"
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-10-01 07:24:59 -07:00
|
|
|
/*
|
|
|
|
* NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them:
|
|
|
|
* http://www.ffmpeg.org/
|
|
|
|
*/
|
|
|
|
|
2014-02-09 04:51:06 -08:00
|
|
|
#define ALIGNMENT 32
|
2013-10-17 17:21:42 -07:00
|
|
|
|
2014-02-09 04:51:06 -08:00
|
|
|
/* TODO: use memalign for non-windows systems */
|
|
|
|
#if defined(_WIN32)
|
2013-10-17 17:21:42 -07:00
|
|
|
#define ALIGNED_MALLOC 1
|
2014-02-09 04:51:06 -08:00
|
|
|
#else
|
2013-10-01 07:24:59 -07:00
|
|
|
#define ALIGNMENT_HACK 1
|
2013-10-17 17:21:42 -07:00
|
|
|
#endif
|
2013-10-01 07:24:59 -07:00
|
|
|
|
|
|
|
static void *a_malloc(size_t size)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2013-10-17 17:21:42 -07:00
|
|
|
#ifdef ALIGNED_MALLOC
|
2013-10-01 07:24:59 -07:00
|
|
|
return _aligned_malloc(size, ALIGNMENT);
|
2013-10-17 17:21:42 -07:00
|
|
|
#elif ALIGNMENT_HACK
|
2013-10-01 07:24:59 -07:00
|
|
|
void *ptr = NULL;
|
|
|
|
long diff;
|
|
|
|
|
|
|
|
ptr = malloc(size + ALIGNMENT);
|
2015-11-18 18:46:21 -08:00
|
|
|
if (ptr) {
|
|
|
|
diff = ((~(long)ptr) & (ALIGNMENT - 1)) + 1;
|
|
|
|
ptr = (char *)ptr + diff;
|
|
|
|
((char *)ptr)[-1] = (char)diff;
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
return ptr;
|
2013-10-17 17:21:42 -07:00
|
|
|
#else
|
|
|
|
return malloc(size);
|
2013-10-01 07:24:59 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *a_realloc(void *ptr, size_t size)
|
|
|
|
{
|
2013-10-17 17:21:42 -07:00
|
|
|
#ifdef ALIGNED_MALLOC
|
2013-10-01 07:24:59 -07:00
|
|
|
return _aligned_realloc(ptr, size, ALIGNMENT);
|
2013-10-17 17:21:42 -07:00
|
|
|
#elif ALIGNMENT_HACK
|
2013-10-01 07:24:59 -07:00
|
|
|
long diff;
|
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return a_malloc(size);
|
|
|
|
diff = ((char *)ptr)[-1];
|
|
|
|
ptr = realloc((char*)ptr - diff, size + diff);
|
|
|
|
if (ptr)
|
|
|
|
ptr = (char *)ptr + diff;
|
|
|
|
return ptr;
|
2013-10-17 17:21:42 -07:00
|
|
|
#else
|
|
|
|
return realloc(ptr, size);
|
2013-09-30 19:37:13 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void a_free(void *ptr)
|
|
|
|
{
|
2013-10-17 17:21:42 -07:00
|
|
|
#ifdef ALIGNED_MALLOC
|
2013-09-30 19:37:13 -07:00
|
|
|
_aligned_free(ptr);
|
2013-10-17 17:21:42 -07:00
|
|
|
#elif ALIGNMENT_HACK
|
2013-10-01 07:24:59 -07:00
|
|
|
if (ptr)
|
|
|
|
free((char *)ptr - ((char*)ptr)[-1]);
|
2013-10-17 17:21:42 -07:00
|
|
|
#else
|
|
|
|
free(ptr);
|
2013-09-30 19:37:13 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-10-01 07:24:59 -07:00
|
|
|
static struct base_allocator alloc = {a_malloc, a_realloc, a_free};
|
2014-04-07 01:25:38 -07:00
|
|
|
static long num_allocs = 0;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
void base_set_allocator(struct base_allocator *defs)
|
|
|
|
{
|
|
|
|
memcpy(&alloc, defs, sizeof(struct base_allocator));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *bmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ptr = alloc.malloc(size);
|
|
|
|
if (!ptr && !size)
|
|
|
|
ptr = alloc.malloc(1);
|
2015-11-18 12:22:56 -08:00
|
|
|
if (!ptr) {
|
|
|
|
os_breakpoint();
|
2013-09-30 19:37:13 -07:00
|
|
|
bcrash("Out of memory while trying to allocate %lu bytes",
|
|
|
|
(unsigned long)size);
|
2015-11-18 12:22:56 -08:00
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-04-07 01:25:38 -07:00
|
|
|
os_atomic_inc_long(&num_allocs);
|
2013-09-30 19:37:13 -07:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *brealloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
2014-04-07 01:25:38 -07:00
|
|
|
os_atomic_inc_long(&num_allocs);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
ptr = alloc.realloc(ptr, size);
|
|
|
|
if (!ptr && !size)
|
|
|
|
ptr = alloc.realloc(ptr, 1);
|
2015-11-18 12:22:56 -08:00
|
|
|
if (!ptr) {
|
|
|
|
os_breakpoint();
|
2013-09-30 19:37:13 -07:00
|
|
|
bcrash("Out of memory while trying to allocate %lu bytes",
|
|
|
|
(unsigned long)size);
|
2015-11-18 12:22:56 -08:00
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bfree(void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
2014-04-07 01:25:38 -07:00
|
|
|
os_atomic_dec_long(&num_allocs);
|
2013-09-30 19:37:13 -07:00
|
|
|
alloc.free(ptr);
|
|
|
|
}
|
|
|
|
|
2014-04-07 01:25:38 -07:00
|
|
|
long bnum_allocs(void)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
return num_allocs;
|
|
|
|
}
|
|
|
|
|
2014-02-09 04:51:06 -08:00
|
|
|
int base_get_alignment(void)
|
|
|
|
{
|
|
|
|
return ALIGNMENT;
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
void *bmemdup(const void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *out = bmalloc(size);
|
|
|
|
if (size)
|
|
|
|
memcpy(out, ptr, size);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|