MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / ggml_aligned_malloc

Function ggml_aligned_malloc

smallthinker/ggml/src/ggml.c:281–335  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

279
280
281void * ggml_aligned_malloc(size_t size) {
282#if defined(__s390x__)
283 const int alignment = 256;
284#else
285 const int alignment = 64;
286#endif
287
288#if defined(_MSC_VER) || defined(__MINGW32__)
289 return _aligned_malloc(size, alignment);
290#else
291 if (size == 0) {
292 GGML_LOG_WARN("Behavior may be unexpected when allocating 0 bytes for ggml_aligned_malloc!\n");
293 return NULL;
294 }
295 void * aligned_memory = NULL;
296 #ifdef GGML_USE_CPU_HBM
297 int result = hbw_posix_memalign(&aligned_memory, alignment, size);
298 #elif TARGET_OS_OSX
299 GGML_UNUSED(alignment);
300 kern_return_t alloc_status = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t *) &aligned_memory, size, VM_FLAGS_ANYWHERE);
301 int result = EFAULT;
302 switch (alloc_status) {
303 case KERN_SUCCESS:
304 result = 0;
305 break;
306 case KERN_INVALID_ADDRESS:
307 result = EINVAL;
308 break;
309 case KERN_NO_SPACE:
310 result = ENOMEM;
311 break;
312 default:
313 result = EFAULT;
314 break;
315 }
316 #else
317 int result = posix_memalign(&aligned_memory, alignment, size);
318 #endif
319 if (result != 0) {
320 // Handle allocation failure
321 const char *error_desc = "unknown allocation error";
322 switch (result) {
323 case EINVAL:
324 error_desc = "invalid alignment value";
325 break;
326 case ENOMEM:
327 error_desc = "insufficient memory";
328 break;
329 }
330 GGML_LOG_ERROR("%s: %s (attempted to allocate %6.2f MB)\n", __func__, error_desc, size/(1024.0*1024.0));
331 return NULL;
332 }
333 return aligned_memory;
334#endif
335}
336
337void ggml_aligned_free(void * ptr, size_t size) {
338 GGML_UNUSED(size);

Calls

no outgoing calls

Tested by

no test coverage detected