diff options
author | wm4 <wm4@nowhere> | 2014-01-29 18:11:13 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-02-02 20:02:40 +0100 |
commit | da4af2b6d86491536b0999f5b731c30b13ff0ac9 (patch) | |
tree | 9b941ab8dba74b2804993fe1bd7aa2fe9063926d /libass/ass_utils.c | |
parent | 7514642f2575fd58c4f4809d74a0099f22d256f0 (diff) | |
download | libass-da4af2b6d86491536b0999f5b731c30b13ff0ac9.tar.bz2 libass-da4af2b6d86491536b0999f5b731c30b13ff0ac9.tar.xz |
Use a function for aligned memory allocations
...instead of doing this manually.
Diffstat (limited to 'libass/ass_utils.c')
-rw-r--r-- | libass/ass_utils.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/libass/ass_utils.c b/libass/ass_utils.c index 72993d7..e119c02 100644 --- a/libass/ass_utils.c +++ b/libass/ass_utils.c @@ -18,8 +18,10 @@ #include "config.h" +#include <stddef.h> #include <stdlib.h> #include <stdio.h> +#include <stdint.h> #include <inttypes.h> #include <strings.h> @@ -63,6 +65,29 @@ int has_avx2(void) #endif // ASM +void *ass_aligned_alloc(size_t alignment, size_t size) +{ + if (alignment & (alignment - 1)) + abort(); // not a power of 2 + if (size >= SIZE_MAX - alignment - sizeof(void *)) + return NULL; + char *allocation = malloc(size + sizeof(void *) + alignment - 1); + if (!allocation) + return NULL; + char *ptr = allocation + sizeof(void *); + unsigned int misalign = (uintptr_t)ptr & (alignment - 1); + if (misalign) + ptr += alignment - misalign; + *((void **)ptr - 1) = allocation; + return ptr; +} + +void ass_aligned_free(void *ptr) +{ + if (ptr) + free(*((void **)ptr - 1)); +} + int mystrtoi(char **p, int *res) { double temp_res; |