Загрузка данных


#ifndef WEBRTC_DTCM_ALLOC_H
#define WEBRTC_DTCM_ALLOC_H

#include <zephyr/kernel.h>
#include <string.h>
#include <stdint.h>

#define WEBRTC_DTCM_POOL_SIZE (40 * 1024)

// Размещаем пул строго в секции DTCM BSS
__attribute__((section(".dtcm_bss"))) static uint8_t webrtc_dtcm_pool[WEBRTC_DTCM_POOL_SIZE];
static size_t webrtc_dtcm_ptr = 0;

static inline void *custom_webrtc_alloc(size_t size)
{
    // Выравнивание по 4 байта для Cortex-M7
    size = (size + 3) & ~3;

    if (webrtc_dtcm_ptr + size <= WEBRTC_DTCM_POOL_SIZE) {
        void *ptr = &webrtc_dtcm_pool[webrtc_dtcm_ptr];
        webrtc_dtcm_ptr += size;
        
        // Быстрый printk Zephyr вместо тяжелого printf
        printk("\n[WEBRTC DTCM REPORT] Allocated: %5d / %5d bytes (%d%% used)\n", 
               webrtc_dtcm_ptr, WEBRTC_DTCM_POOL_SIZE, (webrtc_dtcm_ptr * 100) / WEBRTC_DTCM_POOL_SIZE);
        
        return ptr;
    }
    
    printk("[WEBRTC WARNING] DTCM Full! Falling back to system heap for %d bytes\n", size);
    return k_malloc(size); // Используем безопасный аллокатор Zephyr
}

static inline void *custom_webrtc_calloc(size_t num, size_t size)
{
    size_t total_size = num * size;
    void *ptr = custom_webrtc_alloc(total_size);
    if (ptr) {
        memset(ptr, 0, total_size);
    }
    return ptr;
}

static inline void custom_webrtc_free(void *ptr)
{
    if (ptr == NULL) return;
    
    // Если указатель из нашего DTCM пула, мы его "не освобождаем" (сброс будет при перезапуске/разрушении стрима)
    if ((uint8_t *)ptr >= webrtc_dtcm_pool && (uint8_t *)ptr < (webrtc_dtcm_pool + WEBRTC_DTCM_POOL_SIZE)) {
        return;
    }
    k_free(ptr);
}

// Сброс указателя пула при уничтожении аудио-стрима PJSIP
static inline void custom_webrtc_reset_pool(void)
{
    webrtc_dtcm_ptr = 0;
    printk("[WEBRTC DTCM] Pool reset successful\n");
}

#endif // WEBRTC_DTCM_ALLOC_H
#include "путь_к_файлу/webrtc_dtcm_alloc.h"

#define malloc(size)         custom_webrtc_alloc(size)
#define calloc(num, size)    custom_webrtc_calloc(num, size)
#define free(ptr)            custom_webrtc_free(ptr)