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/include/ch.h | 1 + os/kernel/include/heap.h | 47 ++++++++++++++++++++++++++-- os/kernel/include/mem.h | 61 ------------------------------------- os/kernel/include/memcore.h | 74 +++++++++++++++++++++++++++++++++++++++++++++ os/kernel/include/threads.h | 4 +-- 5 files changed, 122 insertions(+), 65 deletions(-) delete mode 100644 os/kernel/include/mem.h create mode 100644 os/kernel/include/memcore.h (limited to 'os/kernel/include') diff --git a/os/kernel/include/ch.h b/os/kernel/include/ch.h index 46d917e29..fa30c0eaf 100644 --- a/os/kernel/include/ch.h +++ b/os/kernel/include/ch.h @@ -75,6 +75,7 @@ #include "events.h" #include "messages.h" #include "mailboxes.h" +#include "memcore.h" #include "heap.h" #include "mempools.h" #include "threads.h" diff --git a/os/kernel/include/heap.h b/os/kernel/include/heap.h index 4fbd48225..1f07faa91 100644 --- a/os/kernel/include/heap.h +++ b/os/kernel/include/heap.h @@ -27,17 +27,60 @@ #ifndef _HEAP_H_ #define _HEAP_H_ +#if CH_USE_HEAP + +/* + * Module dependancies check. + */ +#if !CH_USE_MEMCORE && !CH_USE_MALLOC_HEAP +#error "CH_USE_HEAP requires CH_USE_MEM" +#endif + +#if !CH_USE_MUTEXES && !CH_USE_SEMAPHORES +#error "CH_USE_HEAP requires CH_USE_MUTEXES and/or CH_USE_SEMAPHORES" +#endif + +typedef struct memory_heap MemoryHeap; + +/** + * @brief Memory heap block header. + */ +struct heap_header { + union { + struct heap_header *h_next; /**< @brief Next block in free list. */ + MemoryHeap *h_heap; /**< @brief Block owner heap. */ + }; + size_t h_size; /**< @brief Size of the memory block. */ +}; + +/** + * @brief Structure describing a memory heap. + */ +struct memory_heap { + memgetfunc_t h_provider; /**< @brief Memory blocks provider for + this heap. */ + struct heap_header h_free; /**< @brief Free blocks list header. */ +#if CH_USE_MUTEXES + Mutex h_mtx; /**< @brief Heap access mutex. */ +#else + Semaphore h_sem; /**< @brief Heap access semaphore. */ +#endif +}; + #ifdef __cplusplus extern "C" { #endif void heap_init(void); - void *chHeapAlloc(size_t size); + void chHeapInit(MemoryHeap *heapp, void *buf, size_t size); + void *chHeapAlloc(MemoryHeap *heapp, size_t size); void chHeapFree(void *p); - size_t chHeapStatus(size_t *sizep); + size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep); #ifdef __cplusplus } #endif +#endif /* CH_USE_HEAP */ + #endif /* _HEAP_H_ */ /** @} */ diff --git a/os/kernel/include/mem.h b/os/kernel/include/mem.h deleted file mode 100644 index c7f5ba07a..000000000 --- a/os/kernel/include/mem.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - 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 mem.h - * @brief Low level memory manager macros and structures. - * @addtogroup coremem - * @{ - */ - -#ifndef _MEM_H_ -#define _MEM_H_ - -#if CH_USE_COREMEM - -/** - * @brief Memory alignment type. - */ -typedef void *align_t; - -/** - * @brief Alignment mask constant. - */ -#define MEM_ALIGN_MASK (sizeof(align_t) - 1) - -/** - * @brief Alignment helper macro. - */ -#define MEM_ALIGN_SIZE(p) (((size_t)(p) + ALIGN_MASK) & ~ALIGN_MASK) - -#ifdef __cplusplus -extern "C" { -#endif - void mem_init(void); - void *chMemAlloc(size_t size); - void *chMemAllocI(size_t size); -#ifdef __cplusplus -} -#endif - -#endif /* CH_USE_COREMEM */ - -#endif /* _MEM_H_ */ - -/** @} */ diff --git a/os/kernel/include/memcore.h b/os/kernel/include/memcore.h new file mode 100644 index 000000000..8f3fe4671 --- /dev/null +++ b/os/kernel/include/memcore.h @@ -0,0 +1,74 @@ +/* + 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 memcore.h + * @brief Core memory manager macros and structures. + * @addtogroup memcore + * @{ + */ + +#ifndef _MEMCORE_H_ +#define _MEMCORE_H_ + +#if CH_USE_MEMCORE + +/** + * @brief Memory alignment type. + */ +typedef void *align_t; + +/** + * @brief Memory get function. + * @note This type must be assignment compatible with the @p chMemAlloc() + * function. + */ +typedef void *(*memgetfunc_t)(size_t size); + +/** + * @brief Alignment mask constant. + */ +#define MEM_ALIGN_MASK (sizeof(align_t) - 1) + +/** + * @brief Alignment helper macro. + */ +#define MEM_ALIGN_SIZE(p) (((size_t)(p) + MEM_ALIGN_MASK) & ~MEM_ALIGN_MASK) + +/** + * @brief Returns whatever a pointer or memory size is aligned to + * the type @p align_t. + */ +#define MEM_IS_ALIGNED(p) (((size_t)(p) & MEM_ALIGN_MASK) == 0) + +#ifdef __cplusplus +extern "C" { +#endif + void core_init(void); + void *chCoreAlloc(size_t size); + void *chCoreAllocI(size_t size); +#ifdef __cplusplus +} +#endif + +#endif /* CH_USE_MEMCORE */ + +#endif /* _MEMCORE_H_ */ + +/** @} */ diff --git a/os/kernel/include/threads.h b/os/kernel/include/threads.h index 1b9850435..ccafc3e96 100644 --- a/os/kernel/include/threads.h +++ b/os/kernel/include/threads.h @@ -167,8 +167,8 @@ extern "C" { Thread *chThdCreateStatic(void *wsp, size_t size, tprio_t prio, tfunc_t pf, void *arg); #if CH_USE_DYNAMIC && CH_USE_WAITEXIT && CH_USE_HEAP - Thread *chThdCreateFromHeap(size_t size, tprio_t prio, - tfunc_t pf, void *arg); + Thread *chThdCreateFromHeap(MemoryHeap *heapp, size_t size, + tprio_t prio, tfunc_t pf, void *arg); #endif #if CH_USE_DYNAMIC && CH_USE_WAITEXIT && CH_USE_MEMPOOLS Thread *chThdCreateFromMemoryPool(MemoryPool *mp, tprio_t prio, -- cgit v1.2.3