Alloc different sizes for pipe and fifo buffers

This commit is contained in:
Gregory Nutt 2016-07-19 13:34:18 -06:00
parent 8505bf814e
commit d6bf67f9ff
6 changed files with 72 additions and 26 deletions

View File

@ -29,6 +29,7 @@ config DEV_RANDOM
bool "Enable /dev/random" bool "Enable /dev/random"
default y default y
depends on ARCH_HAVE_RNG depends on ARCH_HAVE_RNG
---help---
Enable support for /dev/urandom provided by a hardware TRNG. Enable support for /dev/urandom provided by a hardware TRNG.
config DEV_URANDOM config DEV_URANDOM

View File

@ -3,8 +3,22 @@
# see the file kconfig-language.txt in the NuttX tools repository. # see the file kconfig-language.txt in the NuttX tools repository.
# #
config DEV_PIPE_SIZE config DEV_PIPE_MAXSIZE
int "Pipe Size" int "Maximum pipe/FIFO size"
default 1024 default 1024
---help--- ---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.

View File

@ -48,7 +48,7 @@
#include "pipe_common.h" #include "pipe_common.h"
#if CONFIG_DEV_PIPE_SIZE > 0 #if CONFIG_DEV_FIFO_SIZE > 0
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
@ -110,7 +110,7 @@ int mkfifo(FAR const char *pathname, mode_t mode)
/* Allocate and initialize a new device structure instance */ /* Allocate and initialize a new device structure instance */
dev = pipecommon_allocdev(); dev = pipecommon_allocdev(CONFIG_DEV_FIFO_SIZE);
if (!dev) if (!dev)
{ {
return -ENOMEM; return -ENOMEM;
@ -125,4 +125,4 @@ int mkfifo(FAR const char *pathname, mode_t mode)
return ret; return ret;
} }
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */ #endif /* CONFIG_DEV_FIFO_SIZE > 0 */

View File

@ -222,7 +222,7 @@ int pipe(int fd[2])
{ {
/* No.. Allocate and initialize a new device structure instance */ /* No.. Allocate and initialize a new device structure instance */
dev = pipecommon_allocdev(); dev = pipecommon_allocdev(CONFIG_DEV_PIPE_SIZE);
if (!dev) if (!dev)
{ {
(void)sem_post(&g_pipesem); (void)sem_post(&g_pipesem);

View File

@ -62,7 +62,7 @@
#include "pipe_common.h" #include "pipe_common.h"
#if CONFIG_DEV_PIPE_SIZE > 0 #ifdef CONFIG_PIPES
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
@ -153,10 +153,12 @@ static void pipecommon_pollnotify(FAR struct pipe_dev_s *dev,
* Name: pipecommon_allocdev * 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; FAR struct pipe_dev_s *dev;
DEBUGASSERT(bufsize <= CONFIG_DEV_PIPE_MAXSIZE)
/* Allocate a private structure to manage the pipe */ /* Allocate a private structure to manage the pipe */
dev = (FAR struct pipe_dev_s *)kmm_malloc(sizeof(struct pipe_dev_s)); 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_bfsem, 0, 1);
sem_init(&dev->d_rdsem, 0, 0); sem_init(&dev->d_rdsem, 0, 0);
sem_init(&dev->d_wrsem, 0, 0); sem_init(&dev->d_wrsem, 0, 0);
dev->d_bufsize = bufsize;
} }
return dev; return dev;
@ -217,7 +221,7 @@ int pipecommon_open(FAR struct file *filep)
if (dev->d_refs == 0 && dev->d_buffer == NULL) 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) if (!dev->d_buffer)
{ {
(void)sem_post(&dev->d_bfsem); (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) while ((size_t)nread < len && dev->d_wrndx != dev->d_rdndx)
{ {
*buffer++ = dev->d_buffer[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; 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 */ /* Calculate the write index AFTER the next byte is written */
nxtwrndx = dev->d_wrndx + 1; nxtwrndx = dev->d_wrndx + 1;
if (nxtwrndx >= CONFIG_DEV_PIPE_SIZE) if (nxtwrndx >= dev->d_bufsize)
{ {
nxtwrndx = 0; nxtwrndx = 0;
} }
@ -677,14 +681,14 @@ int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds,
} }
else 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 /* Notify the POLLOUT event if the pipe is not full, but only if
* there is readers. */ * there is readers. */
eventset = 0; eventset = 0;
if (nbytes < (CONFIG_DEV_PIPE_SIZE-1)) if (nbytes < (dev->d_bufsize - 1))
{ {
eventset |= POLLOUT; 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) 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 else
{ {
@ -813,7 +817,7 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
} }
else 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; *(FAR int *)((uintptr_t)arg) = count;
@ -865,4 +869,4 @@ int pipecommon_unlink(FAR struct inode *inode)
} }
#endif #endif
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */ #endif /* CONFIG_PIPES */

View File

@ -47,15 +47,42 @@
#include <stdbool.h> #include <stdbool.h>
#include <poll.h> #include <poll.h>
/****************************************************************************
* 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 #ifndef CONFIG_DEV_PIPE_SIZE
# define CONFIG_DEV_PIPE_SIZE 1024 # define CONFIG_DEV_PIPE_SIZE 1024
#endif #endif
#if CONFIG_DEV_PIPE_SIZE > 0 #ifndef CONFIG_DEV_FIFO_SIZE
# define CONFIG_DEV_FIFO_SIZE 1024
/**************************************************************************** #endif
* Pre-processor Definitions
****************************************************************************/
/* Maximum number of threads than can be waiting for POLL events */ /* 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 */ /* 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 */ 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 */ typedef uint16_t pipe_ndx_t; /* 16-bit index */
#else #else
typedef uint8_t pipe_ndx_t; /* 8-bit index */ 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 */ 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_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_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_refs; /* References counts on pipe (limited to 255) */
uint8_t d_nwriters; /* Number of reference counts for write access */ uint8_t d_nwriters; /* Number of reference counts for write access */
uint8_t d_nreaders; /* Number of reference counts for read access */ uint8_t d_nreaders; /* Number of reference counts for read access */
@ -138,7 +166,7 @@ extern "C" {
struct file; /* Forward reference */ struct file; /* Forward reference */
struct inode; /* 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); void pipecommon_freedev(FAR struct pipe_dev_s *dev);
int pipecommon_open(FAR struct file *filep); int pipecommon_open(FAR struct file *filep);
int pipecommon_close(FAR struct file *filep); int pipecommon_close(FAR struct file *filep);
@ -158,5 +186,4 @@ int pipecommon_unlink(FAR struct inode *priv);
} }
#endif #endif
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */
#endif /* __DRIVERS_PIPES_PIPE_COMMON_H */ #endif /* __DRIVERS_PIPES_PIPE_COMMON_H */