Skip to content

Commit

Permalink
Added possibility of selecting between static and dynamic memory mana…
Browse files Browse the repository at this point in the history
…gement
  • Loading branch information
Tellicious committed Mar 3, 2024
1 parent 9f5cd24 commit 547be7c
Show file tree
Hide file tree
Showing 18 changed files with 1,056 additions and 79 deletions.
10 changes: 7 additions & 3 deletions .github/releaseBody.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Removed warning in `GaussNewton_Sens_Cal`
## Added possibility of using static and/or dynamic memory management

**Bugfix:**
- Initialized some variables in `GaussNewton_Sens_Cal` to avoid a `-Wmaybe-uninitialized` warning
### Contains breaking change on memory management. `ADVUTILS_USE_STATIC_ALLOCATION` and/or `ADVUTILS_USE_DYNAMIC_ALLOCATION` must be defined by user

**New features:**
- Added `*Static` variant of multiple functions with static memory management
- Added possibility of setting static and/or dynamic memory management for ADVUtils library via definition of `ADVUTILS_USE_STATIC_ALLOCATION` and `ADVUTILS_USE_DYNAMIC_ALLOCATION`. At least one of the two must be defined by the user
- Added possibility of using FreeRTOS-specific dynamic memory management functions with `set(ADVUtils_DYN_MEM_MGMT "USE_FREERTOS" CACHE STRING "" FORCE)` in CMake

See [Changelog](Changelog.md)
18 changes: 16 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
#
# libName_COMPILE_OPTS: If defined, it provide compiler options for generated library.
# libName_COMPILE_DEFS: If defined, it provides "-D" definitions to the library build
# add set(${libName}_DYN_MEM_MGMT "USE_FREERTOS" CACHE STRING "" FORCE) before add_subdirectory() to automatically configure the library to use FreeRTOS memory management functions (malloc, calloc, free)
#
cmake_minimum_required(VERSION 3.20)
cmake_minimum_required(VERSION 3.22)

set (libName ADVutils)
set (libName ADVUtils)

