mirror of
https://github.com/MaJerle/lwmem.git
synced 2025-01-26 06:02:54 +08:00
Remove len parameter for region definition - start v2
This commit is contained in:
parent
35ea75dfbb
commit
a5ac7a81ec
@ -2,8 +2,9 @@
|
||||
|
||||
## Develop
|
||||
|
||||
- Make `len` parameter deprecated for memory assignment
|
||||
- Prepare for version v2
|
||||
- Remove `len` parameter
|
||||
- Move to v2.x
|
||||
- Create separate branch for v1.x, for critical maintenance purpose only
|
||||
|
||||
## v1.6.0
|
||||
|
||||
|
@ -29,7 +29,6 @@
|
||||
* This file is part of Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -12,7 +12,7 @@ lwmem_region_t regions[] = {
|
||||
|
||||
/* Later in the initialization process */
|
||||
/* Assign regions for manager */
|
||||
lwmem_assignmem(regions, 0);
|
||||
lwmem_assignmem(regions);
|
||||
|
||||
/* Usage in program... */
|
||||
|
||||
|
@ -20,7 +20,7 @@ main(void) {
|
||||
printf("Cannot allocate memory for regions for debug purpose!\r\n");
|
||||
return -1;
|
||||
}
|
||||
lwmem_assignmem(regions_used, 0);
|
||||
lwmem_assignmem(regions_used);
|
||||
printf("Manager is ready!\r\n");
|
||||
lwmem_debug_print(1, 1);
|
||||
|
||||
|
@ -15,5 +15,5 @@ lwmem_region_t regions[] = {
|
||||
|
||||
/* Later in the initialization process */
|
||||
/* Assign regions for manager */
|
||||
lwmem_assignmem(regions, 0);
|
||||
lwmem_assignmem(regions);
|
||||
lwmem_debug_free(); /* This is debug function for sake of this example */
|
@ -14,6 +14,6 @@ lwmem_region_t regions[] = {
|
||||
|
||||
/* Later in the initialization process */
|
||||
/* Assign regions for manager */
|
||||
lwmem_assignmem(regions, 0);
|
||||
lwmem_assignmem(regions);
|
||||
/* or */
|
||||
lwmem_assignmem_ex(NULL, regions, 0);
|
||||
lwmem_assignmem_ex(NULL, regions);
|
@ -20,4 +20,4 @@ lwmem_region_t regions[] = {
|
||||
|
||||
/* Later in the initialization process */
|
||||
/* Assign regions for custom instance */
|
||||
lwmem_assignmem_ex(&lw_custom, regions, 0);
|
||||
lwmem_assignmem_ex(&lw_custom, regions);
|
@ -29,7 +29,6 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -27,7 +27,6 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
@ -27,7 +27,6 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#include "main.h"
|
||||
#include "cmsis_os.h"
|
||||
@ -69,7 +68,7 @@ main(void) {
|
||||
|
||||
/* Initialize LwMEM */
|
||||
printf("Initializing LwMEM...\r\n");
|
||||
if (!lwmem_assignmem(regions, 0)) {
|
||||
if (!lwmem_assignmem(regions)) {
|
||||
printf("Cannot initialize LwMEM. Make sure your regions are not overlapping each other and are in ascending memory order\r\n");
|
||||
while (1) {}
|
||||
} else {
|
||||
|
@ -29,7 +29,6 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -27,7 +27,6 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
@ -27,7 +27,6 @@
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#include "main.h"
|
||||
#include "lwmem/lwmem.h"
|
||||
@ -66,7 +65,7 @@ main(void) {
|
||||
|
||||
/* Initialize LwMEM */
|
||||
printf("Initializing LwMEM...\r\n");
|
||||
if (!lwmem_assignmem(regions, 0)) {
|
||||
if (!lwmem_assignmem(regions)) {
|
||||
printf("Cannot initialize LwMEM. Make sure your regions are not overlapping each other and are in ascending memory order\r\n");
|
||||
while (1) {}
|
||||
} else {
|
||||
|
@ -29,7 +29,6 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -19,7 +19,7 @@ main(void) {
|
||||
void* ptr, *ptr2;
|
||||
|
||||
/* Initialize default LwMEM instance with single region */
|
||||
if (!lwmem_assignmem(regions, 0)) {
|
||||
if (!lwmem_assignmem(regions)) {
|
||||
printf("Could not initialize LwMEM!");
|
||||
return -1;
|
||||
}
|
||||
|
@ -29,7 +29,6 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include "lwmem/lwmem.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Define multiple regions for default instance */
|
||||
uint8_t lw0_region1_data[1024];
|
||||
@ -47,14 +48,14 @@ main(void) {
|
||||
}
|
||||
|
||||
/* Initialize default LwMEM instance with single region */
|
||||
if (!lwmem_assignmem(lw0_regions, 0)) {
|
||||
if (!lwmem_assignmem(lw0_regions)) {
|
||||
printf("Could not initialize default LwMEM instance!");
|
||||
return -1;
|
||||
}
|
||||
printf("Default LwMEM instance initialized and ready to use!\r\n");
|
||||
|
||||
/* Initialize custom LwMEM instance with its custom regions */
|
||||
if (!lwmem_assignmem_ex(&lw1, lw1_regions, 0)) {
|
||||
if (!lwmem_assignmem_ex(&lw1, lw1_regions)) {
|
||||
printf("Could not initialize custom LwMEM instance!");
|
||||
return -1;
|
||||
}
|
||||
|
@ -29,7 +29,6 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include "lwmem/lwmem.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Define multiple regions */
|
||||
uint8_t region1_data[1024];
|
||||
@ -29,7 +30,7 @@ main(void) {
|
||||
}
|
||||
|
||||
/* Initialize default LwMEM instance with single region */
|
||||
if (!lwmem_assignmem(regions, 0)) {
|
||||
if (!lwmem_assignmem(regions)) {
|
||||
printf("Could not initialize LwMEM!");
|
||||
return -1;
|
||||
}
|
||||
|
@ -29,7 +29,6 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -21,7 +21,7 @@ static int thread_func(void* arg);
|
||||
int
|
||||
main(void) {
|
||||
/* Initialize default LwMEM instance with single region */
|
||||
if (!lwmem_assignmem(regions, 0)) {
|
||||
if (!lwmem_assignmem(regions)) {
|
||||
printf("Could not initialize LwMEM!");
|
||||
return -1;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "LwMEM",
|
||||
"version": "1.6.0",
|
||||
"version": "2.0.0",
|
||||
"description": "Lightweight dynamic memory manager optimized for embedded systems",
|
||||
"keywords": "lwmem, memory, dynamic, heap, malloc, calloc, realloc, free, lightweight, manager, embedded, stm32, win32",
|
||||
"repository": {
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
* Version: v2.0.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_H
|
||||
#define LWMEM_HDR_H
|
||||
@ -102,7 +102,7 @@ typedef struct {
|
||||
size_t size; /*!< Size of region in units of bytes */
|
||||
} lwmem_region_t;
|
||||
|
||||
size_t lwmem_assignmem_ex(lwmem_t* const lw, const lwmem_region_t* regions, size_t len);
|
||||
size_t lwmem_assignmem_ex(lwmem_t* const lw, const lwmem_region_t* regions);
|
||||
void* lwmem_malloc_ex(lwmem_t* const lw, const lwmem_region_t* region, const size_t size);
|
||||
void* lwmem_calloc_ex(lwmem_t* const lw, const lwmem_region_t* region, const size_t nitems, const size_t size);
|
||||
void* lwmem_realloc_ex(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, const size_t size);
|
||||
@ -114,25 +114,21 @@ size_t lwmem_get_size_ex(lwmem_t* const lw, void* ptr);
|
||||
/**
|
||||
* \note This is a wrapper for \ref lwmem_assignmem_ex function.
|
||||
* It operates in default LwMEM instance and uses first available region for memory operations
|
||||
* \param[in] regions: Array of regions with address and its size.
|
||||
* \param[in] regions: Pointer to array of regions with address and respective size.
|
||||
* Regions must be in increasing order (start address) and must not overlap in-between.
|
||||
* When `len` param is set to `0`, regions array must contain last entry with `NULL` address and `0` length,
|
||||
* indicating end of regions (similar to end of string)
|
||||
* Last region entry must have address `NULL` and size set to `0`
|
||||
* \code{.c}
|
||||
//Example definition
|
||||
lwmem_region_t regions[] = {
|
||||
{ addr1, size1 },
|
||||
{ addr2, size2 },
|
||||
{ addr3, size3 },
|
||||
{ NULL, 0 } //Regions array termination = end of descriptor
|
||||
{ (void *)0x10000000, 0x1000 }, //Region starts at address 0x10000000 and is 0x1000 bytes long
|
||||
{ (void *)0x20000000, 0x2000 }, //Region starts at address 0x20000000 and is 0x2000 bytes long
|
||||
{ (void *)0x30000000, 0x3000 }, //Region starts at address 0x30000000 and is 0x3000 bytes long
|
||||
{ NULL, 0 } //Array termination indicator
|
||||
}
|
||||
\endcode
|
||||
* \param[in] len: Number of regions in array.
|
||||
* Can be set to `0` to describe number of regions with `regions` parameter only.
|
||||
* \note `len` is deprecated and will be removed in the future versions.
|
||||
* Describe regions with `regions` parameter only instead
|
||||
* \return `0` on failure, number of final regions used for memory manager on success
|
||||
*/
|
||||
#define lwmem_assignmem(regions, len) lwmem_assignmem_ex(NULL, (regions), (len))
|
||||
#define lwmem_assignmem(regions) lwmem_assignmem_ex(NULL, (regions))
|
||||
|
||||
/**
|
||||
* \note This is a wrapper for \ref lwmem_malloc_ex function.
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
* Version: v2.0.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPT_H
|
||||
#define LWMEM_HDR_OPT_H
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
* Version: v2.0.0
|
||||
*/
|
||||
#ifndef LWMEM_HDR_OPTS_H
|
||||
#define LWMEM_HDR_OPTS_H
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
* Version: v2.0.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.6.0
|
||||
* Version: v2.0.0
|
||||
*/
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
@ -660,46 +660,60 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
|
||||
/**
|
||||
* \brief Initializes and assigns user regions for memory used by allocator algorithm
|
||||
* \param[in] lw: LwMEM instance. Set to `NULL` to use default instance
|
||||
* \param[in] regions: Array of regions with address and its size.
|
||||
* \param[in] regions: Pointer to array of regions with address and respective size.
|
||||
* Regions must be in increasing order (start address) and must not overlap in-between.
|
||||
* When `len` param is set to `0`, regions array must contain last entry with `NULL` address and `0` length,
|
||||
* indicating end of regions (similar to end of string)
|
||||
* Last region entry must have address `NULL` and size set to `0`
|
||||
* \code{.c}
|
||||
//Example definition
|
||||
lwmem_region_t regions[] = {
|
||||
{ addr1, size1 },
|
||||
{ addr2, size2 },
|
||||
{ addr3, size3 },
|
||||
{ NULL, 0 } //Regions array termination = end of descriptor
|
||||
{ (void *)0x10000000, 0x1000 }, //Region starts at address 0x10000000 and is 0x1000 bytes long
|
||||
{ (void *)0x20000000, 0x2000 }, //Region starts at address 0x20000000 and is 0x2000 bytes long
|
||||
{ (void *)0x30000000, 0x3000 }, //Region starts at address 0x30000000 and is 0x3000 bytes long
|
||||
{ NULL, 0 } //Array termination indicator
|
||||
}
|
||||
\endcode
|
||||
* \param[in] len: Number of regions in array.
|
||||
* Can be set to `0` to describe number of regions with `regions` parameter only.
|
||||
* \note `len` is deprecated and will be removed in the future versions.
|
||||
* Describe regions with `regions` parameter only instead
|
||||
* \return `0` on failure, number of final regions used for memory manager on success
|
||||
* \note This function is not thread safe when used with operating system.
|
||||
* It must be called only once to setup memory regions
|
||||
*/
|
||||
size_t
|
||||
lwmem_assignmem_ex(lwmem_t* const lw, const lwmem_region_t* regions, size_t len) {
|
||||
lwmem_assignmem_ex(lwmem_t* const lw, const lwmem_region_t* regions) {
|
||||
uint8_t* mem_start_addr;
|
||||
size_t mem_size;
|
||||
size_t mem_size, len = 0;
|
||||
lwmem_block_t* first_block, *prev_end_block;
|
||||
|
||||
/* Check first entries */
|
||||
if (LWMEM_GET_LW(lw)->end_block != NULL /* Init function may only be called once per lwmem instance */
|
||||
/* Check first things first */
|
||||
if (regions == NULL
|
||||
|| LWMEM_GET_LW(lw)->end_block != NULL /* Init function may only be called once per lwmem instance */
|
||||
|| (((size_t)LWMEM_CFG_ALIGN_NUM) & (((size_t)LWMEM_CFG_ALIGN_NUM) - 1)) > 0) { /* Must be power of 2 */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* When len == 0, number of regions is defined by pointer to regions.
|
||||
*
|
||||
* Last entry, indicating end of region, must have address and length to `NULL` and `0` respectively
|
||||
*/
|
||||
if (regions != NULL && len == 0) {
|
||||
/* Go through array of regions */
|
||||
for (const lwmem_region_t* r = regions; r->size > 0 && r->start_addr != NULL; ++len, ++r) {}
|
||||
/* Check values entered by application */
|
||||
mem_start_addr = (void*)0;
|
||||
mem_size = 0;
|
||||
for (size_t i = 0;; ++i) {
|
||||
/*
|
||||
* Check for valid entry or end of array descriptor
|
||||
*
|
||||
* Invalid entry is considered as "end-of-region" indicator
|
||||
*/
|
||||
if (regions[i].size == 0 && regions[i].start_addr == NULL) {
|
||||
len = i;
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* Save new values for next round */
|
||||
mem_start_addr = regions[i].start_addr;
|
||||
mem_size = regions[i].size;
|
||||
}
|
||||
|
||||
/* Process further checks of valid inputs */
|
||||
@ -712,26 +726,6 @@ lwmem_assignmem_ex(lwmem_t* const lw, const lwmem_region_t* regions, size_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
/* Make sure for valid entry */
|
||||
if (regions[i].size == 0 && regions[i].start_addr == NULL) {
|
||||
len = i;
|
||||
break;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* Save new values for next try */
|
||||
mem_start_addr = regions[i].start_addr;
|
||||
mem_size = regions[i].size;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; ++i, ++regions) {
|
||||
/* Get region start address and size */
|
||||
if (!prv_get_region_addr_size(regions, &mem_start_addr, &mem_size)) {
|
||||
|
@ -29,7 +29,7 @@
|
||||
* This file is part of LwMEM - Lightweight dynamic memory manager library.
|
||||
*
|
||||
* Author: Tilen MAJERLE <tilen@majerle.eu>
|
||||
* Version: v1.6.0
|
||||
* Version: v2.0.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.6.0
|
||||
* Version: v2.0.0
|
||||
*/
|
||||
#include "system/lwmem_sys.h"
|
||||
|
||||
|
@ -59,11 +59,11 @@ lwmem_test_run(void) {
|
||||
|
||||
/* Initialize default lwmem instance */
|
||||
/* Use one of 2 possible function calls: */
|
||||
lwmem_assignmem(lw_regions, 0);
|
||||
//lwmem_assignmem_ex(NULL, lw_regions, 0);
|
||||
lwmem_assignmem(lw_regions);
|
||||
//lwmem_assignmem_ex(NULL, lw_regions);
|
||||
|
||||
/* Initialize another, custom instance */
|
||||
lwmem_assignmem_ex(&lw_c, lw_c_regions, 0);
|
||||
lwmem_assignmem_ex(&lw_c, lw_c_regions);
|
||||
|
||||
/* Regions initialized... */
|
||||
|
||||
@ -150,7 +150,7 @@ lwmem_test_memory_structure(void) {
|
||||
* Assign memory for LwMEM. Set len parameter to 0 to calculate
|
||||
* Number of regions with regions pointer, with last entry being set to NULL and 0
|
||||
*/
|
||||
used_regions = lwmem_assignmem(regions_used, 0);
|
||||
used_regions = lwmem_assignmem(regions_used);
|
||||
printf("Manager is ready with %d regions!\r\n", (int)used_regions);
|
||||
lwmem_debug_print(1, 1);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user