From 66d85632c47236fe5e1ccb6b3d78836b3f02a003 Mon Sep 17 00:00:00 2001 From: tilz0R Date: Mon, 10 Jun 2019 02:43:37 +0200 Subject: [PATCH] Prepare code for thread-safe support --- src/include/lwmem/lwmem.h | 9 +-- src/include/lwmem/lwmem_config_default.h | 74 +++++++++++++++++++++ src/include/lwmem/lwmem_config_template.h | 46 +++++++++++++ src/include/system/lwmem_sys.h | 63 ++++++++++++++++++ src/lwmem/lwmem.c | 2 +- src/system/lwmem_sys_cmsis_os.c | 68 +++++++++++++++++++ src/system/lwmem_sys_template.c | 79 +++++++++++++++++++++++ src/system/lwmem_sys_win32.c | 59 +++++++++++++++++ 8 files changed, 395 insertions(+), 5 deletions(-) create mode 100644 src/include/lwmem/lwmem_config_default.h create mode 100644 src/include/lwmem/lwmem_config_template.h create mode 100644 src/include/system/lwmem_sys.h create mode 100644 src/system/lwmem_sys_cmsis_os.c create mode 100644 src/system/lwmem_sys_template.c create mode 100644 src/system/lwmem_sys_win32.c diff --git a/src/include/lwmem/lwmem.h b/src/include/lwmem/lwmem.h index 582d685..e77a6b3 100644 --- a/src/include/lwmem/lwmem.h +++ b/src/include/lwmem/lwmem.h @@ -26,7 +26,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * - * This file is part of Lightweight dynamic memory manager library. + * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE */ @@ -35,9 +35,10 @@ #ifdef __cplusplus extern "C" { -#endif +#endif /* __cplusplus */ #include "string.h" +#include "lwmem_config.h" /** * \defgroup LWMEM Lightweight dynamic memory manager @@ -60,7 +61,7 @@ extern "C" { /** * \brief Memory region descriptor */ -typedef struct { +typedef struct { void* start_addr; /*!< Region start address */ size_t size; /*!< Size of region in units of bytes */ } LWMEM_PREF(region_t); @@ -81,6 +82,6 @@ void LWMEM_PREF(free_s)(void** const ptr); #ifdef __cplusplus } -#endif +#endif /* __cplusplus */ #endif /* LWMEM_HDR_H */ \ No newline at end of file diff --git a/src/include/lwmem/lwmem_config_default.h b/src/include/lwmem/lwmem_config_default.h new file mode 100644 index 0000000..cb9fe83 --- /dev/null +++ b/src/include/lwmem/lwmem_config_default.h @@ -0,0 +1,74 @@ +/** + * \file lwmem_config_default.h + * \brief LwMEM default config + */ + +/* + * Copyright (c) 2018 Tilen Majerle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * This file is part of LwMEM - Lightweight dynamic memory manager library. + * + * Author: Tilen MAJERLE + */ +#ifndef LWMEM_HDR_CONFIG_DEFAULT_H +#define LWMEM_HDR_CONFIG_DEFAULT_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * \defgroup LWMEM_CONFIG Configuration + * \brief Configuration for OneWire library + * \{ + */ + +/** + * \brief Enables `1` or disables `0` operating system support in the library + * + * \note When `LWMEM_CFG_OS` is enabled, user must implement functions in \ref LWMEM_SYS group. + */ +#ifndef LWMEM_CFG_OS +#define LWMEM_CFG_OS 0 +#endif + +/** + * \brief Mutex handle type + * + * \note This value must be set in case \ref LWMEM_CFG_OS is set to `1`. + * If data type is not known to compiler, include header file with + * definition before you define handle type + */ +#ifndef LWMEM_CFG_OS_MUTEX_HANDLE +#define LWMEM_CFG_OS_MUTEX_HANDLE void * +#endif + +/** + * \} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* LWMEM_HDR_CONFIG_DEFAULT_H */ diff --git a/src/include/lwmem/lwmem_config_template.h b/src/include/lwmem/lwmem_config_template.h new file mode 100644 index 0000000..f41d110 --- /dev/null +++ b/src/include/lwmem/lwmem_config_template.h @@ -0,0 +1,46 @@ +/** + * \file lwmem_config_template.h + * \brief LwMEM configuration file + */ + +/* + * Copyright (c) 2018 Tilen Majerle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * This file is part of LwMEM - Lightweight dynamic memory manager library. + * + * Author: Tilen MAJERLE + */ +#ifndef LWMEM_HDR_CONFIG_H +#define LWMEM_HDR_CONFIG_H + +/* Rename this file to "lwmem_config.h" for your application */ + +/* + * Open "include/lwmem/lwmem_config_default.h" and + * copy & replace here settings you want to change values + */ + +/* After user configuration, call default config to merge config together */ +#include "lwmem/lwmem_config_default.h" + +#endif /* LWMEM_HDR_CONFIG_H */ diff --git a/src/include/system/lwmem_sys.h b/src/include/system/lwmem_sys.h new file mode 100644 index 0000000..50df4bf --- /dev/null +++ b/src/include/system/lwmem_sys.h @@ -0,0 +1,63 @@ +/** + * \file lwmem_sys.h + * \brief System functions for OS + */ + +/* + * Copyright (c) 2018 Tilen Majerle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * This file is part of LwMEM - Lightweight dynamic memory manager library. + * + * Author: Tilen MAJERLE + */ +#ifndef LWMEM_HDR_SYS_H +#define LWMEM_HDR_SYS_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include "stdint.h" +#include "stddef.h" +#include "lwmem/lwmem.h" + +/** + * \defgroup LWMEM_SYS System functions + * \brief System functions when used with operating system + * \{ + */ + +uint8_t lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* mutex); +uint8_t lwmem_sys_mutex_delete(LWMEM_CFG_OS_MUTEX_HANDLE* mutex); +uint8_t lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* mutex); +uint8_t lwmem_sys_mutex_release(LWMEM_CFG_OS_MUTEX_HANDLE* mutex); + +/** + * \} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* LWMEM_HDR_SYS_H */ diff --git a/src/lwmem/lwmem.c b/src/lwmem/lwmem.c index 7065513..04b5006 100644 --- a/src/lwmem/lwmem.c +++ b/src/lwmem/lwmem.c @@ -26,7 +26,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * - * This file is part of Lightweight dynamic memory manager library. + * This file is part of LwMEM - Lightweight dynamic memory manager library. * * Author: Tilen MAJERLE */ diff --git a/src/system/lwmem_sys_cmsis_os.c b/src/system/lwmem_sys_cmsis_os.c new file mode 100644 index 0000000..627a718 --- /dev/null +++ b/src/system/lwmem_sys_cmsis_os.c @@ -0,0 +1,68 @@ +/** + * \file lwmem_sys_cmsis_os.c + * \brief System functions for CMSIS-OS based operating system + */ + +/* + * Copyright (c) 2018 Tilen Majerle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * This file is part of LwMEM - Lightweight dynamic memory manager library. + * + * Author: Tilen MAJERLE + */ +#include "system/lwmem_sys.h" + +#if LWMEM_CFG_OS && !__DOXYGEN__ + +#include "cmsis_os.h" + +uint8_t +lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + osMutexDef(m); + *mutex = osMutexCreate(osMutex(m)); /* Create new mutex */ + return 1; +} + +uint8_t +lwmem_sys_mutex_delete(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + osMutexDelete(*mutex); /* Delete mutex */ + return 1; +} + +uint8_t +lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + if (osMutexWait(*mutex, osWaitForever) != osOK) { + return 0; + } + return 1; +} + +uint8_t +lwmem_sys_mutex_release(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + if (osMutexRelease(*mutex) != osOK) { + return 0; + } + return 1; +} + +#endif /* LWMEM_CFG_OS && !__DOXYGEN__ */ diff --git a/src/system/lwmem_sys_template.c b/src/system/lwmem_sys_template.c new file mode 100644 index 0000000..13aab8b --- /dev/null +++ b/src/system/lwmem_sys_template.c @@ -0,0 +1,79 @@ +/** + * \file lwmem_sys_template.c + * \brief System functions template file + */ + +/* + * Copyright (c) 2018 Tilen Majerle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * This file is part of LwMEM - Lightweight dynamic memory manager library. + * + * Author: Tilen MAJERLE + */ +#include "system/lwmem_sys.h" + +#if LWMEM_CFG_OS || __DOXYGEN__ + +#include "cmsis_os.h" + +/** + * \brief Create a new mutex and assign value to handle + * \param[out] mutex: Output variable to save mutex handle + * \return `1` on success, `0` otherwise + */ +uint8_t +lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +/** + * \brief Delete existing mutex and invalidate mutex variable + * \param[in] mutex: Mutex handle to remove and invalidate + * \return `1` on success, `0` otherwise + */ +uint8_t +lwmem_sys_mutex_delete(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +/** + * \brief Wait for a mutex until ready (unlimited time) + * \param[in] mutex: Mutex handle to wait for + * \return `1` on success, `0` otherwise + */ +uint8_t +lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +/** + * \brief Release already locked mutex + * \param[in] mutex: Mutex handle to release + * \return `1` on success, `0` otherwise + */ +uint8_t +lwmem_sys_mutex_release(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +#endif /* LWMEM_CFG_OS || __DOXYGEN__ */ diff --git a/src/system/lwmem_sys_win32.c b/src/system/lwmem_sys_win32.c new file mode 100644 index 0000000..78ec654 --- /dev/null +++ b/src/system/lwmem_sys_win32.c @@ -0,0 +1,59 @@ +/** + * \file lwmem_sys_win32.c + * \brief System functions for WIN32 + */ + +/* + * Copyright (c) 2018 Tilen Majerle + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * This file is part of LwMEM - Lightweight dynamic memory manager library. + * + * Author: Tilen MAJERLE + */ +#include "system/lwmem_sys.h" + +#if LWMEM_CFG_OS && !__DOXYGEN__ + +#include "cmsis_os.h" + +uint8_t +lwmem_sys_mutex_create(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +uint8_t +lwmem_sys_mutex_delete(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +uint8_t +lwmem_sys_mutex_wait(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +uint8_t +lwmem_sys_mutex_release(LWMEM_CFG_OS_MUTEX_HANDLE* mutex) { + return 1; +} + +#endif /* LWMEM_CFG_OS && !__DOXYGEN__ */