static*/
| 81 | |
| 82 | |
| 83 | /*static*/ void Memory::pool_init(Memory::pool_info& pool_info) { |
| 84 | if (pool_info.pool_init) { |
| 85 | return; |
| 86 | } |
| 87 | pool_info.pool_init = true; |
| 88 | printf("Initializing TLSF pool...\n"); |
| 89 | |
| 90 | #if defined(ESP32) |
| 91 | heap_pool_info.contiguous_size = ESP.getMaxAllocHeap(); |
| 92 | psram_pool_info.contiguous_size = ESP.getMaxAllocPsram(); |
| 93 | altheap_pool_info.contiguous_size = ESP.getMaxAllocHeap(); |
| 94 | printf("TLSF: Heap contiguous_size: %u\n", ESP.getMaxAllocHeap()); |
| 95 | printf("TLSF: PSRAM contiguous_size: %u\n", ESP.getMaxAllocPsram()); |
| 96 | printf("TLSF: contiguous_size: %u\n", pool_info.contiguous_size); |
| 97 | if (pool_info.buffer_size == 0) { |
| 98 | // CBA NOTE Using fp mathhere somehow causes tlsf_create_with_pool() to fail. |
| 99 | //pool_info.buffer_size = (size_t)(pool_info.contiguous_size * BUFFER_FRACTION); |
| 100 | // Compute 80% exactly using integers |
| 101 | pool_info.buffer_size = (pool_info.contiguous_size * 4) / 5; |
| 102 | } |
| 103 | // Round DOWN to TLSF alignment |
| 104 | size_t align = tlsf_align_size(); |
| 105 | pool_info.buffer_size &= ~(align - 1); |
| 106 | void* raw_buffer = nullptr; |
| 107 | // If pool type is PSRAM then allocate pool buffer from PSRAM |
| 108 | if (pool_info.type == RNS_PSRAM_POOL_ALLOCATOR) { |
| 109 | raw_buffer = (void*)ps_malloc(pool_info.buffer_size); |
| 110 | } |
| 111 | // Otherwise just allocate from heap |
| 112 | else { |
| 113 | raw_buffer = (void*)aligned_alloc(align, pool_info.buffer_size); |
| 114 | } |
| 115 | #elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT) |
| 116 | heap_pool_info.contiguous_size = dbgHeapFree(); |
| 117 | altheap_pool_info.contiguous_size = dbgHeapFree(); |
| 118 | TRACEF("TLSF: contiguous_size: %u", pool_info.contiguous_size); |
| 119 | TRACEF("TLSF: buffer_size: %u", pool_info.buffer_size); |
| 120 | // If buffer size is unset the set based on fraction of contiguous size |
| 121 | if (pool_info.buffer_size == 0) { |
| 122 | pool_info.buffer_size = (size_t)(pool_info.contiguous_size * BUFFER_FRACTION); |
| 123 | } |
| 124 | // CBA TODO: Check request buffer size against available SRAM and adjust down if necessary (never up) |
| 125 | // For NRF52 round to kB |
| 126 | pool_info.buffer_size = (size_t)(pool_info.buffer_size / 1024) * 1024; |
| 127 | TRACEF("TLSF: buffer_size: %u", pool_info.buffer_size); |
| 128 | void* raw_buffer = malloc(pool_info.buffer_size); |
| 129 | /* |
| 130 | // CBA Try reducing allocation size until it succeeds |
| 131 | void* raw_buffer = nullptr; |
| 132 | for (uint8_t count = 0; raw_buffer == nullptr && count < 10; ++count) { |
| 133 | // For NRF52 round to kB |
| 134 | pool_info.buffer_size = (size_t)(pool_info.buffer_size / 1024) * 1024; |
| 135 | TRACEF("TLSF: buffer_size: %u", pool_info.buffer_size); |
| 136 | raw_buffer = malloc(pool_info.buffer_size); |
| 137 | // If allocation fails then reduce size by 10% |
| 138 | if (raw_buffer == nullptr) pool_info.buffer_size -= (size_t)(pool_info.buffer_size * 0.1); |
| 139 | } |
| 140 | */ |
nothing calls this directly
no test coverage detected