From 1ea7355d85e316aadfd90468b3e808bb3dc95ee9 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 16 Aug 2009 13:07:24 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1073 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 276 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 os/kernel/src/chheap.c (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c new file mode 100644 index 000000000..82b1ba785 --- /dev/null +++ b/os/kernel/src/chheap.c @@ -0,0 +1,276 @@ +/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file chheap.c + * @brief Heap code. + * @addtogroup Heap + * @{ + */ + +#include + +#if CH_USE_HEAP + +#if !CH_USE_MALLOC_HEAP + +#define MAGIC 0xF5A0 +#define ALIGN_TYPE void * +#define ALIGN_MASK (sizeof(ALIGN_TYPE) - 1) +#define ALIGN_SIZE(p) (((size_t)(p) + ALIGN_MASK) & ~ALIGN_MASK) + +struct header { + union { + struct header *h_next; + size_t h_magic; + }; + size_t h_size; +}; + +static struct { + struct header free; /* Guaranteed to be not adjacent to the heap */ +#if CH_USE_MUTEXES +#define H_LOCK() chMtxLock(&heap.hmtx) +#define H_UNLOCK() chMtxUnlock() + Mutex hmtx; +#elif CH_USE_SEMAPHORES +#define H_LOCK() chSemWait(&heap.hsem) +#define H_UNLOCK() chSemSignal(&heap.hsem) + Semaphore hsem; +#else +#error "The heap allocator requires mutexes or semaphores to be enabled" +#endif +#if CH_HEAP_SIZE > 0 + union { + ALIGN_TYPE alignment; + char buffer[ALIGN_SIZE(CH_HEAP_SIZE)]; + }; +#endif +} heap; + +/** + * @brief Initializes the allocator subsystem. + * + * @note Internal use only. + */ +void heap_init(void) { + struct header *hp; + +#if CH_HEAP_SIZE == 0 + extern char __heap_base__; + extern char __heap_end__; + + hp = (void *)&__heap_base__; + hp->h_size = &__heap_end__ - &__heap_base__ - sizeof(struct header); +#else + hp = (void *)&heap.buffer[0]; + hp->h_size = (&heap.buffer[ALIGN_SIZE(CH_HEAP_SIZE)] - &heap.buffer[0]) - + sizeof(struct header); +#endif + hp->h_next = NULL; + heap.free.h_next = hp; + heap.free.h_size = 0; +#if CH_USE_MUTEXES + chMtxInit(&heap.hmtx); +#else + chSemInit(&heap.hsem, 1); +#endif +} + +/** + * @brief Allocates a block of memory from the heap by using the first-fit + * algorithm. + * @details The allocated block is guaranteed to be properly aligned for a + * pointer data type. + * + * @param[in] size the size of the block to be allocated. Note that the + * allocated block may be a bit bigger than the requested + * size for alignment and fragmentation reasons. + * @return A pointer to the allocated block. + * @retval NULL if the block cannot be allocated. + */ +void *chHeapAlloc(size_t size) { + struct header *qp, *hp, *fp; + + size = ALIGN_SIZE(size); + qp = &heap.free; + H_LOCK(); + + while (qp->h_next != NULL) { + hp = qp->h_next; + if (hp->h_size >= size) { + if (hp->h_size < size + sizeof(struct header)) { + /* Gets the whole block even if it is slightly bigger than the + requested size because the fragment would be too small to be + useful */ + qp->h_next = hp->h_next; + } + else { + /* Block bigger enough, must split it */ + fp = (void *)((char *)(hp) + sizeof(struct header) + size); + fp->h_next = hp->h_next; + fp->h_size = hp->h_size - sizeof(struct header) - size; + qp->h_next = fp; + hp->h_size = size; + } + hp->h_magic = MAGIC; + + H_UNLOCK(); + return (void *)(hp + 1); + } + qp = hp; + } + + H_UNLOCK(); + return NULL; +} + +#define LIMIT(p) (struct header *)((char *)(p) + \ + sizeof(struct header) + \ + (p)->h_size) + +/** + * @brief Frees a previously allocated memory block. + * + * @param[in] p the memory block pointer + */ +void chHeapFree(void *p) { + struct header *qp, *hp; + + chDbgCheck(p != NULL, "chHeapFree"); + + hp = (struct header *)p - 1; + chDbgAssert(hp->h_magic == MAGIC, + "chHeapFree(), #1", + "it is not magic"); + qp = &heap.free; + H_LOCK(); + + while (TRUE) { + + chDbgAssert((hp < qp) || (hp >= LIMIT(qp)), + "chHeapFree(), #2", + "within free block"); + + if (((qp == &heap.free) || (hp > qp)) && + ((qp->h_next == NULL) || (hp < qp->h_next))) { + /* Insertion after qp */ + hp->h_next = qp->h_next; + qp->h_next = hp; + /* Verifies if the newly inserted block should be merged */ + if (LIMIT(hp) == hp->h_next) { + /* Merge with the next block */ + hp->h_size += hp->h_next->h_size + sizeof(struct header); + hp->h_next = hp->h_next->h_next; + } + if ((LIMIT(qp) == hp)) { /* Cannot happen when qp == &heap.free */ + /* Merge with the previous block */ + qp->h_size += hp->h_size + sizeof(struct header); + qp->h_next = hp->h_next; + } + + H_UNLOCK(); + return; + } + qp = qp->h_next; + } +} + +/** + * @brief Reports the heap status. + * + * @param[in] sizep pointer to a variable that will receive the total + * fragmented free space + * @return The number of fragments in the heap. + * @note This function is meant to be used in the test suite, it should not be + * really useful for the application code. + * @note This function is not implemented when the @p CH_USE_MALLOC_HEAP + * configuration option is used (it always returns zero). + */ +size_t chHeapStatus(size_t *sizep) { + struct header *qp; + size_t n, sz; + + H_LOCK(); + + sz = 0; + for (n = 0, qp = &heap.free; qp->h_next; n++, qp = qp->h_next) + sz += qp->h_next->h_size; + if (sizep) + *sizep = sz; + + H_UNLOCK(); + return n; +} + +#else /* CH_USE_MALLOC_HEAP */ + +#include + +#if CH_USE_MUTEXES +#define H_LOCK() chMtxLock(&hmtx) +#define H_UNLOCK() chMtxLock(&hmtx) +static Mutex hmtx; +#elif CH_USE_SEMAPHORES +#define H_LOCK() chSemWait(&hsem) +#define H_UNLOCK() chSemSignal(&hsem) +static Semaphore hsem; +#else +#error "The heap allocator requires mutexes or semaphores to be enabled" +#endif + +void heap_init(void) { + +#if CH_USE_MUTEXES + chMtxInit(&hmtx); +#else + chSemInit(&hsem, 1); +#endif +} + +void *chHeapAlloc(size_t size) { + void *p; + + H_LOCK(); + p = malloc(size); + H_UNLOCK(); + return p; +} + +void chHeapFree(void *p) { + + chDbgCheck(p != NULL, "chHeapFree"); + + H_LOCK(); + free(p); + H_UNLOCK(); +} + +size_t chHeapStatus(size_t *sizep) { + + if (sizep) + *sizep = 0; + return 0; +} + +#endif /* CH_USE_MALLOC_HEAP */ + +#endif /* CH_USE_HEAP */ + +/** @} */ -- cgit v1.2.3 From 397ccffac55ffd139d0e3e82add83e51413c1347 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 30 Aug 2009 06:59:43 +0000 Subject: Documentation reorganization. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1133 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 82b1ba785..b5ad50505 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -20,7 +20,7 @@ /** * @file chheap.c * @brief Heap code. - * @addtogroup Heap + * @addtogroup heap * @{ */ -- cgit v1.2.3 From 2c46df1916a25b7880416aee974a518cc607717a Mon Sep 17 00:00:00 2001 From: gdisirio Date: Fri, 16 Oct 2009 17:45:19 +0000 Subject: New heap manager. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1221 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 222 ++++++++++++++++++++++++++++--------------------- 1 file changed, 127 insertions(+), 95 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index b5ad50505..e7a975f3d 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -28,41 +28,23 @@ #if CH_USE_HEAP -#if !CH_USE_MALLOC_HEAP - -#define MAGIC 0xF5A0 -#define ALIGN_TYPE void * -#define ALIGN_MASK (sizeof(ALIGN_TYPE) - 1) -#define ALIGN_SIZE(p) (((size_t)(p) + ALIGN_MASK) & ~ALIGN_MASK) - -struct header { - union { - struct header *h_next; - size_t h_magic; - }; - size_t h_size; -}; - -static struct { - struct header free; /* Guaranteed to be not adjacent to the heap */ +/* + * Defaults on the best synchronization mechanism available. + */ #if CH_USE_MUTEXES -#define H_LOCK() chMtxLock(&heap.hmtx) -#define H_UNLOCK() chMtxUnlock() - Mutex hmtx; -#elif CH_USE_SEMAPHORES -#define H_LOCK() chSemWait(&heap.hsem) -#define H_UNLOCK() chSemSignal(&heap.hsem) - Semaphore hsem; +#define H_LOCK(h) chMtxLock(&(h)->h_mtx) +#define H_UNLOCK(h) chMtxUnlock() #else -#error "The heap allocator requires mutexes or semaphores to be enabled" +#define H_LOCK(h) chSemWait(&(h)->h_sem) +#define H_UNLOCK(h) chSemSignal(&(h)->h_sem) #endif -#if CH_HEAP_SIZE > 0 - union { - ALIGN_TYPE alignment; - char buffer[ALIGN_SIZE(CH_HEAP_SIZE)]; - }; -#endif -} heap; + +#if !CH_USE_MALLOC_HEAP + +/** + * @brief Default heap descriptor. + */ +static MemoryHeap default_heap; /** * @brief Initializes the allocator subsystem. @@ -70,26 +52,40 @@ static struct { * @note Internal use only. */ void heap_init(void) { - struct header *hp; - -#if CH_HEAP_SIZE == 0 - extern char __heap_base__; - extern char __heap_end__; - - hp = (void *)&__heap_base__; - hp->h_size = &__heap_end__ - &__heap_base__ - sizeof(struct header); + default_heap.h_provider = chCoreAlloc; + default_heap.h_free.h_next = NULL; + default_heap.h_free.h_size = 0; +#if CH_USE_MUTEXES + chMtxInit(&default_heap.h_mtx); #else - hp = (void *)&heap.buffer[0]; - hp->h_size = (&heap.buffer[ALIGN_SIZE(CH_HEAP_SIZE)] - &heap.buffer[0]) - - sizeof(struct header); + chSemInit(&default_heap.h_sem, 1); #endif +} + +/** + * @brief Initializes a memory heap. + * + * @param[out] heapp pointer to a memory heap descriptor to be initialized + * @param[in] buf heap buffer base + * @param[in] size heap size + * + * @note Both the heap buffer base and the heap size must be aligned to + * the @p align_t type size. + */ +void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { + struct heap_header *hp; + + chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit"); + + heapp->h_provider = NULL; + heapp->h_free.h_next = hp = buf; + heapp->h_free.h_size = 0; hp->h_next = NULL; - heap.free.h_next = hp; - heap.free.h_size = 0; + hp->h_size = size - sizeof(struct heap_header); #if CH_USE_MUTEXES - chMtxInit(&heap.hmtx); + chMtxInit(&heapp->h_mtx); #else - chSemInit(&heap.hsem, 1); + chSemInit(&heapp->h_sem, 1); #endif } @@ -97,53 +93,75 @@ void heap_init(void) { * @brief Allocates a block of memory from the heap by using the first-fit * algorithm. * @details The allocated block is guaranteed to be properly aligned for a - * pointer data type. + * pointer data type (@p align_t). * + * @param[in] heapp pointer to a heap descriptor or @p NULL in order to access + * the default heap. * @param[in] size the size of the block to be allocated. Note that the * allocated block may be a bit bigger than the requested * size for alignment and fragmentation reasons. * @return A pointer to the allocated block. * @retval NULL if the block cannot be allocated. */ -void *chHeapAlloc(size_t size) { - struct header *qp, *hp, *fp; +void *chHeapAlloc(MemoryHeap *heapp, size_t size) { + struct heap_header *qp, *hp, *fp; - size = ALIGN_SIZE(size); - qp = &heap.free; - H_LOCK(); + if (heapp == NULL) + heapp = &default_heap; + + size = MEM_ALIGN_SIZE(size); + qp = &heapp->h_free; + H_LOCK(heapp); while (qp->h_next != NULL) { hp = qp->h_next; if (hp->h_size >= size) { - if (hp->h_size < size + sizeof(struct header)) { - /* Gets the whole block even if it is slightly bigger than the - requested size because the fragment would be too small to be - useful */ + if (hp->h_size < size + sizeof(struct heap_header)) { + /* + * Gets the whole block even if it is slightly bigger than the + * requested size because the fragment would be too small to be + * useful. + */ qp->h_next = hp->h_next; } else { - /* Block bigger enough, must split it */ - fp = (void *)((char *)(hp) + sizeof(struct header) + size); + /* + * Block bigger enough, must split it. + */ + fp = (void *)((uint8_t *)(hp) + sizeof(struct heap_header) + size); fp->h_next = hp->h_next; - fp->h_size = hp->h_size - sizeof(struct header) - size; + fp->h_size = hp->h_size - sizeof(struct heap_header) - size; qp->h_next = fp; hp->h_size = size; } - hp->h_magic = MAGIC; + hp->h_heap = heapp; - H_UNLOCK(); + H_UNLOCK(heapp); return (void *)(hp + 1); } qp = hp; } - H_UNLOCK(); + H_UNLOCK(heapp); + + /* + * More memory is required, tries to get it from the associated provider. + */ + if (heapp->h_provider) { + hp = heapp->h_provider(size + sizeof(struct heap_header)); + if (hp != NULL) { + hp->h_heap = heapp; + hp->h_size = size; + hp++; + return (void *)hp; + } + } return NULL; } -#define LIMIT(p) (struct header *)((char *)(p) + \ - sizeof(struct header) + \ - (p)->h_size) +#define LIMIT(p) (struct heap_header *)((uint8_t *)(p) + \ + sizeof(struct heap_header) + \ + (p)->h_size) /** * @brief Frees a previously allocated memory block. @@ -151,50 +169,59 @@ void *chHeapAlloc(size_t size) { * @param[in] p the memory block pointer */ void chHeapFree(void *p) { - struct header *qp, *hp; + struct heap_header *qp, *hp; + MemoryHeap *heapp; chDbgCheck(p != NULL, "chHeapFree"); - hp = (struct header *)p - 1; - chDbgAssert(hp->h_magic == MAGIC, - "chHeapFree(), #1", - "it is not magic"); - qp = &heap.free; - H_LOCK(); + hp = (struct heap_header *)p - 1; + heapp = hp->h_heap; + qp = &heapp->h_free; + H_LOCK(heapp); while (TRUE) { - chDbgAssert((hp < qp) || (hp >= LIMIT(qp)), - "chHeapFree(), #2", + "chHeapFree(), #1", "within free block"); - if (((qp == &heap.free) || (hp > qp)) && + if (((qp == &heapp->h_free) || (hp > qp)) && ((qp->h_next == NULL) || (hp < qp->h_next))) { - /* Insertion after qp */ + /* + * Insertion after qp. + */ hp->h_next = qp->h_next; qp->h_next = hp; - /* Verifies if the newly inserted block should be merged */ + /* + * Verifies if the newly inserted block should be merged. + */ if (LIMIT(hp) == hp->h_next) { - /* Merge with the next block */ - hp->h_size += hp->h_next->h_size + sizeof(struct header); + /* + * Merge with the next block. + */ + hp->h_size += hp->h_next->h_size + sizeof(struct heap_header); hp->h_next = hp->h_next->h_next; } - if ((LIMIT(qp) == hp)) { /* Cannot happen when qp == &heap.free */ - /* Merge with the previous block */ - qp->h_size += hp->h_size + sizeof(struct header); + if ((LIMIT(qp) == hp)) { + /* + * Merge with the previous block. + */ + qp->h_size += hp->h_size + sizeof(struct heap_header); qp->h_next = hp->h_next; } - - H_UNLOCK(); - return; + break; } qp = qp->h_next; } + + H_UNLOCK(heapp); + return; } /** * @brief Reports the heap status. * + * @param[in] heapp pointer to a heap descriptor or @p NULL in order to access + * the default heap. * @param[in] sizep pointer to a variable that will receive the total * fragmented free space * @return The number of fragments in the heap. @@ -203,19 +230,22 @@ void chHeapFree(void *p) { * @note This function is not implemented when the @p CH_USE_MALLOC_HEAP * configuration option is used (it always returns zero). */ -size_t chHeapStatus(size_t *sizep) { - struct header *qp; +size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { + struct heap_header *qp; size_t n, sz; - H_LOCK(); + if (heapp == NULL) + heapp = &default_heap; + + H_LOCK(heapp); sz = 0; - for (n = 0, qp = &heap.free; qp->h_next; n++, qp = qp->h_next) + for (n = 0, qp = &heapp->h_free; qp->h_next; n++, qp = qp->h_next) sz += qp->h_next->h_size; if (sizep) *sizep = sz; - H_UNLOCK(); + H_UNLOCK(heapp); return n; } @@ -231,8 +261,6 @@ static Mutex hmtx; #define H_LOCK() chSemWait(&hsem) #define H_UNLOCK() chSemSignal(&hsem) static Semaphore hsem; -#else -#error "The heap allocator requires mutexes or semaphores to be enabled" #endif void heap_init(void) { @@ -244,9 +272,11 @@ void heap_init(void) { #endif } -void *chHeapAlloc(size_t size) { +void *chHeapAlloc(MemoryHeap *heapp, size_t size) { void *p; + chDbgCheck(heapp == NULL, "chHeapAlloc"); + H_LOCK(); p = malloc(size); H_UNLOCK(); @@ -262,7 +292,9 @@ void chHeapFree(void *p) { H_UNLOCK(); } -size_t chHeapStatus(size_t *sizep) { +size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { + + chDbgCheck(heapp == NULL, "chHeapStatus"); if (sizep) *sizep = 0; -- cgit v1.2.3 From 7d689a5fae4f0950c8cb8e291744465ea5d2ca93 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 20 Oct 2009 17:26:27 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1242 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index e7a975f3d..d1dab1580 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -19,8 +19,8 @@ /** * @file chheap.c - * @brief Heap code. - * @addtogroup heap + * @brief Heaps code. + * @addtogroup heaps * @{ */ @@ -47,7 +47,7 @@ static MemoryHeap default_heap; /** - * @brief Initializes the allocator subsystem. + * @brief Initializes the default heap. * * @note Internal use only. */ @@ -63,7 +63,7 @@ void heap_init(void) { } /** - * @brief Initializes a memory heap. + * @brief Initializes a memory heap from a static memory area. * * @param[out] heapp pointer to a memory heap descriptor to be initialized * @param[in] buf heap buffer base -- cgit v1.2.3 From 43f9fd4180030081daae9122bd57a521ec9c58e1 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Fri, 30 Oct 2009 15:45:38 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1258 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index d1dab1580..203d69d13 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -255,7 +255,7 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { #if CH_USE_MUTEXES #define H_LOCK() chMtxLock(&hmtx) -#define H_UNLOCK() chMtxLock(&hmtx) +#define H_UNLOCK() chMtxUnock() static Mutex hmtx; #elif CH_USE_SEMAPHORES #define H_LOCK() chSemWait(&hsem) -- cgit v1.2.3 From bdb7f4ab20bd3daf261ab93dfe733e0ff11dca0f Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 8 Dec 2009 17:37:49 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1397 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 203d69d13..d9d88d78b 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -24,7 +24,7 @@ * @{ */ -#include +#include "ch.h" #if CH_USE_HEAP -- cgit v1.2.3 From 86eb39129b8ac6ba74778c72261f2048c2da1114 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 24 Jan 2010 09:04:55 +0000 Subject: Changes suggested by Adamo and Enrico. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1543 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index d9d88d78b..e434fd2ab 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -53,7 +53,7 @@ static MemoryHeap default_heap; */ void heap_init(void) { default_heap.h_provider = chCoreAlloc; - default_heap.h_free.h_next = NULL; + default_heap.h_free.h_u.next = (struct heap_header *)NULL; default_heap.h_free.h_size = 0; #if CH_USE_MUTEXES chMtxInit(&default_heap.h_mtx); @@ -70,17 +70,17 @@ void heap_init(void) { * @param[in] size heap size * * @note Both the heap buffer base and the heap size must be aligned to - * the @p align_t type size. + * the @p align_t type size. */ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { struct heap_header *hp; chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit"); - heapp->h_provider = NULL; - heapp->h_free.h_next = hp = buf; + heapp->h_provider = (memgetfunc_t)NULL; + heapp->h_free.h_u.next = hp = buf; heapp->h_free.h_size = 0; - hp->h_next = NULL; + hp->h_u.next = NULL; hp->h_size = size - sizeof(struct heap_header); #if CH_USE_MUTEXES chMtxInit(&heapp->h_mtx); @@ -113,8 +113,8 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { qp = &heapp->h_free; H_LOCK(heapp); - while (qp->h_next != NULL) { - hp = qp->h_next; + while (qp->h_u.next != NULL) { + hp = qp->h_u.next; if (hp->h_size >= size) { if (hp->h_size < size + sizeof(struct heap_header)) { /* @@ -122,19 +122,19 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { * requested size because the fragment would be too small to be * useful. */ - qp->h_next = hp->h_next; + qp->h_u.next = hp->h_u.next; } else { /* * Block bigger enough, must split it. */ fp = (void *)((uint8_t *)(hp) + sizeof(struct heap_header) + size); - fp->h_next = hp->h_next; + fp->h_u.next = hp->h_u.next; fp->h_size = hp->h_size - sizeof(struct heap_header) - size; - qp->h_next = fp; + qp->h_u.next = fp; hp->h_size = size; } - hp->h_heap = heapp; + hp->h_u.heap = heapp; H_UNLOCK(heapp); return (void *)(hp + 1); @@ -150,7 +150,7 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { if (heapp->h_provider) { hp = heapp->h_provider(size + sizeof(struct heap_header)); if (hp != NULL) { - hp->h_heap = heapp; + hp->h_u.heap = heapp; hp->h_size = size; hp++; return (void *)hp; @@ -175,7 +175,7 @@ void chHeapFree(void *p) { chDbgCheck(p != NULL, "chHeapFree"); hp = (struct heap_header *)p - 1; - heapp = hp->h_heap; + heapp = hp->h_u.heap; qp = &heapp->h_free; H_LOCK(heapp); @@ -185,32 +185,32 @@ void chHeapFree(void *p) { "within free block"); if (((qp == &heapp->h_free) || (hp > qp)) && - ((qp->h_next == NULL) || (hp < qp->h_next))) { + ((qp->h_u.next == NULL) || (hp < qp->h_u.next))) { /* * Insertion after qp. */ - hp->h_next = qp->h_next; - qp->h_next = hp; + hp->h_u.next = qp->h_u.next; + qp->h_u.next = hp; /* * Verifies if the newly inserted block should be merged. */ - if (LIMIT(hp) == hp->h_next) { + if (LIMIT(hp) == hp->h_u.next) { /* * Merge with the next block. */ - hp->h_size += hp->h_next->h_size + sizeof(struct heap_header); - hp->h_next = hp->h_next->h_next; + hp->h_size += hp->h_u.next->h_size + sizeof(struct heap_header); + hp->h_u.next = hp->h_u.next->h_u.next; } if ((LIMIT(qp) == hp)) { /* * Merge with the previous block. */ qp->h_size += hp->h_size + sizeof(struct heap_header); - qp->h_next = hp->h_next; + qp->h_u.next = hp->h_u.next; } break; } - qp = qp->h_next; + qp = qp->h_u.next; } H_UNLOCK(heapp); @@ -240,8 +240,8 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { H_LOCK(heapp); sz = 0; - for (n = 0, qp = &heapp->h_free; qp->h_next; n++, qp = qp->h_next) - sz += qp->h_next->h_size; + for (n = 0, qp = &heapp->h_free; qp->h_u.next; n++, qp = qp->h_u.next) + sz += qp->h_u.next->h_size; if (sizep) *sizep = sz; -- cgit v1.2.3 From f17db1931e95f5ebb42f557b6eead2bf1320db5a Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 6 Feb 2010 10:55:53 +0000 Subject: Reformatted doxygen tags into the kernel sources to make them more readable. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1567 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 100 +++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 57 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index e434fd2ab..45431d5f4 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -18,8 +18,9 @@ */ /** - * @file chheap.c - * @brief Heaps code. + * @file chheap.c + * @brief Heaps code. + * * @addtogroup heaps * @{ */ @@ -42,14 +43,13 @@ #if !CH_USE_MALLOC_HEAP /** - * @brief Default heap descriptor. + * @brief Default heap descriptor. */ static MemoryHeap default_heap; /** - * @brief Initializes the default heap. - * - * @note Internal use only. + * @brief Initializes the default heap. + * @note Internal use only. */ void heap_init(void) { default_heap.h_provider = chCoreAlloc; @@ -63,14 +63,13 @@ void heap_init(void) { } /** - * @brief Initializes a memory heap from a static memory area. - * - * @param[out] heapp pointer to a memory heap descriptor to be initialized - * @param[in] buf heap buffer base - * @param[in] size heap size + * @brief Initializes a memory heap from a static memory area. + * @note Both the heap buffer base and the heap size must be aligned to + * the @p align_t type size. * - * @note Both the heap buffer base and the heap size must be aligned to - * the @p align_t type size. + * @param[out] heapp pointer to the memory heap descriptor to be initialized + * @param[in] buf heap buffer base + * @param[in] size heap size */ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { struct heap_header *hp; @@ -90,18 +89,18 @@ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { } /** - * @brief Allocates a block of memory from the heap by using the first-fit - * algorithm. + * @brief Allocates a block of memory from the heap by using the first-fit + * algorithm. * @details The allocated block is guaranteed to be properly aligned for a * pointer data type (@p align_t). * - * @param[in] heapp pointer to a heap descriptor or @p NULL in order to access - * the default heap. - * @param[in] size the size of the block to be allocated. Note that the - * allocated block may be a bit bigger than the requested - * size for alignment and fragmentation reasons. - * @return A pointer to the allocated block. - * @retval NULL if the block cannot be allocated. + * @param[in] heapp pointer to a heap descriptor or @p NULL in order to + * access the default heap. + * @param[in] size the size of the block to be allocated. Note that the + * allocated block may be a bit bigger than the requested + * size for alignment and fragmentation reasons. + * @return A pointer to the allocated block. + * @retval NULL if the block cannot be allocated. */ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { struct heap_header *qp, *hp, *fp; @@ -117,17 +116,13 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { hp = qp->h_u.next; if (hp->h_size >= size) { if (hp->h_size < size + sizeof(struct heap_header)) { - /* - * Gets the whole block even if it is slightly bigger than the - * requested size because the fragment would be too small to be - * useful. - */ + /* Gets the whole block even if it is slightly bigger than the + requested size because the fragment would be too small to be + useful.*/ qp->h_u.next = hp->h_u.next; } else { - /* - * Block bigger enough, must split it. - */ + /* Block bigger enough, must split it.*/ fp = (void *)((uint8_t *)(hp) + sizeof(struct heap_header) + size); fp->h_u.next = hp->h_u.next; fp->h_size = hp->h_size - sizeof(struct heap_header) - size; @@ -144,9 +139,8 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { H_UNLOCK(heapp); - /* - * More memory is required, tries to get it from the associated provider. - */ + /* More memory is required, tries to get it from the associated provider + else fails.*/ if (heapp->h_provider) { hp = heapp->h_provider(size + sizeof(struct heap_header)); if (hp != NULL) { @@ -164,9 +158,9 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { (p)->h_size) /** - * @brief Frees a previously allocated memory block. + * @brief Frees a previously allocated memory block. * - * @param[in] p the memory block pointer + * @param[in] p pointer to the memory block to be freed */ void chHeapFree(void *p) { struct heap_header *qp, *hp; @@ -186,25 +180,17 @@ void chHeapFree(void *p) { if (((qp == &heapp->h_free) || (hp > qp)) && ((qp->h_u.next == NULL) || (hp < qp->h_u.next))) { - /* - * Insertion after qp. - */ + /* Insertion after qp.*/ hp->h_u.next = qp->h_u.next; qp->h_u.next = hp; - /* - * Verifies if the newly inserted block should be merged. - */ + /* Verifies if the newly inserted block should be merged.*/ if (LIMIT(hp) == hp->h_u.next) { - /* - * Merge with the next block. - */ + /* Merge with the next block.*/ hp->h_size += hp->h_u.next->h_size + sizeof(struct heap_header); hp->h_u.next = hp->h_u.next->h_u.next; } if ((LIMIT(qp) == hp)) { - /* - * Merge with the previous block. - */ + /* Merge with the previous block.*/ qp->h_size += hp->h_size + sizeof(struct heap_header); qp->h_u.next = hp->h_u.next; } @@ -218,17 +204,17 @@ void chHeapFree(void *p) { } /** - * @brief Reports the heap status. + * @brief Reports the heap status. + * @note This function is meant to be used in the test suite, it should + * not be really useful for the application code. + * @note This function is not implemented when the @p CH_USE_MALLOC_HEAP + * configuration option is used (it always returns zero). * - * @param[in] heapp pointer to a heap descriptor or @p NULL in order to access - * the default heap. - * @param[in] sizep pointer to a variable that will receive the total - * fragmented free space - * @return The number of fragments in the heap. - * @note This function is meant to be used in the test suite, it should not be - * really useful for the application code. - * @note This function is not implemented when the @p CH_USE_MALLOC_HEAP - * configuration option is used (it always returns zero). + * @param[in] heapp pointer to a heap descriptor or @p NULL in order to + * access the default heap. + * @param[in] sizep pointer to a variable that will receive the total + * fragmented free space + * @return The number of fragments in the heap. */ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { struct heap_header *qp; -- cgit v1.2.3 From 0c85d7906646524a75121867ba02dd1bb809cd21 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 16 Feb 2010 19:21:42 +0000 Subject: Tentative fix for bug 2952961. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1619 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 80 +++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 45431d5f4..91b7d01bc 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -53,8 +53,8 @@ static MemoryHeap default_heap; */ void heap_init(void) { default_heap.h_provider = chCoreAlloc; - default_heap.h_free.h_u.next = (struct heap_header *)NULL; - default_heap.h_free.h_size = 0; + default_heap.h_free.h.u.next = (union heap_header *)NULL; + default_heap.h_free.h.size = 0; #if CH_USE_MUTEXES chMtxInit(&default_heap.h_mtx); #else @@ -72,15 +72,15 @@ void heap_init(void) { * @param[in] size heap size */ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { - struct heap_header *hp; + union heap_header *hp; chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit"); heapp->h_provider = (memgetfunc_t)NULL; - heapp->h_free.h_u.next = hp = buf; - heapp->h_free.h_size = 0; - hp->h_u.next = NULL; - hp->h_size = size - sizeof(struct heap_header); + heapp->h_free.h.u.next = hp = buf; + heapp->h_free.h.size = 0; + hp->h.u.next = NULL; + hp->h.size = size - sizeof(union heap_header); #if CH_USE_MUTEXES chMtxInit(&heapp->h_mtx); #else @@ -103,7 +103,7 @@ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { * @retval NULL if the block cannot be allocated. */ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { - struct heap_header *qp, *hp, *fp; + union heap_header *qp, *hp, *fp; if (heapp == NULL) heapp = &default_heap; @@ -112,24 +112,24 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { qp = &heapp->h_free; H_LOCK(heapp); - while (qp->h_u.next != NULL) { - hp = qp->h_u.next; - if (hp->h_size >= size) { - if (hp->h_size < size + sizeof(struct heap_header)) { + while (qp->h.u.next != NULL) { + hp = qp->h.u.next; + if (hp->h.size >= size) { + if (hp->h.size < size + sizeof(union heap_header)) { /* Gets the whole block even if it is slightly bigger than the requested size because the fragment would be too small to be useful.*/ - qp->h_u.next = hp->h_u.next; + qp->h.u.next = hp->h.u.next; } else { /* Block bigger enough, must split it.*/ - fp = (void *)((uint8_t *)(hp) + sizeof(struct heap_header) + size); - fp->h_u.next = hp->h_u.next; - fp->h_size = hp->h_size - sizeof(struct heap_header) - size; - qp->h_u.next = fp; - hp->h_size = size; + fp = (void *)((uint8_t *)(hp) + sizeof(union heap_header) + size); + fp->h.u.next = hp->h.u.next; + fp->h.size = hp->h.size - sizeof(union heap_header) - size; + qp->h.u.next = fp; + hp->h.size = size; } - hp->h_u.heap = heapp; + hp->h.u.heap = heapp; H_UNLOCK(heapp); return (void *)(hp + 1); @@ -142,10 +142,10 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { /* More memory is required, tries to get it from the associated provider else fails.*/ if (heapp->h_provider) { - hp = heapp->h_provider(size + sizeof(struct heap_header)); + hp = heapp->h_provider(size + sizeof(union heap_header)); if (hp != NULL) { - hp->h_u.heap = heapp; - hp->h_size = size; + hp->h.u.heap = heapp; + hp->h.size = size; hp++; return (void *)hp; } @@ -153,9 +153,9 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { return NULL; } -#define LIMIT(p) (struct heap_header *)((uint8_t *)(p) + \ - sizeof(struct heap_header) + \ - (p)->h_size) +#define LIMIT(p) (union heap_header *)((uint8_t *)(p) + \ + sizeof(union heap_header) + \ + (p)->h.size) /** * @brief Frees a previously allocated memory block. @@ -163,13 +163,13 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { * @param[in] p pointer to the memory block to be freed */ void chHeapFree(void *p) { - struct heap_header *qp, *hp; + union heap_header *qp, *hp; MemoryHeap *heapp; chDbgCheck(p != NULL, "chHeapFree"); - hp = (struct heap_header *)p - 1; - heapp = hp->h_u.heap; + hp = (union heap_header *)p - 1; + heapp = hp->h.u.heap; qp = &heapp->h_free; H_LOCK(heapp); @@ -179,24 +179,24 @@ void chHeapFree(void *p) { "within free block"); if (((qp == &heapp->h_free) || (hp > qp)) && - ((qp->h_u.next == NULL) || (hp < qp->h_u.next))) { + ((qp->h.u.next == NULL) || (hp < qp->h.u.next))) { /* Insertion after qp.*/ - hp->h_u.next = qp->h_u.next; - qp->h_u.next = hp; + hp->h.u.next = qp->h.u.next; + qp->h.u.next = hp; /* Verifies if the newly inserted block should be merged.*/ - if (LIMIT(hp) == hp->h_u.next) { + if (LIMIT(hp) == hp->h.u.next) { /* Merge with the next block.*/ - hp->h_size += hp->h_u.next->h_size + sizeof(struct heap_header); - hp->h_u.next = hp->h_u.next->h_u.next; + hp->h.size += hp->h.u.next->h.size + sizeof(union heap_header); + hp->h.u.next = hp->h.u.next->h.u.next; } if ((LIMIT(qp) == hp)) { /* Merge with the previous block.*/ - qp->h_size += hp->h_size + sizeof(struct heap_header); - qp->h_u.next = hp->h_u.next; + qp->h.size += hp->h.size + sizeof(union heap_header); + qp->h.u.next = hp->h.u.next; } break; } - qp = qp->h_u.next; + qp = qp->h.u.next; } H_UNLOCK(heapp); @@ -217,7 +217,7 @@ void chHeapFree(void *p) { * @return The number of fragments in the heap. */ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { - struct heap_header *qp; + union heap_header *qp; size_t n, sz; if (heapp == NULL) @@ -226,8 +226,8 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { H_LOCK(heapp); sz = 0; - for (n = 0, qp = &heapp->h_free; qp->h_u.next; n++, qp = qp->h_u.next) - sz += qp->h_u.next->h_size; + for (n = 0, qp = &heapp->h_free; qp->h.u.next; n++, qp = qp->h.u.next) + sz += qp->h.u.next->h.size; if (sizep) *sizep = sz; -- cgit v1.2.3 From 157b6f9695e7f72f2d54b231c19cb4045710ed01 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 21 Feb 2010 07:24:53 +0000 Subject: Updated license dates. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1646 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 91b7d01bc..ce8ce385b 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -1,5 +1,5 @@ /* - ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. This file is part of ChibiOS/RT. -- cgit v1.2.3 From ad3d21e81592481539a56e93234f5bf1fa2c0504 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 16 Mar 2010 19:36:21 +0000 Subject: Documentation reorganization. Moved the description from kernel.dox into the source code for ease of editing and reference. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1746 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index ce8ce385b..ede326b59 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -22,6 +22,18 @@ * @brief Heaps code. * * @addtogroup heaps + * @details Heap Allocator related APIs. + *

Operation mode

+ * The heap allocator implements a first-fit strategy and its APIs + * are functionally equivalent to the usual @p malloc() and @p free() + * library functions. The main difference is that the OS heap APIs + * are guaranteed to be thread safe.
+ * By enabling the @p CH_USE_MALLOC_HEAP option the heap manager + * will use the runtime-provided @p malloc() and @p free() as + * backend for the heap APIs instead of the system provided + * allocator.
+ * In order to use the heap APIs the @p CH_USE_HEAP option must + * be enabled in @p chconf.h. * @{ */ -- cgit v1.2.3 From e3a5e25315231a31932e865d3f6be9c1cd1f159b Mon Sep 17 00:00:00 2001 From: ecarusi Date: Mon, 5 Jul 2010 17:47:44 +0000 Subject: CH_USE_MALLOC_HEAP and H_(UN)LOCK redefinition fix git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2055 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index ede326b59..820f15362 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -41,6 +41,8 @@ #if CH_USE_HEAP +#if !CH_USE_MALLOC_HEAP + /* * Defaults on the best synchronization mechanism available. */ @@ -52,8 +54,6 @@ #define H_UNLOCK(h) chSemSignal(&(h)->h_sem) #endif -#if !CH_USE_MALLOC_HEAP - /** * @brief Default heap descriptor. */ @@ -253,7 +253,7 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { #if CH_USE_MUTEXES #define H_LOCK() chMtxLock(&hmtx) -#define H_UNLOCK() chMtxUnock() +#define H_UNLOCK() chMtxUnlock() static Mutex hmtx; #elif CH_USE_SEMAPHORES #define H_LOCK() chSemWait(&hsem) -- cgit v1.2.3 From 9ffea7e261ec4016d788abbbf7c4a6d3a78e0a04 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 18 Sep 2010 06:48:56 +0000 Subject: Documentation improvements, renamed some event APIs. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2179 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 820f15362..fc92e3c18 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -31,22 +31,22 @@ * By enabling the @p CH_USE_MALLOC_HEAP option the heap manager * will use the runtime-provided @p malloc() and @p free() as * backend for the heap APIs instead of the system provided - * allocator.
- * In order to use the heap APIs the @p CH_USE_HEAP option must + * allocator. + * @pre In order to use the heap APIs the @p CH_USE_HEAP option must * be enabled in @p chconf.h. * @{ */ #include "ch.h" -#if CH_USE_HEAP +#if CH_USE_HEAP || defined(__DOXYGEN__) -#if !CH_USE_MALLOC_HEAP +#if !CH_USE_MALLOC_HEAP || defined(__DOXYGEN__) /* * Defaults on the best synchronization mechanism available. */ -#if CH_USE_MUTEXES +#if CH_USE_MUTEXES || defined(__DOXYGEN__) #define H_LOCK(h) chMtxLock(&(h)->h_mtx) #define H_UNLOCK(h) chMtxUnlock() #else @@ -67,7 +67,7 @@ void heap_init(void) { default_heap.h_provider = chCoreAlloc; default_heap.h_free.h.u.next = (union heap_header *)NULL; default_heap.h_free.h.size = 0; -#if CH_USE_MUTEXES +#if CH_USE_MUTEXES || defined(__DOXYGEN__) chMtxInit(&default_heap.h_mtx); #else chSemInit(&default_heap.h_sem, 1); @@ -76,8 +76,8 @@ void heap_init(void) { /** * @brief Initializes a memory heap from a static memory area. - * @note Both the heap buffer base and the heap size must be aligned to - * the @p align_t type size. + * @pre Both the heap buffer base and the heap size must be aligned to + * the @p stkalign_t type size. * * @param[out] heapp pointer to the memory heap descriptor to be initialized * @param[in] buf heap buffer base @@ -93,7 +93,7 @@ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { heapp->h_free.h.size = 0; hp->h.u.next = NULL; hp->h.size = size - sizeof(union heap_header); -#if CH_USE_MUTEXES +#if CH_USE_MUTEXES || defined(__DOXYGEN__) chMtxInit(&heapp->h_mtx); #else chSemInit(&heapp->h_sem, 1); @@ -104,7 +104,7 @@ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { * @brief Allocates a block of memory from the heap by using the first-fit * algorithm. * @details The allocated block is guaranteed to be properly aligned for a - * pointer data type (@p align_t). + * pointer data type (@p stkalign_t). * * @param[in] heapp pointer to a heap descriptor or @p NULL in order to * access the default heap. -- cgit v1.2.3 From 07351222e6d0b6b3dcd4f50ecb18bc09e7402d1c Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 21 Sep 2010 10:22:06 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2184 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index fc92e3c18..13db7cf6b 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -61,7 +61,8 @@ static MemoryHeap default_heap; /** * @brief Initializes the default heap. - * @note Internal use only. + * + * @notapi */ void heap_init(void) { default_heap.h_provider = chCoreAlloc; @@ -82,6 +83,8 @@ void heap_init(void) { * @param[out] heapp pointer to the memory heap descriptor to be initialized * @param[in] buf heap buffer base * @param[in] size heap size + * + * @init */ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { union heap_header *hp; @@ -113,6 +116,8 @@ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { * size for alignment and fragmentation reasons. * @return A pointer to the allocated block. * @retval NULL if the block cannot be allocated. + * + * @api */ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { union heap_header *qp, *hp, *fp; @@ -173,6 +178,8 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { * @brief Frees a previously allocated memory block. * * @param[in] p pointer to the memory block to be freed + * + * @api */ void chHeapFree(void *p) { union heap_header *qp, *hp; @@ -227,6 +234,8 @@ void chHeapFree(void *p) { * @param[in] sizep pointer to a variable that will receive the total * fragmented free space * @return The number of fragments in the heap. + * + * @api */ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { union heap_header *qp; -- cgit v1.2.3 From 24594525990ee1769ee933261b821211b4c299e8 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 24 Feb 2011 14:57:38 +0000 Subject: Fixed bugs 3191107 and 3191112. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2762 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 13db7cf6b..c655e3852 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -125,7 +125,7 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { if (heapp == NULL) heapp = &default_heap; - size = MEM_ALIGN_SIZE(size); + size = MEM_ALIGN_NEXT(size); qp = &heapp->h_free; H_LOCK(heapp); -- cgit v1.2.3 From e7e79a6ccb4f3e320b2b8b7bad1b14d65218641d Mon Sep 17 00:00:00 2001 From: gdisirio Date: Fri, 18 Mar 2011 18:38:08 +0000 Subject: License updated. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2827 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index c655e3852..a04646bcf 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -1,5 +1,6 @@ /* - ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. + ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, + 2011 Giovanni Di Sirio. This file is part of ChibiOS/RT. -- cgit v1.2.3 From f5ae2552307f20f3fa3d987591fa60576981ce3d Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 29 Mar 2011 14:51:08 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2850 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index a04646bcf..bcfca9b77 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -65,7 +65,7 @@ static MemoryHeap default_heap; * * @notapi */ -void heap_init(void) { +void _heap_init(void) { default_heap.h_provider = chCoreAlloc; default_heap.h_free.h.u.next = (union heap_header *)NULL; default_heap.h_free.h.size = 0; -- cgit v1.2.3 From 5e1249af266c9688ec575e5a2f14ecfe6084de49 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 19 May 2011 09:13:24 +0000 Subject: Fixed bug 3303841. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2973 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index bcfca9b77..b90b21909 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -80,6 +80,8 @@ 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 stkalign_t type size. + * @pre In order to use this function the option @p CH_USE_MALLOC_HEAP + * must be disabled. * * @param[out] heapp pointer to the memory heap descriptor to be initialized * @param[in] buf heap buffer base @@ -271,7 +273,7 @@ static Mutex hmtx; static Semaphore hsem; #endif -void heap_init(void) { +void _heap_init(void) { #if CH_USE_MUTEXES chMtxInit(&hmtx); -- cgit v1.2.3 From de5dcbba856524599a8f06d3a9bdbf1b01db44c2 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 21 Jan 2012 14:29:42 +0000 Subject: License text updated with new year. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@3846 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index b90b21909..f84276779 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -1,6 +1,6 @@ /* ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, - 2011 Giovanni Di Sirio. + 2011,2012 Giovanni Di Sirio. This file is part of ChibiOS/RT. -- cgit v1.2.3 From f5a3976c393fffdc95627f27d4c49136fa9ec8ca Mon Sep 17 00:00:00 2001 From: gdisirio Date: Mon, 26 Mar 2012 09:13:37 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4055 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index f84276779..1e7f99dce 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -31,7 +31,7 @@ * are guaranteed to be thread safe.
* By enabling the @p CH_USE_MALLOC_HEAP option the heap manager * will use the runtime-provided @p malloc() and @p free() as - * backend for the heap APIs instead of the system provided + * back end for the heap APIs instead of the system provided * allocator. * @pre In order to use the heap APIs the @p CH_USE_HEAP option must * be enabled in @p chconf.h. -- cgit v1.2.3 From 184a71345c6a36a9a8664eda8fbcc3ea728267a8 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 2 Feb 2013 10:58:09 +0000 Subject: Updated license years. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@5102 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chheap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'os/kernel/src/chheap.c') diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 1e7f99dce..23bcff6b4 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -1,6 +1,6 @@ /* ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, - 2011,2012 Giovanni Di Sirio. + 2011,2012,2013 Giovanni Di Sirio. This file is part of ChibiOS/RT. -- cgit v1.2.3