MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / ggml_aligned_malloc

Function ggml_aligned_malloc

ggml/src/ggml.c:326–380  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Calls

no outgoing calls

Tested by

no test coverage detected