mirror of
https://github.com/MaJerle/lwmem.git
synced 2025-01-13 21:42:53 +08:00
Merge branch 'release-v1.2.0'
This commit is contained in:
commit
c2dab54e09
@ -4,11 +4,12 @@
|
||||
|
||||
- Written in ANSI C99, compatible with `size_t` for size data types
|
||||
- Implements standard C library functions for memory allocation, `malloc`, `calloc`, `realloc` and `free`
|
||||
- Supports different memory regions to allow use of framented memories
|
||||
- Uses `first-fit` algorithm to search free block
|
||||
- Supports different memory regions to allow use of fragmented memories
|
||||
- Suitable for embedded applications with fragmented memories
|
||||
- Suitable for automotive applications
|
||||
- 100% open source, code available
|
||||
- Suitable for automotive applications
|
||||
- Supports advanced free/realloc algorithms to optimize memory usage
|
||||
- Operating system ready, thread-safe API
|
||||
- User friendly MIT license
|
||||
|
||||
## Documentation
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_CONFIG_H
|
||||
#define LWMEM_HDR_CONFIG_H
|
||||
|
@ -8,19 +8,18 @@
|
||||
*
|
||||
* - Written in ANSI C99, compatible with `size_t` for size data types
|
||||
* - Implements standard C library functions for memory allocation, `malloc`, `calloc`, `realloc` and `free`
|
||||
* - Supports different memory regions to allow use of framented memories
|
||||
* - Uses `first-fit` algorithm to search free block
|
||||
* - Implements optimized reallocation algorithm to find best block
|
||||
* - Supports different memory regions to allow use of fragmented memories
|
||||
* - Suitable for embedded applications with fragmented memories
|
||||
* - Suitable for automotive applications
|
||||
* - Operating system ready
|
||||
* - 100% open source, code available
|
||||
* - Supports advanced free/realloc algorithms to optimize memory usage
|
||||
* - Operating system ready, thread-safe API
|
||||
* - User friendly MIT license
|
||||
*
|
||||
* \section sect_resources Download & Resources
|
||||
*
|
||||
* - <a class="download_url" href="https://github.com/MaJerle/lwmem/releases">Download library from Github releases</a>
|
||||
* - <a href="https://github.com/MaJerle/lwmem_res">Resources and examples repository</a>
|
||||
* - <a class="download_url" href="https://github.com/MaJerle/lwmem/releases">Download library at Github releases</a>
|
||||
* - <a href="https://github.com/MaJerle/lwmem">Resources and examples repository</a>
|
||||
* - Read \ref page_appnote before you start development
|
||||
* - <a href="https://github.com/MaJerle/lwmem">Official development repository on Github</a>
|
||||
*
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_CONFIG_H
|
||||
#define LWMEM_HDR_CONFIG_H
|
||||
|
@ -27,7 +27,7 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
@ -27,7 +27,7 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#include "main.h"
|
||||
#include "lwmem/lwmem.h"
|
||||
|
@ -41,7 +41,7 @@
|
||||
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.includepaths.118137004" name="Include paths (-I)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.includepaths" useByScannerDiscovery="false" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/st_hal/STM32L4xx_HAL_Driver/Inc"/>
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/FreeRTOS/include"/>
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS"/>
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS_V2"/>
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/st_hal/CMSIS/Device/ST/STM32L4xx/Include"/>
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/st_hal/CMSIS/Include"/>
|
||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs"/>
|
||||
|
@ -74,14 +74,9 @@
|
||||
</natures>
|
||||
<linkedResources>
|
||||
<link>
|
||||
<name>FreeRTOS/cmsis_os.c</name>
|
||||
<name>FreeRTOS/cmsis_os2.c</name>
|
||||
<type>1</type>
|
||||
<locationURI>PARENT-4-PROJECT_LOC/third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS/cmsis_os.c</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>FreeRTOS/cpu_utils.c</name>
|
||||
<type>1</type>
|
||||
<locationURI>PARENT-4-PROJECT_LOC/third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS/cpu_utils.c</locationURI>
|
||||
<locationURI>PARENT-4-PROJECT_LOC/third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS_V2/cmsis_os2.c</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>FreeRTOS/croutine.c</name>
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.
|
||||
FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
|
||||
All rights reserved
|
||||
|
||||
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
|
||||
@ -8,14 +8,14 @@
|
||||
|
||||
FreeRTOS is free software; you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License (version 2) as published by the
|
||||
Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
|
||||
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
|
||||
|
||||
***************************************************************************
|
||||
***************************************************************************
|
||||
>>! NOTE: The modification to the GPL is included to allow you to !<<
|
||||
>>! distribute a combined work that includes FreeRTOS without being !<<
|
||||
>>! obliged to provide the source code for proprietary components !<<
|
||||
>>! outside of the FreeRTOS kernel. !<<
|
||||
***************************************************************************
|
||||
***************************************************************************
|
||||
|
||||
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
@ -37,17 +37,17 @@
|
||||
***************************************************************************
|
||||
|
||||
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
|
||||
the FAQ page "My application does not run, what could be wrong?". Have you
|
||||
defined configASSERT()?
|
||||
the FAQ page "My application does not run, what could be wrong?". Have you
|
||||
defined configASSERT()?
|
||||
|
||||
http://www.FreeRTOS.org/support - In return for receiving this top quality
|
||||
embedded software for free we request you assist our global community by
|
||||
participating in the support forum.
|
||||
http://www.FreeRTOS.org/support - In return for receiving this top quality
|
||||
embedded software for free we request you assist our global community by
|
||||
participating in the support forum.
|
||||
|
||||
http://www.FreeRTOS.org/training - Investing in training allows your team to
|
||||
be as productive as possible as early as possible. Now you can receive
|
||||
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
|
||||
Ltd, and the world's leading authority on the world's leading RTOS.
|
||||
http://www.FreeRTOS.org/training - Investing in training allows your team to
|
||||
be as productive as possible as early as possible. Now you can receive
|
||||
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
|
||||
Ltd, and the world's leading authority on the world's leading RTOS.
|
||||
|
||||
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
|
||||
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
|
||||
@ -89,64 +89,60 @@
|
||||
extern uint32_t SystemCoreClock;
|
||||
#endif
|
||||
|
||||
#define configUSE_PREEMPTION 1
|
||||
#define configUSE_IDLE_HOOK 1
|
||||
#define configUSE_TICK_HOOK 1
|
||||
#define configCPU_CLOCK_HZ (SystemCoreClock)
|
||||
#define configTICK_RATE_HZ ((TickType_t) 1000)
|
||||
#define configMAX_PRIORITIES (7)
|
||||
#define configMINIMAL_STACK_SIZE ((uint16_t) 512)
|
||||
#define configTOTAL_HEAP_SIZE ((size_t)(24 * 1024))
|
||||
#define configMAX_TASK_NAME_LEN (16)
|
||||
#define configUSE_TRACE_FACILITY 1
|
||||
#define configUSE_16_BIT_TICKS 0
|
||||
#define configIDLE_SHOULD_YIELD 1
|
||||
#define configUSE_MUTEXES 1
|
||||
#define configQUEUE_REGISTRY_SIZE 8
|
||||
#define configUSE_RECURSIVE_MUTEXES 1
|
||||
#define configUSE_MALLOC_FAILED_HOOK 0
|
||||
#define configUSE_APPLICATION_TASK_TAG 0
|
||||
#define configUSE_COUNTING_SEMAPHORES 1
|
||||
#define configGENERATE_RUN_TIME_STATS 0
|
||||
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
|
||||
|
||||
#define traceTASK_SWITCHED_IN() extern void StartIdleMonitor(void); \
|
||||
StartIdleMonitor()
|
||||
#define traceTASK_SWITCHED_OUT() extern void EndIdleMonitor(void); \
|
||||
EndIdleMonitor()
|
||||
#define configUSE_PREEMPTION 1
|
||||
#define configUSE_IDLE_HOOK 0
|
||||
#define configUSE_TICK_HOOK 0
|
||||
#define configCPU_CLOCK_HZ (SystemCoreClock)
|
||||
#define configTICK_RATE_HZ ((TickType_t)1000)
|
||||
#define configMAX_PRIORITIES (7)
|
||||
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
|
||||
#define configTOTAL_HEAP_SIZE ((size_t)(15 * 1024))
|
||||
#define configMAX_TASK_NAME_LEN (16)
|
||||
#define configUSE_TRACE_FACILITY 1
|
||||
#define configUSE_16_BIT_TICKS 0
|
||||
#define configIDLE_SHOULD_YIELD 1
|
||||
#define configUSE_MUTEXES 1
|
||||
#define configQUEUE_REGISTRY_SIZE 8
|
||||
#define configCHECK_FOR_STACK_OVERFLOW 0
|
||||
#define configUSE_RECURSIVE_MUTEXES 1
|
||||
#define configUSE_MALLOC_FAILED_HOOK 0
|
||||
#define configUSE_APPLICATION_TASK_TAG 0
|
||||
#define configUSE_COUNTING_SEMAPHORES 1
|
||||
#define configGENERATE_RUN_TIME_STATS 0
|
||||
#define configSUPPORT_STATIC_ALLOCATION 1
|
||||
|
||||
/* Co-routine definitions. */
|
||||
#define configUSE_CO_ROUTINES 0
|
||||
#define configMAX_CO_ROUTINE_PRIORITIES (2)
|
||||
#define configUSE_CO_ROUTINES 0
|
||||
#define configMAX_CO_ROUTINE_PRIORITIES 2
|
||||
|
||||
/* Software timer definitions. */
|
||||
#define configUSE_TIMERS 0
|
||||
#define configTIMER_TASK_PRIORITY (2)
|
||||
#define configTIMER_QUEUE_LENGTH 10
|
||||
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)
|
||||
#define configUSE_TIMERS 0
|
||||
#define configTIMER_TASK_PRIORITY 2
|
||||
#define configTIMER_QUEUE_LENGTH 10
|
||||
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)
|
||||
|
||||
/* Set the following definitions to 1 to include the API function, or zero
|
||||
to exclude the API function. */
|
||||
#define INCLUDE_vTaskPrioritySet 1
|
||||
#define INCLUDE_uxTaskPriorityGet 1
|
||||
#define INCLUDE_vTaskDelete 1
|
||||
#define INCLUDE_vTaskCleanUpResources 0
|
||||
#define INCLUDE_vTaskSuspend 1
|
||||
#define INCLUDE_vTaskDelayUntil 0
|
||||
#define INCLUDE_vTaskDelay 1
|
||||
#define INCLUDE_xTaskGetSchedulerState 1
|
||||
#define INCLUDE_vTaskPrioritySet 1
|
||||
#define INCLUDE_uxTaskPriorityGet 1
|
||||
#define INCLUDE_vTaskDelete 1
|
||||
#define INCLUDE_vTaskCleanUpResources 0
|
||||
#define INCLUDE_vTaskSuspend 1
|
||||
#define INCLUDE_vTaskDelayUntil 0
|
||||
#define INCLUDE_vTaskDelay 1
|
||||
#define INCLUDE_xTaskGetSchedulerState 1
|
||||
|
||||
/* Cortex-M specific definitions. */
|
||||
#ifdef __NVIC_PRIO_BITS
|
||||
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
|
||||
#define configPRIO_BITS __NVIC_PRIO_BITS
|
||||
#define configPRIO_BITS __NVIC_PRIO_BITS
|
||||
#else
|
||||
#define configPRIO_BITS 4 /* 15 priority levels */
|
||||
#define configPRIO_BITS 4 /* 15 priority levels */
|
||||
#endif
|
||||
|
||||
/* The lowest interrupt priority that can be used in a call to a "set priority"
|
||||
function. */
|
||||
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0x0F
|
||||
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0x0F
|
||||
|
||||
/* The highest interrupt priority that can be used by any interrupt service
|
||||
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
|
||||
@ -156,19 +152,19 @@ PRIORITY THAN THIS! (higher priorities are lower numeric values. */
|
||||
|
||||
/* Interrupt priorities used by the kernel port layer itself. These are generic
|
||||
to all Cortex-M ports, and do not rely on any particular library functions. */
|
||||
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
|
||||
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
|
||||
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
|
||||
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
|
||||
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
|
||||
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
|
||||
|
||||
/* Normal assert() semantics without relying on the provision of an assert.h
|
||||
header file. */
|
||||
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
|
||||
#define configASSERT( x ) if ((x) == 0) { taskDISABLE_INTERRUPTS(); for (;;) {} }
|
||||
|
||||
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
|
||||
standard names. */
|
||||
#define vPortSVCHandler SVC_Handler
|
||||
#define xPortPendSVHandler PendSV_Handler
|
||||
#define vPortSVCHandler SVC_Handler
|
||||
#define xPortPendSVHandler PendSV_Handler
|
||||
|
||||
/* IMPORTANT: This define MUST be commented when used with STM32Cube firmware,
|
||||
to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_CONFIG_H
|
||||
#define LWMEM_HDR_CONFIG_H
|
||||
|
@ -27,7 +27,7 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
@ -27,7 +27,7 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#include "main.h"
|
||||
#include "cmsis_os.h"
|
||||
@ -51,8 +51,7 @@ regions[] = {
|
||||
/* Add more regions if needed */
|
||||
};
|
||||
|
||||
static void app_thread(void const* arg);
|
||||
osThreadDef(app_thread, app_thread, osPriorityNormal, 0, 512);
|
||||
static void app_thread(void* arg);
|
||||
|
||||
/**
|
||||
* \brief Program entry point
|
||||
@ -65,6 +64,8 @@ main(void) {
|
||||
|
||||
printf("Application running on STM32L496G-Discovery!\r\n");
|
||||
|
||||
osKernelInitialize(); /* Initialize kernel */
|
||||
|
||||
/* Initialize LwMEM */
|
||||
printf("Initializing LwMEM...\r\n");
|
||||
if (!lwmem_assignmem(regions, sizeof(regions) / sizeof(regions[0]))) {
|
||||
@ -74,7 +75,7 @@ main(void) {
|
||||
printf("LwMEM initialized and ready to use\r\n");
|
||||
}
|
||||
|
||||
osThreadCreate(osThread(app_thread), NULL); /* Create application thread */
|
||||
osThreadNew(app_thread, NULL, NULL); /* Create application thread */
|
||||
osKernelStart(); /* Start kernel */
|
||||
|
||||
while (1) {}
|
||||
@ -85,7 +86,7 @@ main(void) {
|
||||
* \param[in] arg: Thread argument
|
||||
*/
|
||||
static void
|
||||
app_thread(void const* arg) {
|
||||
app_thread(void* arg) {
|
||||
void* ptr1, *ptr2;
|
||||
|
||||
/* Allocate */
|
||||
@ -122,7 +123,7 @@ app_thread(void const* arg) {
|
||||
lwmem_free(ptr1); /* Free memory */
|
||||
|
||||
printf("Terminating application thread\r\n");
|
||||
osThreadTerminate(NULL); /* Terminate thread */
|
||||
osThreadExit();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,7 +104,8 @@ DebugMon_Handler(void) {
|
||||
*/
|
||||
void
|
||||
SysTick_Handler(void) {
|
||||
osSystickHandler();
|
||||
extern void xPortSysTickHandler(void);
|
||||
xPortSysTickHandler();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_H
|
||||
#define LWMEM_HDR_H
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_CONFIG_DEFAULT_H
|
||||
#define LWMEM_HDR_CONFIG_DEFAULT_H
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_CONFIG_H
|
||||
#define LWMEM_HDR_CONFIG_H
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_SYS_H
|
||||
#define LWMEM_HDR_SYS_H
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#include "lwmem/lwmem.h"
|
||||
#include <limits.h>
|
||||
@ -344,7 +344,7 @@ prv_alloc(const size_t size) {
|
||||
* \brief Free input pointer
|
||||
* \param[in] ptr: Input pointer to free
|
||||
*/
|
||||
void
|
||||
static void
|
||||
prv_free(void* const ptr) {
|
||||
lwmem_block_t* const block = LWMEM_GET_BLOCK_FROM_PTR(ptr);
|
||||
if (LWMEM_BLOCK_IS_ALLOC(block)) { /* Check if block is valid */
|
||||
@ -389,7 +389,7 @@ LWMEM_PREF(assignmem)(const LWMEM_PREF(region_t)* regions, const size_t len) {
|
||||
/* Ensure regions are growing linearly and do not overlap in between */
|
||||
mem_start_addr = (void *)0;
|
||||
mem_size = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
/* New region(s) must be higher (in address space) than previous one */
|
||||
if ((mem_start_addr + mem_size) > LWMEM_TO_BYTE_PTR(regions[i].start_addr)) {
|
||||
return 0;
|
||||
@ -400,7 +400,7 @@ LWMEM_PREF(assignmem)(const LWMEM_PREF(region_t)* regions, const size_t len) {
|
||||
mem_size = regions[i].size;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; i++, regions++) {
|
||||
for (size_t i = 0; i < len; ++i, ++regions) {
|
||||
/*
|
||||
* Check region start address and align start address accordingly
|
||||
* It is ok to cast to size_t, even if pointer could be larger
|
||||
@ -418,7 +418,7 @@ LWMEM_PREF(assignmem)(const LWMEM_PREF(region_t)* regions, const size_t len) {
|
||||
mem_start_addr = regions->start_addr;
|
||||
if (((size_t)mem_start_addr) & LWMEM_ALIGN_BITS) { /* Check alignment boundary */
|
||||
mem_start_addr += LWMEM_ALIGN_NUM - ((size_t)mem_start_addr & LWMEM_ALIGN_BITS);
|
||||
mem_size -= mem_start_addr - LWMEM_TO_BYTE_PTR(regions->start_addr);
|
||||
mem_size -= (size_t)(mem_start_addr - LWMEM_TO_BYTE_PTR(regions->start_addr));
|
||||
}
|
||||
|
||||
/* Ensure region size has enough memory after all the alignment checks */
|
||||
@ -467,7 +467,7 @@ LWMEM_PREF(assignmem)(const LWMEM_PREF(region_t)* regions, const size_t len) {
|
||||
}
|
||||
|
||||
lwmem.mem_available_bytes += first_block->size; /* Increase number of available bytes */
|
||||
lwmem.mem_regions_count++; /* Increase number of used regions */
|
||||
++lwmem.mem_regions_count; /* Increase number of used regions */
|
||||
}
|
||||
|
||||
#if defined(LWMEM_DEV)
|
||||
@ -666,12 +666,16 @@ LWMEM_PREF(realloc)(void* const ptr, const size_t size) {
|
||||
/* Move memory from block to block previous to current */
|
||||
void* const old_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(block);
|
||||
void* const new_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(prev);
|
||||
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); /* Copy old buffer size to new location */
|
||||
|
||||
/*
|
||||
* If memmove overwrites metadata of current block (when shifting content up),
|
||||
* it is not an issue as we know its size and next is already NULL
|
||||
* it is not an issue as we know its size (block_size) and next is already NULL.
|
||||
*
|
||||
* Memmove must be used to guarantee move of data as addresses + their sizes may overlap
|
||||
*
|
||||
* Metadata of "prev" are not modified during memmove
|
||||
*/
|
||||
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size);
|
||||
|
||||
lwmem.mem_available_bytes -= prev->size;/* For now decrease effective available bytes */
|
||||
prev->size += block_size; /* Increase size of input block size */
|
||||
@ -686,9 +690,9 @@ LWMEM_PREF(realloc)(void* const ptr, const size_t size) {
|
||||
/*
|
||||
* At this point, it was not possible to expand existing block with free before or free after due to:
|
||||
* - Input block & next free block do not create contiguous block or its new size is too small
|
||||
* - Last free block & input block do not create contiguous block or its new size is too small
|
||||
* - Previous free block & input block do not create contiguous block or its new size is too small
|
||||
*
|
||||
* Last option is to check if last free block before "prev", input block "block" and next free block "prev->next" create contiguous block
|
||||
* Last option is to check if previous free block "prev", input block "block" and next free block "prev->next" create contiguous block
|
||||
* and size of new block (from 3 contiguous blocks) together is big enough
|
||||
*/
|
||||
if ((LWMEM_TO_BYTE_PTR(prev) + prev->size) == LWMEM_TO_BYTE_PTR(block) /* Input block and free block before create contiguous block */
|
||||
@ -700,8 +704,12 @@ LWMEM_PREF(realloc)(void* const ptr, const size_t size) {
|
||||
void* const new_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(prev);
|
||||
|
||||
/*
|
||||
* It is necessary to use memmove and not memcpy as memmove takes care of memory overlapping
|
||||
* It is not a problem if data shifted up overwrite old block metadata
|
||||
* If memmove overwrites metadata of current block (when shifting content up),
|
||||
* it is not an issue as we know its size (block_size) and next is already NULL.
|
||||
*
|
||||
* Memmove must be used to guarantee move of data as addresses + their sizes may overlap
|
||||
*
|
||||
* Metadata of "prev" are not modified during memmove
|
||||
*/
|
||||
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); /* Copy old buffer size to new location */
|
||||
|
||||
@ -846,7 +854,7 @@ create_regions(size_t count, size_t size) {
|
||||
}
|
||||
|
||||
/* Allocate memory for regions */
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
regions[i].size = size;
|
||||
regions[i].start_addr = malloc(regions[i].size);
|
||||
if (regions[i].start_addr == NULL) {
|
||||
@ -855,8 +863,8 @@ create_regions(size_t count, size_t size) {
|
||||
}
|
||||
|
||||
/* Sort regions, make sure they grow linearly */
|
||||
for (size_t x = 0; x < count; x++) {
|
||||
for (size_t y = 0; y < count; y++) {
|
||||
for (size_t x = 0; x < count; ++x) {
|
||||
for (size_t y = 0; y < count; ++y) {
|
||||
if (regions[x].start_addr < regions[y].start_addr) {
|
||||
memcpy(&tmp, ®ions[x], sizeof(regions[x]));
|
||||
memcpy(®ions[x], ®ions[y], sizeof(regions[x]));
|
||||
@ -908,11 +916,11 @@ lwmem_debug_print(unsigned char print_alloc, unsigned char print_free) {
|
||||
block = &lwmem.start_block_first_use;
|
||||
print_block(0, &lwmem.start_block_first_use);
|
||||
printf("|-------|--------------|--------|------|------------------|-----------------|\r\n");
|
||||
for (size_t i = 0, j = 1; i < regions_count; i++) {
|
||||
for (size_t i = 0, j = 1; i < regions_count; ++i) {
|
||||
block = regions_orig[i].start_addr;
|
||||
|
||||
/* Print all blocks */
|
||||
for (;; j++) {
|
||||
for (;; ++j) {
|
||||
block_size = block->size & ~LWMEM_ALLOC_BIT;
|
||||
|
||||
print_block(j, block);
|
||||
@ -944,7 +952,7 @@ lwmem_debug_create_regions(lwmem_region_t** regs_out, size_t count, size_t size)
|
||||
void
|
||||
lwmem_debug_save_state(void) {
|
||||
memcpy(&lwmem_temp, &lwmem, sizeof(lwmem_temp));
|
||||
for (size_t i = 0; i < regions_count; i++) {
|
||||
for (size_t i = 0; i < regions_count; ++i) {
|
||||
memcpy(regions_temp[i].start_addr, regions_orig[i].start_addr, regions_temp[i].size);
|
||||
}
|
||||
printf(" -- > Current state saved!\r\n");
|
||||
@ -953,7 +961,7 @@ lwmem_debug_save_state(void) {
|
||||
void
|
||||
lwmem_debug_restore_to_saved(void) {
|
||||
memcpy(&lwmem, &lwmem_temp, sizeof(lwmem_temp));
|
||||
for (size_t i = 0; i < regions_count; i++) {
|
||||
for (size_t i = 0; i < regions_count; ++i) {
|
||||
memcpy(regions_orig[i].start_addr, regions_temp[i].start_addr, regions_temp[i].size);
|
||||
}
|
||||
printf(" -- > State restored to last saved!\r\n");
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#include "system/lwmem_sys.h"
|
||||
|
||||
@ -39,8 +39,7 @@
|
||||
|
||||
uint8_t
|
||||
lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
||||
osMutexDef(mut);
|
||||
*m = osMutexCreate(osMutex(mut));
|
||||
*m = osMutexNew(NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -51,7 +50,7 @@ lwmem_sys_mutex_isvalid(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
||||
|
||||
uint8_t
|
||||
lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
||||
if (osMutexWait(*m, osWaitForever) != osOK) {
|
||||
if (osMutexAcquire(*m, osWaitForever) != osOK) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#include "system/lwmem_sys.h"
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.1
|
||||
* Version: v1.2.0
|
||||
*/
|
||||
#include "system/lwmem_sys.h"
|
||||
|
||||
|
2
third_party/Embedded_Libs
vendored
2
third_party/Embedded_Libs
vendored
@ -1 +1 @@
|
||||
Subproject commit fc5b3a0dc07107c63d3f2e6a5a6d31843b0606ca
|
||||
Subproject commit f74dc34b472d9bd189f3cdec4f33492c3c1ab465
|
Loading…
x
Reference in New Issue
Block a user