2013-09-30 19:37:13 -07:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (c) 2013 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "base.h"
|
|
|
|
#include "bmem.h"
|
|
|
|
|
2013-10-01 07:24:59 -07:00
|
|
|
/*
|
|
|
|
* NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them:
|
|
|
|
* http://www.ffmpeg.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ALIGNMENT 16
|
2013-10-17 17:21:42 -07:00
|
|
|
|
|
|
|
#if defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#define ALIGNED_MALLOC 1
|
|
|
|
#elif !defined(__LP64__)
|
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);
|
|
|
|
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};
|
2013-12-07 08:39:43 -08:00
|
|
|
static uint64_t 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);
|
|
|
|
if (!ptr)
|
|
|
|
bcrash("Out of memory while trying to allocate %lu bytes",
|
|
|
|
(unsigned long)size);
|
|
|
|
|
|
|
|
num_allocs++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *brealloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
num_allocs++;
|
|
|
|
|
|
|
|
ptr = alloc.realloc(ptr, size);
|
|
|
|
if (!ptr && !size)
|
|
|
|
ptr = alloc.realloc(ptr, 1);
|
|
|
|
if (!ptr)
|
|
|
|
bcrash("Out of memory while trying to allocate %lu bytes",
|
|
|
|
(unsigned long)size);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bfree(void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
num_allocs--;
|
|
|
|
alloc.free(ptr);
|
|
|
|
}
|
|
|
|
|
2013-12-07 08:39:43 -08:00
|
|
|
uint64_t bnum_allocs(void)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
return num_allocs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *bmemdup(const void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *out = bmalloc(size);
|
|
|
|
if (size)
|
|
|
|
memcpy(out, ptr, size);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|