Apply clang-format

This commit is contained in:
Tilen Majerle 2022-09-01 19:05:40 +02:00
parent c16c2a3ef0
commit b268fbbb86
6 changed files with 199 additions and 183 deletions

View File

@ -1,8 +1,8 @@
#include "lwmem/lwmem.h"
#include "lwmem/lwmem.hpp"
#include <string.h>
#include <stdint.h>
#include <iostream>
#include <stdint.h>
#include <string.h>
extern "C" void lwmem_test_run(void);
extern "C" void lwmem_test_memory_structure(void);

View File

@ -34,8 +34,8 @@
#ifndef LWMEM_HDR_H
#define LWMEM_HDR_H
#include <stdint.h>
#include <limits.h>
#include <stdint.h>
#include "lwmem/lwmem_opt.h"
#ifdef __cplusplus
@ -59,8 +59,8 @@ extern "C" {
* \brief Memory block structure
*/
typedef struct lwmem_block {
struct lwmem_block* next; /*!< Next free memory block on linked list.
Set to \ref LWMEM_BLOCK_ALLOC_MARK when block is allocated and in use */
struct lwmem_block*
next; /*!< Next free memory block on linked list. Set to \ref LWMEM_BLOCK_ALLOC_MARK when block is allocated and in use */
size_t size; /*!< Size of block, including metadata part.
MSB bit is set to `1` when block is allocated and in use,
or `0` when block is considered free */
@ -72,7 +72,8 @@ typedef struct lwmem_block {
typedef struct {
uint32_t mem_size_bytes; /*!< Total memory size of all regions combined */
uint32_t mem_available_bytes; /*!< Free memory available for allocation */
uint32_t minimum_ever_mem_available_bytes; /*!< Minimum amount of total free memory there has been in the heap since the system booted. */
uint32_t
minimum_ever_mem_available_bytes; /*!< Minimum amount of total free memory there has been in the heap since the system booted. */
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;

View File

@ -43,7 +43,6 @@
* \{
*/
namespace Lwmem {
/**
@ -67,10 +66,7 @@ class LwmemLight {
public:
LwmemLight() {
/* Simple region descriptor with one region */
const lwmem_region_t regions[] = {
{m_reg_data, sizeof(m_reg_data)},
{NULL, 0}
};
const lwmem_region_t regions[] = {{m_reg_data, sizeof(m_reg_data)}, {NULL, 0}};
lwmem_assignmem_ex(&m_lw, regions);
}
@ -130,7 +126,7 @@ private:
uint8_t m_reg_data[LEN];
};
};
}; // namespace Lwmem
/**
* \}

View File

@ -34,8 +34,8 @@
#ifndef LWMEM_HDR_SYS_H
#define LWMEM_HDR_SYS_H
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
#include "lwmem/lwmem.h"
#ifdef __cplusplus

View File

@ -31,9 +31,9 @@
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v2.0.0
*/
#include "lwmem/lwmem.h"
#include <limits.h>
#include <string.h>
#include "lwmem/lwmem.h"
#if LWMEM_CFG_OS
#include "system/lwmem_sys.h"
@ -90,13 +90,21 @@
* \brief Set block as allocated
* \param[in] block: Block to set as allocated
*/
#define LWMEM_BLOCK_SET_ALLOC(block) do { if ((block) != NULL) { (block)->size |= LWMEM_ALLOC_BIT; (block)->next = (void *)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK); }} while (0)
#define LWMEM_BLOCK_SET_ALLOC(block) \
do { \
if ((block) != NULL) { \
(block)->size |= LWMEM_ALLOC_BIT; \
(block)->next = (void*)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK); \
} \
} while (0)
/**
* \brief Check if input block is properly allocated and valid
* \param[in] block: Block to check if properly set as allocated
*/
#define LWMEM_BLOCK_IS_ALLOC(block) ((block) != NULL && ((block)->size & LWMEM_ALLOC_BIT) && (block)->next == (void *)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK))
#define LWMEM_BLOCK_IS_ALLOC(block) \
((block) != NULL && ((block)->size & LWMEM_ALLOC_BIT) \
&& (block)->next == (void*)(LWMEM_TO_BYTE_PTR(0) + LWMEM_BLOCK_ALLOC_MARK))
/**
* \brief Get block handle from application pointer
@ -108,7 +116,8 @@
* \brief Get block handle from application pointer
* \param[in] block: Input pointer to get block from
*/
#define LWMEM_GET_PTR_FROM_BLOCK(block) (void *)((block) != NULL ? ((LWMEM_TO_BYTE_PTR(block)) + LWMEM_BLOCK_META_SIZE) : NULL)
#define LWMEM_GET_PTR_FROM_BLOCK(block) \
(void*)((block) != NULL ? ((LWMEM_TO_BYTE_PTR(block)) + LWMEM_BLOCK_META_SIZE) : NULL)
/**
* \brief Minimum amount of memory required to make new empty block
@ -130,11 +139,10 @@
* \param[in] in_pp: Previous previous of input block
* \param[in] in_p: Previous of input block
*/
#define LWMEM_GET_PREV_CURR_OF_BLOCK(in_lw, in_b, in_pp, in_p) do { \
for ((in_pp) = NULL, (in_p) = &((in_lw)->start_block); \
(in_p) != NULL && (in_p)->next < (in_b); \
(in_pp) = (in_p), (in_p) = (in_p)->next \
) {} \
#define LWMEM_GET_PREV_CURR_OF_BLOCK(in_lw, in_b, in_pp, in_p) \
do { \
for ((in_pp) = NULL, (in_p) = &((in_lw)->start_block); (in_p) != NULL && (in_p)->next < (in_b); \
(in_pp) = (in_p), (in_p) = (in_p)->next) {} \
} while (0)
#if LWMEM_CFG_OS
@ -148,7 +156,8 @@
/* Statistics part */
#if LWMEM_CFG_ENABLE_STATS
#define LWMEM_INC_STATS(field) (++(field))
#define LWMEM_UPDATE_MIN_FREE(lw) do { \
#define LWMEM_UPDATE_MIN_FREE(lw) \
do { \
if ((lw)->mem_available_bytes < (lw)->stats.minimum_ever_mem_available_bytes) { \
(lw)->stats.minimum_ever_mem_available_bytes = (lw)->mem_available_bytes; \
} \
@ -276,7 +285,9 @@ prv_insert_free_block(lwmem_t* const lw, lwmem_block_t* nb) {
if (prev->next == lw->end_block) { /* Does it points to the end? */
nb->next = lw->end_block; /* Set end block pointer */
} else {
nb->size += prev->next->size; /* Expand of current block for size of next free block which is right behind new block */
nb->size +=
prev->next
->size; /* Expand of current block for size of next free block which is right behind new block */
nb->next = prev->next->next; /* Next free is pointed to the next one of previous next */
}
} else {
@ -468,7 +479,8 @@ static void*
prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, const size_t size) {
lwmem_block_t *block, *prevprev, *prev;
size_t block_size; /* Holds size of input block (ptr), including metadata size */
const size_t final_size = LWMEM_ALIGN(size) + LWMEM_BLOCK_META_SIZE;/* Holds size of new requested block size, including metadata size */
const size_t final_size =
LWMEM_ALIGN(size) + LWMEM_BLOCK_META_SIZE; /* Holds size of new requested block size, including metadata size */
void* retval; /* Return pointer, used with LWMEM_RETURN macro */
/* Check optional input parameters */
@ -540,7 +552,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
prev->next = (void*)(LWMEM_TO_BYTE_PTR(prev->next) - (block_size - final_size));
prev->next->size = tmp_size + (block_size - final_size);
prev->next->next = tmp_next;
lw->mem_available_bytes += block_size - final_size; /* Increase available bytes by increase of free block */
lw->mem_available_bytes +=
block_size - final_size; /* Increase available bytes by increase of free block */
block->size = final_size; /* Block size is requested size */
}
@ -564,7 +577,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
/* Input block points to address somewhere between "prev" and "prev->next" pointers */
/* Check if "block" and next free "prev->next" create contiguous memory with size of at least new requested size */
if ((LWMEM_TO_BYTE_PTR(block) + block_size) == LWMEM_TO_BYTE_PTR(prev->next)/* Blocks create contiguous block */
if ((LWMEM_TO_BYTE_PTR(block) + block_size)
== LWMEM_TO_BYTE_PTR(prev->next) /* Blocks create contiguous block */
&& (block_size + prev->next->size) >= final_size) { /* Size is greater or equal to requested */
/*
@ -574,7 +588,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
lw->mem_available_bytes -= prev->next->size; /* For now decrease effective available bytes */
LWMEM_UPDATE_MIN_FREE(lw);
block->size = block_size + prev->next->size; /* Increase effective size of new block */
prev->next = prev->next->next; /* Set next to next's next, effectively remove expanded block from free list */
prev->next =
prev->next->next; /* Set next to next's next, effectively remove expanded block from free list */
prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */
LWMEM_BLOCK_SET_ALLOC(block); /* Set block as allocated */
@ -605,7 +620,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
lw->mem_available_bytes -= prev->size; /* For now decrease effective available bytes */
LWMEM_UPDATE_MIN_FREE(lw);
prev->size += block_size; /* Increase size of input block size */
prevprev->next = prev->next; /* Remove prev from free list as it is now being used for allocation together with existing block */
prevprev->next =
prev->next; /* Remove prev from free list as it is now being used for allocation together with existing block */
block = prev; /* Move block pointer to previous one */
prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */
@ -621,8 +637,10 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
* Last option is to check if previous free block "prev", input block "block" and next free block "prev->next" create contiguous block
* and size of new block (from 3 contiguous blocks) together is big enough
*/
if ((LWMEM_TO_BYTE_PTR(prev) + prev->size) == LWMEM_TO_BYTE_PTR(block) /* Input block and free block before create contiguous block */
&& (LWMEM_TO_BYTE_PTR(block) + block_size) == LWMEM_TO_BYTE_PTR(prev->next) /* Input block and free block after create contiguous block */
if ((LWMEM_TO_BYTE_PTR(prev) + prev->size)
== LWMEM_TO_BYTE_PTR(block) /* Input block and free block before create contiguous block */
&& (LWMEM_TO_BYTE_PTR(block) + block_size)
== LWMEM_TO_BYTE_PTR(prev->next) /* Input block and free block after create contiguous block */
&& (prev->size + block_size + prev->next->size) >= final_size) { /* Size is greater or equal to requested */
/* Move memory from block to block previous to current */
@ -639,10 +657,15 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
*/
LWMEM_MEMMOVE(new_data_ptr, old_data_ptr, block_size); /* Copy old buffer size to new location */
lw->mem_available_bytes -= prev->size + prev->next->size; /* Decrease effective available bytes for free blocks before and after input block */
lw->mem_available_bytes -=
prev->size
+ prev->next
->size; /* Decrease effective available bytes for free blocks before and after input block */
LWMEM_UPDATE_MIN_FREE(lw);
prev->size += block_size + prev->next->size; /* Increase size of new block by size of 2 free blocks */
prevprev->next = prev->next->next; /* Remove free block before current one and block after current one from linked list (remove 2) */
prevprev->next =
prev->next
->next; /* Remove free block before current one and block after current one from linked list (remove 2) */
block = prev; /* Previous block is now current */
prv_split_too_big_block(lw, block, final_size); /* Split block if it is too big */
@ -665,7 +688,8 @@ prv_realloc(lwmem_t* const lw, const lwmem_region_t* region, void* const ptr, co
*/
retval = prv_alloc(lw, region, size); /* Try to allocate new block */
if (retval != NULL) {
block_size = (block->size & ~LWMEM_ALLOC_BIT) - LWMEM_BLOCK_META_SIZE; /* Get application size from input pointer */
block_size =
(block->size & ~LWMEM_ALLOC_BIT) - LWMEM_BLOCK_META_SIZE; /* Get application size from input pointer */
LWMEM_MEMCPY(retval, ptr, size > block_size ? block_size : size); /* Copy content to new allocated block */
prv_free(lw, ptr); /* Free input pointer */
}
@ -699,8 +723,7 @@ lwmem_assignmem_ex(lwmem_t* lw, const lwmem_region_t* regions) {
lw = LWMEM_GET_LW(lw);
/* Check first things first */
if (regions == NULL
|| lw->end_block != NULL /* Init function may only be called once per lwmem instance */
if (regions == NULL || 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;
}
@ -1082,11 +1105,7 @@ print_block(size_t i, lwmem_block_t* block) {
is_free = (block->size & LWMEM_ALLOC_BIT) == 0 && block != &lwmem_default.start_block_first_use && block->size > 0;
block_size = block->size & ~LWMEM_ALLOC_BIT;
printf("| %5d | %16p | %6d | %4d | %16d |",
(int)i,
(void*)block,
(int)is_free,
(int)block_size,
printf("| %5d | %16p | %6d | %4d | %16d |", (int)i, (void*)block, (int)is_free, (int)block_size,
(int)(is_free ? (block_size - LWMEM_BLOCK_META_SIZE) : 0));
if (block == &lwmem_default.start_block_first_use) {
printf(" Start block ");