summaryrefslogtreecommitdiff
path: root/nuttx/mm
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-09-01 10:46:51 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-09-01 10:46:51 -0600
commitf23b744ac0c3750a0a1742b8ff7e18c63e45253c (patch)
treea9231d215cdb9bdb6cbb2932f877297f7c5f8874 /nuttx/mm
parent83c30d32a3c84c9c7e992c8d1b29bf03e9fa8a3a (diff)
downloadpx4-nuttx-f23b744ac0c3750a0a1742b8ff7e18c63e45253c.tar.gz
px4-nuttx-f23b744ac0c3750a0a1742b8ff7e18c63e45253c.tar.bz2
px4-nuttx-f23b744ac0c3750a0a1742b8ff7e18c63e45253c.zip
Completes the implementation of sbrk() (untested)
Diffstat (limited to 'nuttx/mm')
-rw-r--r--nuttx/mm/Makefile12
-rwxr-xr-xnuttx/mm/kmm_sbrk.c5
-rw-r--r--nuttx/mm/mm_brkaddr.c5
-rw-r--r--nuttx/mm/mm_sbrk.c6
-rw-r--r--nuttx/mm/umm_malloc.c33
-rw-r--r--nuttx/mm/umm_sbrk.c5
-rw-r--r--nuttx/mm/umm_zalloc.c16
7 files changed, 62 insertions, 20 deletions
diff --git a/nuttx/mm/Makefile b/nuttx/mm/Makefile
index a62a1de93..4c5fe0e19 100644
--- a/nuttx/mm/Makefile
+++ b/nuttx/mm/Makefile
@@ -56,11 +56,9 @@ CSRCS += mm_shrinkchunk.c
CSRCS += mm_brkaddr.c mm_calloc.c mm_extend.c mm_free.c mm_mallinfo.c
CSRCS += mm_malloc.c mm_memalign.c mm_realloc.c mm_zalloc.c
-ifeq ($(CONFIG_MM_PGALLOC),y)
-ifeq ($(CONFIG_ARCH_USE_MMU),y)
+ifeq ($(CONFIG_ARCH_ADDRENV),y)
CSRCS += mm_sbrk.c
endif
-endif
# User allocator
@@ -68,11 +66,9 @@ CSRCS += umm_initialize.c umm_addregion.c umm_sem.c
CSRCS += umm_brkaddr.c umm_calloc.c umm_extend.c umm_free.c umm_mallinfo.c
CSRCS += umm_malloc.c umm_memalign.c umm_realloc.c umm_zalloc.c
-ifeq ($(CONFIG_MM_PGALLOC),y)
-ifeq ($(CONFIG_ARCH_USE_MMU),y)
+ifeq ($(CONFIG_ARCH_ADDRENV),y)
CSRCS += umm_sbrk.c
endif
-endif
# Kernel allocator
@@ -81,11 +77,9 @@ CSRCS += kmm_initialize.c kmm_addregion.c kmm_sem.c
CSRCS += kmm_brkaddr.c kmm_calloc.c kmm_extend.c kmm_free.c kmm_mallinfo.c
CSRCS += kmm_malloc.c kmm_memalign.c kmm_realloc.c kmm_zalloc.c
-ifeq ($(CONFIG_MM_PGALLOC),y)
-ifeq ($(CONFIG_ARCH_USE_MMU),y)
+ifeq ($(CONFIG_ARCH_ADDRENV),y)
CSRCS += kmm_sbrk.c
endif
-endif
ifeq ($(CONFIG_DEBUG),y)
CSRCS += kmm_heapmember.c
diff --git a/nuttx/mm/kmm_sbrk.c b/nuttx/mm/kmm_sbrk.c
index 89e6129c3..09da8d93d 100755
--- a/nuttx/mm/kmm_sbrk.c
+++ b/nuttx/mm/kmm_sbrk.c
@@ -41,8 +41,7 @@
#include <nuttx/mm.h>
-#if defined(CONFIG_MM_KERNEL_HEAP) && defined(CONFIG_MM_PGALLOC) && \
- defined(CONFIG_ARCH_USE_MMU)
+#if defined(CONFIG_MM_KERNEL_HEAP) && defined(CONFIG_ARCH_ADDRENV)
/****************************************************************************
* Pre-processor Definitions
@@ -89,4 +88,4 @@ FAR void *kmm_sbrk(intptr_t incr)
return mm_sbrk(&g_kmmheap, incr, UINTPTR_MAX);
}
-#endif /* CONFIG_MM_USER_HEAP && CONFIG_MM_PGALLOC && CONFIG_ARCH_USE_MMU */
+#endif /* CONFIG_MM_USER_HEAP && CONFIG_ARCH_ADDRENV */
diff --git a/nuttx/mm/mm_brkaddr.c b/nuttx/mm/mm_brkaddr.c
index a75bcd68b..693b81086 100644
--- a/nuttx/mm/mm_brkaddr.c
+++ b/nuttx/mm/mm_brkaddr.c
@@ -55,7 +55,8 @@
* Name: mm_brkaddr
*
* Description:
- * Return the break address of a heap region
+ * Return the break address of a heap region. Zero is returned if the
+ * memory region is not initialized.
*
****************************************************************************/
@@ -65,5 +66,5 @@ FAR void *mm_brkaddr(FAR struct mm_heap_s *heap, int region)
DEBUGASSERT(heap && region < heap->mm_nregions);
brkaddr = (uintptr_t)heap->mm_heapend[region];
- return (FAR void *)(brkaddr + SIZEOF_MM_ALLOCNODE);
+ return brkaddr ? (FAR void *)(brkaddr + SIZEOF_MM_ALLOCNODE) : 0;
}
diff --git a/nuttx/mm/mm_sbrk.c b/nuttx/mm/mm_sbrk.c
index 02fbab38d..c0c640735 100644
--- a/nuttx/mm/mm_sbrk.c
+++ b/nuttx/mm/mm_sbrk.c
@@ -47,7 +47,7 @@
#include <nuttx/mm.h>
#include <nuttx/pgalloc.h>
-#if defined(CONFIG_MM_PGALLOC) && defined(CONFIG_ARCH_USE_MMU)
+#ifdef CONFIG_ARCH_ADDRENV
/****************************************************************************
* Pre-processor Definitions
@@ -115,7 +115,7 @@ FAR void *mm_sbrk(FAR struct mm_heap_s *heap, intptr_t incr,
/* Check if this increment would exceed the maximum break value */
- if ((maxbreak - brkaddr) < (pgincr << MM_PGSHIFT))
+ if ((brkaddr > 0) && ((maxbreak - brkaddr) < (pgincr << MM_PGSHIFT)))
{
err = ENOMEM;
goto errout;
@@ -143,4 +143,4 @@ errout:
set_errno(err);
return (FAR void *)-1;
}
-#endif /* CONFIG_MM_PGALLOC && CONFIG_ARCH_USE_MMU */
+#endif /* CONFIG_ARCH_ADDRENV */
diff --git a/nuttx/mm/umm_malloc.c b/nuttx/mm/umm_malloc.c
index 6d08fbc29..a2f68eca3 100644
--- a/nuttx/mm/umm_malloc.c
+++ b/nuttx/mm/umm_malloc.c
@@ -40,6 +40,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
+#include <unistd.h>
#include <nuttx/mm.h>
@@ -85,7 +86,39 @@
FAR void *malloc(size_t size)
{
+#ifdef CONFIG_ARCH_ADDRENV
+ FAR void *brkaddr;
+ FAR void *mem;
+
+ /* Loop until we successfully allocate the memory or until an error
+ * occurs. If we fail to allocate memory on the first pass, then call
+ * sbrk to extend the heap by one page. This may require several
+ * passes if more the size of the allocation is more than one page.
+ *
+ * An alternative would be to increase the size of the heap by the
+ * full requested allocation in sbrk(). Then the loop should never
+ * execute more than twice (but more memory than we need may be
+ * allocated).
+ */
+
+ do
+ {
+ mem = mm_malloc(&g_mmheap, size);
+ if (!mem)
+ {
+ brkaddr = sbrk(size);
+ if (brkaddr == (FAR void *)-1)
+ {
+ return NULL;
+ }
+ }
+ }
+ while (mem == NULL);
+
+ return mem;
+#else
return mm_malloc(&g_mmheap, size);
+#endif
}
#endif /* CONFIG_MM_USER_HEAP */
diff --git a/nuttx/mm/umm_sbrk.c b/nuttx/mm/umm_sbrk.c
index c03b69840..b404b020c 100644
--- a/nuttx/mm/umm_sbrk.c
+++ b/nuttx/mm/umm_sbrk.c
@@ -44,8 +44,7 @@
#include <nuttx/mm.h>
#include <nuttx/pgalloc.h>
-#if defined(CONFIG_MM_USER_HEAP) && defined(CONFIG_MM_PGALLOC) && \
- defined(CONFIG_ARCH_USE_MMU)
+#if defined(CONFIG_MM_USER_HEAP) && defined(CONFIG_ARCH_ADDRENV)
/****************************************************************************
* Pre-processor Definitions
@@ -92,4 +91,4 @@ FAR void *sbrk(intptr_t incr)
return mm_sbrk(&g_mmheap, incr, CONFIG_ARCH_STACK_NPAGES << MM_PGSHIFT);
}
-#endif /* CONFIG_MM_USER_HEAP && CONFIG_MM_PGALLOC && CONFIG_ARCH_USE_MMU */
+#endif /* CONFIG_MM_USER_HEAP && CONFIG_ARCH_ADDRENV */
diff --git a/nuttx/mm/umm_zalloc.c b/nuttx/mm/umm_zalloc.c
index d4d5bf5fd..835841a75 100644
--- a/nuttx/mm/umm_zalloc.c
+++ b/nuttx/mm/umm_zalloc.c
@@ -40,6 +40,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
+#include <string.h>
#include <nuttx/mm.h>
@@ -69,7 +70,22 @@
FAR void *zalloc(size_t size)
{
+#ifdef CONFIG_ARCH_ADDRENV
+ /* Use malloc() because it implements the sbrk() logic */
+
+ FAR void *alloc = malloc(size);
+ if (alloc)
+ {
+ memset(alloc, 0, size);
+ }
+
+ return alloc;
+
+#else
+ /* Use mm_zalloc() becuase it implements the clear */
+
return mm_zalloc(&g_mmheap, size);
+#endif
}
#endif /* CONFIG_MM_USER_HEAP */