From aadd8ebfc6ba3f9d50dd2473c724804102a25b7d Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 31 Aug 2014 14:42:45 -0600 Subject: Add low-level memory management hooks that will be needed to support brk() and sbrk() --- nuttx/include/nuttx/mm.h | 33 +++++++++++++ nuttx/mm/Makefile | 12 ++--- nuttx/mm/kmm_brkaddr.c | 69 ++++++++++++++++++++++++++ nuttx/mm/kmm_extend.c | 68 ++++++++++++++++++++++++++ nuttx/mm/mm_brkaddr.c | 69 ++++++++++++++++++++++++++ nuttx/mm/mm_extend.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++ nuttx/mm/umm_brkaddr.c | 69 ++++++++++++++++++++++++++ nuttx/mm/umm_extend.c | 68 ++++++++++++++++++++++++++ 8 files changed, 505 insertions(+), 6 deletions(-) create mode 100644 nuttx/mm/kmm_brkaddr.c create mode 100644 nuttx/mm/kmm_extend.c create mode 100644 nuttx/mm/mm_brkaddr.c create mode 100644 nuttx/mm/mm_extend.c create mode 100644 nuttx/mm/umm_brkaddr.c create mode 100644 nuttx/mm/umm_extend.c diff --git a/nuttx/include/nuttx/mm.h b/nuttx/include/nuttx/mm.h index cfafc8381..53eb95d2b 100644 --- a/nuttx/include/nuttx/mm.h +++ b/nuttx/include/nuttx/mm.h @@ -337,6 +337,39 @@ FAR void *mm_zalloc(FAR struct mm_heap_s *heap, size_t size); FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, size_t size); +/* Functions contained in mm_brkaddr.c **************************************/ + +FAR void *mm_brkaddr(FAR struct mm_heap_s *heap, int region); + +/* Functions contained in umm_brkaddr.c *************************************/ + +#ifdef CONFIG_MM_USER_HEAP +FAR void *umm_brkaddr(int region); +#endif + +/* Functions contained in kmm_brkaddr.c *************************************/ + +#ifdef CONFIG_MM_KERNEL_HEAP +FAR void *umm_brkaddr(int region); +#endif + +/* Functions contained in mm_extend.c ***************************************/ + +void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size, + int region); + +/* Functions contained in umm_extend.c **************************************/ + +#ifdef CONFIG_MM_USER_HEAP +void umm_extend(FAR void *mem, size_t size, int region); +#endif + +/* Functions contained in kmm_extend.c **************************************/ + +#ifdef CONFIG_MM_KERNEL_HEAP +void kmm_extend(FAR void *mem, size_t size, int region); +#endif + /* Functions contained in mm_mallinfo.c *************************************/ struct mallinfo; /* Forward reference */ diff --git a/nuttx/mm/Makefile b/nuttx/mm/Makefile index 054393734..f396b3158 100644 --- a/nuttx/mm/Makefile +++ b/nuttx/mm/Makefile @@ -53,21 +53,21 @@ endif ASRCS = CSRCS = mm_initialize.c mm_sem.c mm_addfreechunk.c mm_size2ndx.c CSRCS += mm_shrinkchunk.c -CSRCS += mm_calloc.c mm_free.c mm_mallinfo.c mm_malloc.c mm_memalign.c -CSRCS += mm_realloc.c mm_zalloc.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 # User allocator CSRCS += umm_initialize.c umm_addregion.c umm_sem.c -CSRCS += umm_calloc.c umm_free.c umm_mallinfo.c umm_malloc.c -CSRCS += umm_memalign.c umm_realloc.c umm_zalloc.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 # Kernel allocator ifeq ($(CONFIG_MM_KERNEL_HEAP),y) CSRCS += kmm_initialize.c kmm_addregion.c kmm_sem.c -CSRCS += kmm_calloc.c kmm_free.c kmm_mallinfo.c kmm_malloc.c -CSRCS += kmm_memalign.c kmm_realloc.c kmm_zalloc.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_DEBUG),y) CSRCS += kmm_heapmember.c endif diff --git a/nuttx/mm/kmm_brkaddr.c b/nuttx/mm/kmm_brkaddr.c new file mode 100644 index 000000000..463744a2a --- /dev/null +++ b/nuttx/mm/kmm_brkaddr.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * mm/kmm_breakaddr.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include + +#ifdef CONFIG_MM_KERNEL_HEAP + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: kmm_brkaddr + * + * Description: + * Return the break address of a region in the user heap + * + ****************************************************************************/ + +FAR void *kmm_brkaddr(int region) +{ + return mm_brkaddr(&g_kmmheap, region); +} + +#endif /* CONFIG_MM_KERNEL_HEAP */ diff --git a/nuttx/mm/kmm_extend.c b/nuttx/mm/kmm_extend.c new file mode 100644 index 000000000..1ae1e2865 --- /dev/null +++ b/nuttx/mm/kmm_extend.c @@ -0,0 +1,68 @@ +/**************************************************************************** + * mm/kmm_extend.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#ifdef CONFIG_MM_KERNEL_HEAP + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: kmm_extend + * + * Description: + * Extend a region in the kernel heap by add a block of (virtually) + * contiguous memory to the end of the heap. + * + ****************************************************************************/ + +void kmm_extend(FAR void *mem, size_t size, int region) +{ + mm_extend(&g_kmmheap, mem, size, region); +} + +#endif /* CONFIG_MM_KERNEL_HEAP */ diff --git a/nuttx/mm/mm_brkaddr.c b/nuttx/mm/mm_brkaddr.c new file mode 100644 index 000000000..a75bcd68b --- /dev/null +++ b/nuttx/mm/mm_brkaddr.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * mm/mm_brkaddr.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mm_brkaddr + * + * Description: + * Return the break address of a heap region + * + ****************************************************************************/ + +FAR void *mm_brkaddr(FAR struct mm_heap_s *heap, int region) +{ + uintptr_t brkaddr; + DEBUGASSERT(heap && region < heap->mm_nregions); + + brkaddr = (uintptr_t)heap->mm_heapend[region]; + return (FAR void *)(brkaddr + SIZEOF_MM_ALLOCNODE); +} diff --git a/nuttx/mm/mm_extend.c b/nuttx/mm/mm_extend.c new file mode 100644 index 000000000..3aa3f37d7 --- /dev/null +++ b/nuttx/mm/mm_extend.c @@ -0,0 +1,123 @@ +/**************************************************************************** + * mm/mm_extend.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MIN_EXTEND (2 * SIZEOF_MM_ALLOCNODE) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mm_extend + * + * Description: + * Extend a heap region by add a block of (virtually) contiguous memory + * to the end of the heap. + * + ****************************************************************************/ + +void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size, + int region) +{ + struct mm_allocnode_s *oldnode; + struct mm_allocnode_s *newnode; + uintptr_t blockstart; + uintptr_t blockend; + + /* Make sure that we were passed valid parameters */ + + DEBUGASSERT(heap && mem); + DEBUGASSERT(size >= MIN_EXTEND && (unsigned)region < heap->mm_nregions); + + /* Make sure that the memory region are properly aligned */ + + blockstart = (uintptr_t)mem; + blockend = blockstart + size; + + DEBUGASSERT(MM_ALIGN_UP(blockstart) == blockstart); + DEBUGASSERT(MM_ALIGN_DOWN(blockend) == blockend); + + /* Take the memory manager semaphore */ + + mm_takesemaphore(heap); + + /* Get the terminal node in the old heap. The block to extend must + * immediately follow this node. + */ + + oldnode = heap->mm_heapend[region]; + DEBUGASSERT((uintptr_t)oldnode + SIZEOF_MM_ALLOCNODE == (uintptr_t)mem); + + /* The size of the old node now extends to the new terminal node. + * This is the old size (SIZEOF_MM_ALLOCNODE) plus the size of + * the block (size) minus the size of the new terminal node + * (SIZEOF_MM_ALLOCNODE) or simply: + */ + + oldnode->size = size; + + /* The old node should already be marked as allocated */ + + DEBUGASSERT((oldnode->preceding & MM_ALLOC_BIT) != 0); + + /* Get and initialize the new terminal node in the heap */ + + newnode = (FAR struct mm_allocnode_s *)(blockend - SIZEOF_MM_ALLOCNODE); + newnode->size = SIZEOF_MM_ALLOCNODE; + newnode->preceding = oldnode->size | MM_ALLOC_BIT; + + heap->mm_heapend[region] = newnode; + mm_givesemaphore(heap); + + /* Finally "free" the new block of memory where the old terminal node was + * located. + */ + + mm_free(heap, (FAR void *)mem); +} diff --git a/nuttx/mm/umm_brkaddr.c b/nuttx/mm/umm_brkaddr.c new file mode 100644 index 000000000..dc646f20a --- /dev/null +++ b/nuttx/mm/umm_brkaddr.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * mm/umm_breakaddr.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include + +#ifdef CONFIG_MM_USER_HEAP + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: umm_brkaddr + * + * Description: + * Return the break address of a region in the user heap + * + ****************************************************************************/ + +FAR void *umm_brkaddr(int region) +{ + return mm_brkaddr(&g_mmheap, region); +} + +#endif /* CONFIG_MM_USER_HEAP */ diff --git a/nuttx/mm/umm_extend.c b/nuttx/mm/umm_extend.c new file mode 100644 index 000000000..6e16e56f8 --- /dev/null +++ b/nuttx/mm/umm_extend.c @@ -0,0 +1,68 @@ +/**************************************************************************** + * mm/umm_extend.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#ifdef CONFIG_MM_USER_HEAP + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: umm_extend + * + * Description: + * Extend a region in the user heap by add a block of (virtually) + * contiguous memory to the end of the heap. + * + ****************************************************************************/ + +void umm_extend(FAR void *mem, size_t size, int region) +{ + mm_extend(&g_mmheap, mem, size, region); +} + +#endif /* CONFIG_MM_USER_HEAP */ -- cgit v1.2.3