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
This commit is contained in:
patacongo
2007-03-04 15:23:22 +00:00
parent 061279feb6
commit 4b1edc8ea5
19 changed files with 278 additions and 96 deletions
+10
View File
@@ -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
+80 -26
View File
@@ -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;
#undef IDX
#if CONFIG_MM_REGIONS > 1
g_nregions++;
#endif
/* Add the single, large free node to the nodelist */
mm_addfreechunk(node);
/* Initialize the malloc semaphore to one (to support one-at-
* a-time access to private data sets.
*/
mm_seminitialize();
}
+8 -2
View File
@@ -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
+31 -16
View File
@@ -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)
/* Visit each node in the region */
for (node = g_heapstart[region];
node < g_heapend[region];
node = (struct mm_allocnode_s *)((char*)node + node->size))
{
uordblks += node->size;
}
else
{
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);
uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */
DEBUGASSERT(node == g_heapend[region]);
uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */
}
#undef region
DEBUGASSERT(uordblks + fordblks == g_heapsize);
#ifdef CONFIG_CAN_PASS_STRUCTS
+78 -44
View File
@@ -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;