From 86eef8ce3ae4ac0e67cecdf21f1e71c6754cd15b Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Thu, 23 Aug 2018 09:38:49 -0600 Subject: [PATCH] mm/: add mm_heapmember function and reimplement kmm_heapmember base on mm_heapmember since this function is very useful if multiple heaps exist. --- binfmt/binfmt_initialize.c | 101 ++++++++++++++++++++++++++++++++++ include/nuttx/mm/mm.h | 10 +++- mm/kmm_heap/Make.defs | 6 +- mm/kmm_heap/kmm_heapmember.c | 41 +------------- mm/mm_heap/Make.defs | 2 +- mm/mm_heap/mm_heapmember.c | 103 +++++++++++++++++++++++++++++++++++ mm/umm_heap/Make.defs | 2 +- mm/umm_heap/umm_heapmember.c | 69 +++++++++++++++++++++++ 8 files changed, 288 insertions(+), 46 deletions(-) create mode 100644 binfmt/binfmt_initialize.c create mode 100644 mm/mm_heap/mm_heapmember.c create mode 100644 mm/umm_heap/umm_heapmember.c diff --git a/binfmt/binfmt_initialize.c b/binfmt/binfmt_initialize.c new file mode 100644 index 0000000000..6d50a72954 --- /dev/null +++ b/binfmt/binfmt_initialize.c @@ -0,0 +1,101 @@ +/**************************************************************************** + * binfmt/binfmt_initialize.c + * + * Copyright (C) 2018 Pinecone Inc. All rights reserved. + * Author: Xiang Xiao + * + * 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 +#include +#include +#include + +#ifndef CONFIG_BINFMT_DISABLE + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: binfmt_initialize + * + * Description: + * initialize binfmt subsystem + * + ****************************************************************************/ + +void binfmt_initialize(void) +{ + int ret; + +#ifdef CONFIG_FS_BINFS + ret = builtin_initialize(); + if (ret < 0) + { + berr("ERROR: builtin_initialize failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_ELF + ret = elf_initialize(); + if (ret < 0) + { + berr("ERROR: elf_initialize failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_BINFMT_PCODE + ret = pcode_initialize(); + if (ret < 0) + { + berr("ERROR: pcode_initialize failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_NXFLAT + ret = nxflat_initialize(); + if (ret < 0) + { + berr("ERROR: nxflat_initialize failed: %d\n", ret); + } +#endif + + UNUSED(ret); +} + +#endif /* CONFIG_BINFMT_DISABLE */ diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h index ff71ef2308..698479d421 100644 --- a/include/nuttx/mm/mm.h +++ b/include/nuttx/mm/mm.h @@ -426,9 +426,17 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment, FAR void *kmm_memalign(size_t alignment, size_t size); #endif +/* Functions contained in mm_heapmember.c ***********************************/ + +bool mm_heapmember(FAR struct mm_heap_s *heap, FAR void *mem); + +/* Functions contained in mm_uheapmember.c **********************************/ + +bool umm_heapmember(FAR void *mem); + /* Functions contained in kmm_heapmember.c **********************************/ -#if defined(CONFIG_MM_KERNEL_HEAP) && defined(CONFIG_DEBUG_FEATURES) +#ifdef CONFIG_MM_KERNEL_HEAP bool kmm_heapmember(FAR void *mem); #endif diff --git a/mm/kmm_heap/Make.defs b/mm/kmm_heap/Make.defs index 66ac454bde..03a512cf1e 100644 --- a/mm/kmm_heap/Make.defs +++ b/mm/kmm_heap/Make.defs @@ -39,16 +39,12 @@ 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 +CSRCS += kmm_malloc.c kmm_memalign.c kmm_realloc.c kmm_zalloc.c kmm_heapmember.c ifeq ($(CONFIG_BUILD_KERNEL),y) CSRCS += kmm_sbrk.c endif -ifeq ($(CONFIG_DEBUG_FEATURES),y) -CSRCS += kmm_heapmember.c -endif - # Add the kernel heap directory to the build DEPPATH += --dep-path kmm_heap diff --git a/mm/kmm_heap/kmm_heapmember.c b/mm/kmm_heap/kmm_heapmember.c index 5999631009..5be3515efd 100644 --- a/mm/kmm_heap/kmm_heapmember.c +++ b/mm/kmm_heap/kmm_heapmember.c @@ -43,7 +43,7 @@ #include -#if defined(CONFIG_MM_KERNEL_HEAP) && defined(CONFIG_DEBUG_FEATURES) +#ifdef CONFIG_MM_KERNEL_HEAP /**************************************************************************** * Public Functions @@ -67,42 +67,7 @@ bool kmm_heapmember(FAR void *mem) { -#if CONFIG_MM_REGIONS > 1 - int i; - - /* A valid address from the kernel heap for this region would have to lie - * between the region's two guard nodes. - */ - - for (i = 0; i < g_kmmheap.mm_nregions; i++) - { - if (mem > (FAR void *)g_kmmheap.mm_heapstart[i] && - mem < (FAR void *)g_kmmheap.mm_heapend[i]) - { - return true; - } - } - - /* The address does not like any any region assigned to kernel heap */ - - return false; - -#else - /* A valid address from the kernel heap would have to lie between the - * two guard nodes. - */ - - if (mem > (FAR void *)g_kmmheap.mm_heapstart[0] && - mem < (FAR void *)g_kmmheap.mm_heapend[0]) - { - return true; - } - - /* Otherwise, the address does not lie in the kernel heap */ - - return false; - -#endif + return mm_heapmember(&g_kmmheap, mem); } -#endif /* CONFIG_MM_KERNEL_HEAP && CONFIG_DEBUG_FEATURES */ +#endif /* CONFIG_MM_KERNEL_HEAP */ diff --git a/mm/mm_heap/Make.defs b/mm/mm_heap/Make.defs index 07559ad5b3..0dd77306b5 100644 --- a/mm/mm_heap/Make.defs +++ b/mm/mm_heap/Make.defs @@ -38,7 +38,7 @@ CSRCS += mm_initialize.c mm_sem.c mm_addfreechunk.c mm_size2ndx.c 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 +CSRCS += mm_malloc.c mm_memalign.c mm_realloc.c mm_zalloc.c mm_heapmember.c ifeq ($(CONFIG_BUILD_KERNEL),y) CSRCS += mm_sbrk.c diff --git a/mm/mm_heap/mm_heapmember.c b/mm/mm_heap/mm_heapmember.c new file mode 100644 index 0000000000..86bf13ec7c --- /dev/null +++ b/mm/mm_heap/mm_heapmember.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * mm/mm_heap/mm_heapmember.c + * + * Copyright (C) 2018 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 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mm_heapmember + * + * Description: + * Check if an address lies in the heap. + * + * Parameters: + * heap - The heap to check + * mem - The address to check + * + * Return Value: + * true if the address is a member of the heap. false if not + * not. If the address is not a member of the heap, then it + * must be a member of the user-space heap (unchecked) + * + ****************************************************************************/ + +bool mm_heapmember(FAR struct mm_heap_s *heap, FAR void *mem) +{ +#if CONFIG_MM_REGIONS > 1 + int i; + + /* A valid address from the heap for this region would have to lie + * between the region's two guard nodes. + */ + + for (i = 0; i < heap->mm_nregions; i++) + { + if (mem > (FAR void *)heap->mm_heapstart[i] && + mem < (FAR void *)heap->mm_heapend[i]) + { + return true; + } + } + + /* The address does not like any any region assigned to the heap */ + + return false; + +#else + /* A valid address from the heap would have to lie between the + * two guard nodes. + */ + + if (mem > (FAR void *)heap->mm_heapstart[0] && + mem < (FAR void *)heap->mm_heapend[0]) + { + return true; + } + + /* Otherwise, the address does not lie in the heap */ + + return false; + +#endif +} diff --git a/mm/umm_heap/Make.defs b/mm/umm_heap/Make.defs index 20409c3806..11f43ebf5a 100644 --- a/mm/umm_heap/Make.defs +++ b/mm/umm_heap/Make.defs @@ -37,7 +37,7 @@ 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 +CSRCS += umm_malloc.c umm_memalign.c umm_realloc.c umm_zalloc.c umm_heapmember.c CSRCS += umm_globals.c ifeq ($(CONFIG_BUILD_KERNEL),y) diff --git a/mm/umm_heap/umm_heapmember.c b/mm/umm_heap/umm_heapmember.c new file mode 100644 index 0000000000..218795a5a9 --- /dev/null +++ b/mm/umm_heap/umm_heapmember.c @@ -0,0 +1,69 @@ +/**************************************************************************** + * umm/umm_heap/umm_heapmember.c + * + * Copyright (C) 2018 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 "umm_heap/umm_heap.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: umm_heapmember + * + * Description: + * Check if an address lies in the user heap. + * + * Parameters: + * mem - The address to check + * + * Return Value: + * true if the address is a member of the user heap. false if not + * not. If the address is not a member of the user heap, then it + * must be a member of the user-space heap (unchecked) + * + ****************************************************************************/ + +bool umm_heapmember(FAR void *mem) +{ + return mm_heapmember(USR_HEAP, mem); +}