From b44ba065437ff0dcc3a9387e2bc65e00567d3110 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Tue, 1 Oct 2013 07:24:59 -0700 Subject: [PATCH] fixed the alignment issues for good --- libobs/graphics/graphics.c | 16 ++++----- libobs/graphics/graphics.h | 12 +++---- libobs/util/AlignedNew.hpp | 8 ++--- libobs/util/bmem.c | 72 +++++++++++++++++++++----------------- libobs/util/bmem.h | 6 ---- libobs/util/darray.h | 10 +++--- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/libobs/graphics/graphics.c b/libobs/graphics/graphics.c index 5bb8e73bc..9ff3e2c7e 100644 --- a/libobs/graphics/graphics.c +++ b/libobs/graphics/graphics.c @@ -55,14 +55,14 @@ static bool graphics_init(struct graphics_subsystem *graphics) vbd = vbdata_create(); vbd->num = IMMEDIATE_COUNT; - vbd->points = baligned_malloc(sizeof(struct vec3)*IMMEDIATE_COUNT, 16); - vbd->normals = baligned_malloc(sizeof(struct vec3)*IMMEDIATE_COUNT, 16); - vbd->colors = baligned_malloc(sizeof(uint32_t) *IMMEDIATE_COUNT, 16); + vbd->points = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT); + vbd->normals = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT); + vbd->colors = bmalloc(sizeof(uint32_t) *IMMEDIATE_COUNT); vbd->num_tex = 1; - vbd->tvarray = baligned_malloc(sizeof(struct tvertarray), 16); + vbd->tvarray = bmalloc(sizeof(struct tvertarray)); vbd->tvarray[0].width = 2; vbd->tvarray[0].array = - baligned_malloc(sizeof(struct vec2) * IMMEDIATE_COUNT, 16); + bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT); graphics->immediate_vertbuffer = graphics->exports. device_create_vertexbuffer(graphics->device, vbd, GS_DYNAMIC); @@ -71,11 +71,11 @@ static bool graphics_init(struct graphics_subsystem *graphics) vbd = vbdata_create(); vbd->num = 4; - vbd->points = baligned_malloc(sizeof(struct vec3) * 4, 16); + vbd->points = bmalloc(sizeof(struct vec3) * 4); vbd->num_tex = 1; - vbd->tvarray = baligned_malloc(sizeof(struct tvertarray), 16); + vbd->tvarray = bmalloc(sizeof(struct tvertarray)); vbd->tvarray[0].width = 2; - vbd->tvarray[0].array = baligned_malloc(sizeof(struct vec2) * 4, 16); + vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4); memset(vbd->points, 0, sizeof(struct vec3) * 4); memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4); diff --git a/libobs/graphics/graphics.h b/libobs/graphics/graphics.h index f3f31f354..1c72d0a66 100644 --- a/libobs/graphics/graphics.h +++ b/libobs/graphics/graphics.h @@ -193,13 +193,13 @@ static inline void vbdata_destroy(struct vb_data *data) if (!data) return; - baligned_free(data->points); - baligned_free(data->normals); - baligned_free(data->tangents); - baligned_free(data->colors); + bfree(data->points); + bfree(data->normals); + bfree(data->tangents); + bfree(data->colors); for (i = 0; i < data->num_tex; i++) - baligned_free(data->tvarray[i].array); - baligned_free(data->tvarray); + bfree(data->tvarray[i].array); + bfree(data->tvarray); bfree(data); } diff --git a/libobs/util/AlignedNew.hpp b/libobs/util/AlignedNew.hpp index fc2ed199b..6f058bee0 100644 --- a/libobs/util/AlignedNew.hpp +++ b/libobs/util/AlignedNew.hpp @@ -27,20 +27,20 @@ inline void* operator new(size_t size) { - return baligned_malloc(size, 16); + return bmalloc(size); } inline void operator delete(void* data) { - baligned_free(data); + bfree(data); } inline void* operator new[](size_t size) { - return baligned_malloc(size, 16); + return bmalloc(size); } inline void operator delete[](void* data) { - baligned_free(data); + bfree(data); } diff --git a/libobs/util/bmem.c b/libobs/util/bmem.c index 090356564..b27babdd2 100644 --- a/libobs/util/bmem.c +++ b/libobs/util/bmem.c @@ -27,33 +27,59 @@ #include "base.h" #include "bmem.h" -static void *a_malloc(size_t size, size_t align) +/* + * NOTE: totally jacked the mem alignment trick from ffmpeg, credit to them: + * http://www.ffmpeg.org/ + */ + +#define ALIGNMENT 16 +#define ALIGNMENT_HACK 1 + +static void *a_malloc(size_t size) { -#if defined(_WIN32) - return _aligned_malloc(size, align); -#elif defined (__posix__) - void *ptr; - if (posix_memalign(&ptr, align, size) != 0) - return NULL; +#ifdef _WIN32 + return _aligned_malloc(size, ALIGNMENT); +#else + void *ptr = NULL; + long diff; + + ptr = malloc(size + ALIGNMENT); + diff = ((~(long)ptr) & (ALIGNMENT - 1)) + 1; + ptr = (char *)ptr + diff; + ((char *)ptr)[-1] = (char)diff; return ptr; +#endif +} + +static void *a_realloc(void *ptr, size_t size) +{ +#ifdef _WIN32 + return _aligned_realloc(ptr, size, ALIGNMENT); #else -#error unknown OS + 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; #endif } static void a_free(void *ptr) { -#if defined(_WIN32) +#ifdef _WIN32 _aligned_free(ptr); -#elif defined (__posix__) - free(ptr); #else -#error unknown OS + if (ptr) + free((char *)ptr - ((char*)ptr)[-1]); #endif } -static struct base_allocator alloc = {malloc, realloc, free, a_malloc, a_free}; +static struct base_allocator alloc = {a_malloc, a_realloc, a_free}; static size_t num_allocs = 0; void base_set_allocator(struct base_allocator *defs) @@ -96,26 +122,6 @@ void bfree(void *ptr) alloc.free(ptr); } -void *baligned_malloc(size_t size, size_t align) -{ - void *ptr = alloc.aligned_malloc(size, align); - if (!ptr && !size) - ptr = alloc.aligned_malloc(1, align); - if (!ptr) - bcrash("Out of memory while trying to allocate %lu bytes", - (unsigned long)size); - - num_allocs++; - return ptr; -} - -void baligned_free(void *ptr) -{ - if (ptr) - num_allocs--; - alloc.aligned_free(ptr); -} - size_t bnum_allocs(void) { return num_allocs; diff --git a/libobs/util/bmem.h b/libobs/util/bmem.h index f686c68be..448071d07 100644 --- a/libobs/util/bmem.h +++ b/libobs/util/bmem.h @@ -37,9 +37,6 @@ struct base_allocator { void *(*malloc)(size_t); void *(*realloc)(void *, size_t); void (*free)(void *); - - void *(*aligned_malloc)(size_t, size_t); - void (*aligned_free)(void *); }; EXPORT void base_set_allocator(struct base_allocator *defs); @@ -48,9 +45,6 @@ EXPORT void *bmalloc(size_t size); EXPORT void *brealloc(void *ptr, size_t size); EXPORT void bfree(void *ptr); -EXPORT void *baligned_malloc(size_t size, size_t align); -EXPORT void baligned_free(void *ptr); - EXPORT size_t bnum_allocs(void); EXPORT void *bmemdup(const void *ptr, size_t size); diff --git a/libobs/util/darray.h b/libobs/util/darray.h index 685b27412..8a921900b 100644 --- a/libobs/util/darray.h +++ b/libobs/util/darray.h @@ -59,7 +59,7 @@ static inline void darray_init(struct darray *dst) static inline void darray_free(struct darray *dst) { - baligned_free(dst->array); + bfree(dst->array); dst->array = NULL; dst->num = 0; dst->capacity = 0; @@ -93,11 +93,11 @@ static inline void darray_reserve(const size_t element_size, if (capacity == 0 || capacity <= dst->num) return; - ptr = baligned_malloc(element_size*capacity, 16); + ptr = bmalloc(element_size*capacity); if (dst->num) memcpy(ptr, dst->array, element_size*dst->num); if (dst->array) - baligned_free(dst->array); + bfree(dst->array); dst->array = ptr; dst->capacity = capacity; } @@ -113,11 +113,11 @@ static inline void darray_ensure_capacity(const size_t element_size, new_cap = (!dst->capacity) ? new_size : dst->capacity*2; if (new_size > new_cap) new_cap = new_size; - ptr = baligned_malloc(element_size*new_cap, 16); + ptr = bmalloc(element_size*new_cap); if (dst->capacity) memcpy(ptr, dst->array, element_size*dst->capacity); if (dst->array) - baligned_free(dst->array); + bfree(dst->array); dst->array = ptr; dst->capacity = new_cap; }