From 5fb8eb530ad4c493a0d6a6ac0f91b07bc8acb037 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 3 Apr 2011 20:41:49 +0000 Subject: [PATCH] Move memory manager into user space git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@3460 7fd9a85b-ad96-42d3-883c-3090e2eb8679 --- nuttx/Makefile | 24 +++++++++++----------- nuttx/arch/sh/src/common/up_createstack.c | 2 +- nuttx/arch/z16/src/common/up_createstack.c | 2 +- nuttx/arch/z80/src/common/up_createstack.c | 2 +- nuttx/configs/ea3131/locked/mklocked.sh | 2 +- nuttx/fs/fs_files.c | 5 +++-- nuttx/include/nuttx/kmalloc.h | 19 +++++++++++------ nuttx/lib/misc/lib_init.c | 12 +++++++---- nuttx/mm/mm_environment.h | 2 +- nuttx/net/net_sockets.c | 2 +- nuttx/sched/env_dup.c | 7 +++++-- nuttx/sched/env_dupenv.c | 9 +++++--- nuttx/sched/env_putenv.c | 10 +++++---- nuttx/sched/env_setenv.c | 11 +++++----- nuttx/sched/env_unsetenv.c | 9 ++++---- nuttx/sched/mq_open.c | 4 ++-- nuttx/sched/pthread_create.c | 6 +++--- nuttx/sched/sched_garbage.c | 5 ++--- nuttx/sched/task_create.c | 8 ++++++-- nuttx/sched/timer_create.c | 13 ++++++------ 20 files changed, 90 insertions(+), 64 deletions(-) diff --git a/nuttx/Makefile b/nuttx/Makefile index 88ccec36d..30e92e135 100644 --- a/nuttx/Makefile +++ b/nuttx/Makefile @@ -106,7 +106,7 @@ endif # USERDIRS - When NuttX is build is a monolithic kernel, this provides the # list of directories that must be built -NONFSDIRS = sched $(ARCH_SRC) mm $(NUTTX_ADDONS) +NONFSDIRS = sched $(ARCH_SRC) $(NUTTX_ADDONS) FSDIRS = fs drivers binfmt NETFSDIRS = fs drivers CONTEXTDIRS = $(APPDIR) @@ -115,9 +115,9 @@ USERDIRS = ifeq ($(CONFIG_NUTTX_KERNEL),y) NONFSDIRS += syscall CONTEXTDIRS += syscall -USERDIRS += syscall lib $(USER_ADDONS) +USERDIRS += syscall lib mm $(USER_ADDONS) else -NONFSDIRS += lib +NONFSDIRS += lib mm endif ifeq ($(CONFIG_NX),y) @@ -175,16 +175,18 @@ endif # USERLIBS is the list of libraries used to build the final user-space # application -NUTTXLIBS = sched/libsched$(LIBEXT) $(ARCH_SRC)/libarch$(LIBEXT) mm/libmm$(LIBEXT) \ - lib/liblib$(LIBEXT) +NUTTXLIBS = sched/libsched$(LIBEXT) $(ARCH_SRC)/libarch$(LIBEXT) lib/liblib$(LIBEXT) USERLIBS = # Add libraries for syscall support. The C library will be needed by -# both the kernel- and user-space builds. +# both the kernel- and user-space builds. For now, the memory manager (mm) +# is placed in user space (only). ifeq ($(CONFIG_NUTTX_KERNEL),y) NUTTXLIBS += syscall/libstubs$(LIBEXT) -USERLIBS += syscall/libproxies$(LIBEXT) lib/liblib$(LIBEXT) +USERLIBS += syscall/libproxies$(LIBEXT) lib/liblib$(LIBEXT) mm/libmm$(LIBEXT) +else +NUTTXLIBS += mm/libmm$(LIBEXT) endif # Add libraries for network support. CXX, CXXFLAGS, and COMPILEXX must @@ -332,11 +334,6 @@ graphics/libgraphics$(LIBEXT): context syscall/libstubs$(LIBEXT): context @$(MAKE) -C syscall TOPDIR="$(TOPDIR)" libstubs$(LIBEXT) EXTRADEFINES=$(KDEFINE) -# Still need to think about this one - -mm/libmm$(LIBEXT): context - @$(MAKE) -C mm TOPDIR="$(TOPDIR)" libmm$(LIBEXT) - # Possible user-mode builds lib/liblib$(LIBEXT): context @@ -345,6 +342,9 @@ lib/liblib$(LIBEXT): context libxx/liblibxx$(LIBEXT): context @$(MAKE) -C libxx TOPDIR="$(TOPDIR)" liblibxx$(LIBEXT) +mm/libmm$(LIBEXT): context + @$(MAKE) -C mm TOPDIR="$(TOPDIR)" libmm$(LIBEXT) EXTRADEFINES=$(KDEFINE) + $(APPDIR)/libapps$(LIBEXT): context @$(MAKE) -C $(APPDIR) TOPDIR="$(TOPDIR)" libapps$(LIBEXT) diff --git a/nuttx/arch/sh/src/common/up_createstack.c b/nuttx/arch/sh/src/common/up_createstack.c index 35651fe86..37346b84b 100644 --- a/nuttx/arch/sh/src/common/up_createstack.c +++ b/nuttx/arch/sh/src/common/up_createstack.c @@ -94,7 +94,7 @@ int up_create_stack(_TCB *tcb, size_t stack_size) if (!tcb->stack_alloc_ptr) { - tcb->stack_alloc_ptr = (uint32_t*)kzmalloc(stack_size); + tcb->stack_alloc_ptr = (uint32_t*)kzalloc(stack_size); } if (tcb->stack_alloc_ptr) diff --git a/nuttx/arch/z16/src/common/up_createstack.c b/nuttx/arch/z16/src/common/up_createstack.c index a59c79c84..1adcc7835 100644 --- a/nuttx/arch/z16/src/common/up_createstack.c +++ b/nuttx/arch/z16/src/common/up_createstack.c @@ -95,7 +95,7 @@ int up_create_stack(_TCB *tcb, size_t stack_size) if (!tcb->stack_alloc_ptr) { - tcb->stack_alloc_ptr = (uint32_t*)kzmalloc(stack_size); + tcb->stack_alloc_ptr = (uint32_t*)kzalloc(stack_size); } if (tcb->stack_alloc_ptr) diff --git a/nuttx/arch/z80/src/common/up_createstack.c b/nuttx/arch/z80/src/common/up_createstack.c index c9da4cb9f..6ffaa272b 100644 --- a/nuttx/arch/z80/src/common/up_createstack.c +++ b/nuttx/arch/z80/src/common/up_createstack.c @@ -93,7 +93,7 @@ int up_create_stack(_TCB *tcb, size_t stack_size) if (!tcb->stack_alloc_ptr) { - tcb->stack_alloc_ptr = (uint32_t*)kzmalloc(stack_size); + tcb->stack_alloc_ptr = (uint32_t*)kzalloc(stack_size); } if (tcb->stack_alloc_ptr) diff --git a/nuttx/configs/ea3131/locked/mklocked.sh b/nuttx/configs/ea3131/locked/mklocked.sh index bf6d52614..3bfd0726d 100755 --- a/nuttx/configs/ea3131/locked/mklocked.sh +++ b/nuttx/configs/ea3131/locked/mklocked.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash ########################################################################### # configs/ea3131/locked/mklocked.sh # diff --git a/nuttx/fs/fs_files.c b/nuttx/fs/fs_files.c index 4ea735b69..1444d8805 100644 --- a/nuttx/fs/fs_files.c +++ b/nuttx/fs/fs_files.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/fs_files.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,6 +45,7 @@ #include #include #include + #include #include @@ -157,7 +158,7 @@ void files_initialize(void) FAR struct filelist *files_alloclist(void) { FAR struct filelist *list; - list = (FAR struct filelist*)kzmalloc(sizeof(struct filelist)); + list = (FAR struct filelist*)kzalloc(sizeof(struct filelist)); if (list) { /* Start with a reference count of one */ diff --git a/nuttx/include/nuttx/kmalloc.h b/nuttx/include/nuttx/kmalloc.h index 2f2df3663..7fb37a5d0 100644 --- a/nuttx/include/nuttx/kmalloc.h +++ b/nuttx/include/nuttx/kmalloc.h @@ -1,7 +1,7 @@ /**************************************************************************** * include/nuttx/kmalloc.h * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -63,21 +63,28 @@ extern "C" { # include # define kmalloc(s) malloc(s) #else -KMALLOC_EXTERN FAR void *kmalloc(size_t); +KMALLOC_EXTERN FAR void *kmalloc(size_t); #endif -#ifndef CONFIG_ARCH_KZMALLOC +#ifndef CONFIG_ARCH_KZALLOC # include -# define kzmalloc(s) zalloc(s) +# define kzalloc(s) zalloc(s) #else -KMALLOC_EXTERN FAR void *kzalloc(size_t); +KMALLOC_EXTERN FAR void *kzalloc(size_t); +#endif + +#ifndef CONFIG_ARCH_KREALLOC +# include +# define krealloc(p,s) realloc(p,s) +#else +KMALLOC_EXTERN FAR void *krealloc(FAR void*, size_t); #endif #ifndef CONFIG_ARCH_KFREE # include # define kfree(p) free(p) #else -KMALLOC_EXTERN void kfree(FAR void*); +KMALLOC_EXTERN void kfree(FAR void*); #endif /* Functions defined in os_list.c *******************************************/ diff --git a/nuttx/lib/misc/lib_init.c b/nuttx/lib/misc/lib_init.c index bd2c6e7ba..107c73c07 100644 --- a/nuttx/lib/misc/lib_init.c +++ b/nuttx/lib/misc/lib_init.c @@ -1,7 +1,7 @@ /************************************************************ - * lib_init.c + * lib/misc/lib_init.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -37,12 +37,16 @@ * Included Files ************************************************************/ +#include + #include +#include #include #include -#include + #include #include + #include "lib_internal.h" /************************************************************ @@ -79,7 +83,7 @@ void weak_const_function lib_initialize(void) FAR struct streamlist *lib_alloclist(void) { FAR struct streamlist *list; - list = (FAR struct streamlist*)kzmalloc(sizeof(struct streamlist)); + list = (FAR struct streamlist*)zalloc(sizeof(struct streamlist)); if (list) { int i; diff --git a/nuttx/mm/mm_environment.h b/nuttx/mm/mm_environment.h index 67c2ec05a..3525eccc0 100644 --- a/nuttx/mm/mm_environment.h +++ b/nuttx/mm/mm_environment.h @@ -114,7 +114,7 @@ extern void mm_addregion(FAR void *heapstart, size_t heapsize); # define mvdbg(format, arg...) printf(format, ##arg) #else -# define mm_errno (*get_errno_ptr()) +# define mm_errno get_errno() #endif /**************************************************************************** diff --git a/nuttx/net/net_sockets.c b/nuttx/net/net_sockets.c index dce8aa1f0..a1e8f15b0 100644 --- a/nuttx/net/net_sockets.c +++ b/nuttx/net/net_sockets.c @@ -121,7 +121,7 @@ void net_initialize(void) FAR struct socketlist *net_alloclist(void) { FAR struct socketlist *list; - list = (FAR struct socketlist*)kzmalloc(sizeof(struct socketlist)); + list = (FAR struct socketlist*)kzalloc(sizeof(struct socketlist)); if (list) { /* Start with a reference count of one */ diff --git a/nuttx/sched/env_dup.c b/nuttx/sched/env_dup.c index 716dcc47c..cbde5251d 100644 --- a/nuttx/sched/env_dup.c +++ b/nuttx/sched/env_dup.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/env_dup.c * - * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -45,6 +45,9 @@ #include #include #include + +#include + #include "os_internal.h" #include "env_internal.h" @@ -101,7 +104,7 @@ int env_dup(FAR _TCB *ptcb) /* Yes..The parent task has an environment, duplicate it */ size_t envlen = parent->envp->ev_alloc; - envp = (environ_t*)malloc(SIZEOF_ENVIRON_T( envlen )); + envp = (environ_t*)kmalloc(SIZEOF_ENVIRON_T( envlen )); if (!envp) { ret = -ENOMEM; diff --git a/nuttx/sched/env_dupenv.c b/nuttx/sched/env_dupenv.c index d11e44330..fd3ccd7b7 100644 --- a/nuttx/sched/env_dupenv.c +++ b/nuttx/sched/env_dupenv.c @@ -1,7 +1,7 @@ /**************************************************************************** * eched/env_dupenv.c * - * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,7 +43,9 @@ #include #include -#include + +#include + #include "os_internal.h" /**************************************************************************** @@ -91,7 +93,7 @@ FAR environ_t *dupenv(FAR _TCB *ptcb) /* Yes..The parent task has an environment, duplicate it */ size_t envlen = ptcb->envp->ev_alloc - envp = (environ_t*)malloc(SIZEOF_ENVIRON_T( envlen )); + envp = (environ_t*)kmalloc(SIZEOF_ENVIRON_T( envlen )); if (envp) { envp->ev_crefs = 1; @@ -99,6 +101,7 @@ FAR environ_t *dupenv(FAR _TCB *ptcb) memcmp( envp->ev_env, ptcb->envp->ev_env, envlen ); } } + sched_unlock(); return envp; } diff --git a/nuttx/sched/env_putenv.c b/nuttx/sched/env_putenv.c index 43ca44d4d..65c3d4c03 100644 --- a/nuttx/sched/env_putenv.c +++ b/nuttx/sched/env_putenv.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/env_putenv.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,9 +43,10 @@ #include #include -#include #include +#include + /**************************************************************************** * Private Data ****************************************************************************/ @@ -106,11 +107,12 @@ int putenv(FAR const char *string) *pequal = '\0'; ret = setenv(pname, pequal+1, TRUE); } - free(pname); + + kfree(pname); return ret; errout: - *get_errno_ptr() = ret; + errno = ret; return ERROR; } diff --git a/nuttx/sched/env_setenv.c b/nuttx/sched/env_setenv.c index 0eacd7793..8193aa564 100644 --- a/nuttx/sched/env_setenv.c +++ b/nuttx/sched/env_setenv.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/env_setenv.c * - * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -44,9 +44,10 @@ #include #include #include -#include #include +#include + #include "os_internal.h" #include "env_internal.h" @@ -156,7 +157,7 @@ int setenv(const char *name, const char *value, int overwrite) if (envp) { int alloc = envp->ev_alloc; - environ_t *tmp = (environ_t*)realloc(envp, SIZEOF_ENVIRON_T(alloc + varlen)); + environ_t *tmp = (environ_t*)krealloc(envp, SIZEOF_ENVIRON_T(alloc + varlen)); if (!tmp) { ret = ENOMEM; @@ -169,7 +170,7 @@ int setenv(const char *name, const char *value, int overwrite) } else { - envp = (environ_t*)malloc(SIZEOF_ENVIRON_T(varlen)); + envp = (environ_t*)kmalloc(SIZEOF_ENVIRON_T(varlen)); if (!envp) { ret = ENOMEM; @@ -196,7 +197,7 @@ int setenv(const char *name, const char *value, int overwrite) errout_with_lock: sched_unlock(); errout: - *get_errno_ptr() = ret; + errno = ret; return ERROR; } diff --git a/nuttx/sched/env_unsetenv.c b/nuttx/sched/env_unsetenv.c index f397eab6b..a751661c7 100644 --- a/nuttx/sched/env_unsetenv.c +++ b/nuttx/sched/env_unsetenv.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/env_unsetenv.c * - * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,9 +43,10 @@ #include #include -#include #include +#include + #include "os_internal.h" #include "env_internal.h" @@ -109,7 +110,7 @@ int unsetenv(const char *name) /* Reallocate the new environment buffer */ alloc = envp->ev_alloc; - tmp = (environ_t*)realloc(envp, SIZEOF_ENVIRON_T(alloc)); + tmp = (environ_t*)krealloc(envp, SIZEOF_ENVIRON_T(alloc)); if (!tmp) { ret = ENOMEM; @@ -127,7 +128,7 @@ int unsetenv(const char *name) errout_with_lock: sched_unlock(); errout: - *get_errno_ptr() = ret; + errno = ret; return ERROR; } diff --git a/nuttx/sched/mq_open.c b/nuttx/sched/mq_open.c index 9e3a3e77c..de9388195 100644 --- a/nuttx/sched/mq_open.c +++ b/nuttx/sched/mq_open.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/mq_open.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -157,7 +157,7 @@ mqd_t mq_open(const char *mq_name, int oflags, ...) * of the message queue name+1. */ - msgq = (FAR msgq_t*)kzmalloc(SIZEOF_MQ_HEADER + namelen + 1); + msgq = (FAR msgq_t*)kzalloc(SIZEOF_MQ_HEADER + namelen + 1); if (msgq) { /* Create a message queue descriptor for the TCB */ diff --git a/nuttx/sched/pthread_create.c b/nuttx/sched/pthread_create.c index bedb7d3ba..d1f838a9e 100644 --- a/nuttx/sched/pthread_create.c +++ b/nuttx/sched/pthread_create.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/pthread_create.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -262,7 +262,7 @@ int pthread_create(FAR pthread_t *thread, FAR pthread_attr_t *attr, /* Allocate a TCB for the new task. */ - ptcb = (FAR _TCB*)kzmalloc(sizeof(_TCB)); + ptcb = (FAR _TCB*)kzalloc(sizeof(_TCB)); if (!ptcb) { return ENOMEM; @@ -283,7 +283,7 @@ int pthread_create(FAR pthread_t *thread, FAR pthread_attr_t *attr, /* Allocate a detachable structure to support pthread_join logic */ - pjoin = (FAR join_t*)kzmalloc(sizeof(join_t)); + pjoin = (FAR join_t*)kzalloc(sizeof(join_t)); if (!pjoin) { sched_releasetcb(ptcb); diff --git a/nuttx/sched/sched_garbage.c b/nuttx/sched/sched_garbage.c index 7c9f434a1..946198ede 100755 --- a/nuttx/sched/sched_garbage.c +++ b/nuttx/sched/sched_garbage.c @@ -38,8 +38,7 @@ ****************************************************************************/ #include - -#include +#include #include "os_internal.h" @@ -111,7 +110,7 @@ void sched_garbagecollection(void) if (address) { - free(address); + kfree(address); } } } diff --git a/nuttx/sched/task_create.c b/nuttx/sched/task_create.c index d70f337c0..3965e61ef 100644 --- a/nuttx/sched/task_create.c +++ b/nuttx/sched/task_create.c @@ -38,11 +38,15 @@ ****************************************************************************/ #include + #include #include #include #include + #include +#include + #include "os_internal.h" #include "env_internal.h" @@ -121,10 +125,10 @@ int task_create(const char *name, int priority, /* Allocate a TCB for the new task. */ - tcb = (FAR _TCB*)kzmalloc(sizeof(_TCB)); + tcb = (FAR _TCB*)kzalloc(sizeof(_TCB)); if (!tcb) { - *get_errno_ptr() = ENOMEM; + errno = ENOMEM; return ERROR; } diff --git a/nuttx/sched/timer_create.c b/nuttx/sched/timer_create.c index c4f3cbd87..b1ad142c7 100644 --- a/nuttx/sched/timer_create.c +++ b/nuttx/sched/timer_create.c @@ -1,7 +1,7 @@ /******************************************************************************** * sched/timer_create.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -40,13 +40,14 @@ #include #include -#include #include #include #include #include #include +#include + #include "timer_internal.h" #ifndef CONFIG_DISABLE_POSIX_TIMERS @@ -99,7 +100,7 @@ static struct posix_timer_s *timer_allocate(void) { /* Allocate a new timer from the heap */ - ret = (struct posix_timer_s*)malloc(sizeof(struct posix_timer_s)); + ret = (struct posix_timer_s*)kmalloc(sizeof(struct posix_timer_s)); pt_flags = 0; } @@ -183,7 +184,7 @@ int timer_create(clockid_t clockid, FAR struct sigevent *evp, FAR timer_t *timer if (!timerid || clockid != CLOCK_REALTIME) { - *get_errno_ptr() = EINVAL; + errno = EINVAL; return ERROR; } @@ -192,7 +193,7 @@ int timer_create(clockid_t clockid, FAR struct sigevent *evp, FAR timer_t *timer wdog = wd_create(); if (!wdog) { - *get_errno_ptr() = EAGAIN; + errno = EAGAIN; return ERROR; } @@ -201,7 +202,7 @@ int timer_create(clockid_t clockid, FAR struct sigevent *evp, FAR timer_t *timer ret = timer_allocate(); if (!ret) { - *get_errno_ptr() = EAGAIN; + errno = EAGAIN; return ERROR; }