MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / ggml_aligned_malloc

Function ggml_aligned_malloc

external/ggml/src/ggml.c:331–385  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

329
330
331void * ggml_aligned_malloc(size_t size) {
332#if defined(__s390x__)
333 const int alignment = 256;
334#else
335 const int alignment = 64;
336#endif
337
338#if defined(_MSC_VER) || defined(__MINGW32__)
339 return _aligned_malloc(size, alignment);
340#else
341 if (size == 0) {
342 GGML_LOG_WARN("Behavior may be unexpected when allocating 0 bytes for ggml_aligned_malloc!\n");
343 return NULL;
344 }
345 void * aligned_memory = NULL;
346 #ifdef GGML_USE_CPU_HBM
347 int result = hbw_posix_memalign(&aligned_memory, alignment, size);
348 #elif TARGET_OS_OSX
349 GGML_UNUSED(alignment);
350 kern_return_t alloc_status = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t *) &aligned_memory, size, VM_FLAGS_ANYWHERE);
351 int result = EFAULT;
352 switch (alloc_status) {
353 case KERN_SUCCESS:
354 result = 0;
355 break;
356 case KERN_INVALID_ADDRESS:
357 result = EINVAL;
358 break;
359 case KERN_NO_SPACE:
360 result = ENOMEM;
361 break;
362 default:
363 result = EFAULT;
364 break;
365 }
366 #else
367 int result = posix_memalign(&aligned_memory, alignment, size);
368 #endif
369 if (result != 0) {
370 // Handle allocation failure
371 const char *error_desc = "unknown allocation error";
372 switch (result) {
373 case EINVAL:
374 error_desc = "invalid alignment value";
375 break;
376 case ENOMEM:
377 error_desc = "insufficient memory";
378 break;
379 }
380 GGML_LOG_ERROR("%s: %s (attempted to allocate %6.2f MB)\n", __func__, error_desc, size/(1024.0*1024.0));
381 return NULL;
382 }
383 return aligned_memory;
384#endif
385}
386
387void ggml_aligned_free(void * ptr, size_t size) {
388 GGML_UNUSED(size);

Calls

no outgoing calls

Tested by

no test coverage detected