From 4b17048a2e72369f95bd374dfa3368843ff36d8f Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Tue, 31 Oct 2017 08:32:16 +0000 Subject: Fixed bug #899. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10911 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/common/oslib/src/chheap.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'os/common/oslib/src/chheap.c') diff --git a/os/common/oslib/src/chheap.c b/os/common/oslib/src/chheap.c index ee21b802c..2e8e4a05f 100644 --- a/os/common/oslib/src/chheap.c +++ b/os/common/oslib/src/chheap.c @@ -118,8 +118,9 @@ void _heap_init(void) { /** * @brief Initializes a memory heap from a static memory area. - * @pre Both the heap buffer base and the heap size must be aligned to - * the @p heap_header_t type size. + * @note The heap buffer base and size are adjusted if the passed buffer + * is not aligned to @p CH_HEAP_ALIGNMENT. This mean that the + * effective heap size can be less than @p size. * * @param[out] heapp pointer to the memory heap descriptor to be initialized * @param[in] buf heap buffer base @@ -128,12 +129,15 @@ void _heap_init(void) { * @init */ void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size) { - heap_header_t *hp = buf; + heap_header_t *hp = (heap_header_t *)MEM_ALIGN_NEXT(buf, CH_HEAP_ALIGNMENT); - chDbgCheck((heapp != NULL) && (size > 0U) && - MEM_IS_ALIGNED(buf, CH_HEAP_ALIGNMENT) && - MEM_IS_ALIGNED(size, CH_HEAP_ALIGNMENT)); + chDbgCheck((heapp != NULL) && (size > 0U)); + /* Adjusting the size in case the initial block was not correctly + aligned.*/ + size -= (size_t)((uint8_t *)hp - (uint8_t *)buf); + + /* Initializing the heap header.*/ heapp->provider = NULL; H_NEXT(&heapp->header) = hp; H_PAGES(&heapp->header) = 0; -- cgit v1.2.3