diff --git a/bsp/board/bl602dk/board.c b/bsp/board/bl602dk/board.c index 3b42883c..ce07c9ad 100644 --- a/bsp/board/bl602dk/board.c +++ b/bsp/board/bl602dk/board.c @@ -3,7 +3,11 @@ #include "bflb_clock.h" #include "bflb_rtc.h" #include "bflb_flash.h" -#include "mmheap.h" +#ifdef CONFIG_TLSF +#include "bflb_tlsf.h" +#else +#include "bflb_mmheap.h" +#endif #include "board.h" #include "bl602_glb.h" #include "bl602_sflash.h" @@ -11,14 +15,20 @@ extern uint32_t __HeapBase; extern uint32_t __HeapLimit; +#ifndef CONFIG_TLSF struct heap_info mmheap_root; -static struct bflb_device_s *uart0; - static struct heap_region system_mmheap[] = { { NULL, 0 }, { NULL, 0 }, /* Terminates the array. */ }; +#endif + +static struct bflb_device_s *uart0; + +#if defined(CONFIG_BFLOG) +static struct bflb_device_s *rtc; +#endif static void system_clock_init(void) { @@ -126,19 +136,28 @@ void board_init(void) bflb_irq_restore(flag); +#ifdef CONFIG_TLSF + bflb_mmheap_init((void *)&__HeapBase, ((size_t)&__HeapLimit - (size_t)&__HeapBase)); +#else system_mmheap[0].addr = (uint8_t *)&__HeapBase; system_mmheap[0].mem_size = ((size_t)&__HeapLimit - (size_t)&__HeapBase); if (system_mmheap[0].mem_size > 0) { - mmheap_init(&mmheap_root, system_mmheap); + bflb_mmheap_init(&mmheap_root, system_mmheap); } +#endif console_init(); bl_show_log(); bl_show_flashinfo(); - printf("dynamic memory init success,heap size = %d Kbyte \r\n", system_mmheap[0].mem_size / 1024); + printf("dynamic memory init success,heap size = %d Kbyte \r\n", ((size_t)&__HeapLimit - (size_t)&__HeapBase) / 1024); + + printf("cgen1:%08x\r\n", getreg32(BFLB_GLB_CGEN1_BASE)); +#if defined(CONFIG_BFLOG) + rtc = bflb_device_get_by_name("rtc"); +#endif } void board_uartx_gpio_init() @@ -183,6 +202,19 @@ void board_pwm_gpio_init() // bflb_gpio_init(gpio, GPIO_PIN_4, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); } +void board_ir_gpio_init(void) +{ + struct bflb_device_s *gpio; + + gpio = bflb_device_get_by_name("gpio"); + /* IR TX only support GPIO 11 */ + bflb_gpio_init(gpio, GPIO_PIN_11, GPIO_ANALOG | GPIO_SMT_EN | GPIO_DRV_0); + + /* IR RX support GPIO 11 ~ GPIO 13 */ + bflb_gpio_init(gpio, GPIO_PIN_12, GPIO_INPUT | GPIO_SMT_EN | GPIO_DRV_0); + GLB_IR_RX_GPIO_Sel(GLB_GPIO_PIN_12); +} + void board_adc_gpio_init() { // struct bflb_device_s *gpio; @@ -202,4 +234,21 @@ void board_dac_gpio_init() // bflb_gpio_init(gpio, GPIO_PIN_11, GPIO_ANALOG | GPIO_SMT_EN | GPIO_DRV_0); // /* DAC_CHB */ // bflb_gpio_init(gpio, GPIO_PIN_17, GPIO_ANALOG | GPIO_SMT_EN | GPIO_DRV_0); -} \ No newline at end of file +} + +#ifdef CONFIG_BFLOG +__attribute__((weak)) uint64_t bflog_clock(void) +{ + return bflb_mtimer_get_time_us(); +} + +__attribute__((weak)) uint32_t bflog_time(void) +{ + return BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)); +} + +__attribute__((weak)) char *bflog_thread(void) +{ + return ""; +} +#endif \ No newline at end of file diff --git a/bsp/board/bl602dk/board.h b/bsp/board/bl602dk/board.h index c8fb9b35..48d9de88 100644 --- a/bsp/board/bl602dk/board.h +++ b/bsp/board/bl602dk/board.h @@ -10,6 +10,7 @@ void board_adc_gpio_init(); void board_dac_gpio_init(); void board_emac_gpio_init(); void board_pwm_gpio_init(); +void board_ir_gpio_init(); #define DEFAULT_TEST_UART "uart1" #define DEFAULT_TEST_UART_DMA_TX_REQUEST DMA_REQUEST_UART1_TX diff --git a/bsp/board/bl616dk/board.c b/bsp/board/bl616dk/board.c index ecd59895..12bf2a36 100644 --- a/bsp/board/bl616dk/board.c +++ b/bsp/board/bl616dk/board.c @@ -3,7 +3,11 @@ #include "bflb_clock.h" #include "bflb_rtc.h" #include "bflb_flash.h" -#include "mmheap.h" +#ifdef CONFIG_TLSF +#include "bflb_tlsf.h" +#else +#include "bflb_mmheap.h" +#endif #include "board.h" #include "bl616_tzc_sec.h" #include "bl616_psram.h" @@ -19,12 +23,14 @@ extern uint32_t __HeapBase; extern uint32_t __HeapLimit; +#ifndef CONFIG_TLSF struct heap_info mmheap_root; static struct heap_region system_mmheap[] = { { NULL, 0 }, { NULL, 0 }, /* Terminates the array. */ }; +#endif static struct bflb_device_s *uart0; @@ -54,6 +60,7 @@ static void peripheral_clock_init(void) PERIPHERAL_CLOCK_IR_ENABLE(); PERIPHERAL_CLOCK_I2S_ENABLE(); PERIPHERAL_CLOCK_USB_ENABLE(); + PERIPHERAL_CLOCK_CAN_ENABLE(); GLB_Set_UART_CLK(ENABLE, HBN_UART_CLK_XCLK, 0); GLB_Set_SPI_CLK(ENABLE, GLB_SPI_CLK_MCU_MUXPLL_160M, 0); @@ -63,13 +70,12 @@ static void peripheral_clock_init(void) GLB_Set_DIG_512K_CLK(ENABLE, ENABLE, 0x4E); GLB_Set_PWM1_IO_Sel(GLB_PWM1_IO_DIFF_END); GLB_Set_IR_CLK(ENABLE, GLB_IR_CLK_SRC_XCLK, 19); + GLB_Set_CAM_CLK(ENABLE, GLB_CAM_CLK_WIFIPLL_96M, 3); GLB_Set_PKA_CLK_Sel(GLB_PKA_CLK_MCU_MUXPLL_160M); #ifdef CONFIG_BSP_SDH_SDCARD PERIPHERAL_CLOCK_SDH_ENABLE(); - GLB_Set_SDH_CLK(ENABLE, GLB_SDH_CLK_WIFIPLL_96M, 0); GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_EXT_SDH); - SDH_ClockSet(400000, 96000000, 48000000); #endif GLB_Set_USB_CLK_From_WIFIPLL(1); @@ -210,22 +216,26 @@ void board_init(void) bflb_irq_restore(flag); +#ifdef CONFIG_TLSF + bflb_mmheap_init((void *)&__HeapBase, ((size_t)&__HeapLimit - (size_t)&__HeapBase)); +#else system_mmheap[0].addr = (uint8_t *)&__HeapBase; system_mmheap[0].mem_size = ((size_t)&__HeapLimit - (size_t)&__HeapBase); if (system_mmheap[0].mem_size > 0) { - mmheap_init(&mmheap_root, system_mmheap); + bflb_mmheap_init(&mmheap_root, system_mmheap); } - +#endif console_init(); bl_show_log(); bl_show_flashinfo(); - printf("dynamic memory init success,heap size = %d Kbyte \r\n", system_mmheap[0].mem_size / 1024); + printf("dynamic memory init success,heap size = %d Kbyte \r\n", ((size_t)&__HeapLimit - (size_t)&__HeapBase) / 1024); - printf("sig1:%08lx\r\n", BL_RD_REG(GLB_BASE, GLB_UART_CFG1)); - printf("sig2:%08lx\r\n", BL_RD_REG(GLB_BASE, GLB_UART_CFG2)); + printf("sig1:%08x\r\n", BL_RD_REG(GLB_BASE, GLB_UART_CFG1)); + printf("sig2:%08x\r\n", BL_RD_REG(GLB_BASE, GLB_UART_CFG2)); + printf("cgen1:%08x\r\n", getreg32(BFLB_GLB_CGEN1_BASE)); #if (defined(CONFIG_LUA) || defined(CONFIG_BFLOG) || defined(CONFIG_FATFS)) rtc = bflb_device_get_by_name("rtc"); @@ -361,15 +371,90 @@ void board_ir_gpio_init(void) GLB_IR_RX_GPIO_Sel(GLB_GPIO_PIN_10); } +void board_dvp1_gpio_init(void) +{ + struct bflb_device_s *gpio; + + gpio = bflb_device_get_by_name("gpio"); + /* I2C GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + + /* Power down GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_2, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_reset(gpio, GPIO_PIN_2); + + /* Reset GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_3, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_set(gpio, GPIO_PIN_3); + + /* MCLK GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_23, GPIO_FUNC_CLKOUT | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + + /* DVP1 GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_24, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_25, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_26, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_27, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_28, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_29, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_30, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_31, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_32, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_33, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_34, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); +} + +void board_dvp2_gpio_init(void) +{ + struct bflb_device_s *gpio; + + gpio = bflb_device_get_by_name("gpio"); + /* I2C GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_28, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_27, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + + /* Power down GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_29, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_reset(gpio, GPIO_PIN_29); + + /* Reset GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_30, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_set(gpio, GPIO_PIN_30); + + /* MCLK GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_20, GPIO_FUNC_CLKOUT | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + + /* DVP2 GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_3, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_10, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_11, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_12, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_13, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_14, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_15, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_16, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_17, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); +} + +void board_iso11898_gpio_init() +{ + // struct bflb_device_s *gpio; + + // gpio = bflb_device_get_by_name("gpio"); +} + #ifdef CONFIG_BFLOG __attribute__((weak)) uint64_t bflog_clock(void) { - return CPU_Get_MTimer_Counter(); + return bflb_mtimer_get_time_us(); } __attribute__((weak)) uint32_t bflog_time(void) { - return BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)) + 1640995200; + return BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)); } __attribute__((weak)) char *bflog_thread(void) @@ -381,12 +466,12 @@ __attribute__((weak)) char *bflog_thread(void) #ifdef CONFIG_LUA __attribute__((weak)) clock_t luaport_clock(void) { - return (clock_t)CPU_Get_MTimer_Counter(); + return (clock_t)bflb_mtimer_get_time_us(); } __attribute__((weak)) time_t luaport_time(time_t *seconds) { - time_t t = (time_t)BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)) + 1640995200; + time_t t = (time_t)BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)); if (seconds != NULL) { *seconds = t; } @@ -401,7 +486,7 @@ __attribute__((weak)) uint32_t get_fattime(void) { bflb_timestamp_t tm; - bflb_timestamp_utc2time(BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)) + 1640995200, &tm); + bflb_timestamp_utc2time(BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)), &tm); return ((uint32_t)(tm.year - 1980) << 25) /* Year 2015 */ | ((uint32_t)tm.mon << 21) /* Month 1 */ diff --git a/bsp/board/bl616dk/board.h b/bsp/board/bl616dk/board.h index 9b732ca1..694a9e8c 100644 --- a/bsp/board/bl616dk/board.h +++ b/bsp/board/bl616dk/board.h @@ -12,6 +12,9 @@ void board_dac_gpio_init(); void board_emac_gpio_init(); void board_sdh_gpio_init(); void board_ir_gpio_init(); +void board_dvp1_gpio_init(); +void board_dvp2_gpio_init(); +void board_iso11898_gpio_init(); #define DEFAULT_TEST_UART "uart1" #define DEFAULT_TEST_UART_DMA_TX_REQUEST DMA_REQUEST_UART1_TX diff --git a/bsp/board/bl702dk/board.c b/bsp/board/bl702dk/board.c index 620e43e0..173affda 100644 --- a/bsp/board/bl702dk/board.c +++ b/bsp/board/bl702dk/board.c @@ -3,7 +3,11 @@ #include "bflb_clock.h" #include "bflb_rtc.h" #include "bflb_flash.h" -#include "mmheap.h" +#ifdef CONFIG_TLSF +#include "bflb_tlsf.h" +#else +#include "bflb_mmheap.h" +#endif #include "board.h" #include "bl702_glb.h" #include "bl702_sflash.h" @@ -11,14 +15,20 @@ extern uint32_t __HeapBase; extern uint32_t __HeapLimit; +#ifndef CONFIG_TLSF struct heap_info mmheap_root; -static struct bflb_device_s *uart0; - static struct heap_region system_mmheap[] = { { NULL, 0 }, { NULL, 0 }, /* Terminates the array. */ }; +#endif + +static struct bflb_device_s *uart0; + +#if defined(CONFIG_BFLOG) +static struct bflb_device_s *rtc; +#endif static void system_clock_init(void) { @@ -44,6 +54,7 @@ static void peripheral_clock_init(void) GLB_Set_UART_CLK(ENABLE, HBN_UART_CLK_96M, 0); GLB_Set_SPI_CLK(ENABLE, 0); GLB_Set_I2C_CLK(ENABLE, 0); + GLB_Set_IR_CLK(ENABLE, GLB_IR_CLK_SRC_XCLK, 15); GLB_Set_ADC_CLK(ENABLE, GLB_ADC_CLK_XCLK, 1); GLB_Set_DAC_CLK(ENABLE, GLB_DAC_CLK_XCLK, 0x3E); @@ -131,20 +142,28 @@ void board_init(void) bflb_irq_restore(flag); +#ifdef CONFIG_TLSF + bflb_mmheap_init((void *)&__HeapBase, ((size_t)&__HeapLimit - (size_t)&__HeapBase)); +#else system_mmheap[0].addr = (uint8_t *)&__HeapBase; system_mmheap[0].mem_size = ((size_t)&__HeapLimit - (size_t)&__HeapBase); if (system_mmheap[0].mem_size > 0) { - mmheap_init(&mmheap_root, system_mmheap); + bflb_mmheap_init(&mmheap_root, system_mmheap); } +#endif console_init(); bl_show_log(); - bl_show_flashinfo(); - printf("dynamic memory init success,heap size = %d Kbyte \r\n", system_mmheap[0].mem_size / 1024); + printf("dynamic memory init success,heap size = %d Kbyte \r\n", ((size_t)&__HeapLimit - (size_t)&__HeapBase) / 1024); + + printf("cgen1:%08x\r\n", getreg32(BFLB_GLB_CGEN1_BASE)); +#if defined(CONFIG_BFLOG) + rtc = bflb_device_get_by_name("rtc"); +#endif } void board_uartx_gpio_init() @@ -192,6 +211,21 @@ void board_pwm_gpio_init() bflb_gpio_init(gpio, GPIO_PIN_4, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); } +void board_ir_gpio_init(void) +{ + struct bflb_device_s *gpio; + + gpio = bflb_device_get_by_name("gpio"); + /* IR TX support GPIO 22 or GPIO 23 */ + bflb_gpio_init(gpio, GPIO_PIN_22, GPIO_ANALOG | GPIO_SMT_EN | GPIO_DRV_0); + GLB_IR_LED_Driver_Output_Disable(GLB_GPIO_PIN_23); + GLB_IR_LED_Driver_Output_Enable(GLB_GPIO_PIN_22); + + /* IR RX support GPIO 17 ~ GPIO 31 */ + bflb_gpio_init(gpio, GPIO_PIN_18, GPIO_INPUT | GPIO_SMT_EN | GPIO_DRV_0); + GLB_IR_RX_GPIO_Sel(GLB_GPIO_PIN_18); +} + void board_adc_gpio_init() { struct bflb_device_s *gpio; @@ -232,4 +266,39 @@ void board_emac_gpio_init() /* enable audio clock */ PDS_Enable_PLL_Clk(PDS_PLL_CLK_48M); PDS_Set_Audio_PLL_Freq(AUDIO_PLL_50000000_HZ); -} \ No newline at end of file +} + +void board_keyscan_gpio_init(void) +{ + struct bflb_device_s *gpio; + + gpio = bflb_device_get_by_name("gpio"); + bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUN_KEY_SCAN_COL | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_OUTPUT | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUN_KEY_SCAN_COL | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_OUTPUT | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_2, GPIO_FUN_KEY_SCAN_COL | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_OUTPUT | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_3, GPIO_FUN_KEY_SCAN_COL | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_OUTPUT | GPIO_SMT_EN | GPIO_DRV_1); + + bflb_gpio_init(gpio, GPIO_PIN_16, GPIO_FUN_KEY_SCAN_ROW | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_INPUT | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_17, GPIO_FUN_KEY_SCAN_ROW | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_INPUT | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_18, GPIO_FUN_KEY_SCAN_ROW | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_INPUT | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_19, GPIO_FUN_KEY_SCAN_ROW | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_INPUT | GPIO_SMT_EN | GPIO_DRV_1); + + GLB_Set_QDEC_CLK(GLB_QDEC_CLK_F32K, 0); +} + +#ifdef CONFIG_BFLOG +__attribute__((weak)) uint64_t bflog_clock(void) +{ + return bflb_mtimer_get_time_us(); +} + +__attribute__((weak)) uint32_t bflog_time(void) +{ + return BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)); +} + +__attribute__((weak)) char *bflog_thread(void) +{ + return ""; +} +#endif \ No newline at end of file diff --git a/bsp/board/bl702dk/board.h b/bsp/board/bl702dk/board.h index c8fb9b35..48d9de88 100644 --- a/bsp/board/bl702dk/board.h +++ b/bsp/board/bl702dk/board.h @@ -10,6 +10,7 @@ void board_adc_gpio_init(); void board_dac_gpio_init(); void board_emac_gpio_init(); void board_pwm_gpio_init(); +void board_ir_gpio_init(); #define DEFAULT_TEST_UART "uart1" #define DEFAULT_TEST_UART_DMA_TX_REQUEST DMA_REQUEST_UART1_TX diff --git a/bsp/board/bl808dk/bl808_flash_d0.ld b/bsp/board/bl808dk/bl808_flash_d0.ld index 15d31c54..c7077ffc 100644 --- a/bsp/board/bl808dk/bl808_flash_d0.ld +++ b/bsp/board/bl808dk/bl808_flash_d0.ld @@ -26,7 +26,7 @@ MEMORY itcm_memory (rx) : ORIGIN = 0x3eff0000, LENGTH = 28K dtcm_memory (rx) : ORIGIN = 0x3eff7000, LENGTH = 4K nocache_ram_memory (!rx) : ORIGIN = 0x3eff8000, LENGTH = 0K - ram_memory (!rx) : ORIGIN = 0x3eff8000, LENGTH = 64K + ram_memory (!rx) : ORIGIN = 0x3eff8000, LENGTH = 32K + 32K xram_memory (!rx) : ORIGIN = 0x40004000, LENGTH = 16K } diff --git a/bsp/board/bl808dk/board.c b/bsp/board/bl808dk/board.c index 99b44a6b..5116b970 100644 --- a/bsp/board/bl808dk/board.c +++ b/bsp/board/bl808dk/board.c @@ -3,7 +3,11 @@ #include "bflb_clock.h" #include "bflb_rtc.h" #include "bflb_flash.h" -#include "mmheap.h" +#ifdef CONFIG_TLSF +#include "bflb_tlsf.h" +#else +#include "bflb_mmheap.h" +#endif #include "bl808_glb.h" #include "bl808_sflash.h" #include "bl808_psram_uhs.h" @@ -19,18 +23,21 @@ extern uint32_t __HeapBase; extern uint32_t __HeapLimit; +#ifndef CONFIG_TLSF struct heap_info mmheap_root; static struct heap_region system_mmheap[] = { { NULL, 0 }, { NULL, 0 }, /* Terminates the array. */ }; +#endif static struct bflb_device_s *uart0; #if (defined(CONFIG_LUA) || defined(CONFIG_BFLOG) || defined(CONFIG_FATFS)) static struct bflb_device_s *rtc; #endif + #if defined(CPU_M0) static void system_clock_init(void) { @@ -42,6 +49,7 @@ static void system_clock_init(void) GLB_Set_MCU_System_CLK(GLB_MCU_SYS_CLK_WIFIPLL_320M); GLB_Set_DSP_System_CLK(GLB_DSP_SYS_CLK_CPUPLL_400M); + GLB_Config_CPU_PLL(GLB_XTAL_40M, cpuPllCfg_480M); CPU_Set_MTimer_CLK(ENABLE, CPU_Get_MTimer_Source_Clock() / 1000 / 1000 - 1); } @@ -60,6 +68,7 @@ static void peripheral_clock_init(void) PERIPHERAL_CLOCK_IR_ENABLE(); PERIPHERAL_CLOCK_I2S_ENABLE(); PERIPHERAL_CLOCK_USB_ENABLE(); + PERIPHERAL_CLOCK_CAN_UART2_ENABLE(); GLB_Set_ADC_CLK(ENABLE, GLB_ADC_CLK_XCLK, 4); GLB_Set_UART_CLK(ENABLE, HBN_UART_CLK_XCLK, 0); @@ -71,8 +80,10 @@ static void peripheral_clock_init(void) GLB_Set_DIG_CLK_Sel(GLB_DIG_CLK_XCLK); GLB_Set_DIG_512K_CLK(ENABLE, ENABLE, 0x4E); GLB_Set_PWM1_IO_Sel(GLB_PWM1_IO_DIFF_END); + GLB_Set_CAM_CLK(ENABLE, GLB_CAM_CLK_WIFIPLL_96M, 3); GLB_Set_PKA_CLK_Sel(GLB_PKA_CLK_MCU_MUXPLL_160M); + #ifdef CONFIG_BSP_SDH_SDCARD PERIPHERAL_CLOCK_SDH_ENABLE(); uint32_t tmp_val; @@ -81,12 +92,11 @@ static void peripheral_clock_init(void) tmp_val2 &= ~(1 << 0); tmp_val = BL_SET_REG_BITS_VAL(tmp_val, PDS_CR_PDS_GPIO_KEEP_EN, tmp_val2); BL_WR_REG(PDS_BASE, PDS_CTL5, tmp_val); - - GLB_Set_SDH_CLK(ENABLE, GLB_SDH_CLK_WIFIPLL_96M, 0); GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_SDH); #endif GLB_Set_USB_CLK_From_WIFIPLL(1); } + #ifdef CONFIG_PSRAM #define WB_4MB_PSRAM (1) #define UHS_32MB_PSRAM (2) @@ -236,12 +246,16 @@ void board_init(void) bflb_irq_restore(flag); +#ifdef CONFIG_TLSF + bflb_mmheap_init((void *)&__HeapBase, ((size_t)&__HeapLimit - (size_t)&__HeapBase)); +#else system_mmheap[0].addr = (uint8_t *)&__HeapBase; system_mmheap[0].mem_size = ((size_t)&__HeapLimit - (size_t)&__HeapBase); if (system_mmheap[0].mem_size > 0) { - mmheap_init(&mmheap_root, system_mmheap); + bflb_mmheap_init(&mmheap_root, system_mmheap); } +#endif console_init(); @@ -275,21 +289,26 @@ void board_init(void) bflb_irq_initialize(); +#ifdef CONFIG_TLSF + bflb_mmheap_init((void *)&__HeapBase, ((size_t)&__HeapLimit - (size_t)&__HeapBase)); +#else system_mmheap[0].addr = (uint8_t *)&__HeapBase; system_mmheap[0].mem_size = ((size_t)&__HeapLimit - (size_t)&__HeapBase); if (system_mmheap[0].mem_size > 0) { - mmheap_init(&mmheap_root, system_mmheap); + bflb_mmheap_init(&mmheap_root, system_mmheap); } +#endif console_init(); bl_show_log(); - printf("dynamic memory init success,heap size = %d Kbyte \r\n", system_mmheap[0].mem_size / 1024); + printf("dynamic memory init success,heap size = %d Kbyte \r\n", ((size_t)&__HeapLimit - (size_t)&__HeapBase) / 1024); printf("sig1:%08x\r\n", BL_RD_REG(GLB_BASE, GLB_UART_CFG1)); printf("sig2:%08x\r\n", BL_RD_REG(GLB_BASE, GLB_UART_CFG2)); + printf("cgen1:%08x\r\n", getreg32(BFLB_GLB_CGEN1_BASE)); } #endif @@ -419,7 +438,6 @@ void board_emac_gpio_init(void) #endif } -#ifdef CONFIG_BSP_SDH_SDCARD void board_sdh_gpio_init(void) { struct bflb_device_s *gpio; @@ -431,27 +449,58 @@ void board_sdh_gpio_init(void) bflb_gpio_init(gpio, GPIO_PIN_3, GPIO_FUNC_SDH | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_2); bflb_gpio_init(gpio, GPIO_PIN_4, GPIO_FUNC_SDH | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_2); bflb_gpio_init(gpio, GPIO_PIN_5, GPIO_FUNC_SDH | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_2); - - /* config sdh clock */ - SDH_ClockSet(400000, 96000000, 48000000); - -#ifdef CONFIG_BSP_FATFS_SDH_SDCARD - extern void fatfs_sdh_driver_register(void); - fatfs_sdh_driver_register(); -#endif } -#endif +void board_dvp1_gpio_init(void) +{ + struct bflb_device_s *gpio; + + gpio = bflb_device_get_by_name("gpio"); + /* I2C GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_22, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_23, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + + /* Power down GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_21, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_reset(gpio, GPIO_PIN_21); + + /* Reset GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_20, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_set(gpio, GPIO_PIN_20); + + /* MCLK GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_33, GPIO_FUNC_CLKOUT | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + + /* DVP1 GPIO */ + bflb_gpio_init(gpio, GPIO_PIN_16, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_17, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_24, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_25, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_26, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_27, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_28, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_29, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_30, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_31, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); + bflb_gpio_init(gpio, GPIO_PIN_32, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1); +} + +void board_iso11898_gpio_init() +{ + // struct bflb_device_s *gpio; + + // gpio = bflb_device_get_by_name("gpio"); +} #ifdef CONFIG_BFLOG __attribute__((weak)) uint64_t bflog_clock(void) { - return CPU_Get_MTimer_Counter(); + return bflb_mtimer_get_time_us(); } __attribute__((weak)) uint32_t bflog_time(void) { - return BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)) + 1640995200; + return BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)); } __attribute__((weak)) char *bflog_thread(void) @@ -463,12 +512,12 @@ __attribute__((weak)) char *bflog_thread(void) #ifdef CONFIG_LUA __attribute__((weak)) clock_t luaport_clock(void) { - return (clock_t)CPU_Get_MTimer_Counter(); + return (clock_t)bflb_mtimer_get_time_us(); } __attribute__((weak)) time_t luaport_time(time_t *seconds) { - time_t t = (time_t)BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)) + 1640995200; + time_t t = (time_t)BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)); if (seconds != NULL) { *seconds = t; } @@ -483,7 +532,7 @@ __attribute__((weak)) uint32_t get_fattime(void) { bflb_timestamp_t tm; - bflb_timestamp_utc2time(BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)) + 1640995200, &tm); + bflb_timestamp_utc2time(BFLB_RTC_TIME2SEC(bflb_rtc_get_time(rtc)), &tm); return ((uint32_t)(tm.year - 1980) << 25) /* Year 2015 */ | ((uint32_t)tm.mon << 21) /* Month 1 */ diff --git a/bsp/board/bl808dk/board.h b/bsp/board/bl808dk/board.h index 8bd89e8c..76558144 100644 --- a/bsp/board/bl808dk/board.h +++ b/bsp/board/bl808dk/board.h @@ -12,15 +12,14 @@ void board_adc_gpio_init(void); void board_dac_gpio_init(void); void board_ir_gpio_init(void); void board_emac_gpio_init(void); - -#ifdef CONFIG_BSP_SDH_SDCARD void board_sdh_gpio_init(void); -#endif +void board_dvp1_gpio_init(void); +void board_iso11898_gpio_init(); -#define DEFAULT_TEST_UART "uart1" +#define DEFAULT_TEST_UART "uart1" #define DEFAULT_TEST_UART_DMA_TX_REQUEST DMA_REQUEST_UART1_TX #define DEFAULT_TEST_UART_DMA_RX_REQUEST DMA_REQUEST_UART1_RX -#define DEFAULT_TEST_UART_DMA_TDR DMA_ADDR_UART1_TDR -#define DEFAULT_TEST_UART_DMA_RDR DMA_ADDR_UART1_RDR +#define DEFAULT_TEST_UART_DMA_TDR DMA_ADDR_UART1_TDR +#define DEFAULT_TEST_UART_DMA_RDR DMA_ADDR_UART1_RDR #endif \ No newline at end of file diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 79e05e27..4a7d4a31 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -6,8 +6,9 @@ sdk_add_subdirectory_ifdef(CONFIG_BFLOG bflog) sdk_add_subdirectory_ifdef(CONFIG_LUA lua) sdk_add_subdirectory_ifdef(CONFIG_LVGL lvgl) sdk_add_subdirectory_ifdef(CONFIG_LWIP lwip) -sdk_add_subdirectory_ifdef(CONFIG_BLE ble) +sdk_add_subdirectory_ifdef(CONFIG_BLUETOOTH bluetooth) sdk_add_subdirectory_ifdef(CONFIG_XZ xz) sdk_add_subdirectory_ifdef(CONFIG_TINYMAIX TinyMaix) sdk_add_subdirectory_ifdef(CONFIG_TENSORFLOWLITE TensorFlowLite) +sdk_add_subdirectory_ifdef(CONFIG_TLSF tlsf) add_subdirectory(utils) diff --git a/components/tlsf/CMakeLists.txt b/components/tlsf/CMakeLists.txt new file mode 100644 index 00000000..83b0e582 --- /dev/null +++ b/components/tlsf/CMakeLists.txt @@ -0,0 +1,4 @@ +sdk_generate_library() +sdk_library_add_sources(tlsf.c bflb_tlsf.c) +sdk_add_include_directories(.) +sdk_add_compile_definitions(-DCONFIG_TLSF) \ No newline at end of file diff --git a/components/tlsf/bflb_tlsf.c b/components/tlsf/bflb_tlsf.c new file mode 100644 index 00000000..c9bcbae7 --- /dev/null +++ b/components/tlsf/bflb_tlsf.c @@ -0,0 +1,59 @@ +#include "tlsf.h" +#include "bflb_tlsf.h" + +static tlsf_t tlsf_ptr = 0; + +int bflb_mmheap_init(void *begin_addr, uint32_t size) +{ + tlsf_ptr = (tlsf_t)tlsf_create_with_pool(begin_addr, size); + + if (tlsf_ptr == 0) { + return -1; + } + return 0; +} + +void *bflb_malloc(size_t nbytes) +{ + void *ptr; + ptr = tlsf_malloc(tlsf_ptr, nbytes); + + return ptr; +} + +void bflb_free(void *ptr) +{ + tlsf_free(tlsf_ptr, ptr); +} + +void *bflb_realloc(void *ptr, size_t nbytes) +{ + if (tlsf_ptr) { + ptr = tlsf_realloc(tlsf_ptr, ptr, nbytes); + } + return ptr; +} + +void *bflb_calloc(size_t count, size_t nbytes) +{ + void *ptr = NULL; + size_t total_size; + + total_size = count * nbytes; + ptr = tlsf_malloc(tlsf_ptr, nbytes); + if (ptr != NULL) { + /* clean memory */ + memset(ptr, 0, total_size); + } + + return ptr; +} + +void *bflb_malloc_align(size_t align, size_t size) +{ + void *ptr = NULL; + + ptr = tlsf_memalign(tlsf_ptr, align, size); + + return ptr; +} \ No newline at end of file diff --git a/components/tlsf/bflb_tlsf.h b/components/tlsf/bflb_tlsf.h new file mode 100644 index 00000000..c71b0e9d --- /dev/null +++ b/components/tlsf/bflb_tlsf.h @@ -0,0 +1,14 @@ +#ifndef BFLB_TLSF_H +#define BFLB_TLSF_H + +#include +#include + +int bflb_mmheap_init(void *begin_addr, uint32_t size); +void *bflb_malloc(size_t nbytes); +void bflb_free(void *ptr); +void *bflb_realloc(void *ptr, size_t nbytes); +void *bflb_calloc(size_t count, size_t size); +void *bflb_malloc_align(size_t align, size_t size); + +#endif \ No newline at end of file diff --git a/components/tlsf/tlsf.c b/components/tlsf/tlsf.c new file mode 100644 index 00000000..08d2a15f --- /dev/null +++ b/components/tlsf/tlsf.c @@ -0,0 +1,1286 @@ +#include +#include +#include +#include +#include +#include + +#include "tlsf.h" + +#if defined(__cplusplus) + #define tlsf_decl inline +#else + #define tlsf_decl static +#endif + +/* +** Architecture-specific bit manipulation routines. +** +** TLSF achieves O(1) cost for malloc and free operations by limiting +** the search for a free block to a free list of guaranteed size +** adequate to fulfill the request, combined with efficient free list +** queries using bitmasks and architecture-specific bit-manipulation +** routines. +** +** Most modern processors provide instructions to count leading zeroes +** in a word, find the lowest and highest set bit, etc. These +** specific implementations will be used when available, falling back +** to a reasonably efficient generic implementation. +** +** NOTE: TLSF spec relies on ffs/fls returning value 0..31. +** ffs/fls return 1-32 by default, returning 0 for error. +*/ + +/* +** Detect whether or not we are building for a 32- or 64-bit (LP/LLP) +** architecture. There is no reliable portable method at compile-time. +*/ +#if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) \ + || defined (_WIN64) || defined (__LP64__) || defined (__LLP64__) + #define TLSF_64BIT +#endif + +/* +** gcc 3.4 and above have builtin support, specialized for architecture. +** Some compilers masquerade as gcc; patchlevel test filters them out. +*/ +#if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \ + && defined (__GNUC_PATCHLEVEL__) + +#if defined (__SNC__) +/* SNC for Playstation 3. */ + +tlsf_decl int tlsf_ffs(unsigned int word) +{ + const unsigned int reverse = word & (~word + 1); + const int bit = 32 - __builtin_clz(reverse); + return bit - 1; +} + +#else + +tlsf_decl int tlsf_ffs(unsigned int word) +{ + return __builtin_ffs(word) - 1; +} + +#endif + +tlsf_decl int tlsf_fls(unsigned int word) +{ + const int bit = word ? 32 - __builtin_clz(word) : 0; + return bit - 1; +} + +#elif defined (_MSC_VER) && (_MSC_VER >= 1400) && (defined (_M_IX86) || defined (_M_X64)) +/* Microsoft Visual C++ support on x86/X64 architectures. */ + +#include + +#pragma intrinsic(_BitScanReverse) +#pragma intrinsic(_BitScanForward) + +tlsf_decl int tlsf_fls(unsigned int word) +{ + unsigned long index; + return _BitScanReverse(&index, word) ? index : -1; +} + +tlsf_decl int tlsf_ffs(unsigned int word) +{ + unsigned long index; + return _BitScanForward(&index, word) ? index : -1; +} + +#elif defined (_MSC_VER) && defined (_M_PPC) +/* Microsoft Visual C++ support on PowerPC architectures. */ + +#include + +tlsf_decl int tlsf_fls(unsigned int word) +{ + const int bit = 32 - _CountLeadingZeros(word); + return bit - 1; +} + +tlsf_decl int tlsf_ffs(unsigned int word) +{ + const unsigned int reverse = word & (~word + 1); + const int bit = 32 - _CountLeadingZeros(reverse); + return bit - 1; +} + +#elif defined (__ARMCC_VERSION) +/* RealView Compilation Tools for ARM */ + +tlsf_decl int tlsf_ffs(unsigned int word) +{ + const unsigned int reverse = word & (~word + 1); + const int bit = 32 - __clz(reverse); + return bit - 1; +} + +tlsf_decl int tlsf_fls(unsigned int word) +{ + const int bit = word ? 32 - __clz(word) : 0; + return bit - 1; +} + +#elif defined (__ghs__) +/* Green Hills support for PowerPC */ + +#include + +tlsf_decl int tlsf_ffs(unsigned int word) +{ + const unsigned int reverse = word & (~word + 1); + const int bit = 32 - __CLZ32(reverse); + return bit - 1; +} + +tlsf_decl int tlsf_fls(unsigned int word) +{ + const int bit = word ? 32 - __CLZ32(word) : 0; + return bit - 1; +} + +#else +/* Fall back to generic implementation. */ + +tlsf_decl int tlsf_fls_generic(unsigned int word) +{ + int bit = 32; + + if (!word) bit -= 1; + if (!(word & 0xffff0000)) + { + word <<= 16; + bit -= 16; + } + if (!(word & 0xff000000)) + { + word <<= 8; + bit -= 8; + } + if (!(word & 0xf0000000)) + { + word <<= 4; + bit -= 4; + } + if (!(word & 0xc0000000)) + { + word <<= 2; + bit -= 2; + } + if (!(word & 0x80000000)) + { + word <<= 1; + bit -= 1; + } + + return bit; +} + +/* Implement ffs in terms of fls. */ +tlsf_decl int tlsf_ffs(unsigned int word) +{ + return tlsf_fls_generic(word & (~word + 1)) - 1; +} + +tlsf_decl int tlsf_fls(unsigned int word) +{ + return tlsf_fls_generic(word) - 1; +} + +#endif + +/* Possibly 64-bit version of tlsf_fls. */ +#if defined (TLSF_64BIT) +tlsf_decl int tlsf_fls_sizet(size_t size) +{ + int high = (int)(size >> 32); + int bits = 0; + if (high) + { + bits = 32 + tlsf_fls(high); + } + else + { + bits = tlsf_fls((int)size & 0xffffffff); + + } + return bits; +} +#else +#define tlsf_fls_sizet tlsf_fls +#endif + +#undef tlsf_decl + +/* +** Constants. +*/ + +/* Public constants: may be modified. */ +enum tlsf_public +{ + /* log2 of number of linear subdivisions of block sizes. Larger + ** values require more memory in the control structure. Values of + ** 4 or 5 are typical. + */ + SL_INDEX_COUNT_LOG2 = 5, +}; + +/* Private constants: do not modify. */ +enum tlsf_private +{ +#if defined (TLSF_64BIT) + /* All allocation sizes and addresses are aligned to 8 bytes. */ + ALIGN_SIZE_LOG2 = 3, +#else + /* All allocation sizes and addresses are aligned to 4 bytes. */ + ALIGN_SIZE_LOG2 = 2, +#endif + ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2), + + /* + ** We support allocations of sizes up to (1 << FL_INDEX_MAX) bits. + ** However, because we linearly subdivide the second-level lists, and + ** our minimum size granularity is 4 bytes, it doesn't make sense to + ** create first-level lists for sizes smaller than SL_INDEX_COUNT * 4, + ** or (1 << (SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be + ** trying to split size ranges into more slots than we have available. + ** Instead, we calculate the minimum threshold size, and place all + ** blocks below that size into the 0th first-level list. + */ + +#if defined (TLSF_64BIT) + /* + ** TODO: We can increase this to support larger sizes, at the expense + ** of more overhead in the TLSF structure. + */ + FL_INDEX_MAX = 32, +#else + FL_INDEX_MAX = 30, +#endif + SL_INDEX_COUNT = (1 << SL_INDEX_COUNT_LOG2), + FL_INDEX_SHIFT = (SL_INDEX_COUNT_LOG2 + ALIGN_SIZE_LOG2), + FL_INDEX_COUNT = (FL_INDEX_MAX - FL_INDEX_SHIFT + 1), + + SMALL_BLOCK_SIZE = (1 << FL_INDEX_SHIFT), +}; + +/* +** Cast and min/max macros. +*/ + +#define tlsf_cast(t, exp) ((t) (exp)) +#define tlsf_min(a, b) ((a) < (b) ? (a) : (b)) +#define tlsf_max(a, b) ((a) > (b) ? (a) : (b)) + +#define tlsf_assert +/* +** Set assert macro, if it has not been provided by the user. +*/ +#if !defined (tlsf_assert) + #define tlsf_assert assert +#endif + +/* +** Static assertion mechanism. +*/ + +#define _tlsf_glue2(x, y) x ## y +#define _tlsf_glue(x, y) _tlsf_glue2(x, y) +#define tlsf_static_assert(exp) \ + typedef char _tlsf_glue(static_assert, __LINE__) [(exp) ? 1 : -1] + +/* This code has been tested on 32- and 64-bit (LP/LLP) architectures. */ +tlsf_static_assert(sizeof(int) * CHAR_BIT == 32); +tlsf_static_assert(sizeof(size_t) * CHAR_BIT >= 32); +tlsf_static_assert(sizeof(size_t) * CHAR_BIT <= 64); + +/* SL_INDEX_COUNT must be <= number of bits in sl_bitmap's storage type. */ +tlsf_static_assert(sizeof(unsigned int) * CHAR_BIT >= SL_INDEX_COUNT); + +/* Ensure we've properly tuned our sizes. */ +tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT); + +/* +** Data structures and associated constants. +*/ + +/* +** Block header structure. +** +** There are several implementation subtleties involved: +** - The prev_phys_block field is only valid if the previous block is free. +** - The prev_phys_block field is actually stored at the end of the +** previous block. It appears at the beginning of this structure only to +** simplify the implementation. +** - The next_free / prev_free fields are only valid if the block is free. +*/ +typedef struct block_header_t +{ + /* Points to the previous physical block. */ + struct block_header_t *prev_phys_block; + + /* The size of this block, excluding the block header. */ + size_t size; + + /* Next and previous free blocks. */ + struct block_header_t *next_free; + struct block_header_t *prev_free; +} block_header_t; + +/* +** Since block sizes are always at least a multiple of 4, the two least +** significant bits of the size field are used to store the block status: +** - bit 0: whether block is busy or free +** - bit 1: whether previous block is busy or free +*/ +static const size_t block_header_free_bit = 1 << 0; +static const size_t block_header_prev_free_bit = 1 << 1; + +/* +** The size of the block header exposed to used blocks is the size field. +** The prev_phys_block field is stored *inside* the previous free block. +*/ +static const size_t block_header_overhead = sizeof(size_t); + +/* User data starts directly after the size field in a used block. */ +static const size_t block_start_offset = + offsetof(block_header_t, size) + sizeof(size_t); + +/* +** A free block must be large enough to store its header minus the size of +** the prev_phys_block field, and no larger than the number of addressable +** bits for FL_INDEX. +*/ +static const size_t block_size_min = + sizeof(block_header_t) - sizeof(block_header_t *); +static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX; + + +/* The TLSF control structure. */ +typedef struct control_t +{ + /* Empty lists point at this block to indicate they are free. */ + block_header_t block_null; + + /* Bitmaps for free lists. */ + unsigned int fl_bitmap; + unsigned int sl_bitmap[FL_INDEX_COUNT]; + + /* Head of free lists. */ + block_header_t *blocks[FL_INDEX_COUNT][SL_INDEX_COUNT]; +} control_t; + +/* A type used for casting when doing pointer arithmetic. */ +typedef ptrdiff_t tlsfptr_t; + +/* +** block_header_t member functions. +*/ + +static size_t block_size(const block_header_t *block) +{ + return block->size & ~(block_header_free_bit | block_header_prev_free_bit); +} + +static void block_set_size(block_header_t *block, size_t size) +{ + const size_t oldsize = block->size; + block->size = size | (oldsize & (block_header_free_bit | block_header_prev_free_bit)); +} + +static int block_is_last(const block_header_t *block) +{ + return block_size(block) == 0; +} + +static int block_is_free(const block_header_t *block) +{ + return tlsf_cast(int, block->size & block_header_free_bit); +} + +static void block_set_free(block_header_t *block) +{ + block->size |= block_header_free_bit; +} + +static void block_set_used(block_header_t *block) +{ + block->size &= ~block_header_free_bit; +} + +static int block_is_prev_free(const block_header_t *block) +{ + return tlsf_cast(int, block->size & block_header_prev_free_bit); +} + +static void block_set_prev_free(block_header_t *block) +{ + block->size |= block_header_prev_free_bit; +} + +static void block_set_prev_used(block_header_t *block) +{ + block->size &= ~block_header_prev_free_bit; +} + +static block_header_t *block_from_ptr(const void *ptr) +{ + return tlsf_cast(block_header_t *, + tlsf_cast(unsigned char *, ptr) - block_start_offset); +} + +static void *block_to_ptr(const block_header_t *block) +{ + return tlsf_cast(void *, + tlsf_cast(unsigned char *, block) + block_start_offset); +} + +/* Return location of next block after block of given size. */ +static block_header_t *offset_to_block(const void *ptr, size_t size) +{ + return tlsf_cast(block_header_t *, tlsf_cast(tlsfptr_t, ptr) + size); +} + +/* Return location of previous block. */ +static block_header_t *block_prev(const block_header_t *block) +{ + tlsf_assert(block_is_prev_free(block) && "previous block must be free"); + return block->prev_phys_block; +} + +/* Return location of next existing block. */ +static block_header_t *block_next(const block_header_t *block) +{ + block_header_t *next = offset_to_block(block_to_ptr(block), + block_size(block) - block_header_overhead); + tlsf_assert(!block_is_last(block)); + return next; +} + +/* Link a new block with its physical neighbor, return the neighbor. */ +static block_header_t *block_link_next(block_header_t *block) +{ + block_header_t *next = block_next(block); + next->prev_phys_block = block; + return next; +} + +static void block_mark_as_free(block_header_t *block) +{ + /* Link the block to the next block, first. */ + block_header_t *next = block_link_next(block); + block_set_prev_free(next); + block_set_free(block); +} + +static void block_mark_as_used(block_header_t *block) +{ + block_header_t *next = block_next(block); + block_set_prev_used(next); + block_set_used(block); +} + +static size_t align_up(size_t x, size_t align) +{ + tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); + return (x + (align - 1)) & ~(align - 1); +} + +static size_t align_down(size_t x, size_t align) +{ + tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); + return x - (x & (align - 1)); +} + +static void *align_ptr(const void *ptr, size_t align) +{ + const tlsfptr_t aligned = + (tlsf_cast(tlsfptr_t, ptr) + (align - 1)) & ~(align - 1); + tlsf_assert(0 == (align & (align - 1)) && "must align to a power of two"); + return tlsf_cast(void *, aligned); +} + +/* +** Adjust an allocation size to be aligned to word size, and no smaller +** than internal minimum. +*/ +static size_t adjust_request_size(size_t size, size_t align) +{ + size_t adjust = 0; + if (size) + { + const size_t aligned = align_up(size, align); + + /* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */ + if (aligned < block_size_max) + { + adjust = tlsf_max(aligned, block_size_min); + } + } + return adjust; +} + +/* +** TLSF utility functions. In most cases, these are direct translations of +** the documentation found in the white paper. +*/ + +static void mapping_insert(size_t size, int *fli, int *sli) +{ + int fl, sl; + if (size < SMALL_BLOCK_SIZE) + { + /* Store small blocks in first list. */ + fl = 0; + sl = tlsf_cast(int, size) / (SMALL_BLOCK_SIZE / SL_INDEX_COUNT); + } + else + { + fl = tlsf_fls_sizet(size); + sl = tlsf_cast(int, size >> (fl - SL_INDEX_COUNT_LOG2)) ^ (1 << SL_INDEX_COUNT_LOG2); + fl -= (FL_INDEX_SHIFT - 1); + } + *fli = fl; + *sli = sl; +} + +/* This version rounds up to the next block size (for allocations) */ +static void mapping_search(size_t size, int *fli, int *sli) +{ + if (size >= SMALL_BLOCK_SIZE) + { + const size_t round = (1 << (tlsf_fls_sizet(size) - SL_INDEX_COUNT_LOG2)) - 1; + size += round; + } + mapping_insert(size, fli, sli); +} + +static block_header_t *search_suitable_block(control_t *control, int *fli, int *sli) +{ + int fl = *fli; + int sl = *sli; + + /* + ** First, search for a block in the list associated with the given + ** fl/sl index. + */ + unsigned int sl_map = control->sl_bitmap[fl] & (~0U << sl); + if (!sl_map) + { + /* No block exists. Search in the next largest first-level list. */ + const unsigned int fl_map = control->fl_bitmap & (~0U << (fl + 1)); + if (!fl_map) + { + /* No free blocks available, memory has been exhausted. */ + return 0; + } + + fl = tlsf_ffs(fl_map); + *fli = fl; + sl_map = control->sl_bitmap[fl]; + } + tlsf_assert(sl_map && "internal error - second level bitmap is null"); + sl = tlsf_ffs(sl_map); + *sli = sl; + + /* Return the first block in the free list. */ + return control->blocks[fl][sl]; +} + +/* Remove a free block from the free list.*/ +static void remove_free_block(control_t *control, block_header_t *block, int fl, int sl) +{ + block_header_t *prev = block->prev_free; + block_header_t *next = block->next_free; + tlsf_assert(prev && "prev_free field can not be null"); + tlsf_assert(next && "next_free field can not be null"); + next->prev_free = prev; + prev->next_free = next; + + /* If this block is the head of the free list, set new head. */ + if (control->blocks[fl][sl] == block) + { + control->blocks[fl][sl] = next; + + /* If the new head is null, clear the bitmap. */ + if (next == &control->block_null) + { + control->sl_bitmap[fl] &= ~(1U << sl); + + /* If the second bitmap is now empty, clear the fl bitmap. */ + if (!control->sl_bitmap[fl]) + { + control->fl_bitmap &= ~(1U << fl); + } + } + } +} + +/* Insert a free block into the free block list. */ +static void insert_free_block(control_t *control, block_header_t *block, int fl, int sl) +{ + block_header_t *current = control->blocks[fl][sl]; + tlsf_assert(current && "free list cannot have a null entry"); + tlsf_assert(block && "cannot insert a null entry into the free list"); + block->next_free = current; + block->prev_free = &control->block_null; + current->prev_free = block; + + tlsf_assert(block_to_ptr(block) == align_ptr(block_to_ptr(block), ALIGN_SIZE) + && "block not aligned properly"); + /* + ** Insert the new block at the head of the list, and mark the first- + ** and second-level bitmaps appropriately. + */ + control->blocks[fl][sl] = block; + control->fl_bitmap |= (1U << fl); + control->sl_bitmap[fl] |= (1U << sl); +} + +/* Remove a given block from the free list. */ +static void block_remove(control_t *control, block_header_t *block) +{ + int fl, sl; + mapping_insert(block_size(block), &fl, &sl); + remove_free_block(control, block, fl, sl); +} + +/* Insert a given block into the free list. */ +static void block_insert(control_t *control, block_header_t *block) +{ + int fl, sl; + mapping_insert(block_size(block), &fl, &sl); + insert_free_block(control, block, fl, sl); +} + +static int block_can_split(block_header_t *block, size_t size) +{ + return block_size(block) >= sizeof(block_header_t) + size; +} + +/* Split a block into two, the second of which is free. */ +static block_header_t *block_split(block_header_t *block, size_t size) +{ + /* Calculate the amount of space left in the remaining block. */ + block_header_t *remaining = + offset_to_block(block_to_ptr(block), size - block_header_overhead); + + const size_t remain_size = block_size(block) - (size + block_header_overhead); + + tlsf_assert(block_to_ptr(remaining) == align_ptr(block_to_ptr(remaining), ALIGN_SIZE) + && "remaining block not aligned properly"); + + tlsf_assert(block_size(block) == remain_size + size + block_header_overhead); + block_set_size(remaining, remain_size); + tlsf_assert(block_size(remaining) >= block_size_min && "block split with invalid size"); + + block_set_size(block, size); + block_mark_as_free(remaining); + + return remaining; +} + +/* Absorb a free block's storage into an adjacent previous free block. */ +static block_header_t *block_absorb(block_header_t *prev, block_header_t *block) +{ + tlsf_assert(!block_is_last(prev) && "previous block can't be last"); + /* Note: Leaves flags untouched. */ + prev->size += block_size(block) + block_header_overhead; + block_link_next(prev); + return prev; +} + +/* Merge a just-freed block with an adjacent previous free block. */ +static block_header_t *block_merge_prev(control_t *control, block_header_t *block) +{ + if (block_is_prev_free(block)) + { + block_header_t *prev = block_prev(block); + tlsf_assert(prev && "prev physical block can't be null"); + tlsf_assert(block_is_free(prev) && "prev block is not free though marked as such"); + block_remove(control, prev); + block = block_absorb(prev, block); + } + + return block; +} + +/* Merge a just-freed block with an adjacent free block. */ +static block_header_t *block_merge_next(control_t *control, block_header_t *block) +{ + block_header_t *next = block_next(block); + tlsf_assert(next && "next physical block can't be null"); + + if (block_is_free(next)) + { + tlsf_assert(!block_is_last(block) && "previous block can't be last"); + block_remove(control, next); + block = block_absorb(block, next); + } + + return block; +} + +/* Trim any trailing block space off the end of a block, return to pool. */ +static void block_trim_free(control_t *control, block_header_t *block, size_t size) +{ + tlsf_assert(block_is_free(block) && "block must be free"); + if (block_can_split(block, size)) + { + block_header_t *remaining_block = block_split(block, size); + block_link_next(block); + block_set_prev_free(remaining_block); + block_insert(control, remaining_block); + } +} + +/* Trim any trailing block space off the end of a used block, return to pool. */ +static void block_trim_used(control_t *control, block_header_t *block, size_t size) +{ + tlsf_assert(!block_is_free(block) && "block must be used"); + if (block_can_split(block, size)) + { + /* If the next block is free, we must coalesce. */ + block_header_t *remaining_block = block_split(block, size); + block_set_prev_used(remaining_block); + + remaining_block = block_merge_next(control, remaining_block); + block_insert(control, remaining_block); + } +} + +static block_header_t *block_trim_free_leading(control_t *control, block_header_t *block, size_t size) +{ + block_header_t *remaining_block = block; + if (block_can_split(block, size)) + { + /* We want the 2nd block. */ + remaining_block = block_split(block, size - block_header_overhead); + block_set_prev_free(remaining_block); + + block_link_next(block); + block_insert(control, block); + } + + return remaining_block; +} + +static block_header_t *block_locate_free(control_t *control, size_t size) +{ + int fl = 0, sl = 0; + block_header_t *block = 0; + + if (size) + { + mapping_search(size, &fl, &sl); + + /* + ** mapping_search can futz with the size, so for excessively large sizes it can sometimes wind up + ** with indices that are off the end of the block array. + ** So, we protect against that here, since this is the only callsite of mapping_search. + ** Note that we don't need to check sl, since it comes from a modulo operation that guarantees it's always in range. + */ + if (fl < FL_INDEX_COUNT) + { + block = search_suitable_block(control, &fl, &sl); + } + } + + if (block) + { + tlsf_assert(block_size(block) >= size); + remove_free_block(control, block, fl, sl); + } + + return block; +} + +static void *block_prepare_used(control_t *control, block_header_t *block, size_t size) +{ + void *p = 0; + if (block) + { + tlsf_assert(size && "size must be non-zero"); + block_trim_free(control, block, size); + block_mark_as_used(block); + p = block_to_ptr(block); + } + return p; +} + +/* Clear structure and point all empty lists at the null block. */ +static void control_construct(control_t *control) +{ + int i, j; + + control->block_null.next_free = &control->block_null; + control->block_null.prev_free = &control->block_null; + + control->fl_bitmap = 0; + for (i = 0; i < FL_INDEX_COUNT; ++i) + { + control->sl_bitmap[i] = 0; + for (j = 0; j < SL_INDEX_COUNT; ++j) + { + control->blocks[i][j] = &control->block_null; + } + } +} + +/* +** Debugging utilities. +*/ + +typedef struct integrity_t +{ + int prev_status; + int status; +} integrity_t; + +#define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } } + +static void integrity_walker(void *ptr, size_t size, int used, void *user) +{ + block_header_t *block = block_from_ptr(ptr); + integrity_t *integ = tlsf_cast(integrity_t *, user); + const int this_prev_status = block_is_prev_free(block) ? 1 : 0; + const int this_status = block_is_free(block) ? 1 : 0; + const size_t this_block_size = block_size(block); + + int status = 0; + (void)used; + tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect"); + tlsf_insist(size == this_block_size && "block size incorrect"); + + integ->prev_status = this_status; + integ->status += status; +} + +int tlsf_check(tlsf_t tlsf) +{ + int i, j; + + control_t *control = tlsf_cast(control_t *, tlsf); + int status = 0; + + /* Check that the free lists and bitmaps are accurate. */ + for (i = 0; i < FL_INDEX_COUNT; ++i) + { + for (j = 0; j < SL_INDEX_COUNT; ++j) + { + const int fl_map = control->fl_bitmap & (1U << i); + const int sl_list = control->sl_bitmap[i]; + const int sl_map = sl_list & (1U << j); + const block_header_t *block = control->blocks[i][j]; + + /* Check that first- and second-level lists agree. */ + if (!fl_map) + { + tlsf_insist(!sl_map && "second-level map must be null"); + } + + if (!sl_map) + { + tlsf_insist(block == &control->block_null && "block list must be null"); + continue; + } + + /* Check that there is at least one free block. */ + tlsf_insist(sl_list && "no free blocks in second-level map"); + tlsf_insist(block != &control->block_null && "block should not be null"); + + while (block != &control->block_null) + { + int fli, sli; + tlsf_insist(block_is_free(block) && "block should be free"); + tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced"); + tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced"); + tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free"); + tlsf_insist(block_size(block) >= block_size_min && "block not minimum size"); + + mapping_insert(block_size(block), &fli, &sli); + tlsf_insist(fli == i && sli == j && "block size indexed in wrong list"); + block = block->next_free; + } + } + } + + return status; +} + +#undef tlsf_insist + +static void default_walker(void *ptr, size_t size, int used, void *user) +{ + (void)user; + printf("\t%x %s size: %x (%x)\n", ptr, used ? "used" : "free", (unsigned int)size, block_from_ptr(ptr)); +} + +void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void *user) +{ + tlsf_walker pool_walker = walker ? walker : default_walker; + block_header_t *block = + offset_to_block(pool, -(size_t)block_header_overhead); + + while (block && !block_is_last(block)) + { + pool_walker( + block_to_ptr(block), + block_size(block), + !block_is_free(block), + user); + block = block_next(block); + } +} + +size_t tlsf_block_size(void *ptr) +{ + size_t size = 0; + if (ptr) + { + const block_header_t *block = block_from_ptr(ptr); + size = block_size(block); + } + return size; +} + +int tlsf_check_pool(pool_t pool) +{ + /* Check that the blocks are physically correct. */ + integrity_t integ = { 0, 0 }; + tlsf_walk_pool(pool, integrity_walker, &integ); + + return integ.status; +} + +/* +** Size of the TLSF structures in a given memory block passed to +** tlsf_create, equal to the size of a control_t +*/ +size_t tlsf_size(void) +{ + return sizeof(control_t); +} + +size_t tlsf_align_size(void) +{ + return ALIGN_SIZE; +} + +size_t tlsf_block_size_min(void) +{ + return block_size_min; +} + +size_t tlsf_block_size_max(void) +{ + return block_size_max; +} + +/* +** Overhead of the TLSF structures in a given memory block passed to +** tlsf_add_pool, equal to the overhead of a free block and the +** sentinel block. +*/ +size_t tlsf_pool_overhead(void) +{ + return 2 * block_header_overhead; +} + +size_t tlsf_alloc_overhead(void) +{ + return block_header_overhead; +} + +pool_t tlsf_add_pool(tlsf_t tlsf, void *mem, size_t bytes) +{ + block_header_t *block; + block_header_t *next; + + const size_t pool_overhead = tlsf_pool_overhead(); + const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE); + + if (((ptrdiff_t)mem % ALIGN_SIZE) != 0) + { + printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n", + (unsigned int)ALIGN_SIZE); + return 0; + } + + if (pool_bytes < block_size_min || pool_bytes > block_size_max) + { +#if defined (TLSF_64BIT) + printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n", + (unsigned int)(pool_overhead + block_size_min), + (unsigned int)((pool_overhead + block_size_max) / 256)); +#else + printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n", + (unsigned int)(pool_overhead + block_size_min), + (unsigned int)(pool_overhead + block_size_max)); +#endif + return 0; + } + + /* + ** Create the main free block. Offset the start of the block slightly + ** so that the prev_phys_block field falls outside of the pool - + ** it will never be used. + */ + block = offset_to_block(mem, -(size_t)block_header_overhead); + block_set_size(block, pool_bytes); + block_set_free(block); + block_set_prev_used(block); + block_insert(tlsf_cast(control_t *, tlsf), block); + + /* Split the block to create a zero-size sentinel block. */ + next = block_link_next(block); + block_set_size(next, 0); + block_set_used(next); + block_set_prev_free(next); + + return mem; +} + +void tlsf_remove_pool(tlsf_t tlsf, pool_t pool) +{ + control_t *control = tlsf_cast(control_t *, tlsf); + block_header_t *block = offset_to_block(pool, -(size_t)block_header_overhead); + + int fl = 0, sl = 0; + + tlsf_assert(block_is_free(block) && "block should be free"); + tlsf_assert(!block_is_free(block_next(block)) && "next block should not be free"); + tlsf_assert(block_size(block_next(block)) == 0 && "next block size should be zero"); + + mapping_insert(block_size(block), &fl, &sl); + remove_free_block(control, block, fl, sl); +} + +/* +** TLSF main interface. +*/ + +#if _DEBUG +int test_ffs_fls() +{ + /* Verify ffs/fls work properly. */ + int rv = 0; + rv += (tlsf_ffs(0) == -1) ? 0 : 0x1; + rv += (tlsf_fls(0) == -1) ? 0 : 0x2; + rv += (tlsf_ffs(1) == 0) ? 0 : 0x4; + rv += (tlsf_fls(1) == 0) ? 0 : 0x8; + rv += (tlsf_ffs(0x80000000) == 31) ? 0 : 0x10; + rv += (tlsf_ffs(0x80008000) == 15) ? 0 : 0x20; + rv += (tlsf_fls(0x80000008) == 31) ? 0 : 0x40; + rv += (tlsf_fls(0x7FFFFFFF) == 30) ? 0 : 0x80; + +#if defined (TLSF_64BIT) + rv += (tlsf_fls_sizet(0x80000000) == 31) ? 0 : 0x100; + rv += (tlsf_fls_sizet(0x100000000) == 32) ? 0 : 0x200; + rv += (tlsf_fls_sizet(0xffffffffffffffff) == 63) ? 0 : 0x400; +#endif + + if (rv) + { + printf("test_ffs_fls: %x ffs/fls tests failed.\n", rv); + } + return rv; +} +#endif + +tlsf_t tlsf_create(void *mem) +{ +#if _DEBUG + if (test_ffs_fls()) + { + return 0; + } +#endif + + if (((tlsfptr_t)mem % ALIGN_SIZE) != 0) + { + printf("tlsf_create: Memory must be aligned to %u bytes.\n", + (unsigned int)ALIGN_SIZE); + return 0; + } + + control_construct(tlsf_cast(control_t *, mem)); + + return tlsf_cast(tlsf_t, mem); +} + +tlsf_t tlsf_create_with_pool(void *mem, size_t bytes) +{ + tlsf_t tlsf = tlsf_create(mem); + tlsf_add_pool(tlsf, (char *)mem + tlsf_size(), bytes - tlsf_size()); + return tlsf; +} + +void tlsf_destroy(tlsf_t tlsf) +{ + /* Nothing to do. */ + (void)tlsf; +} + +pool_t tlsf_get_pool(tlsf_t tlsf) +{ + return tlsf_cast(pool_t, (char *)tlsf + tlsf_size()); +} + +void *tlsf_malloc(tlsf_t tlsf, size_t size) +{ + control_t *control = tlsf_cast(control_t *, tlsf); + const size_t adjust = adjust_request_size(size, ALIGN_SIZE); + block_header_t *block = block_locate_free(control, adjust); + return block_prepare_used(control, block, adjust); +} + +void *tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) +{ + control_t *control = tlsf_cast(control_t *, tlsf); + const size_t adjust = adjust_request_size(size, ALIGN_SIZE); + + /* + ** We must allocate an additional minimum block size bytes so that if + ** our free block will leave an alignment gap which is smaller, we can + ** trim a leading free block and release it back to the pool. We must + ** do this because the previous physical block is in use, therefore + ** the prev_phys_block field is not valid, and we can't simply adjust + ** the size of that block. + */ + const size_t gap_minimum = sizeof(block_header_t); + const size_t size_with_gap = adjust_request_size(adjust + align + gap_minimum, align); + + /* + ** If alignment is less than or equals base alignment, we're done. + ** If we requested 0 bytes, return null, as tlsf_malloc(0) does. + */ + const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust; + + block_header_t *block = block_locate_free(control, aligned_size); + + /* This can't be a static assert. */ + tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead); + + if (block) + { + void *ptr = block_to_ptr(block); + void *aligned = align_ptr(ptr, align); + size_t gap = tlsf_cast(size_t, + tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); + + /* If gap size is too small, offset to next aligned boundary. */ + if (gap && gap < gap_minimum) + { + const size_t gap_remain = gap_minimum - gap; + const size_t offset = tlsf_max(gap_remain, align); + const void *next_aligned = tlsf_cast(void *, + tlsf_cast(tlsfptr_t, aligned) + offset); + + aligned = align_ptr(next_aligned, align); + gap = tlsf_cast(size_t, + tlsf_cast(tlsfptr_t, aligned) - tlsf_cast(tlsfptr_t, ptr)); + } + + if (gap) + { + tlsf_assert(gap >= gap_minimum && "gap size too small"); + block = block_trim_free_leading(control, block, gap); + } + } + + return block_prepare_used(control, block, adjust); +} + +void tlsf_free(tlsf_t tlsf, void *ptr) +{ + /* Don't attempt to free a NULL pointer. */ + if (ptr) + { + control_t *control = tlsf_cast(control_t *, tlsf); + block_header_t *block = block_from_ptr(ptr); + tlsf_assert(!block_is_free(block) && "block already marked as free"); + block_mark_as_free(block); + block = block_merge_prev(control, block); + block = block_merge_next(control, block); + block_insert(control, block); + } +} + +/* +** The TLSF block information provides us with enough information to +** provide a reasonably intelligent implementation of realloc, growing or +** shrinking the currently allocated block as required. +** +** This routine handles the somewhat esoteric edge cases of realloc: +** - a non-zero size with a null pointer will behave like malloc +** - a zero size with a non-null pointer will behave like free +** - a request that cannot be satisfied will leave the original buffer +** untouched +** - an extended buffer size will leave the newly-allocated area with +** contents undefined +*/ +void *tlsf_realloc(tlsf_t tlsf, void *ptr, size_t size) +{ + control_t *control = tlsf_cast(control_t *, tlsf); + void *p = 0; + + /* Zero-size requests are treated as free. */ + if (ptr && size == 0) + { + tlsf_free(tlsf, ptr); + } + /* Requests with NULL pointers are treated as malloc. */ + else if (!ptr) + { + p = tlsf_malloc(tlsf, size); + } + else + { + block_header_t *block = block_from_ptr(ptr); + block_header_t *next = block_next(block); + + const size_t cursize = block_size(block); + const size_t combined = cursize + block_size(next) + block_header_overhead; + const size_t adjust = adjust_request_size(size, ALIGN_SIZE); + + tlsf_assert(!block_is_free(block) && "block already marked as free"); + + /* + ** If the next block is used, or when combined with the current + ** block, does not offer enough space, we must reallocate and copy. + */ + if (adjust > cursize && (!block_is_free(next) || adjust > combined)) + { + p = tlsf_malloc(tlsf, size); + if (p) + { + const size_t minsize = tlsf_min(cursize, size); + memcpy(p, ptr, minsize); + tlsf_free(tlsf, ptr); + } + } + else + { + /* Do we need to expand to the next block? */ + if (adjust > cursize) + { + block_merge_next(control, block); + block_mark_as_used(block); + } + + /* Trim the resulting block and return the original pointer. */ + block_trim_used(control, block, adjust); + p = ptr; + } + } + + return p; +} + diff --git a/components/tlsf/tlsf.h b/components/tlsf/tlsf.h new file mode 100644 index 00000000..5e390626 --- /dev/null +++ b/components/tlsf/tlsf.h @@ -0,0 +1,90 @@ +#ifndef INCLUDED_tlsf +#define INCLUDED_tlsf + +/* +** Two Level Segregated Fit memory allocator, version 3.1. +** Written by Matthew Conte +** http://tlsf.baisoku.org +** +** Based on the original documentation by Miguel Masmano: +** http://www.gii.upv.es/tlsf/main/docs +** +** This implementation was written to the specification +** of the document, therefore no GPL restrictions apply. +** +** Copyright (c) 2006-2016, Matthew Conte +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the copyright holder nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */ +/* pool_t: a block of memory that TLSF can manage. */ +typedef void *tlsf_t; +typedef void *pool_t; + +/* Create/destroy a memory pool. */ +tlsf_t tlsf_create(void *mem); +tlsf_t tlsf_create_with_pool(void *mem, size_t bytes); +void tlsf_destroy(tlsf_t tlsf); +pool_t tlsf_get_pool(tlsf_t tlsf); + +/* Add/remove memory pools. */ +pool_t tlsf_add_pool(tlsf_t tlsf, void *mem, size_t bytes); +void tlsf_remove_pool(tlsf_t tlsf, pool_t pool); + +/* malloc/memalign/realloc/free replacements. */ +void *tlsf_malloc(tlsf_t tlsf, size_t bytes); +void *tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes); +void *tlsf_realloc(tlsf_t tlsf, void *ptr, size_t size); +void tlsf_free(tlsf_t tlsf, void *ptr); + +/* Returns internal block size, not original request size */ +size_t tlsf_block_size(void *ptr); + +/* Overheads/limits of internal structures. */ +size_t tlsf_size(void); +size_t tlsf_align_size(void); +size_t tlsf_block_size_min(void); +size_t tlsf_block_size_max(void); +size_t tlsf_pool_overhead(void); +size_t tlsf_alloc_overhead(void); + +/* Debugging. */ +typedef void (*tlsf_walker)(void *ptr, size_t size, int used, void *user); +void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void *user); +/* Returns nonzero if any internal consistency check fails. */ +int tlsf_check(tlsf_t tlsf); +int tlsf_check_pool(pool_t pool); + +#if defined(__cplusplus) +}; +#endif + +#endif