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
|
- 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`
|
- 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
|
- 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 embedded applications with fragmented memories
|
||||||
- Suitable for automotive applications
|
- Suitable for automotive applications
|
||||||
- 100% open source, code available
|
- Supports advanced free/realloc algorithms to optimize memory usage
|
||||||
|
- Operating system ready, thread-safe API
|
||||||
- User friendly MIT license
|
- User friendly MIT license
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of Lightweight dynamic memory manager library.
|
* This file is part of Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_CONFIG_H
|
#ifndef LWMEM_HDR_CONFIG_H
|
||||||
#define 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
|
* - 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`
|
* - 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
|
* - 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 embedded applications with fragmented memories
|
||||||
* - Suitable for automotive applications
|
* - Suitable for automotive applications
|
||||||
* - Operating system ready
|
* - Supports advanced free/realloc algorithms to optimize memory usage
|
||||||
* - 100% open source, code available
|
* - Operating system ready, thread-safe API
|
||||||
* - User friendly MIT license
|
* - User friendly MIT license
|
||||||
*
|
*
|
||||||
* \section sect_resources Download & Resources
|
* \section sect_resources Download & Resources
|
||||||
*
|
*
|
||||||
* - <a class="download_url" href="https://github.com/MaJerle/lwmem/releases">Download library from Github releases</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_res">Resources and examples repository</a>
|
* - <a href="https://github.com/MaJerle/lwmem">Resources and examples repository</a>
|
||||||
* - Read \ref page_appnote before you start development
|
* - Read \ref page_appnote before you start development
|
||||||
* - <a href="https://github.com/MaJerle/lwmem">Official development repository on Github</a>
|
* - <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.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_CONFIG_H
|
#ifndef LWMEM_HDR_CONFIG_H
|
||||||
#define LWMEM_HDR_CONFIG_H
|
#define LWMEM_HDR_CONFIG_H
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef __MAIN_H
|
#ifndef __MAIN_H
|
||||||
#define __MAIN_H
|
#define __MAIN_H
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "lwmem/lwmem.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">
|
<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/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/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/Device/ST/STM32L4xx/Include"/>
|
||||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/st_hal/CMSIS/Include"/>
|
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs/st_hal/CMSIS/Include"/>
|
||||||
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs"/>
|
<listOptionValue builtIn="false" value="../../../../../third_party/Embedded_Libs"/>
|
||||||
|
@ -74,14 +74,9 @@
|
|||||||
</natures>
|
</natures>
|
||||||
<linkedResources>
|
<linkedResources>
|
||||||
<link>
|
<link>
|
||||||
<name>FreeRTOS/cmsis_os.c</name>
|
<name>FreeRTOS/cmsis_os2.c</name>
|
||||||
<type>1</type>
|
<type>1</type>
|
||||||
<locationURI>PARENT-4-PROJECT_LOC/third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS/cmsis_os.c</locationURI>
|
<locationURI>PARENT-4-PROJECT_LOC/third_party/Embedded_Libs/FreeRTOS/CMSIS_RTOS_V2/cmsis_os2.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>
|
|
||||||
</link>
|
</link>
|
||||||
<link>
|
<link>
|
||||||
<name>FreeRTOS/croutine.c</name>
|
<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
|
All rights reserved
|
||||||
|
|
||||||
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
|
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
|
||||||
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
FreeRTOS is free software; you can redistribute it and/or modify it under
|
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
|
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 !<<
|
>>! NOTE: The modification to the GPL is included to allow you to !<<
|
||||||
@ -90,38 +90,34 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define configUSE_PREEMPTION 1
|
#define configUSE_PREEMPTION 1
|
||||||
#define configUSE_IDLE_HOOK 1
|
#define configUSE_IDLE_HOOK 0
|
||||||
#define configUSE_TICK_HOOK 1
|
#define configUSE_TICK_HOOK 0
|
||||||
#define configCPU_CLOCK_HZ (SystemCoreClock)
|
#define configCPU_CLOCK_HZ (SystemCoreClock)
|
||||||
#define configTICK_RATE_HZ ((TickType_t) 1000)
|
#define configTICK_RATE_HZ ((TickType_t)1000)
|
||||||
#define configMAX_PRIORITIES (7)
|
#define configMAX_PRIORITIES (7)
|
||||||
#define configMINIMAL_STACK_SIZE ((uint16_t) 512)
|
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
|
||||||
#define configTOTAL_HEAP_SIZE ((size_t)(24 * 1024))
|
#define configTOTAL_HEAP_SIZE ((size_t)(15 * 1024))
|
||||||
#define configMAX_TASK_NAME_LEN (16)
|
#define configMAX_TASK_NAME_LEN (16)
|
||||||
#define configUSE_TRACE_FACILITY 1
|
#define configUSE_TRACE_FACILITY 1
|
||||||
#define configUSE_16_BIT_TICKS 0
|
#define configUSE_16_BIT_TICKS 0
|
||||||
#define configIDLE_SHOULD_YIELD 1
|
#define configIDLE_SHOULD_YIELD 1
|
||||||
#define configUSE_MUTEXES 1
|
#define configUSE_MUTEXES 1
|
||||||
#define configQUEUE_REGISTRY_SIZE 8
|
#define configQUEUE_REGISTRY_SIZE 8
|
||||||
|
#define configCHECK_FOR_STACK_OVERFLOW 0
|
||||||
#define configUSE_RECURSIVE_MUTEXES 1
|
#define configUSE_RECURSIVE_MUTEXES 1
|
||||||
#define configUSE_MALLOC_FAILED_HOOK 0
|
#define configUSE_MALLOC_FAILED_HOOK 0
|
||||||
#define configUSE_APPLICATION_TASK_TAG 0
|
#define configUSE_APPLICATION_TASK_TAG 0
|
||||||
#define configUSE_COUNTING_SEMAPHORES 1
|
#define configUSE_COUNTING_SEMAPHORES 1
|
||||||
#define configGENERATE_RUN_TIME_STATS 0
|
#define configGENERATE_RUN_TIME_STATS 0
|
||||||
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
|
#define configSUPPORT_STATIC_ALLOCATION 1
|
||||||
|
|
||||||
#define traceTASK_SWITCHED_IN() extern void StartIdleMonitor(void); \
|
|
||||||
StartIdleMonitor()
|
|
||||||
#define traceTASK_SWITCHED_OUT() extern void EndIdleMonitor(void); \
|
|
||||||
EndIdleMonitor()
|
|
||||||
|
|
||||||
/* Co-routine definitions. */
|
/* Co-routine definitions. */
|
||||||
#define configUSE_CO_ROUTINES 0
|
#define configUSE_CO_ROUTINES 0
|
||||||
#define configMAX_CO_ROUTINE_PRIORITIES (2)
|
#define configMAX_CO_ROUTINE_PRIORITIES 2
|
||||||
|
|
||||||
/* Software timer definitions. */
|
/* Software timer definitions. */
|
||||||
#define configUSE_TIMERS 0
|
#define configUSE_TIMERS 0
|
||||||
#define configTIMER_TASK_PRIORITY (2)
|
#define configTIMER_TASK_PRIORITY 2
|
||||||
#define configTIMER_QUEUE_LENGTH 10
|
#define configTIMER_QUEUE_LENGTH 10
|
||||||
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)
|
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)
|
||||||
|
|
||||||
@ -163,7 +159,7 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
|
|||||||
|
|
||||||
/* Normal assert() semantics without relying on the provision of an assert.h
|
/* Normal assert() semantics without relying on the provision of an assert.h
|
||||||
header file. */
|
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
|
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
|
||||||
standard names. */
|
standard names. */
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_CONFIG_H
|
#ifndef LWMEM_HDR_CONFIG_H
|
||||||
#define LWMEM_HDR_CONFIG_H
|
#define LWMEM_HDR_CONFIG_H
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef __MAIN_H
|
#ifndef __MAIN_H
|
||||||
#define __MAIN_H
|
#define __MAIN_H
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "cmsis_os.h"
|
#include "cmsis_os.h"
|
||||||
@ -51,8 +51,7 @@ regions[] = {
|
|||||||
/* Add more regions if needed */
|
/* Add more regions if needed */
|
||||||
};
|
};
|
||||||
|
|
||||||
static void app_thread(void const* arg);
|
static void app_thread(void* arg);
|
||||||
osThreadDef(app_thread, app_thread, osPriorityNormal, 0, 512);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Program entry point
|
* \brief Program entry point
|
||||||
@ -65,6 +64,8 @@ main(void) {
|
|||||||
|
|
||||||
printf("Application running on STM32L496G-Discovery!\r\n");
|
printf("Application running on STM32L496G-Discovery!\r\n");
|
||||||
|
|
||||||
|
osKernelInitialize(); /* Initialize kernel */
|
||||||
|
|
||||||
/* Initialize LwMEM */
|
/* Initialize LwMEM */
|
||||||
printf("Initializing LwMEM...\r\n");
|
printf("Initializing LwMEM...\r\n");
|
||||||
if (!lwmem_assignmem(regions, sizeof(regions) / sizeof(regions[0]))) {
|
if (!lwmem_assignmem(regions, sizeof(regions) / sizeof(regions[0]))) {
|
||||||
@ -74,7 +75,7 @@ main(void) {
|
|||||||
printf("LwMEM initialized and ready to use\r\n");
|
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 */
|
osKernelStart(); /* Start kernel */
|
||||||
|
|
||||||
while (1) {}
|
while (1) {}
|
||||||
@ -85,7 +86,7 @@ main(void) {
|
|||||||
* \param[in] arg: Thread argument
|
* \param[in] arg: Thread argument
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
app_thread(void const* arg) {
|
app_thread(void* arg) {
|
||||||
void* ptr1, *ptr2;
|
void* ptr1, *ptr2;
|
||||||
|
|
||||||
/* Allocate */
|
/* Allocate */
|
||||||
@ -122,7 +123,7 @@ app_thread(void const* arg) {
|
|||||||
lwmem_free(ptr1); /* Free memory */
|
lwmem_free(ptr1); /* Free memory */
|
||||||
|
|
||||||
printf("Terminating application thread\r\n");
|
printf("Terminating application thread\r\n");
|
||||||
osThreadTerminate(NULL); /* Terminate thread */
|
osThreadExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,7 +104,8 @@ DebugMon_Handler(void) {
|
|||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
SysTick_Handler(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.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_H
|
#ifndef LWMEM_HDR_H
|
||||||
#define LWMEM_HDR_H
|
#define LWMEM_HDR_H
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_CONFIG_DEFAULT_H
|
#ifndef LWMEM_HDR_CONFIG_DEFAULT_H
|
||||||
#define 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.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_CONFIG_H
|
#ifndef LWMEM_HDR_CONFIG_H
|
||||||
#define LWMEM_HDR_CONFIG_H
|
#define LWMEM_HDR_CONFIG_H
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#ifndef LWMEM_HDR_SYS_H
|
#ifndef LWMEM_HDR_SYS_H
|
||||||
#define LWMEM_HDR_SYS_H
|
#define LWMEM_HDR_SYS_H
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#include "lwmem/lwmem.h"
|
#include "lwmem/lwmem.h"
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -344,7 +344,7 @@ prv_alloc(const size_t size) {
|
|||||||
* \brief Free input pointer
|
* \brief Free input pointer
|
||||||
* \param[in] ptr: Input pointer to free
|
* \param[in] ptr: Input pointer to free
|
||||||
*/
|
*/
|
||||||
void
|
static void
|
||||||
prv_free(void* const ptr) {
|
prv_free(void* const ptr) {
|
||||||
lwmem_block_t* const block = LWMEM_GET_BLOCK_FROM_PTR(ptr);
|
lwmem_block_t* const block = LWMEM_GET_BLOCK_FROM_PTR(ptr);
|
||||||
if (LWMEM_BLOCK_IS_ALLOC(block)) { /* Check if block is valid */
|
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 */
|
/* Ensure regions are growing linearly and do not overlap in between */
|
||||||
mem_start_addr = (void *)0;
|
mem_start_addr = (void *)0;
|
||||||
mem_size = 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 */
|
/* 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)) {
|
if ((mem_start_addr + mem_size) > LWMEM_TO_BYTE_PTR(regions[i].start_addr)) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -400,7 +400,7 @@ LWMEM_PREF(assignmem)(const LWMEM_PREF(region_t)* regions, const size_t len) {
|
|||||||
mem_size = regions[i].size;
|
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
|
* Check region start address and align start address accordingly
|
||||||
* It is ok to cast to size_t, even if pointer could be larger
|
* 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;
|
mem_start_addr = regions->start_addr;
|
||||||
if (((size_t)mem_start_addr) & LWMEM_ALIGN_BITS) { /* Check alignment boundary */
|
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_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 */
|
/* 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_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)
|
#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 */
|
/* Move memory from block to block previous to current */
|
||||||
void* const old_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(block);
|
void* const old_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(block);
|
||||||
void* const new_data_ptr = LWMEM_GET_PTR_FROM_BLOCK(prev);
|
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),
|
* 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 */
|
lwmem.mem_available_bytes -= prev->size;/* For now decrease effective available bytes */
|
||||||
prev->size += block_size; /* Increase size of input block size */
|
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:
|
* 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
|
* - 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
|
* 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 */
|
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);
|
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
|
* If memmove overwrites metadata of current block (when shifting content up),
|
||||||
* It is not a problem if data shifted up overwrite old block metadata
|
* 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 */
|
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 */
|
/* 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].size = size;
|
||||||
regions[i].start_addr = malloc(regions[i].size);
|
regions[i].start_addr = malloc(regions[i].size);
|
||||||
if (regions[i].start_addr == NULL) {
|
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 */
|
/* Sort regions, make sure they grow linearly */
|
||||||
for (size_t x = 0; x < count; x++) {
|
for (size_t x = 0; x < count; ++x) {
|
||||||
for (size_t y = 0; y < count; y++) {
|
for (size_t y = 0; y < count; ++y) {
|
||||||
if (regions[x].start_addr < regions[y].start_addr) {
|
if (regions[x].start_addr < regions[y].start_addr) {
|
||||||
memcpy(&tmp, ®ions[x], sizeof(regions[x]));
|
memcpy(&tmp, ®ions[x], sizeof(regions[x]));
|
||||||
memcpy(®ions[x], ®ions[y], 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;
|
block = &lwmem.start_block_first_use;
|
||||||
print_block(0, &lwmem.start_block_first_use);
|
print_block(0, &lwmem.start_block_first_use);
|
||||||
printf("|-------|--------------|--------|------|------------------|-----------------|\r\n");
|
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;
|
block = regions_orig[i].start_addr;
|
||||||
|
|
||||||
/* Print all blocks */
|
/* Print all blocks */
|
||||||
for (;; j++) {
|
for (;; ++j) {
|
||||||
block_size = block->size & ~LWMEM_ALLOC_BIT;
|
block_size = block->size & ~LWMEM_ALLOC_BIT;
|
||||||
|
|
||||||
print_block(j, block);
|
print_block(j, block);
|
||||||
@ -944,7 +952,7 @@ lwmem_debug_create_regions(lwmem_region_t** regs_out, size_t count, size_t size)
|
|||||||
void
|
void
|
||||||
lwmem_debug_save_state(void) {
|
lwmem_debug_save_state(void) {
|
||||||
memcpy(&lwmem_temp, &lwmem, sizeof(lwmem_temp));
|
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);
|
memcpy(regions_temp[i].start_addr, regions_orig[i].start_addr, regions_temp[i].size);
|
||||||
}
|
}
|
||||||
printf(" -- > Current state saved!\r\n");
|
printf(" -- > Current state saved!\r\n");
|
||||||
@ -953,7 +961,7 @@ lwmem_debug_save_state(void) {
|
|||||||
void
|
void
|
||||||
lwmem_debug_restore_to_saved(void) {
|
lwmem_debug_restore_to_saved(void) {
|
||||||
memcpy(&lwmem, &lwmem_temp, sizeof(lwmem_temp));
|
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);
|
memcpy(regions_orig[i].start_addr, regions_temp[i].start_addr, regions_temp[i].size);
|
||||||
}
|
}
|
||||||
printf(" -- > State restored to last saved!\r\n");
|
printf(" -- > State restored to last saved!\r\n");
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#include "system/lwmem_sys.h"
|
#include "system/lwmem_sys.h"
|
||||||
|
|
||||||
@ -39,8 +39,7 @@
|
|||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
||||||
osMutexDef(mut);
|
*m = osMutexNew(NULL);
|
||||||
*m = osMutexCreate(osMutex(mut));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +50,7 @@ lwmem_sys_mutex_isvalid(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
|||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* m) {
|
||||||
if (osMutexWait(*m, osWaitForever) != osOK) {
|
if (osMutexAcquire(*m, osWaitForever) != osOK) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#include "system/lwmem_sys.h"
|
#include "system/lwmem_sys.h"
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||||
*
|
*
|
||||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||||
* Version: v1.1
|
* Version: v1.2.0
|
||||||
*/
|
*/
|
||||||
#include "system/lwmem_sys.h"
|
#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