summaryrefslogtreecommitdiff
path: root/nuttx/mm
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-03-04 15:23:22 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2007-03-04 15:23:22 +0000
commit3b23ee2703a28a1b46bbd3eb4835b05ae321de29 (patch)
tree08c2619903b1002ca7f846dbcf5258e4f7f1a964 /nuttx/mm
parent6f275ae53307d29e22fde024a6479f80e60baf41 (diff)
downloadpx4-nuttx-3b23ee2703a28a1b46bbd3eb4835b05ae321de29.tar.gz
px4-nuttx-3b23ee2703a28a1b46bbd3eb4835b05ae321de29.tar.bz2
px4-nuttx-3b23ee2703a28a1b46bbd3eb4835b05ae321de29.zip
Add capability to manager memory in discontiguous regions.
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@35 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/mm')
-rw-r--r--nuttx/mm/mm_environment.h10
-rw-r--r--nuttx/mm/mm_initialize.c106
-rw-r--r--nuttx/mm/mm_internal.h10
-rw-r--r--nuttx/mm/mm_mallinfo.c45
-rw-r--r--nuttx/mm/mm_test.c122
5 files changed, 206 insertions, 87 deletions
diff --git a/nuttx/mm/mm_environment.h b/nuttx/mm/mm_environment.h
index 14476957d..8777323b0 100644
--- a/nuttx/mm/mm_environment.h
+++ b/nuttx/mm/mm_environment.h
@@ -54,6 +54,7 @@
# include <debug.h>
# include <errno.h>
# include <assert.h>
+# include <nuttx/os_external.h>
#else
# include <sys/types.h>
# include <string.h>
@@ -70,6 +71,15 @@
#ifdef MM_TEST
+/* Fake NuttX dependencies */
+
+# define FAR /* Normally in compiler.h */
+# define CONFIG_MM_REGIONS 2 /* Normally in config.h */
+# define CONFIG_CAN_PASS_STRUCTS 1 /* Normally in config.h */
+# undef CONFIG_SMALL_MEMORY /* Normally in config.h */
+
+extern void mm_addregion(FAR void *heapstart, size_t heapsize);
+
/* Use the real system errno */
# define mm_errno errno
diff --git a/nuttx/mm/mm_initialize.c b/nuttx/mm/mm_initialize.c
index 6d16a5677..c9c1c4bad 100644
--- a/nuttx/mm/mm_initialize.c
+++ b/nuttx/mm/mm_initialize.c
@@ -54,8 +54,12 @@ size_t g_heapsize;
/* This is the first and last nodes of the heap */
-FAR struct mm_allocnode_s *g_heapstart;
-FAR struct mm_allocnode_s *g_heapend;
+FAR struct mm_allocnode_s *g_heapstart[CONFIG_MM_REGIONS];
+FAR struct mm_allocnode_s *g_heapend[CONFIG_MM_REGIONS];
+
+#if CONFIG_MM_REGIONS > 1
+int g_nregions;
+#endif
/* All free nodes are maintained in a doubly linked list. This
* array provides some hooks into the list at various points to
@@ -76,7 +80,8 @@ FAR struct mm_freenode_s g_nodelist[MM_NNODES];
* boot time.
*
* Parameters:
- * None
+ * heapstart - Start of the initial heap region
+ * heapsize - Size of the initial heap region
*
* Return Value:
* None
@@ -95,16 +100,13 @@ void mm_initialize(FAR void *heapstart, size_t heapsize)
CHECK_ALLOCNODE_SIZE;
CHECK_FREENODE_SIZE;
- /* Adjust the provide heap start and size so that they are
- * both aligned with the MM_MIN_CHUNK size.
- */
+ /* Set up global variables */
- heapbase = MM_ALIGN_UP((size_t)heapstart);
- heapend = MM_ALIGN_DOWN((size_t)heapstart + (size_t)heapsize);
+ g_heapsize = 0;
- /* Save the size of the heap */
-
- g_heapsize = heapend - heapbase;
+#if CONFIG_MM_REGIONS > 1
+ g_nregions = 0;
+#endif
/* Initialize the node array */
@@ -115,6 +117,58 @@ void mm_initialize(FAR void *heapstart, size_t heapsize)
g_nodelist[i].blink = &g_nodelist[i-1];
}
+ /* Initialize the malloc semaphore to one (to support one-at-
+ * a-time access to private data sets.
+ */
+
+ mm_seminitialize();
+
+ /* Add the initial region of memory to the heap */
+
+ mm_addregion(heapstart, heapsize);
+}
+
+/************************************************************
+ * Function: mm_addregion
+ *
+ * Description:
+ * This function gives a region of contiguous memory to
+ * the memory manager
+ *
+ * Parameters:
+ * heapstart - Start of the heap region
+ * heapsize - Size of the heap region
+ *
+ * Return Value:
+ * None
+ *
+ * Assumptions:
+ *
+ ************************************************************/
+
+void mm_addregion(FAR void *heapstart, size_t heapsize)
+{
+ FAR struct mm_freenode_s *node;
+ size_t heapbase;
+ size_t heapend;
+#if CONFIG_MM_REGIONS > 1
+ int IDX = g_nregions;
+#else
+# define IDX 0
+#endif
+
+ /* Adjust the provide heap start and size so that they are
+ * both aligned with the MM_MIN_CHUNK size.
+ */
+
+ heapbase = MM_ALIGN_UP((size_t)heapstart);
+ heapend = MM_ALIGN_DOWN((size_t)heapstart + (size_t)heapsize);
+ heapsize = heapend - heapbase;
+
+ /* Add the size of this region to the total size of the heap */
+
+ g_heapsize += heapsize;
+
/* Create two "allocated" guard nodes at the beginning and end of
* the heap. These only serve to keep us from allocating outside
* of the heap.
@@ -123,25 +177,25 @@ void mm_initialize(FAR void *heapstart, size_t heapsize)
* all available memory.
*/
- g_heapstart = (FAR struct mm_allocnode_s *)heapbase;
- g_heapstart->size = SIZEOF_MM_ALLOCNODE;
- g_heapstart->preceding = MM_ALLOC_BIT;
+ g_heapstart[IDX] = (FAR struct mm_allocnode_s *)heapbase;
+ g_heapstart[IDX]->size = SIZEOF_MM_ALLOCNODE;
+ g_heapstart[IDX]->preceding = MM_ALLOC_BIT;
- node = (FAR struct mm_freenode_s *)(heapbase + SIZEOF_MM_ALLOCNODE);
- node->size = g_heapsize - 2*SIZEOF_MM_ALLOCNODE;
- node->preceding = SIZEOF_MM_ALLOCNODE;
+ node = (FAR struct mm_freenode_s *)(heapbase + SIZEOF_MM_ALLOCNODE);
+ node->size = heapsize - 2*SIZEOF_MM_ALLOCNODE;
+ node->preceding = SIZEOF_MM_ALLOCNODE;
- g_heapend = (FAR struct mm_allocnode_s *)(heapend - SIZEOF_MM_ALLOCNODE);
- g_heapend->size = SIZEOF_MM_ALLOCNODE;
- g_heapend->preceding = node->size | MM_ALLOC_BIT;
+ g_heapend[IDX] = (FAR struct mm_allocnode_s *)(heapend - SIZEOF_MM_ALLOCNODE);
+ g_heapend[IDX]->size = SIZEOF_MM_ALLOCNODE;
+ g_heapend[IDX]->preceding = node->size | MM_ALLOC_BIT;
- /* Add the single, large free node to the nodelist */
+#undef IDX
- mm_addfreechunk(node);
+#if CONFIG_MM_REGIONS > 1
+ g_nregions++;
+#endif
- /* Initialize the malloc semaphore to one (to support one-at-
- * a-time access to private data sets.
- */
+ /* Add the single, large free node to the nodelist */
- mm_seminitialize();
+ mm_addfreechunk(node);
}
diff --git a/nuttx/mm/mm_internal.h b/nuttx/mm/mm_internal.h
index e1b9f9beb..e9ab414dc 100644
--- a/nuttx/mm/mm_internal.h
+++ b/nuttx/mm/mm_internal.h
@@ -160,8 +160,14 @@ extern size_t g_heapsize;
/* This is the first and last nodes of the heap */
-extern FAR struct mm_allocnode_s *g_heapstart;
-extern FAR struct mm_allocnode_s *g_heapend;
+extern FAR struct mm_allocnode_s *g_heapstart[CONFIG_MM_REGIONS];
+extern FAR struct mm_allocnode_s *g_heapend[CONFIG_MM_REGIONS];
+
+#if CONFIG_MM_REGIONS > 1
+extern int g_nregions;
+#else
+# define g_nregions 1
+#endif
/* All free nodes are maintained in a doubly linked list. This
* array provides some hooks into the list at various points to
diff --git a/nuttx/mm/mm_mallinfo.c b/nuttx/mm/mm_mallinfo.c
index 68c2a7bb7..0e6ae4e78 100644
--- a/nuttx/mm/mm_mallinfo.c
+++ b/nuttx/mm/mm_mallinfo.c
@@ -76,6 +76,11 @@ int mallinfo(struct mallinfo *info)
int ordblks = 0; /* Number of non-inuse chunks */
size_t uordblks = 0; /* Total allocated space */
size_t fordblks = 0; /* Total non-inuse space */
+#if CONFIG_MM_REGIONS > 1
+ int region;
+#else
+# define region 0
+#endif
#ifdef CONFIG_CAN_PASS_STRUCTS
static struct mallinfo info;
@@ -85,29 +90,39 @@ int mallinfo(struct mallinfo *info)
return ERROR;
}
#endif
- /* Visit each node in physical memory */
- for (node = g_heapstart;
- node < g_heapend;
- node = (struct mm_allocnode_s *)((char*)node + node->size))
+ /* Visit each region */
+
+#if CONFIG_MM_REGIONS > 1
+ for (region = 0; region < g_nregions; region++)
+#endif
{
- if (node->preceding & MM_ALLOC_BIT)
- {
- uordblks += node->size;
- }
- else
+ /* Visit each node in the region */
+
+ for (node = g_heapstart[region];
+ node < g_heapend[region];
+ node = (struct mm_allocnode_s *)((char*)node + node->size))
{
- ordblks++;
- fordblks += node->size;
- if (node->size > mxordblk)
+ if (node->preceding & MM_ALLOC_BIT)
{
- mxordblk = node->size;
+ uordblks += node->size;
+ }
+ else
+ {
+ ordblks++;
+ fordblks += node->size;
+ if (node->size > mxordblk)
+ {
+ mxordblk = node->size;
+ }
}
}
+
+ DEBUGASSERT(node == g_heapend[region]);
+ uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */
}
+#undef region
- DEBUGASSERT(node == g_heapend);
- uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */
DEBUGASSERT(uordblks + fordblks == g_heapsize);
#ifdef CONFIG_CAN_PASS_STRUCTS
diff --git a/nuttx/mm/mm_test.c b/nuttx/mm/mm_test.c
index 779fd607c..4dadcec27 100644
--- a/nuttx/mm/mm_test.c
+++ b/nuttx/mm/mm_test.c
@@ -37,21 +37,24 @@
#include <stdlib.h>
#include <string.h>
+/* Fake NuttX dependencies */
+
+#define FAR
+#define CONFIG_MM_REGIONS 2
+#define CONFIG_CAN_PASS_STRUCTS 1
+#undef CONFIG_SMALL_MEMORY
+
#include "mm_internal.h"
/* Definitions */
-#define TEST_HEAP_SIZE 0x00100000
+#define TEST_HEAP1_SIZE 0x00080000
+#define TEST_HEAP2_SIZE 0x00080000
#define NTEST_ALLOCS 32
/* #define STOP_ON_ERRORS do{}while(0) */
#define STOP_ON_ERRORS exit(1)
-/* Heap provided to memory manager */
-
-unsigned long heap_base;
-unsigned long heap_size = TEST_HEAP_SIZE;
-
/* Test allocations */
static const int alloc_sizes[NTEST_ALLOCS] =
@@ -94,9 +97,10 @@ static const int alignment[NTEST_ALLOCS/2] =
128, 2048, 131072, 8192, 32, 32768, 16384 , 262144,
512, 4096, 65536, 8, 64, 1024, 16, 4
};
-static void *allocs[NTEST_ALLOCS];
-static struct mallinfo alloc_info;
-static unsigned int g_adjheapsize = 0;
+static void *allocs[NTEST_ALLOCS];
+static struct mallinfo alloc_info;
+static unsigned int g_reportedheapsize = 0;
+static unsigned int g_actualheapsize = 0;
/************************************************************
* mm_showchunkinfo
@@ -121,33 +125,46 @@ static int mm_findinfreelist(struct mm_freenode_s *node)
static void mm_showchunkinfo(void)
{
struct mm_allocnode_s *node;
+#if CONFIG_MM_REGIONS > 1
+ int region;
+#else
+# define region 0
+#endif
int found;
- /* Visit each node in physical memory */
-
printf(" CHUNK LIST:\n");
- for (node = g_heapstart;
- node < g_heapend;
- node = (struct mm_allocnode_s *)((char*)node + node->size))
+ /* Visit each region */
+
+#if CONFIG_MM_REGIONS > 1
+ for (region = 0; region < g_nregions; region++)
+#endif
{
- printf(" %p 0x%08x 0x%08x %s",
- node, node->size, node->preceding & ~MM_ALLOC_BIT,
- node->preceding & MM_ALLOC_BIT ? "Allocated" : "Free ");
- found = mm_findinfreelist((struct mm_freenode_s *)node);
- if (found && (node->preceding & MM_ALLOC_BIT) != 0)
- {
- printf(" Should NOT have been in free list\n");
- }
- else if (!found && (node->preceding & MM_ALLOC_BIT) == 0)
- {
- printf(" SHOULD have been in free listT\n");
- }
- else
- {
- printf(" OK\n");
+ /* Visit each node in each region */
+
+ for (node = g_heapstart[region];
+ node < g_heapend[region];
+ node = (struct mm_allocnode_s *)((char*)node + node->size))
+ {
+ printf(" %p 0x%08x 0x%08x %s",
+ node, node->size, node->preceding & ~MM_ALLOC_BIT,
+ node->preceding & MM_ALLOC_BIT ? "Allocated" : "Free ");
+ found = mm_findinfreelist((struct mm_freenode_s *)node);
+ if (found && (node->preceding & MM_ALLOC_BIT) != 0)
+ {
+ printf(" Should NOT have been in free list\n");
+ }
+ else if (!found && (node->preceding & MM_ALLOC_BIT) == 0)
+ {
+ printf(" SHOULD have been in free listT\n");
+ }
+ else
+ {
+ printf(" OK\n");
+ }
}
}
+#undef region
}
static void mm_showfreelist(void)
@@ -212,21 +229,21 @@ static void mm_showmallinfo(void)
STOP_ON_ERRORS;
}
- if (!g_adjheapsize)
+ if (!g_reportedheapsize)
{
- g_adjheapsize = alloc_info.uordblks + alloc_info.fordblks;
- if (g_adjheapsize > TEST_HEAP_SIZE + 16 ||
- g_adjheapsize < TEST_HEAP_SIZE -16)
+ g_reportedheapsize = alloc_info.uordblks + alloc_info.fordblks;
+ if (g_reportedheapsize > g_actualheapsize + 16*CONFIG_MM_REGIONS ||
+ g_reportedheapsize < g_actualheapsize -16*CONFIG_MM_REGIONS)
{
fprintf(stderr, "Total memory %d not close to uordlbks=%d + fordblks=%d = %d\n",
- TEST_HEAP_SIZE, g_adjheapsize, alloc_info.uordblks, alloc_info.fordblks, g_adjheapsize);
+ g_actualheapsize, alloc_info.uordblks, alloc_info.fordblks, g_reportedheapsize);
STOP_ON_ERRORS;
}
}
- else if (alloc_info.uordblks + alloc_info.fordblks != g_adjheapsize)
+ else if (alloc_info.uordblks + alloc_info.fordblks != g_reportedheapsize)
{
fprintf(stderr, "Total memory %d != uordlbks=%d + fordblks=%d\n",
- g_adjheapsize, alloc_info.uordblks, alloc_info.fordblks);
+ g_reportedheapsize, alloc_info.uordblks, alloc_info.fordblks);
STOP_ON_ERRORS;
}
}
@@ -389,23 +406,39 @@ static do_frees(void **mem, const int *size, const int *rand, int n)
int main(int argc, char **argv, char **envp)
{
- void *heapbase;
+ void *heap1_base;
+ void *heap2_base;
int i, j;
/* Allocate a heap */
- printf("Allocating test heap of %ldKb\n", TEST_HEAP_SIZE/1024);
- heapbase = malloc(TEST_HEAP_SIZE);
- printf("Allocated heap_base=%p\n", heap_base);
- if (heapbase == 0)
+ printf("Allocating test heap #1 of %ldKb\n", TEST_HEAP1_SIZE/1024);
+ heap1_base = malloc(TEST_HEAP1_SIZE);
+ printf("Allocated heap1_base=%p\n", heap1_base);
+ if (heap1_base == 0)
{
- fprintf(stderr, "Failed to allocate test heap\n");
+ fprintf(stderr, "Failed to allocate test heap #1\n");
+ exit(1);
+ }
+
+ printf("Allocating test heap #2 of %ldKb\n", TEST_HEAP2_SIZE/1024);
+ heap2_base = malloc(TEST_HEAP2_SIZE);
+ printf("Allocated heap2_base=%p\n", heap2_base);
+ if (heap2_base == 0)
+ {
+ fprintf(stderr, "Failed to allocate test heap #2\n");
exit(1);
}
/* Initialize the memory manager */
- mm_initialize(heapbase, TEST_HEAP_SIZE);
+ mm_initialize(heap1_base, TEST_HEAP1_SIZE);
+ g_actualheapsize = TEST_HEAP1_SIZE;
+ mm_showmallinfo();
+
+ mm_addregion(heap2_base, TEST_HEAP2_SIZE);
+ g_reportedheapsize = 0;
+ g_actualheapsize += TEST_HEAP2_SIZE;
mm_showmallinfo();
/* Allocate some memory */
@@ -431,7 +464,8 @@ int main(int argc, char **argv, char **envp)
/* Clean up and exit */
- free(heapbase);
+ free(heap1_base);
+ free(heap2_base);
printf("TEST COMPLETE\n");
return 0;