arch/sim: Move share memory allocation to up_hostmemory.c
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
cb1d11a499
commit
7a32a396b0
@ -79,12 +79,9 @@ VPATH = sim
|
||||
DEPPATH = $(patsubst %,--dep-path %,$(subst :, ,$(VPATH)))
|
||||
|
||||
HOSTCFLAGS += ${shell $(INCDIR) "$(CC)" $(ARCH_SRCDIR)}
|
||||
HOSTSRCS = up_hosttime.c
|
||||
|
||||
ifeq ($(CONFIG_LIBC_MODLIB),y)
|
||||
HOSTSRCS += up_hostmemory.c
|
||||
else ifeq ($(CONFIG_BINFMT_LOADABLE),y)
|
||||
HOSTSRCS += up_hostmemory.c
|
||||
HOSTSRCS = up_hostmemory.c up_hosttime.c
|
||||
ifneq ($(CONFIG_HOST_MACOS),y)
|
||||
STDLIBS += -lrt
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_STACK_COLORATION),y)
|
||||
@ -201,10 +198,6 @@ endif
|
||||
|
||||
ifeq ($(CONFIG_RPTUN),y)
|
||||
CSRCS += up_rptun.c
|
||||
HOSTSRCS += up_shmem.c
|
||||
ifneq ($(CONFIG_HOST_MACOS),y)
|
||||
STDLIBS += -lrt
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FS_HOSTFS),y)
|
||||
|
@ -22,10 +22,12 @@
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
@ -47,9 +49,67 @@ void *host_alloc_heap(size_t sz)
|
||||
MAP_ANON | MAP_PRIVATE, -1, 0);
|
||||
if (p == MAP_FAILED)
|
||||
{
|
||||
perror("Failed to allocate heap with mmap");
|
||||
exit(EXIT_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void *host_alloc_shmem(const char *name, size_t size, int master)
|
||||
{
|
||||
void *mem;
|
||||
int oflag;
|
||||
int ret;
|
||||
int fd;
|
||||
|
||||
oflag = O_RDWR;
|
||||
if (master)
|
||||
{
|
||||
oflag |= O_CREAT | O_TRUNC;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
fd = shm_open(name, oflag, S_IRUSR | S_IWUSR);
|
||||
if (fd >= 0)
|
||||
{
|
||||
if (!master)
|
||||
{
|
||||
/* Avoid the second slave instance open successfully */
|
||||
|
||||
shm_unlink(name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (master || errno != ENOENT)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Master isn't ready, sleep and try again */
|
||||
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
ret = ftruncate(fd, size);
|
||||
if (ret < 0)
|
||||
{
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
close(fd); /* Don't need keep fd any more once the memory get mapped */
|
||||
if (mem == MAP_FAILED)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
void host_free_shmem(void *mem)
|
||||
{
|
||||
munmap(mem, 0);
|
||||
}
|
||||
|
@ -227,6 +227,8 @@ void up_longjmp(xcpt_reg_t *jb, int val) noreturn_function;
|
||||
/* up_hostmemory.c **********************************************************/
|
||||
|
||||
void *host_alloc_heap(size_t sz);
|
||||
void *host_alloc_shmem(const char *name, size_t size, int master);
|
||||
void host_free_shmem(void *mem);
|
||||
|
||||
/* up_hosttime.c ************************************************************/
|
||||
|
||||
@ -377,18 +379,11 @@ void netdriver_setmtu(int mtu);
|
||||
void netdriver_loop(void);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_RPTUN
|
||||
|
||||
/* up_shmem.c ***************************************************************/
|
||||
|
||||
void *shmem_open(const char *name, size_t size, int master);
|
||||
void shmem_close(void *mem);
|
||||
|
||||
/* up_rptun.c ***************************************************************/
|
||||
|
||||
#ifdef CONFIG_RPTUN
|
||||
int up_rptun_init(void);
|
||||
void up_rptun_loop(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SIM_SPIFLASH
|
||||
|
@ -192,9 +192,9 @@ int up_rptun_init(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
g_dev.shmem = shmem_open("rptun-shmem",
|
||||
sizeof(*g_dev.shmem),
|
||||
CONFIG_SIM_RPTUN_MASTER);
|
||||
g_dev.shmem = host_alloc_shmem("rptun-shmem",
|
||||
sizeof(*g_dev.shmem),
|
||||
CONFIG_SIM_RPTUN_MASTER);
|
||||
if (g_dev.shmem == NULL)
|
||||
{
|
||||
return -ENOMEM;
|
||||
@ -244,7 +244,7 @@ int up_rptun_init(void)
|
||||
ret = rptun_initialize(&g_dev.rptun);
|
||||
if (ret < 0)
|
||||
{
|
||||
shmem_close(g_dev.shmem);
|
||||
host_free_shmem(g_dev.shmem);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1,110 +0,0 @@
|
||||
/****************************************************************************
|
||||
* arch/sim/src/sim/up_shmem.c
|
||||
*
|
||||
* Copyright (C) 2019 Xiaomi Inc. All rights reserved.
|
||||
* Author: Chao An <anchao@pinecone.net>
|
||||
*
|
||||
* 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
|
||||
****************************************************************************/
|
||||
|
||||
#define _GNU_SOURCE 1
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
void *shmem_open(const char *name, size_t size, int master)
|
||||
{
|
||||
void *mem;
|
||||
int oflag;
|
||||
int ret;
|
||||
int fd;
|
||||
|
||||
oflag = O_RDWR;
|
||||
if (master)
|
||||
{
|
||||
oflag |= O_CREAT | O_TRUNC;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
fd = shm_open(name, oflag, S_IRUSR | S_IWUSR);
|
||||
if (fd >= 0)
|
||||
{
|
||||
if (!master)
|
||||
{
|
||||
/* Avoid the second slave instance open successfully */
|
||||
|
||||
shm_unlink(name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (master || errno != ENOENT)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Master isn't ready, sleep and try again */
|
||||
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
ret = ftruncate(fd, size);
|
||||
if (ret < 0)
|
||||
{
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
close(fd); /* Don't need keep fd any more once the memory get mapped */
|
||||
if (mem == MAP_FAILED)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
void shmem_close(void *mem)
|
||||
{
|
||||
munmap(mem, 0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user