summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-09-01 07:37:54 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-09-01 07:37:54 -0600
commit91bffa9cb1bee98d87b3174fa49cff5f0b9010d2 (patch)
tree8966ceae8ee4eba5b0d0485a4ecd1c60394effab
parentf8d20db23e41f2cb8f1e99f8f4f7965c215c9677 (diff)
downloadnuttx-91bffa9cb1bee98d87b3174fa49cff5f0b9010d2.tar.gz
nuttx-91bffa9cb1bee98d87b3174fa49cff5f0b9010d2.tar.bz2
nuttx-91bffa9cb1bee98d87b3174fa49cff5f0b9010d2.zip
Initial implementatino of sbrk()
-rw-r--r--nuttx/include/nuttx/arch.h22
-rw-r--r--nuttx/include/nuttx/mm.h15
-rw-r--r--nuttx/include/unistd.h11
-rw-r--r--nuttx/mm/Makefile19
-rwxr-xr-xnuttx/mm/kmm_sbrk.c92
-rw-r--r--nuttx/mm/mm_sbrk.c146
-rw-r--r--nuttx/mm/umm_sbrk.c95
7 files changed, 397 insertions, 3 deletions
diff --git a/nuttx/include/nuttx/arch.h b/nuttx/include/nuttx/arch.h
index 0393f6619..a3ca577c8 100644
--- a/nuttx/include/nuttx/arch.h
+++ b/nuttx/include/nuttx/arch.h
@@ -672,6 +672,28 @@ void up_allocate_pgheap(FAR void **heap_start, size_t *heap_size);
#endif
/****************************************************************************
+ * Name: pgalloc
+ *
+ * Description:
+ * If there is a page allocator in the configuration and if and MMU is
+ * available to map physcal addresses to virtual address, then function
+ * must be provided by the platform-specific code. This is part of the
+ * implementation of sbrk(). This function will allocate the requested
+ * number of pages using the page allocator and map them into consecuive
+ * virtual addresses beginning with 'brkaddr'
+ *
+ * NOTE: This function does not use the up_ naming standard because it
+ * is indirectly callable from user-space code via a system trap.
+ * Therefore, it is a system interface and follows a different naming
+ * convention.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_MM_PGALLOC) && defined(CONFIG_ARCH_USE_MMU)
+int pgalloc(uintptr_t vaddr, unsigned int npages);
+#endif
+
+/****************************************************************************
* Name: up_setpicbase, up_getpicbase
*
* Description:
diff --git a/nuttx/include/nuttx/mm.h b/nuttx/include/nuttx/mm.h
index 41095b98a..6b5ec14c6 100644
--- a/nuttx/include/nuttx/mm.h
+++ b/nuttx/include/nuttx/mm.h
@@ -392,7 +392,20 @@ FAR void *umm_brkaddr(int region);
/* Functions contained in kmm_brkaddr.c *************************************/
#ifdef CONFIG_MM_KERNEL_HEAP
-FAR void *umm_brkaddr(int region);
+FAR void *kmm_brkaddr(int region);
+#endif
+
+/* Functions contained in mm_sbrk.c *****************************************/
+
+#if defined(CONFIG_MM_PGALLOC) && defined(CONFIG_ARCH_USE_MMU)
+FAR void *mm_sbrk(FAR struct mm_heap_s *heap, intptr_t incr,
+ uintptr_t maxbreak);
+#endif
+
+/* Functions contained in kmm_sbrk.c ****************************************/
+
+#if defined(CONFIG_MM_PGALLOC) && defined(CONFIG_ARCH_USE_MMU)
+FAR void *kmm_sbrk(intptr_t incr);
#endif
/* Functions contained in mm_extend.c ***************************************/
diff --git a/nuttx/include/unistd.h b/nuttx/include/unistd.h
index 72c26a083..51f4b24b6 100644
--- a/nuttx/include/unistd.h
+++ b/nuttx/include/unistd.h
@@ -1,7 +1,7 @@
/****************************************************************************
* include/unistd.h
*
- * Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved.
+ * Copyright (C) 2007-2009, 2013-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -103,7 +103,8 @@
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
-extern "C" {
+extern "C"
+{
#else
#define EXTERN extern
#endif
@@ -146,6 +147,12 @@ off_t lseek(int fd, off_t offset, int whence);
ssize_t read(int fd, FAR void *buf, size_t nbytes);
ssize_t write(int fd, FAR const void *buf, size_t nbytes);
+/* Memory management */
+
+#if defined(CONFIG_MM_PGALLOC) && defined(CONFIG_ARCH_USE_MMU)
+FAR void *sbrk(intptr_t incr);
+#endif
+
/* Special devices */
int pipe(int fd[2]);
diff --git a/nuttx/mm/Makefile b/nuttx/mm/Makefile
index f396b3158..a62a1de93 100644
--- a/nuttx/mm/Makefile
+++ b/nuttx/mm/Makefile
@@ -56,18 +56,37 @@ 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)
+CSRCS += mm_sbrk.c
+endif
+endif
+
# User allocator
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)
+CSRCS += umm_sbrk.c
+endif
+endif
+
# Kernel allocator
ifeq ($(CONFIG_MM_KERNEL_HEAP),y)
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)
+CSRCS += kmm_sbrk.c
+endif
+endif
+
ifeq ($(CONFIG_DEBUG),y)
CSRCS += kmm_heapmember.c
endif
diff --git a/nuttx/mm/kmm_sbrk.c b/nuttx/mm/kmm_sbrk.c
new file mode 100755
index 000000000..89e6129c3
--- /dev/null
+++ b/nuttx/mm/kmm_sbrk.c
@@ -0,0 +1,92 @@
+/****************************************************************************
+ * mm/kmm_sbrk.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/mm.h>
+
+#if defined(CONFIG_MM_KERNEL_HEAP) && defined(CONFIG_MM_PGALLOC) && \
+ defined(CONFIG_ARCH_USE_MMU)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: kmm_sbrk
+ *
+ * Description:
+ * The sbrk() function is used to change the amount of space allocated
+ * for the calling process. The change is made by resetting the process'
+ * break value and allocating the appropriate amount of space. The amount
+ * of allocated space increases as the break value increases.
+ *
+ * The sbrk() function adds 'incr' bytes to the break value and changes
+ * the allocated space accordingly. If incr is negative, the amount of
+ * allocated space is decreased by incr bytes. The current value of the
+ * program break is returned by sbrk(0).
+ *
+ * Input Parameters:
+ * incr - Specifies the number of bytes to add or to remove from the
+ * space allocated for the process.
+ *
+ * Returned Value:
+ * Upon successful completion, sbrk() returns the prior break value.
+ * Otherwise, it returns (void *)-1 and sets errno to indicate the
+ * error:
+ *
+ * ENOMEM - The requested change would allocate more space than
+ * allowed under system limits.
+ * EAGAIN - The total amount of system memory available for allocation
+ * to this process is temporarily insufficient. This may occur even
+ * though the space requested was less than the maximum data segment
+ * size.
+ *
+ ****************************************************************************/
+
+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 */
diff --git a/nuttx/mm/mm_sbrk.c b/nuttx/mm/mm_sbrk.c
new file mode 100644
index 000000000..02fbab38d
--- /dev/null
+++ b/nuttx/mm/mm_sbrk.c
@@ -0,0 +1,146 @@
+/****************************************************************************
+ * mm/mm_sbrk.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm.h>
+#include <nuttx/pgalloc.h>
+
+#if defined(CONFIG_MM_PGALLOC) && defined(CONFIG_ARCH_USE_MMU)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mm_sbrk
+ *
+ * Description:
+ * The sbrk() function is used to change the amount of space allocated
+ * for the calling process. The change is made by resetting the process'
+ * break value and allocating the appropriate amount of space. The amount
+ * of allocated space increases as the break value increases.
+ *
+ * The sbrk() function adds 'incr' bytes to the break value and changes
+ * the allocated space accordingly. If incr is negative, the amount of
+ * allocated space is decreased by incr bytes. The current value of the
+ * program break is returned by sbrk(0).
+ *
+ * Input Parameters:
+ * incr - Specifies the number of bytes to add or to remove from the
+ * space allocated for the process.
+ *
+ * Returned Value:
+ * Upon successful completion, sbrk() returns the prior break value.
+ * Otherwise, it returns (void *)-1 and sets errno to indicate the
+ * error:
+ *
+ * ENOMEM - The requested change would allocate more space than
+ * allowed under system limits.
+ * EAGAIN - The total amount of system memory available for allocation
+ * to this process is temporarily insufficient. This may occur even
+ * though the space requested was less than the maximum data segment
+ * size.
+ *
+ ****************************************************************************/
+
+FAR void *mm_sbrk(FAR struct mm_heap_s *heap, intptr_t incr,
+ uintptr_t maxbreak)
+{
+ uintptr_t brkaddr;
+ unsigned int pgincr;
+ int err;
+ int ret;
+
+ DEBUGASSERT(incr >= 0);
+ if (incr < 0)
+ {
+ err = ENOSYS;
+ goto errout;
+ }
+
+ /* Get the current break address (NOTE: assumes region 0) */
+
+ brkaddr = (uintptr_t)mm_brkaddr(heap, 0);
+ if (incr > 0)
+ {
+ /* Convert the increment to multiples of the page size */
+
+ pgincr = MM_NPAGES(incr);
+
+ /* Check if this increment would exceed the maximum break value */
+
+ if ((maxbreak - brkaddr) < (pgincr << MM_PGSHIFT))
+ {
+ err = ENOMEM;
+ goto errout;
+ }
+
+ /* Allocate the requested number of pages and map them to the
+ * break address.
+ */
+
+ ret = pgalloc(brkaddr, pgincr);
+ if (ret < 0)
+ {
+ err = -ret;
+ goto errout;
+ }
+
+ /* Extend the heap (region 0) */
+
+ mm_extend(heap, (FAR void *)brkaddr, pgincr << MM_PGSHIFT, 0);
+ }
+
+ return (FAR void *)brkaddr;
+
+errout:
+ set_errno(err);
+ return (FAR void *)-1;
+}
+#endif /* CONFIG_MM_PGALLOC && CONFIG_ARCH_USE_MMU */
diff --git a/nuttx/mm/umm_sbrk.c b/nuttx/mm/umm_sbrk.c
new file mode 100644
index 000000000..c03b69840
--- /dev/null
+++ b/nuttx/mm/umm_sbrk.c
@@ -0,0 +1,95 @@
+/****************************************************************************
+ * mm/umm_sbrk.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <unistd.h>
+
+#include <nuttx/mm.h>
+#include <nuttx/pgalloc.h>
+
+#if defined(CONFIG_MM_USER_HEAP) && defined(CONFIG_MM_PGALLOC) && \
+ defined(CONFIG_ARCH_USE_MMU)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sbrk
+ *
+ * Description:
+ * The sbrk() function is used to change the amount of space allocated
+ * for the calling process. The change is made by resetting the process'
+ * break value and allocating the appropriate amount of space. The amount
+ * of allocated space increases as the break value increases.
+ *
+ * The sbrk() function adds 'incr' bytes to the break value and changes
+ * the allocated space accordingly. If incr is negative, the amount of
+ * allocated space is decreased by incr bytes. The current value of the
+ * program break is returned by sbrk(0).
+ *
+ * Input Parameters:
+ * incr - Specifies the number of bytes to add or to remove from the
+ * space allocated for the process.
+ *
+ * Returned Value:
+ * Upon successful completion, sbrk() returns the prior break value.
+ * Otherwise, it returns (void *)-1 and sets errno to indicate the
+ * error:
+ *
+ * ENOMEM - The requested change would allocate more space than
+ * allowed under system limits.
+ * EAGAIN - The total amount of system memory available for allocation
+ * to this process is temporarily insufficient. This may occur even
+ * though the space requested was less than the maximum data segment
+ * size.
+ *
+ ****************************************************************************/
+
+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 */