diff --git a/ggml.c b/ggml.c index 00bbee503..785f4b91f 100644 --- a/ggml.c +++ b/ggml.c @@ -20,6 +20,7 @@ #include #include #include +#include // if C99 - static_assert is noop // ref: https://stackoverflow.com/a/53923785/4039976 @@ -121,7 +122,11 @@ typedef void* thread_ret_t; #else inline static void* ggml_aligned_malloc(size_t size) { void* aligned_memory = NULL; +#ifdef GGML_USE_METAL + int result = posix_memalign(&aligned_memory, getpagesize(), size); +#else int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size); +#endif if (result != 0) { // Handle allocation failure return NULL; diff --git a/llama-util.h b/llama-util.h index 3cac9f681..195cb46ea 100644 --- a/llama-util.h +++ b/llama-util.h @@ -405,13 +405,29 @@ struct llama_buffer { llama_buffer() = default; void resize(size_t len) { +#ifdef GGML_USE_METAL + free(addr); + int result = posix_memalign((void **) &addr, getpagesize(), len); + if (result == 0) { + memset(addr, 0, len); + } + else { + addr = NULL; + } +#else delete[] addr; addr = new uint8_t[len]; +#endif size = len; } ~llama_buffer() { +#ifdef GGML_USE_METAL + free(addr); +#else delete[] addr; +#endif + addr = NULL; } // disable copy and move