Prepare code for thread-safe support

This commit is contained in:
tilz0R 2019-06-10 02:43:37 +02:00
parent 9e361aed52
commit 66d85632c4
8 changed files with 395 additions and 5 deletions

View File

@ -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 <tilen@majerle.eu>
*/
@ -35,9 +35,10 @@
#ifdef __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
#include "string.h"
#include "lwmem_config.h"
/**
* \defgroup LWMEM Lightweight dynamic memory manager
@ -81,6 +82,6 @@ void LWMEM_PREF(free_s)(void** const ptr);
#ifdef __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* LWMEM_HDR_H */

View File

@ -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 <tilen@majerle.eu>
*/
#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 */

View File

@ -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 <tilen@majerle.eu>
*/
#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 */

View File

@ -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 <tilen@majerle.eu>
*/
#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 */

View File

@ -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 <tilen@majerle.eu>
*/

View File

@ -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 <tilen@majerle.eu>
*/
#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__ */

View File

@ -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 <tilen@majerle.eu>
*/
#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__ */

View File

@ -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 <tilen@majerle.eu>
*/
#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__ */