set(${libName}_src
${CMAKE_CURRENT_LIST_DIR}/src/basicMath.c
Expand All @@ -33,9 +34,22 @@ set(${libName}_inc
${CMAKE_CURRENT_LIST_DIR}/inc/
)

# Append definitions needed to automatically configure build with FreeRTOS
if ("${${libName}_DYN_MEM_MGMT}" STREQUAL "USE_FREERTOS")
list(APPEND ${libName}_COMPILE_DEFS
ADVUTILS_MEMORY_MGMT_HEADER="FreeRTOS.h"
ADVUTILS_MALLOC=pvPortMalloc
ADVUTILS_CALLOC=pvPortCalloc
ADVUTILS_FREE=vPortFree
)
endif()

# Register library to the system
add_library(${libName} STATIC)
target_sources(${libName} PRIVATE ${${libName}_src})
target_include_directories(${libName} PUBLIC ${${libName}_inc})
target_compile_options(${libName} PRIVATE ${${libName}_COMPILE_OPTS})
target_compile_definitions(${libName} PRIVATE ${${libName}_COMPILE_DEFS})
if ("${${libName}_DYN_MEM_MGMT}" STREQUAL "USE_FREERTOS")
target_link_libraries(${libName} freertos_kernel)
endif()
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## v1.9.0

### Contains breaking change on memory management. `ADVUTILS_USE_STATIC_ALLOCATION` and/or `ADVUTILS_USE_DYNAMIC_ALLOCATION` must be defined by user

**New features:**
- Added `*Static` variant of multiple functions with static memory management
- Added possibility of setting static and/or dynamic memory management for ADVUtils library via definition of `ADVUTILS_USE_STATIC_ALLOCATION` and `ADVUTILS_USE_DYNAMIC_ALLOCATION`. At least one of the two must be defined by the user
- Added possibility of using FreeRTOS-specific dynamic memory management functions with `set(ADVUtils_DYN_MEM_MGMT "USE_FREERTOS" CACHE STRING "" FORCE)` in CMake

## v1.8.1

**Bugfix:**
Expand Down
17 changes: 17 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,22 @@
- ***IIRFilters:*** simple discrete-time IIR filters, with on-the-fly conversion continuous -> discrete of derivative and integrator
- ***commonTypes:*** collection of common type definitions

## Configuration:
- User must define `ADVUTILS_USE_STATIC_ALLOCATION` and/or `ADVUTILS_USE_DYNAMIC_ALLOCATION` to select wheter to use static and/or dynamic memory management. `list`, `LPHashTable` and `LKHashTable` are available only with `ADVUTILS_USE_DYNAMIC_ALLOCATION`
- Functions that use static allocation are defined by `Static` suffix
- To automatically use thread-safe FreeRTOS-specific implementation of dynamic memory management functions (`malloc`, `calloc` and `free`) user can add `set(ADVUtils_DYN_MEM_MGMT "USE_FREERTOS" CACHE STRING "" FORCE)` before `add_subdirectory()` in project CMakeLists. This will automatically add the following defines to ADVUtils compilation:
- `ADVUTILS_MEMORY_MGMT_HEADER="FreeRTOS.h"`
- `ADVUTILS_MALLOC=pvPortMalloc`
- `ADVUTILS_CALLOC=pvPortCalloc`
- `ADVUTILS_FREE=vPortFree`
- `target_link_libraries(ADVUtils freertos_kernel)`
- If `ADVUTILS_MEMORY_MGMT_HEADER` is not defined, the library will use `stdlib` version of `malloc`, `calloc` and `free`
- User can use another different implementation of `malloc`, `calloc` and `free` by defining the following four macros:
- `ADVUTILS_MEMORY_MGMT_HEADER`
- `ADVUTILS_MALLOC`
- `ADVUTILS_CALLOC`
- `ADVUTILS_FREE`




2 changes: 2 additions & 0 deletions inc/LKHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef enum { LKHT_REMOVE_ITEM = 0, LKHT_DO_NOT_REMOVE_ITEM = 1 } lkHashTableRe

/* Function prototypes -------------------------------------------------------*/

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Init linked hash-table structure
*
Expand Down Expand Up @@ -125,6 +126,7 @@ static inline void lkHashTableInfo(lkHashTable_t* lkht, uint32_t* size, uint32_t
* \return UTILS_STATUS_SUCCESS if list is flushed correctly, UTILS_STATUS_ERROR if data cannot be flushed
*/
utilsStatus_t lkHashTableFlush(lkHashTable_t* lkht);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions inc/LPHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ typedef enum { LPHT_REMOVE_ITEM = 0, LPHT_DO_NOT_REMOVE_ITEM = 1 } lpHashTableRe

/* Function prototypes -------------------------------------------------------*/

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Init linear-probing hash-table structure
*
Expand Down Expand Up @@ -165,6 +166,7 @@ void lpHashTableIt(lpHashTableIterator_t* it, lpHashTable_t* lpht);
* \return UTILS_STATUS_SUCCESS if iterator is moved to next value, UTILS_STATUS_ERROR if there are no more items
*/
utilsStatus_t lpHashTableItNext(lpHashTableIterator_t* it);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions inc/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef struct {

/* Function prototypes -------------------------------------------------------*/

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Init list structure
*
Expand Down Expand Up @@ -228,6 +229,7 @@ void listIt(listIterator_t* it, list_t* list);
* \return UTILS_STATUS_SUCCESS if iterator is moved to next value, UTILS_STATUS_ERROR if there are no more items
*/
utilsStatus_t listItNext(listIterator_t* it);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef __cplusplus
}
Expand Down
65 changes: 63 additions & 2 deletions inc/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ typedef struct {

/* Function prototypes -------------------------------------------------------*/

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Create a new matrix containing a pointer to an external array
* \brief Create a new matrix with dynamic memory allocation
*
* \param[in] matrix: pointer to matrix object
* \param[in] rows: number of rows
Expand All @@ -72,6 +73,19 @@ typedef struct {
* \return UTILS_STATUS_SUCCESS if matrix was initialized, UTILS_STATUS_ERROR if data was not allocated correctly
*/
utilsStatus_t matrixInit(matrix_t* matrix, uint8_t rows, uint8_t cols);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef ADVUTILS_USE_STATIC_ALLOCATION
/**
* \brief Create a new matrix with static data
*
* \param[in] matrix: pointer to matrix object
* \param[in] data: pointer to data array of size rows * cols
* \param[in] rows: number of rows
* \param[in] cols: number of columns
*/
void matrixInitStatic(matrix_t* matrix, float* data, uint8_t rows, uint8_t cols);
#endif /* ADVUTILS_USE_STATIC_ALLOCATION */

/**
* \brief Set the matrix as an identity matrix
Expand Down Expand Up @@ -168,6 +182,7 @@ utilsStatus_t matrixMult_rhsT(matrix_t* lhs, matrix_t* rhs, matrix_t* result);
*/
void matrixMultScalar(matrix_t* lhs, float sc, matrix_t* result);

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Matrix inversion
*
Expand All @@ -183,6 +198,25 @@ void matrixInversed(matrix_t* lhs, matrix_t* result);
* \param[out] result: pointer to result matrix object
*/
void matrixInversed_rob(matrix_t* lhs, matrix_t* result);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef ADVUTILS_USE_STATIC_ALLOCATION
/**
* \brief Matrix inversion with static allocation
*
* \param[in] lhs: pointer to left-hand side matrix object
* \param[out] result: pointer to result matrix object
*/
void matrixInversedStatic(matrix_t* lhs, matrix_t* result);

/**
* \brief Robust matrix inversion performed with LUP decomposition and static allocation
*
* \param[in] lhs: pointer to left-hand side matrix object
* \param[out] result: pointer to result matrix object
*/
void matrixInversedStatic_rob(matrix_t* lhs, matrix_t* result);
#endif /* ADVUTILS_USE_STATIC_ALLOCATION */

/**
* \brief Matrix transposition
Expand All @@ -200,13 +234,25 @@ void matrixTrans(matrix_t* lhs, matrix_t* result);
*/
void matrixNormalized(matrix_t* lhs, matrix_t* result);

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Matrix Moore-Penrose pseudo-inverse
*
* \param[in] lhs: pointer to left-hand side matrix object
* \param[out] result: pointer to result matrix object
*/
void matrixPseudo_inv(matrix_t* lhs, matrix_t* result);
void matrixPseudoInv(matrix_t* lhs, matrix_t* result);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef ADVUTILS_USE_STATIC_ALLOCATION
/**
* \brief Matrix Moore-Penrose pseudo-inverse with static allocation
*
* \param[in] lhs: pointer to left-hand side matrix object
* \param[out] result: pointer to result matrix object
*/
void matrixPseudoInvStatic(matrix_t* lhs, matrix_t* result);
#endif /* ADVUTILS_USE_STATIC_ALLOCATION */

/**
* \brief Set a single element inside matrix
Expand All @@ -229,6 +275,7 @@ static inline void matrixSet(matrix_t* matrix, uint8_t i, uint8_t j, float value
*/
static inline float matrixGet(matrix_t* matrix, uint8_t i, uint8_t j) { return ELEMP(matrix, i, j); }

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Calculate matrix determinant
*
Expand All @@ -237,6 +284,18 @@ static inline float matrixGet(matrix_t* matrix, uint8_t i, uint8_t j) { return E
* \return determinant
*/
float matrixDet(matrix_t* matrix);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef ADVUTILS_USE_STATIC_ALLOCATION
/**
* \brief Calculate matrix determinant
*
* \param[in] matrix: pointer to matrix object
*
* \return determinant
*/
float matrixDetStatic(matrix_t* matrix);
#endif /* ADVUTILS_USE_STATIC_ALLOCATION */

/**
* \brief Calculate matrix norm
Expand All @@ -247,6 +306,7 @@ float matrixDet(matrix_t* matrix);
*/
float matrixNorm(matrix_t* matrix);

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Delete matrix
*
Expand All @@ -255,6 +315,7 @@ float matrixNorm(matrix_t* matrix);
* \return UTILS_STATUS_SUCCESS if matrix data is deleted, UTILS_STATUS_ERROR if data was not deleted correctly
*/
utilsStatus_t matrixDelete(matrix_t* matrix);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef __cplusplus
}
Expand Down
17 changes: 16 additions & 1 deletion inc/movingAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,26 @@ typedef struct {

/* Function prototypes -------------------------------------------------------*/

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Init moving average structure
* \brief Init moving average structure with dynamic memory allocation
*
* \param[in] movingAvg: pointer to moving average object
* \param[in] size: required queue size
*/
utilsStatus_t movingAvgInit(movingAvg_t* movingAvg, MOVAVG_IND_TYPE size);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef ADVUTILS_USE_STATIC_ALLOCATION
/**
* \brief Init moving average structure with static data
*
* \param[in] movingAvg: pointer to moving average object
* \param[in] data: pointer to data array
* \param[in] size: required queue size
*/
void movingAvgInitStatic(movingAvg_t* movingAvg, MOVAVG_TYPE* data, MOVAVG_IND_TYPE size);
#endif /* ADVUTILS_USE_STATIC_ALLOCATION */

/**
* \brief Calculate moving average
Expand Down Expand Up @@ -95,12 +108,14 @@ static inline MOVAVG_TYPE movingAvgGetLatest(movingAvg_t* movingAvg) { return (m
*/
utilsStatus_t movingAvgFlush(movingAvg_t* movingAvg);

#ifdef ADVUTILS_USE_DYNAMIC_ALLOCATION
/**
* \brief Delete moving average
*
* \param[in] movingAvg: pointer to moving average object
*/
utilsStatus_t movingAvgDelete(movingAvg_t* movingAvg);
#endif /* ADVUTILS_USE_DYNAMIC_ALLOCATION */

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 547be7c

Please sign in to comment.