aboutsummaryrefslogtreecommitdiffstats
path: root/os/kernel
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2009-10-17 09:21:59 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2009-10-17 09:21:59 +0000
commit34fd822f84d409fa649934251fae01994de7888b (patch)
tree095f9c11f7aa2e5f2c0b451c1ba15b08d76f5c47 /os/kernel
parenta7cfbdaf10a03aa10236958bdba38479b301fc4f (diff)
downloadChibiOS-34fd822f84d409fa649934251fae01994de7888b.tar.gz
ChibiOS-34fd822f84d409fa649934251fae01994de7888b.tar.bz2
ChibiOS-34fd822f84d409fa649934251fae01994de7888b.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1226 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/kernel')
-rw-r--r--os/kernel/include/memcore.h12
-rw-r--r--os/kernel/include/mempools.h22
-rw-r--r--os/kernel/kernel.dox23
-rw-r--r--os/kernel/src/chmemcore.c4
-rw-r--r--os/kernel/src/chmempools.c23
5 files changed, 68 insertions, 16 deletions
diff --git a/os/kernel/include/memcore.h b/os/kernel/include/memcore.h
index 8f3fe4671..c60d3ad9d 100644
--- a/os/kernel/include/memcore.h
+++ b/os/kernel/include/memcore.h
@@ -27,10 +27,8 @@
#ifndef _MEMCORE_H_
#define _MEMCORE_H_
-#if CH_USE_MEMCORE
-
/**
- * @brief Memory alignment type.
+ * @brief Memory alignment type.
*/
typedef void *align_t;
@@ -42,21 +40,23 @@ typedef void *align_t;
typedef void *(*memgetfunc_t)(size_t size);
/**
- * @brief Alignment mask constant.
+ * @brief Alignment mask constant.
*/
#define MEM_ALIGN_MASK (sizeof(align_t) - 1)
/**
- * @brief Alignment helper macro.
+ * @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.
+ * the type @p align_t.
*/
#define MEM_IS_ALIGNED(p) (((size_t)(p) & MEM_ALIGN_MASK) == 0)
+#if CH_USE_MEMCORE
+
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/os/kernel/include/mempools.h b/os/kernel/include/mempools.h
index 38279c388..d5e612426 100644
--- a/os/kernel/include/mempools.h
+++ b/os/kernel/include/mempools.h
@@ -40,14 +40,19 @@ struct pool_header {
* @brief Memory pool descriptor.
*/
typedef struct {
- struct pool_header *mp_next; /**< Pointer to the header.*/
- size_t mp_object_size; /**< Memory pool objects size.*/
+ struct pool_header *mp_next; /**< Pointer to the header. */
+ size_t mp_object_size; /**< Memory pool objects size. */
+#if CH_USE_MEMCORE
+ bool_t mp_usecore; /**< Feed from the memory code
+ allocator if empty. */
+#endif
} MemoryPool;
/**
* @brief Data part of a static memory pool initializer.
* @details This macro should be used when statically initializing a
* memory pool that is part of a bigger structure.
+ *
* @param name the name of the memory pool variable
* @param size size of the memory pool contained objects
*/
@@ -57,12 +62,25 @@ typedef struct {
* @brief Static memory pool initializer.
* @details Statically initialized memory pools require no explicit
* initialization using @p chPoolInit().
+ *
* @param name the name of the memory pool variable
* @param size size of the memory pool contained objects
*/
#define MEMORYPOOL_DECL(name, size) \
MemoryPool name = _MEMORYPOOL_DATA(name, size)
+#if CH_USE_MEMCORE || defined(__DOXYGEN__)
+/**
+ * @brief Enables or disables the hungry mode.
+ * @details If enabled, the hungry mode, makes an empty memory pool feed
+ * new objects from the core memory manager.
+ *
+ * @param[in] mp pointer to a @p MemoryPool structure
+ * @param[in] mode hungry mode flag
+ */
+#define chPoolSetHungryMode(mp, mode) ((mp)->mp_usecore = (mode))
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/os/kernel/kernel.dox b/os/kernel/kernel.dox
index d2936fa14..bd8870279 100644
--- a/os/kernel/kernel.dox
+++ b/os/kernel/kernel.dox
@@ -230,7 +230,28 @@
*/
/**
- * @defgroup heap Heap
+ * @defgroup memcore Core Memory Manager
+ * Core Memory Manager related APIs.
+ * <h2>Operation mode</h2>
+ * The core memory manager is a simplified allocator that only allows to
+ * allocate memory blocks without the possibility to free them.<br>
+ * This allocator is meant as a memory blocks provider for the other
+ * allocators such as:
+ * - C-Runtime allocator.
+ * - Heap allocator (see @ref heaps).
+ * - @ref Memory pools allocator (see @ref pools).
+ * .
+ * By having a centralized memory provider the various allocators can coexist
+ * and share the main memory.<br>
+ * This allocator, alone, is also useful for very simple applications that
+ * just require a simple way to get memory blocks.<br>
+ * In order to use the core memory manager APIs the @p CH_USE_MEMCORE option
+ * must be specified in @p chconf.h.
+ * @ingroup memory
+ */
+
+/**
+ * @defgroup heaps Heaps
* Heap Allocator related APIs.
* <h2>Operation mode</h2>
* The heap allocator implements a first-fit strategy and its APIs are
diff --git a/os/kernel/src/chmemcore.c b/os/kernel/src/chmemcore.c
index 00e04a2d6..7177fe432 100644
--- a/os/kernel/src/chmemcore.c
+++ b/os/kernel/src/chmemcore.c
@@ -60,7 +60,7 @@ void core_init(void) {
* <code>sizeof(align_t)</code>.
*
*
- * @param[in] the size of the block to be allocated
+ * @param[in] size the size of the block to be allocated
* @return A pointer to the allocated memory block.
* @retval NULL allocation failed, core memory exhausted.
*/
@@ -79,7 +79,7 @@ void *chCoreAlloc(size_t size) {
* type @p align_t so it is not possible to allocate less than
* <code>sizeof(align_t)</code>.
*
- * @param[in] the size of the block to be allocated.
+ * @param[in] size the size of the block to be allocated.
* @return A pointer to the allocated memory block.
* @retval NULL allocation failed, core memory exhausted.
*/
diff --git a/os/kernel/src/chmempools.c b/os/kernel/src/chmempools.c
index a8d7da818..76ef39b58 100644
--- a/os/kernel/src/chmempools.c
+++ b/os/kernel/src/chmempools.c
@@ -32,14 +32,20 @@
*
* @param[out] mp pointer to a @p MemoryPool structure
* @param[in] size the size of the objects contained in this memory pool,
- * the minimum accepted size is the size of a pointer to void
+ * the minimum accepted size is the size of a pointer to void.
+ *
+ * @note The size is internally aligned to be a multiple of the @p align_t
+ * type size.
*/
void chPoolInit(MemoryPool *mp, size_t size) {
chDbgCheck((mp != NULL) && (size >= sizeof(void *)), "chPoolInit");
mp->mp_next = NULL;
- mp->mp_object_size = size;
+ mp->mp_object_size = MEM_ALIGN_SIZE(size);
+#if CH_USE_MEMCORE
+ mp->mp_usecore = FALSE;
+#endif
}
/**
@@ -56,7 +62,10 @@ void *chPoolAllocI(MemoryPool *mp) {
if ((objp = mp->mp_next) != NULL)
mp->mp_next = mp->mp_next->ph_next;
-
+#if CH_USE_MEMCORE
+ else if (mp->mp_usecore)
+ objp = chCoreAllocI(mp->mp_object_size);
+#endif
return objp;
}
@@ -81,13 +90,17 @@ void *chPoolAlloc(MemoryPool *mp) {
*
* @param[in] mp pointer to a @p MemoryPool structure
* @param[in] objp the pointer to the object to be released or added
- * @note the object is assumed to be of the right size for the specified
+ *
+ * @note The object is assumed to be of the right size for the specified
* memory pool.
+ * @note The object is assumed to be memory aligned to the size of @p align_t
+ * type.
*/
void chPoolFreeI(MemoryPool *mp, void *objp) {
struct pool_header *php = objp;
- chDbgCheck((mp != NULL) && (objp != NULL), "chPoolFreeI");
+ chDbgCheck((mp != NULL) && (objp != NULL) && MEM_IS_ALIGNED(objp),
+ "chPoolFreeI");
php->ph_next = mp->mp_next;
mp->mp_next = php;