diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..77b3607 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms + +custom: ['paypal.me/tilz0R'] diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..db83283 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,35 @@ +# Changelog + +## Develop + +## v1.5.0 + +- Add option to cleanup memory on free and realloc operations + +## v1.4.0 + +- New artistic code style +- Replace configuration +- Several bug fixes + +## v1.3.0 + +- Added option for custom LwMEM instance for complete isolation +- Added `_ex` functions for extended features for custom instances +- Added macros for backward-compatible, default instance, allocation functions +- Added option to force LwMEM to allocate memory at specific region +- Added win32 examples + +## v1.2.0 + +- Fix comments to easily undestand architecture +- Migrate examples to CMSIS-OS v2 +- Use pre-increment instead of post-increment +- Other C code style fixes + +## v1.1.0 + +- Added support for thread-safety application +- Added boundary check of all internal pointers +- Added tests code directly in library +- Bug fixes diff --git a/dev/VisualStudio/lwmem_opts.h b/dev/VisualStudio/lwmem_opts.h index 72e12e4..f767b2c 100644 --- a/dev/VisualStudio/lwmem_opts.h +++ b/dev/VisualStudio/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H @@ -42,7 +42,9 @@ * Open "include/lwmem/lwmem_opt.h" and * copy & replace here settings you want to change values */ -#define LWMEM_CFG_OS 1 -#define LWMEM_CFG_OS_MUTEX_HANDLE HANDLE +#define LWMEM_CFG_OS 1 +#define LWMEM_CFG_OS_MUTEX_HANDLE HANDLE +#define LWMEM_CFG_ENABLE_STATS 0 +#define LWMEM_CFG_CLEAN_MEMORY 1 -#endif /* OW_HDR_OPTS_H */ +#endif /* LWMEM_HDR_OPTS_H */ diff --git a/docs/conf.py b/docs/conf.py index 8b240a8..d394593 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -26,27 +26,32 @@ copyright = '2020, Tilen MAJERLE' author = 'Tilen MAJERLE' -# The full version, including alpha/beta/rc tags -version = '1.4.0' - # Try to get branch at which this is running # and try to determine which version to display in sphinx +# Version is using git tag if on master or "latest-develop" if on develop branch +version = '' git_branch = '' + +# Get current branch res = os.popen('git branch').read().strip() for line in res.split("\n"): if line[0] == '*': git_branch = line[1:].strip() # Decision for display version -try: - if git_branch.index('develop') >= 0: - version = "latest-develop" -except Exception: - print("Exception for index check") - -# For debugging purpose +git_branch = git_branch.replace('(HEAD detached at ', '').replace(')', '') +if git_branch.find('master') >= 0 or git_branch.find('main') >= 0: + version = os.popen('git describe --tags --abbrev=0').read().strip() + if version == '': + version = 'v0.0.0' +elif git_branch.find('develop') != -1 and not (git_branch.find('develop-') >= 0 or git_branch.find('develop/') >= 0): + version = 'latest-develop' +else: + version = 'branch-' + git_branch + +# For debugging purpose only print("GIT BRANCH: " + git_branch) -print("VERSION: " + version) +print("GIT VERSION: " + version) # -- General configuration --------------------------------------------------- @@ -123,7 +128,7 @@ # # breathe_projects = { - "lwmem": "_build/xml/" + "lwmem": "_build/xml/" } breathe_default_project = "lwmem" breathe_default_members = ('members', 'undoc-members') \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index 8e73efd..6c78805 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -5,10 +5,13 @@ Welcome to the documentation for version |version|. LwMEM is lightweight dynamic memory manager optimized for embedded systems. +.. image:: static/images/logo.svg + :align: center + .. rst-class:: center .. rst-class:: index_links - :ref:`download_library` :ref:`getting_started` `Open Github `_ + :ref:`download_library` :ref:`getting_started` `Open Github `_ `Donate `_ Features ^^^^^^^^ diff --git a/docs/requirements.txt b/docs/requirements.txt index c0ffd67..eb5e0fd 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,7 +1,7 @@ breathe>=4.9.1 colorama -docutils>=0.14 -sphinx>=2.0.1 +docutils==0.16 +sphinx>=3.5.1 sphinx_rtd_theme sphinx-tabs sphinxcontrib-svg2pdfconverter diff --git a/docs/static/images/logo.drawio b/docs/static/images/logo.drawio new file mode 100644 index 0000000..8238bdc --- /dev/null +++ b/docs/static/images/logo.drawio @@ -0,0 +1 @@ +jZLLbsMgEEW/xstItkmiZNs0TaUm2WSRNTITgwoei+Di9OuL68EPRZW6gjnzAO4lYTvTHiyv5QkF6CRPRZuw1yTPs3SzCUtHHj3ZrgmUVgkqGsFFfUPsJNooAfdZoUPUTtVzWGBVQeFmjFuLfl52Qz0/teYlPIFLwfUzvSrhZE83q3Tk76BKGU/OUsoYHosJ3CUX6CeI7RO2s4iu35l2B7oTL+rS9739kR0uZqFy/2k4L8QStsYfz6Z5WW4/bvqaLWjKF9cNPfjoT/sT3dg9ogwWm0pANylL2IuXysGl5kWX9cH4wKQzmtJ3Z/FzkGsdyA0rR96yLh60SEPw/JB4K7AO2gmihx0ADTj7CCWUZctV30K/LI+xHz3L1mSEnPgVfeT0Tcph9Khk2JCYMRxN+81Nvj7b/wA= \ No newline at end of file diff --git a/docs/static/images/logo.svg b/docs/static/images/logo.svg new file mode 100644 index 0000000..a4e82b3 --- /dev/null +++ b/docs/static/images/logo.svg @@ -0,0 +1,3 @@ + + +
LwMEM
LwMEM
\ No newline at end of file diff --git a/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/lwmem_opts.h b/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/lwmem_opts.h index 4cc3b81..8700174 100644 --- a/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/lwmem_opts.h +++ b/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/main.h b/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/main.h index 76a6068..a2693bd 100644 --- a/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/main.h +++ b/examples/stm32/lwmem_rtos_stm32l496_discovery/inc/main.h @@ -27,7 +27,7 @@ * OTHER DEALINGS IN THE SOFTWARE. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef __MAIN_H #define __MAIN_H diff --git a/examples/stm32/lwmem_rtos_stm32l496_discovery/src/main.c b/examples/stm32/lwmem_rtos_stm32l496_discovery/src/main.c index 15f6fe2..44da98e 100644 --- a/examples/stm32/lwmem_rtos_stm32l496_discovery/src/main.c +++ b/examples/stm32/lwmem_rtos_stm32l496_discovery/src/main.c @@ -27,7 +27,7 @@ * OTHER DEALINGS IN THE SOFTWARE. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include "main.h" #include "cmsis_os.h" diff --git a/examples/stm32/lwmem_stm32l496_discovery/inc/lwmem_opts.h b/examples/stm32/lwmem_stm32l496_discovery/inc/lwmem_opts.h index 1bf1921..0112e6e 100644 --- a/examples/stm32/lwmem_stm32l496_discovery/inc/lwmem_opts.h +++ b/examples/stm32/lwmem_stm32l496_discovery/inc/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/examples/stm32/lwmem_stm32l496_discovery/inc/main.h b/examples/stm32/lwmem_stm32l496_discovery/inc/main.h index 76a6068..a2693bd 100644 --- a/examples/stm32/lwmem_stm32l496_discovery/inc/main.h +++ b/examples/stm32/lwmem_stm32l496_discovery/inc/main.h @@ -27,7 +27,7 @@ * OTHER DEALINGS IN THE SOFTWARE. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef __MAIN_H #define __MAIN_H diff --git a/examples/stm32/lwmem_stm32l496_discovery/src/main.c b/examples/stm32/lwmem_stm32l496_discovery/src/main.c index 374349b..29beed4 100644 --- a/examples/stm32/lwmem_stm32l496_discovery/src/main.c +++ b/examples/stm32/lwmem_stm32l496_discovery/src/main.c @@ -27,7 +27,7 @@ * OTHER DEALINGS IN THE SOFTWARE. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include "main.h" #include "lwmem/lwmem.h" diff --git a/examples/win32/lwmem/lwmem_opts.h b/examples/win32/lwmem/lwmem_opts.h index 1bf1921..0112e6e 100644 --- a/examples/win32/lwmem/lwmem_opts.h +++ b/examples/win32/lwmem/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/examples/win32/lwmem_multi_ins_multi_region/lwmem_opts.h b/examples/win32/lwmem_multi_ins_multi_region/lwmem_opts.h index 1bf1921..0112e6e 100644 --- a/examples/win32/lwmem_multi_ins_multi_region/lwmem_opts.h +++ b/examples/win32/lwmem_multi_ins_multi_region/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/examples/win32/lwmem_multi_region/lwmem_opts.h b/examples/win32/lwmem_multi_region/lwmem_opts.h index 1bf1921..0112e6e 100644 --- a/examples/win32/lwmem_multi_region/lwmem_opts.h +++ b/examples/win32/lwmem_multi_region/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/examples/win32/lwmem_os/lwmem_opts.h b/examples/win32/lwmem_os/lwmem_opts.h index 0e95788..1243399 100644 --- a/examples/win32/lwmem_os/lwmem_opts.h +++ b/examples/win32/lwmem_os/lwmem_opts.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/lwmem/src/include/lwmem/lwmem.h b/lwmem/src/include/lwmem/lwmem.h index b70136e..07c7163 100644 --- a/lwmem/src/include/lwmem/lwmem.h +++ b/lwmem/src/include/lwmem/lwmem.h @@ -29,12 +29,11 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_H #define LWMEM_HDR_H -#include #include #include #include "lwmem/lwmem_opt.h" @@ -67,6 +66,14 @@ typedef struct lwmem_block { or `0` when block is considered free */ } lwmem_block_t; +/** + * \brief Statistics structure + */ +typedef struct { + uint32_t nr_alloc; /*!< Number of all allocated blocks in single instance */ + uint32_t nr_free; /*!< Number of frees in the LwMEM instance */ +} lwmem_stats_t; + /** * \brief LwMEM main structure */ @@ -78,6 +85,9 @@ typedef struct lwmem { #if LWMEM_CFG_OS || __DOXYGEN__ LWMEM_CFG_OS_MUTEX_HANDLE mutex; /*!< System mutex for OS */ #endif /* LWMEM_CFG_OS || __DOXYGEN__ */ +#if LWMEM_CFG_ENABLE_STATS || __DOXYGEN__ + lwmem_stats_t stats; /*!< Statistics */ +#endif /* LWMEM_CFG_ENABLE_STATS || __DOXYGEN__ */ #if defined(LWMEM_DEV) && !__DOXYGEN__ lwmem_block_t start_block_first_use; /*!< Value of start block for very first time. This is used only during validation process and is removed in final use */ diff --git a/lwmem/src/include/lwmem/lwmem_opt.h b/lwmem/src/include/lwmem/lwmem_opt.h index 13140c4..78beeee 100644 --- a/lwmem/src/include/lwmem/lwmem_opt.h +++ b/lwmem/src/include/lwmem/lwmem_opt.h @@ -29,11 +29,14 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPT_H #define LWMEM_HDR_OPT_H +/* Uncomment to ignore user options (or set macro in compiler flags) */ +/* #define LWMEM_IGNORE_USER_OPTS */ + /* Include application options */ #ifndef LWMEM_IGNORE_USER_OPTS #include "lwmem_opts.h" @@ -82,6 +85,24 @@ extern "C" { #define LWMEM_CFG_ALIGN_NUM ((size_t)4) #endif +/** + * \brief Enables `1` or disables `0` memory cleanup on free operation (or realloc). + * + * It resets unused memory to `0x00` and prevents other applications seeing old data. + * It is disabled by default since it has performance penalties. + * / +#ifndef LWMEM_CFG_CLEAN_MEMORY +#define LWMEM_CFG_CLEAN_MEMORY 0 +#endif + +/** + * \brief Enables `1` or disables `0` statistics in the library + * + */ +#ifndef LWMEM_CFG_ENABLE_STATS +#define LWMEM_CFG_ENABLE_STATS 0 +#endif + /** * \} */ diff --git a/lwmem/src/include/lwmem/lwmem_opts_template.h b/lwmem/src/include/lwmem/lwmem_opts_template.h index 08f7910..b9ad475 100644 --- a/lwmem/src/include/lwmem/lwmem_opts_template.h +++ b/lwmem/src/include/lwmem/lwmem_opts_template.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_OPTS_H #define LWMEM_HDR_OPTS_H diff --git a/lwmem/src/include/system/lwmem_sys.h b/lwmem/src/include/system/lwmem_sys.h index e4bcd66..4d988b0 100644 --- a/lwmem/src/include/system/lwmem_sys.h +++ b/lwmem/src/include/system/lwmem_sys.h @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #ifndef LWMEM_HDR_SYS_H #define LWMEM_HDR_SYS_H diff --git a/lwmem/src/lwmem/lwmem.c b/lwmem/src/lwmem/lwmem.c index d5ee9b0..2ce199f 100644 --- a/lwmem/src/lwmem/lwmem.c +++ b/lwmem/src/lwmem/lwmem.c @@ -29,9 +29,10 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include +#include #include "lwmem/lwmem.h" #if LWMEM_CFG_OS @@ -144,6 +145,13 @@ #define LWMEM_UNPROTECT(lw) #endif /* !LWMEM_CFG_OS */ +/* Statistics part */ +#if LWMEM_CFG_ENABLE_STATS +#define LWMEM_INC_STATS(field) (++(field)) +#else +#define LWMEM_INC_STATS(field) +#endif /* LWMEM_CFG_ENABLE_STATS */ + /** * \brief LwMEM default structure used by application */ @@ -202,6 +210,11 @@ static void prv_insert_free_block(lwmem_t* const lw, lwmem_block_t* nb) { lwmem_block_t* prev; + /* Check valid inputs */ + if (nb == NULL) { + return; + } + /* * Try to find position to put new block in-between * Search until all free block addresses are lower than entry block @@ -218,6 +231,17 @@ prv_insert_free_block(lwmem_t* const lw, lwmem_block_t* nb) { * Previous block is last free block before input block */ +#if LWMEM_CFG_CLEAN_MEMORY + /* + * Reset user memory. This is to reset memory + * after it has been freed by the application. + * + * By doing this, we protect data left by app + * and we make sure new allocations cannot see old information + */ + LWMEM_MEMSET(LWMEM_GET_PTR_FROM_BLOCK(nb), 0x00, nb->size - LWMEM_BLOCK_META_SIZE); +#endif /* LWMEM_CFG_RESET_MEMORY */ + /* * Check if previous block and input block together create one big contiguous block * If this is the case, merge blocks together and increase previous block by input block size @@ -387,6 +411,8 @@ prv_alloc(lwmem_t* const lw, const lwmem_region_t* region, const size_t size) { prv_split_too_big_block(lw, curr, final_size); /* Split block if it is too big */ LWMEM_BLOCK_SET_ALLOC(curr); /* Set block as allocated */ + LWMEM_INC_STATS(LWMEM_GET_LW(lw)->stats.nr_alloc); + return retval; } @@ -403,6 +429,8 @@ prv_free(lwmem_t* const lw, void* const ptr) { LWMEM_GET_LW(lw)->mem_available_bytes += block->size; /* Increase available bytes */ prv_insert_free_block(lw, block); /* Put block back to list of free block */ + + LWMEM_INC_STATS(LWMEM_GET_LW(lw)->stats.nr_free); } } diff --git a/lwmem/src/system/lwmem_sys_cmsis_os.c b/lwmem/src/system/lwmem_sys_cmsis_os.c index 0d964e3..f0831d8 100644 --- a/lwmem/src/system/lwmem_sys_cmsis_os.c +++ b/lwmem/src/system/lwmem_sys_cmsis_os.c @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include "system/lwmem_sys.h" diff --git a/lwmem/src/system/lwmem_sys_template.c b/lwmem/src/system/lwmem_sys_template.c index ab77b8d..1d41e56 100644 --- a/lwmem/src/system/lwmem_sys_template.c +++ b/lwmem/src/system/lwmem_sys_template.c @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include "system/lwmem_sys.h" diff --git a/lwmem/src/system/lwmem_sys_win32.c b/lwmem/src/system/lwmem_sys_win32.c index a957783..6b1e0aa 100644 --- a/lwmem/src/system/lwmem_sys_win32.c +++ b/lwmem/src/system/lwmem_sys_win32.c @@ -29,7 +29,7 @@ * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE - * Version: v1.4.0 + * Version: v1.5.0 */ #include "system/lwmem_sys.h"