MCPcopy Create free account
hub / github.com/attermann/microReticulum / allocator_malloc

Function allocator_malloc

src/microReticulum/Utilities/Memory.cpp:484–521  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

482#if RNS_DEFAULT_ALLOCATOR != RNS_HEAP_ALLOCATOR
483
484void* allocator_malloc(size_t size) {
485 if (size == 0) return nullptr;
486 ++Memory::default_allocator_info.alloc_count;
487 Memory::default_allocator_info.alloc_size += size;
488 if (size < Memory::default_allocator_info.min_alloc_size || Memory::default_allocator_info.min_alloc_size == 0) {
489 Memory::default_allocator_info.min_alloc_size = size;
490 }
491 if (size > Memory::default_allocator_info.max_alloc_size) {
492 Memory::default_allocator_info.max_alloc_size = size;
493 }
494#if RNS_DEFAULT_ALLOCATOR == RNS_HEAP_POOL_ALLOCATOR
495 return Memory::pool_malloc(Memory::heap_pool_info, size);
496#elif RNS_DEFAULT_ALLOCATOR == RNS_PSRAM_ALLOCATOR
497#if BOARD_HAS_PSRAM != 1
498 #error "BOARD_HAS_PSRAM must be defined to use RNS_PSRAM_POOL_ALLOCATOR allocator."
499#endif
500 // CBA PSRAM may not be accessible early in startup, but since we use the same free() for both
501 // HEAP and PSRAM allocations, we canb simply fallback to HEAP allocation when PSRAM fails
502 void* p = ps_malloc(size);
503 if (p == nullptr) {
504 ++Memory::default_allocator_info.alloc_fault;
505 p = malloc(size);
506 }
507 return p;
508#elif RNS_DEFAULT_ALLOCATOR == RNS_PSRAM_POOL_ALLOCATOR
509#error "RNS_DEFAULT_ALLOCATOR can not be set to RNS_PSRAM_POOL_ALLOCATOR because PSRAM isn't initialized early enough."
510#if BOARD_HAS_PSRAM != 1
511 #error "BOARD_HAS_PSRAM must be defined to use RNS_PSRAM_POOL_ALLOCATOR allocator."
512#endif
513 return Memory::pool_malloc(Memory::psram_pool_info, size);
514#elif RNS_DEFAULT_ALLOCATOR == RNS_ALTHEAP_POOL_ALLOCATOR
515 return Memory::pool_malloc(Memory::altheap_pool_info, size);
516#elif RNS_DEFAULT_ALLOCATOR == RNS_HEAP_ALLOCATOR
517 return ::operator new(n * sizeof(value_type));
518#else
519 #error "Unknown or invalid allocator."
520#endif
521}
522
523void allocator_free(void* p, size_t size = 0) noexcept {
524 if (p == nullptr) return;

Callers 2

operator newFunction · 0.85
operator new[]Function · 0.85

Calls 1

operator newFunction · 0.85

Tested by

no test coverage detected