Przeglądaj źródła

fixed the alignment issues for good

jp9000 12 lat temu
rodzic
commit
b44ba06543

+ 8 - 8
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);

+ 6 - 6
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);
 }
 

+ 4 - 4
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);
 }

+ 39 - 33
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;

+ 0 - 6
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);

+ 5 - 5
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;
 }