From d6bf67f9ff1e6df3a4e3c3f6046e70e243691420 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 19 Jul 2016 13:34:18 -0600 Subject: [PATCH] Alloc different sizes for pipe and fifo buffers --- drivers/Kconfig | 1 + drivers/pipes/Kconfig | 20 ++++++++++++++--- drivers/pipes/fifo.c | 6 ++--- drivers/pipes/pipe.c | 2 +- drivers/pipes/pipe_common.c | 24 +++++++++++--------- drivers/pipes/pipe_common.h | 45 +++++++++++++++++++++++++++++-------- 6 files changed, 72 insertions(+), 26 deletions(-) diff --git a/drivers/Kconfig b/drivers/Kconfig index 22f1712768..ce90630be4 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -29,6 +29,7 @@ config DEV_RANDOM bool "Enable /dev/random" default y depends on ARCH_HAVE_RNG + ---help--- Enable support for /dev/urandom provided by a hardware TRNG. config DEV_URANDOM diff --git a/drivers/pipes/Kconfig b/drivers/pipes/Kconfig index 64a3fb720b..760f9384dd 100644 --- a/drivers/pipes/Kconfig +++ b/drivers/pipes/Kconfig @@ -3,8 +3,22 @@ # see the file kconfig-language.txt in the NuttX tools repository. # -config DEV_PIPE_SIZE - int "Pipe Size" +config DEV_PIPE_MAXSIZE + int "Maximum pipe/FIFO size" default 1024 ---help--- - Sets the size of the pipe ringbuffer in bytes. + Maximum configurable size of a pipe or FIFO at runtime. + +config DEV_PIPE_SIZE + int "Default pipe size" + default 1024 + ---help--- + Sets the default size of the pipe ringbuffer in bytes. A value of + zero disables pipe support. + +config DEV_FIFO_SIZE + int "Default FIFO size" + default 1024 + ---help--- + Sets the default size of the FIFO ringbuffer in bytes. A value of + zero disables FIFO support. diff --git a/drivers/pipes/fifo.c b/drivers/pipes/fifo.c index b63f9a1856..ec6c22e708 100644 --- a/drivers/pipes/fifo.c +++ b/drivers/pipes/fifo.c @@ -48,7 +48,7 @@ #include "pipe_common.h" -#if CONFIG_DEV_PIPE_SIZE > 0 +#if CONFIG_DEV_FIFO_SIZE > 0 /**************************************************************************** * Private Data @@ -110,7 +110,7 @@ int mkfifo(FAR const char *pathname, mode_t mode) /* Allocate and initialize a new device structure instance */ - dev = pipecommon_allocdev(); + dev = pipecommon_allocdev(CONFIG_DEV_FIFO_SIZE); if (!dev) { return -ENOMEM; @@ -125,4 +125,4 @@ int mkfifo(FAR const char *pathname, mode_t mode) return ret; } -#endif /* CONFIG_DEV_PIPE_SIZE > 0 */ +#endif /* CONFIG_DEV_FIFO_SIZE > 0 */ diff --git a/drivers/pipes/pipe.c b/drivers/pipes/pipe.c index 715639d21c..e006c3aac6 100644 --- a/drivers/pipes/pipe.c +++ b/drivers/pipes/pipe.c @@ -222,7 +222,7 @@ int pipe(int fd[2]) { /* No.. Allocate and initialize a new device structure instance */ - dev = pipecommon_allocdev(); + dev = pipecommon_allocdev(CONFIG_DEV_PIPE_SIZE); if (!dev) { (void)sem_post(&g_pipesem); diff --git a/drivers/pipes/pipe_common.c b/drivers/pipes/pipe_common.c index 74d36ec170..25c51e76d4 100644 --- a/drivers/pipes/pipe_common.c +++ b/drivers/pipes/pipe_common.c @@ -62,7 +62,7 @@ #include "pipe_common.h" -#if CONFIG_DEV_PIPE_SIZE > 0 +#ifdef CONFIG_PIPES /**************************************************************************** * Pre-processor Definitions @@ -153,10 +153,12 @@ static void pipecommon_pollnotify(FAR struct pipe_dev_s *dev, * Name: pipecommon_allocdev ****************************************************************************/ -FAR struct pipe_dev_s *pipecommon_allocdev(void) +FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize) { FAR struct pipe_dev_s *dev; + DEBUGASSERT(bufsize <= CONFIG_DEV_PIPE_MAXSIZE) + /* Allocate a private structure to manage the pipe */ dev = (FAR struct pipe_dev_s *)kmm_malloc(sizeof(struct pipe_dev_s)); @@ -168,6 +170,8 @@ FAR struct pipe_dev_s *pipecommon_allocdev(void) sem_init(&dev->d_bfsem, 0, 1); sem_init(&dev->d_rdsem, 0, 0); sem_init(&dev->d_wrsem, 0, 0); + + dev->d_bufsize = bufsize; } return dev; @@ -217,7 +221,7 @@ int pipecommon_open(FAR struct file *filep) if (dev->d_refs == 0 && dev->d_buffer == NULL) { - dev->d_buffer = (FAR uint8_t *)kmm_malloc(CONFIG_DEV_PIPE_SIZE); + dev->d_buffer = (FAR uint8_t *)kmm_malloc(dev->d_bufsize); if (!dev->d_buffer) { (void)sem_post(&dev->d_bfsem); @@ -471,7 +475,7 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) while ((size_t)nread < len && dev->d_wrndx != dev->d_rdndx) { *buffer++ = dev->d_buffer[dev->d_rdndx]; - if (++dev->d_rdndx >= CONFIG_DEV_PIPE_SIZE) + if (++dev->d_rdndx >= dev->d_bufsize) { dev->d_rdndx = 0; } @@ -546,7 +550,7 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, /* Calculate the write index AFTER the next byte is written */ nxtwrndx = dev->d_wrndx + 1; - if (nxtwrndx >= CONFIG_DEV_PIPE_SIZE) + if (nxtwrndx >= dev->d_bufsize) { nxtwrndx = 0; } @@ -677,14 +681,14 @@ int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds, } else { - nbytes = (CONFIG_DEV_PIPE_SIZE-1) + dev->d_wrndx - dev->d_rdndx; + nbytes = (dev->d_bufsize - 1) + dev->d_wrndx - dev->d_rdndx; } /* Notify the POLLOUT event if the pipe is not full, but only if * there is readers. */ eventset = 0; - if (nbytes < (CONFIG_DEV_PIPE_SIZE-1)) + if (nbytes < (dev->d_bufsize - 1)) { eventset |= POLLOUT; } @@ -789,7 +793,7 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg) if (dev->d_wrndx < dev->d_rdndx) { - count = (CONFIG_DEV_PIPE_SIZE - dev->d_rdndx) + dev->d_wrndx; + count = (dev->d_bufsize - dev->d_rdndx) + dev->d_wrndx; } else { @@ -813,7 +817,7 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } else { - count = ((CONFIG_DEV_PIPE_SIZE - dev->d_wrndx) + dev->d_rdndx) - 1; + count = ((dev->d_bufsize - dev->d_wrndx) + dev->d_rdndx) - 1; } *(FAR int *)((uintptr_t)arg) = count; @@ -865,4 +869,4 @@ int pipecommon_unlink(FAR struct inode *inode) } #endif -#endif /* CONFIG_DEV_PIPE_SIZE > 0 */ +#endif /* CONFIG_PIPES */ diff --git a/drivers/pipes/pipe_common.h b/drivers/pipes/pipe_common.h index ae301795ec..ae8c401ebc 100644 --- a/drivers/pipes/pipe_common.h +++ b/drivers/pipes/pipe_common.h @@ -47,15 +47,42 @@ #include #include +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Pipe/FIFO support */ + +#ifndef CONFIG_PIPES +# undef CONFIG_DEV_PIPE_MAXSIZE +# undef CONFIG_DEV_PIPE_SIZE +# undef CONFIG_DEV_FIFO_SIZE +# define CONFIG_DEV_PIPE_MAXSIZE 0 +# define CONFIG_DEV_PIPE_SIZE 0 +# define CONFIG_DEV_FIFO_SIZE 0 +#endif + +/* Pipe/FIFO size */ + +#ifndef CONFIG_DEV_PIPE_MAXSIZE +# define CONFIG_DEV_PIPE_MAXSIZE 1024 +#endif + +#if CONFIG_DEV_PIPE_MAXSIZE <= 0 +# undef CONFIG_PIPES +# undef CONFIG_DEV_PIPE_SIZE +# undef CONFIG_DEV_FIFO_SIZE +# define CONFIG_DEV_PIPE_SIZE 0 +# define CONFIG_DEV_FIFO_SIZE 0 +#endif + #ifndef CONFIG_DEV_PIPE_SIZE # define CONFIG_DEV_PIPE_SIZE 1024 #endif -#if CONFIG_DEV_PIPE_SIZE > 0 - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ +#ifndef CONFIG_DEV_FIFO_SIZE +# define CONFIG_DEV_FIFO_SIZE 1024 +#endif /* Maximum number of threads than can be waiting for POLL events */ @@ -87,9 +114,9 @@ /* Make the buffer index as small as possible for the configured pipe size */ -#if CONFIG_DEV_PIPE_SIZE > 65535 +#if CONFIG_DEV_PIPE_MAXSIZE > 65535 typedef uint32_t pipe_ndx_t; /* 32-bit index */ -#elif CONFIG_DEV_PIPE_SIZE > 255 +#elif CONFIG_DEV_PIPE_MAXSIZE > 255 typedef uint16_t pipe_ndx_t; /* 16-bit index */ #else typedef uint8_t pipe_ndx_t; /* 8-bit index */ @@ -107,6 +134,7 @@ struct pipe_dev_s sem_t d_wrsem; /* Full buffer - Writer waits for data read */ pipe_ndx_t d_wrndx; /* Index in d_buffer to save next byte written */ pipe_ndx_t d_rdndx; /* Index in d_buffer to return the next byte read */ + pipe_ndx_t d_bufsize; /* allocated size of d_buffer in bytes */ uint8_t d_refs; /* References counts on pipe (limited to 255) */ uint8_t d_nwriters; /* Number of reference counts for write access */ uint8_t d_nreaders; /* Number of reference counts for read access */ @@ -138,7 +166,7 @@ extern "C" { struct file; /* Forward reference */ struct inode; /* Forward reference */ -FAR struct pipe_dev_s *pipecommon_allocdev(void); +FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize); void pipecommon_freedev(FAR struct pipe_dev_s *dev); int pipecommon_open(FAR struct file *filep); int pipecommon_close(FAR struct file *filep); @@ -158,5 +186,4 @@ int pipecommon_unlink(FAR struct inode *priv); } #endif -#endif /* CONFIG_DEV_PIPE_SIZE > 0 */ #endif /* __DRIVERS_PIPES_PIPE_COMMON_H */