aboutsummaryrefslogtreecommitdiff
path: root/libpipe
diff options
context:
space:
mode:
Diffstat (limited to 'libpipe')
-rw-r--r--libpipe/ChangeLog137
-rw-r--r--libpipe/Makefile7
-rw-r--r--libpipe/pipe-funcs.c2
-rw-r--r--libpipe/pipe.c57
-rw-r--r--libpipe/pipe.h83
-rw-r--r--libpipe/pq-funcs.c2
-rw-r--r--libpipe/pq.c64
-rw-r--r--libpipe/pq.h68
8 files changed, 196 insertions, 224 deletions
diff --git a/libpipe/ChangeLog b/libpipe/ChangeLog
deleted file mode 100644
index cbc850c4..00000000
--- a/libpipe/ChangeLog
+++ /dev/null
@@ -1,137 +0,0 @@
-Tue Jul 16 11:33:34 1996 Michael I. Bushnell, p/BSG <mib@gnu.ai.mit.edu>
-
- * pipe.h (EWOULDBLOCK): Define to work around new libc bug.
-
-Mon Jul 1 17:29:07 1996 Miles Bader <miles@gnu.ai.mit.edu>
-
- * pq.c (pq_queue): Initialize PACKET->buf_vm_alloced.
-
-Tue Jan 23 12:44:40 1996 Miles Bader <miles@gnu.ai.mit.edu>
-
- * pq.h (packet_ensure, packet_ensure_efficiently): Use packet_fit().
- (packet_fit): New function.
- * pq.c (packet_read): If there's lots of empty space at the
- beginning of a vm_alloced buffer, deallocate it.
-
-Mon Jan 22 17:12:39 1996 Miles Bader <miles@gnu.ai.mit.edu>
-
- * pq.c (packet_realloc): Reflect in the new values of BUF_START &
- BUF_END that we've removed any empty space at the beginning of BUF.
-
-Sat Jan 13 13:56:13 1996 Miles Bader <miles@gnu.ai.mit.edu>
-
- * pq.h (packet_ensure, packet_ensure_efficiently): Use
- packet_new_size() instead of packet_size_adjust().
- (packet_size_adjust): Declaration removed.
- (packet_new_size): New declaration.
- * pq.c (packet_size_adjust): Function removed.
- (packet_new_size): New function.
-
- * pq.c (packet_read): Re-arrange to be slightly less confusing.
- Reverse start-past-buf-beginning test that may have leaked memory.
-
- * pipe.c (pipe_send): For non-blocking writes, avoid writing more
- than the user requested.
-
-Fri Jan 12 12:15:06 1996 Miles Bader <miles@gnu.ai.mit.edu>
-
- * pq.c (pq_queue): Initialize the ports_alloced field.
- (packet_read): When a page-aligned read consumes the whole buffer,
- but there's a non-page-multiple amount available, don't let
- buf_len become negative.
-
-Mon Oct 9 14:57:48 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
-
- * Makefile: Specify shared library dependencies.
-
-Thu Sep 7 09:08:30 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pq.c (packet_read): Don't leave PACKET in a fucked up state when
- it's vm_allocate'd but doesn't a page-multiple amount of data and
- we're reading everything.
-
- * pipe.c (_pipe_no_readers): REALLY wake up writers when the pipe
- breaks.
-
-Fri Sep 1 10:42:03 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pipe.c (_pipe_no_readers): Wake up write selects too when the
- pipe breaks.
-
-Thu Aug 31 14:39:21 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pipe.c (pipe_send): Make writes blockable.
- (pipe_recv): Wakeup blocked writers.
- (pipe_kick): Function deleted.
- (pipe_create): Make connection-oriented pipes start out broken.
- (_pipe_first_reader): New function.
- (_pipe_first_writer): Don't check whether PIPE is connection-
- oriented before clearing PIPE_BROKEN, as otherwise it will never
- be set.
- (pipe_pair_select): New function.
- (pipe_multiple_lock): New variable.
- * pipe.h (pipe_wait): Renamed to `pipe_wait_readable'.
- (pipe_select): Renamed to `pipe_select_readable'.
- (pipe_writable, pipe_wait_writable, pipe_select_writable): New funcs.
- (pipe_acquire_reader): Call _pipe_first_reader if necessary.
- (_pipe_first_reader): New declaration.
- (struct pipe): New fields: `write_limit', `write_atomic',
- `pending_writes', `pending_write_selects'.
- (struct pipe): `pending_selects' changed to `pending_read_selects'.
- (pipe_pair_select): New declaration.
-
-Tue Aug 29 14:37:49 1995 Miles Bader <miles@geech.gnu.ai.mit.edu>
-
- * pipe.c (pipe_send): Use condition_broadcast, not condition_signal.
- * pipe.h (pipe_select): New function.
-
- * pipe.h (struct pipe): Remove interrupt_seq_num field.
- (pipe_wait): Use hurd_condition_wait to detect interrupts instead
- of previous ad-hoc mechanism.
-
- * pipe.c (pipe_create): Don't initialize interrupt_seq_num field.
-
- * pipe.h (pipe_acquire_reader, pipe_acquire_writer,
- pipe_add_reader, pipe_add_writer): `aquire' -> `acquire'.
-
-Fri Aug 11 18:35:32 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pipe.c (pipe_create): Initialize READERS & WRITERS, not REFS.
- (_pipe_first_writer): New function.
- (_pipe_no_writers, _pipe_no_writers): New function.
- (pipe_break): Function deleted.
-
-Wed Aug 9 12:53:05 1995 Miles Bader <miles@geech.gnu.ai.mit.edu>
-
- * pipe.h (struct pipe): Keep separate ref counts for readers and
- writers.
- (pipe_aquire_reader, pipe_aquire_writer): New functions.
- (pipe_release_reader, pipe_release_writer): New functions.
- (pipe_add_reader, pipe_add_writer): New functions.
- (pipe_remove_reader, pipe_remove_writer): New functions.
- (_pipe_first_writer): New function decl.
- (_pipe_no_writers, _pipe_no_writers): New function decl.
- (pipe_aquire, pipe_release): Function deleted.
- (pipe_break): Function decl deleted.
-
-Tue Aug 1 12:37:27 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pipe.c (pipe_recv): Get rid of code to deal with getting the
- source address from the control packet if there is no data packet,
- since pipe_write always writes a data packet.
-
-Mon Jul 31 14:50:00 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pipe.c (pipe_recv): Assert that a control packet should only
- have a source address if there is no corresponding data packet.
- (pipe_send): Change the test to determine whether we should write
- a control packet, so that we only do so if we need to. Also,
- don't record the source address in control packets, as it's
- recorded in the following data packet anyway, and this prevents it
- from being dealloc'd twice.
-
-Fri Jul 28 23:03:27 1995 Miles Bader <miles@churchy.gnu.ai.mit.edu>
-
- * pipe.h (stream_pipe_class, dgram_pipe_class, seqpack_pipe_class):
- Make these declarations extern so they don't fuck up initialization.
- (seqpacket_pipe_class): Renamed to `seqpack_pipe_class'.
diff --git a/libpipe/Makefile b/libpipe/Makefile
index 8fed5382..b64166a6 100644
--- a/libpipe/Makefile
+++ b/libpipe/Makefile
@@ -1,6 +1,6 @@
# Makefile for libpipe
#
-# Copyright (C) 1995 Free Software Foundation, Inc.
+# Copyright (C) 1995, 1996 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
@@ -22,12 +22,11 @@ makemode := library
libname = libpipe
installhdrs = pipe.h pq.h
-SRCS = pq.c dgram.c pipe.c stream.c seqpack.c addr.c
+SRCS = pq.c dgram.c pipe.c stream.c seqpack.c addr.c pq-funcs.c pipe-funcs.c
LCLHDRS = pipe.h pq.h
OBJS = $(SRCS:.c=.o)
+HURDLIBS=threads ports
include ../Makeconf
-libpipe.so: $(foreach lib,threads ports,\
- ../lib$(lib)/lib$(lib).so)
diff --git a/libpipe/pipe-funcs.c b/libpipe/pipe-funcs.c
new file mode 100644
index 00000000..79cda2a4
--- /dev/null
+++ b/libpipe/pipe-funcs.c
@@ -0,0 +1,2 @@
+#define PIPE_DEFINE_EI
+#include "pipe.h"
diff --git a/libpipe/pipe.c b/libpipe/pipe.c
index 7f36e5f8..914816bc 100644
--- a/libpipe/pipe.c
+++ b/libpipe/pipe.c
@@ -1,6 +1,6 @@
/* Generic one-way pipes
- Copyright (C) 1995 Free Software Foundation, Inc.
+ Copyright (C) 1995, 1998 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
@@ -66,7 +66,7 @@ pipe_create (struct pipe_class *class, struct pipe **pipe)
condition_init (&new->pending_write_selects);
mutex_init (&new->lock);
- pq_create (&new->queue);
+ pq_create (&new->queue);
if (! pipe_is_connless (new))
new->flags |= PIPE_BROKEN;
@@ -76,7 +76,7 @@ pipe_create (struct pipe_class *class, struct pipe **pipe)
}
/* Free PIPE and any resources it holds. */
-void
+void
pipe_free (struct pipe *pipe)
{
pq_free (pipe->queue);
@@ -149,7 +149,7 @@ void _pipe_no_writers (struct pipe *pipe)
this function (unlike most pipe functions). */
error_t
pipe_pair_select (struct pipe *rpipe, struct pipe *wpipe,
- int *select_type, int data_only)
+ int *select_type, int data_only)
{
error_t err = 0;
@@ -214,7 +214,7 @@ pipe_pair_select (struct pipe *rpipe, struct pipe *wpipe,
! ((wpipe->flags & PIPE_BROKEN)
|| pipe_readable (wpipe, 1) < wlimit);
}
-
+
if (!err)
{
if (rpipe_blocked)
@@ -260,10 +260,12 @@ pipe_send (struct pipe *pipe, int noblock, void *source,
{
size_t left = pipe->write_limit - pipe_readable (pipe, 1);
if (left < data_len)
- if (data_len <= pipe->write_atomic)
- return EWOULDBLOCK;
- else
- data_len = left;
+ {
+ if (data_len <= pipe->write_atomic)
+ return EWOULDBLOCK;
+ else
+ data_len = left;
+ }
}
if (control_len > 0 || num_ports > 0)
@@ -294,7 +296,7 @@ pipe_send (struct pipe *pipe, int noblock, void *source,
if (!err)
{
timestamp (&pipe->write_time);
-
+
/* And wakeup anyone that might be interested in it. */
condition_broadcast (&pipe->pending_reads);
mutex_unlock (&pipe->lock);
@@ -373,26 +375,29 @@ pipe_recv (struct pipe *pipe, int noblock, unsigned *flags, void **source,
}
if (!err)
- if (packet)
- /* Read some data (PACKET must be a data packet at this point). */
- {
- int dq = 1; /* True if we should dequeue this packet. */
-
- if (source)
- packet_read_source (packet, source);
-
- err = (*pipe->class->read)(packet, &dq, flags, data, data_len, amount);
- if (dq)
- pq_dequeue (pq);
- }
- else
- /* Return EOF. */
- *data_len = 0;
+ {
+ if (packet)
+ /* Read some data (PACKET must be a data packet at this point). */
+ {
+ int dq = 1; /* True if we should dequeue this packet. */
+
+ if (source)
+ packet_read_source (packet, source);
+
+ err = (*pipe->class->read)(packet, &dq, flags,
+ data, data_len, amount);
+ if (dq)
+ pq_dequeue (pq);
+ }
+ else
+ /* Return EOF. */
+ *data_len = 0;
+ }
if (!err && packet)
{
timestamp (&pipe->read_time);
-
+
/* And wakeup anyone that might be interested in it. */
condition_broadcast (&pipe->pending_writes);
mutex_unlock (&pipe->lock);
diff --git a/libpipe/pipe.h b/libpipe/pipe.h
index cc765dc6..96432990 100644
--- a/libpipe/pipe.h
+++ b/libpipe/pipe.h
@@ -24,8 +24,16 @@
#define EWOULDBLOCK EAGAIN /* XXX */
#include <cthreads.h> /* For conditions & mutexes */
+#include <features.h>
+
+#ifdef PIPE_DEFINE_EI
+#define PIPE_EI
+#else
+#define PIPE_EI __extern_inline
+#endif
#include "pq.h"
+
/* A description of a class of pipes and how to operate on them. */
struct pipe_class
@@ -93,7 +101,7 @@ struct pipe
PACKET_TYPE_CONTROL. Each data packet represents one datagram for
protocols that maintain record boundaries. Control packets always
represent the control information to be returned from one read
- operation, and will be returned in conjuction with the following data
+ operation, and will be returned in conjunction with the following data
packet (if any). Reads interested only in data just skip control
packets until they find a data packet. */
struct pq *queue;
@@ -102,9 +110,24 @@ struct pipe
/* Pipe flags. */
#define PIPE_BROKEN 0x1 /* This pipe isn't connected. */
+
+extern size_t pipe_readable (struct pipe *pipe, int data_only);
+
+extern int pipe_is_readable (struct pipe *pipe, int data_only);
+
+extern error_t pipe_wait_readable (struct pipe *pipe, int noblock, int data_only);
+
+extern error_t pipe_select_readable (struct pipe *pipe, int data_only);
+
+extern error_t pipe_wait_writable (struct pipe *pipe, int noblock);
+
+extern error_t pipe_select_writable (struct pipe *pipe);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PIPE_DEFINE_EI)
+
/* Returns the number of characters quickly readable from PIPE. If DATA_ONLY
is true, then `control' packets are ignored. */
-extern inline size_t
+PIPE_EI size_t
pipe_readable (struct pipe *pipe, int data_only)
{
size_t readable = 0;
@@ -123,7 +146,7 @@ pipe_readable (struct pipe *pipe, int data_only)
then `control' packets are ignored. Note that this is different than
(pipe_readable (PIPE) > 0) in the case where a control packet containing
only ports is present. */
-extern inline int
+PIPE_EI int
pipe_is_readable (struct pipe *pipe, int data_only)
{
struct pq *pq = pipe->queue;
@@ -134,11 +157,11 @@ pipe_is_readable (struct pipe *pipe, int data_only)
return (packet != NULL);
}
-/* Waits for PIPE to be readable, or an error to occurr. If NOBLOCK is true,
+/* Waits for PIPE to be readable, or an error to occur. If NOBLOCK is true,
this operation will return EWOULDBLOCK instead of blocking when no data is
immediately available. If DATA_ONLY is true, then `control' packets are
ignored. */
-extern inline error_t
+PIPE_EI error_t
pipe_wait_readable (struct pipe *pipe, int noblock, int data_only)
{
while (! pipe_is_readable (pipe, data_only) && ! (pipe->flags & PIPE_BROKEN))
@@ -151,11 +174,11 @@ pipe_wait_readable (struct pipe *pipe, int noblock, int data_only)
return 0;
}
-/* Waits for PIPE to be readable, or an error to occurr. This call only
+/* Waits for PIPE to be readable, or an error to occur. This call only
returns once threads waiting using pipe_wait_readable have been woken and
given a chance to read, and if there is still data available thereafter.
If DATA_ONLY is true, then `control' packets are ignored. */
-extern inline error_t
+PIPE_EI error_t
pipe_select_readable (struct pipe *pipe, int data_only)
{
while (! pipe_is_readable (pipe, data_only) && ! (pipe->flags & PIPE_BROKEN))
@@ -167,7 +190,7 @@ pipe_select_readable (struct pipe *pipe, int data_only)
/* Block until data can be written to PIPE. If NOBLOCK is true, then
EWOULDBLOCK is returned instead of blocking if this can't be done
immediately. */
-extern inline error_t
+PIPE_EI error_t
pipe_wait_writable (struct pipe *pipe, int noblock)
{
size_t limit = pipe->write_limit;
@@ -188,7 +211,7 @@ pipe_wait_writable (struct pipe *pipe, int noblock)
/* Block until some data can be written to PIPE. This call only returns once
threads waiting using pipe_wait_writable have been woken and given a
chance to write, and if there is still space available thereafter. */
-extern inline error_t
+PIPE_EI error_t
pipe_select_writable (struct pipe *pipe)
{
size_t limit = pipe->write_limit;
@@ -198,6 +221,8 @@ pipe_select_writable (struct pipe *pipe)
return 0;
}
+#endif /* Use extern inlines. */
+
/* Creates a new pipe of class CLASS and returns it in RESULT. */
error_t pipe_create (struct pipe_class *class, struct pipe **pipe);
@@ -218,8 +243,28 @@ void _pipe_no_readers (struct pipe *pipe);
should be locked. */
void _pipe_no_writers (struct pipe *pipe);
+extern void pipe_acquire_reader (struct pipe *pipe);
+
+extern void pipe_acquire_writer (struct pipe *pipe);
+
+extern void pipe_release_reader (struct pipe *pipe);
+
+extern void pipe_release_writer (struct pipe *pipe);
+
+extern void pipe_add_reader (struct pipe *pipe);
+
+extern void pipe_add_writer (struct pipe *pipe);
+
+extern void pipe_remove_reader (struct pipe *pipe);
+
+extern void pipe_remove_writer (struct pipe *pipe);
+
+extern void pipe_drain (struct pipe *pipe);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PIPE_DEFINE_EI)
+
/* Lock PIPE and increment its readers count. */
-extern inline void
+PIPE_EI void
pipe_acquire_reader (struct pipe *pipe)
{
mutex_lock (&pipe->lock);
@@ -228,7 +273,7 @@ pipe_acquire_reader (struct pipe *pipe)
}
/* Lock PIPE and increment its writers count. */
-extern inline void
+PIPE_EI void
pipe_acquire_writer (struct pipe *pipe)
{
mutex_lock (&pipe->lock);
@@ -238,7 +283,7 @@ pipe_acquire_writer (struct pipe *pipe)
/* Decrement PIPE's (which should be locked) reader count and unlock it. If
there are no more refs to PIPE, it will be destroyed. */
-extern inline void
+PIPE_EI void
pipe_release_reader (struct pipe *pipe)
{
if (--pipe->readers == 0)
@@ -249,7 +294,7 @@ pipe_release_reader (struct pipe *pipe)
/* Decrement PIPE's (which should be locked) writer count and unlock it. If
there are no more refs to PIPE, it will be destroyed. */
-extern inline void
+PIPE_EI void
pipe_release_writer (struct pipe *pipe)
{
if (--pipe->writers == 0)
@@ -259,7 +304,7 @@ pipe_release_writer (struct pipe *pipe)
}
/* Increment PIPE's reader count. PIPE should be unlocked. */
-extern inline void
+PIPE_EI void
pipe_add_reader (struct pipe *pipe)
{
pipe_acquire_reader (pipe);
@@ -267,7 +312,7 @@ pipe_add_reader (struct pipe *pipe)
}
/* Increment PIPE's writer count. PIPE should be unlocked. */
-extern inline void
+PIPE_EI void
pipe_add_writer (struct pipe *pipe)
{
pipe_acquire_writer (pipe);
@@ -276,7 +321,7 @@ pipe_add_writer (struct pipe *pipe)
/* Decrement PIPE's (which should be unlocked) reader count and unlock it. If
there are no more refs to PIPE, it will be destroyed. */
-extern inline void
+PIPE_EI void
pipe_remove_reader (struct pipe *pipe)
{
mutex_lock (&pipe->lock);
@@ -285,7 +330,7 @@ pipe_remove_reader (struct pipe *pipe)
/* Decrement PIPE's (which should be unlocked) writer count and unlock it. If
there are no more refs to PIPE, it will be destroyed. */
-extern inline void
+PIPE_EI void
pipe_remove_writer (struct pipe *pipe)
{
mutex_lock (&pipe->lock);
@@ -293,12 +338,14 @@ pipe_remove_writer (struct pipe *pipe)
}
/* Empty out PIPE of any data. PIPE should be locked. */
-extern inline void
+PIPE_EI void
pipe_drain (struct pipe *pipe)
{
pq_drain (pipe->queue);
}
+#endif /* Use extern inlines. */
+
/* Writes up to LEN bytes of DATA, to PIPE, which should be locked, and
returns the amount written in AMOUNT. If present, the information in
CONTROL & PORTS is written in a preceding control packet. If an error is
diff --git a/libpipe/pq-funcs.c b/libpipe/pq-funcs.c
new file mode 100644
index 00000000..57061419
--- /dev/null
+++ b/libpipe/pq-funcs.c
@@ -0,0 +1,2 @@
+#define PQ_DEFINE_EI
+#include "pq.h"
diff --git a/libpipe/pq.c b/libpipe/pq.c
index c48f90bd..afdda051 100644
--- a/libpipe/pq.c
+++ b/libpipe/pq.c
@@ -1,6 +1,7 @@
/* Packet queues
- Copyright (C) 1995, 1996 Free Software Foundation, Inc.
+ Copyright (C) 1995, 1996, 1998, 1999, 2002, 2006
+ Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
@@ -21,6 +22,7 @@
#include <malloc.h>
#include <string.h>
#include <stddef.h>
+#include <sys/mman.h>
#include "pq.h"
@@ -52,11 +54,12 @@ free_packets (struct packet *head)
if (head->ports)
free (head->ports);
if (head->buf_len > 0)
- if (head->buf_vm_alloced)
- vm_deallocate (mach_task_self (),
- (vm_address_t)head->buf, head->buf_len);
- else
- free (head->buf);
+ {
+ if (head->buf_vm_alloced)
+ munmap (head->buf, head->buf_len);
+ else
+ free (head->buf);
+ }
free (head);
free_packets (next);
}
@@ -125,13 +128,15 @@ pq_queue (struct pq *pq, unsigned type, void *source)
packet->buf = 0;
packet->buf_len = 0;
packet->ports = 0;
- packet->num_ports = packet->ports_alloced = 0;
- packet->buf_start = packet->buf_end = packet->buf;
+ packet->ports_alloced = 0;
packet->buf_vm_alloced = 0;
}
else
pq->free = packet->next;
+ packet->num_ports = 0;
+ packet->buf_start = packet->buf_end = packet->buf;
+
packet->type = type;
packet->source = source;
packet->next = 0;
@@ -203,7 +208,7 @@ packet_extend (struct packet *packet, size_t new_len)
packet->buf_start = new_buf + (packet->buf_start - old_buf);
packet->buf_end = new_buf + (packet->buf_end - old_buf);
}
-
+
packet->buf_len = new_len;
return 1;
@@ -224,8 +229,10 @@ packet_realloc (struct packet *packet, size_t new_len)
/* Make a new buffer. */
if (vm_alloc)
- err =
- vm_allocate (mach_task_self (), (vm_address_t *)&new_buf, new_len, 1);
+ {
+ new_buf = mmap (0, new_len, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ err = (new_buf == (char *) -1) ? errno : 0;
+ }
else
{
new_buf = malloc (new_len);
@@ -249,10 +256,12 @@ packet_realloc (struct packet *packet, size_t new_len)
/* And get rid of the old buffer. */
if (old_len > 0)
- if (packet->buf_vm_alloced)
- vm_deallocate (mach_task_self (), (vm_address_t)old_buf, old_len);
- else
- free (old_buf);
+ {
+ if (packet->buf_vm_alloced)
+ vm_deallocate (mach_task_self (), (vm_address_t)old_buf, old_len);
+ else
+ free (old_buf);
+ }
packet->buf = new_buf;
packet->buf_len = new_len;
@@ -267,7 +276,7 @@ packet_realloc (struct packet *packet, size_t new_len)
/* ---------------------------------------------------------------- */
/* If PACKET has any ports, deallocates them. */
-void
+void
packet_dealloc_ports (struct packet *packet)
{
unsigned i;
@@ -289,13 +298,14 @@ packet_set_ports (struct packet *packet,
packet_dealloc_ports (packet);
if (num_ports > packet->ports_alloced)
{
- mach_port_t *new_ports = malloc (sizeof (mach_port_t *) * num_ports);
+ mach_port_t *new_ports = malloc (sizeof (mach_port_t) * num_ports);
if (! new_ports)
return ENOMEM;
free (packet->ports);
+ packet->ports = new_ports;
packet->ports_alloced = num_ports;
}
- bcopy (ports, packet->ports, sizeof (mach_port_t *) * num_ports);
+ bcopy (ports, packet->ports, sizeof (mach_port_t) * num_ports);
packet->num_ports = num_ports;
return 0;
}
@@ -306,13 +316,12 @@ error_t
packet_read_ports (struct packet *packet,
mach_port_t **ports, size_t *num_ports)
{
- int length = packet->num_ports * sizeof (mach_port_t *);
+ int length = packet->num_ports * sizeof (mach_port_t);
if (*num_ports < packet->num_ports)
{
- error_t err =
- vm_allocate (mach_task_self (), (vm_address_t *)ports, length, 1);
- if (err)
- return err;
+ *ports = mmap (0, length, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+ if (*ports == (mach_port_t *) -1)
+ return errno;
}
*num_ports = packet->num_ports;
bcopy (packet->ports, *ports, length);
@@ -321,8 +330,8 @@ packet_read_ports (struct packet *packet,
}
/* Append the bytes in DATA, of length DATA_LEN, to what's already in PACKET,
- and return the amount appended in AMOUNT. */
-error_t
+ and return the amount appended in AMOUNT if that's not the null pointer. */
+error_t
packet_write (struct packet *packet,
char *data, size_t data_len, size_t *amount)
{
@@ -334,7 +343,8 @@ packet_write (struct packet *packet,
/* Add the new data. */
bcopy (data, packet->buf_end, data_len);
packet->buf_end += data_len;
- *amount = data_len;
+ if (amount != NULL)
+ *amount = data_len;
return 0;
}
@@ -399,7 +409,7 @@ packet_read (struct packet *packet,
/* Just copy the data the old fashioned way.... */
{
if (*data_len < amount)
- vm_allocate (mach_task_self (), (vm_address_t *)data, amount, 1);
+ *data = mmap (0, amount, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
bcopy (start, *data, amount);
start += amount;
diff --git a/libpipe/pq.h b/libpipe/pq.h
index abd193f8..0fffe254 100644
--- a/libpipe/pq.h
+++ b/libpipe/pq.h
@@ -1,6 +1,6 @@
/* Packet queues
- Copyright (C) 1995 Free Software Foundation, Inc.
+ Copyright (C) 1995, 1996, 2006 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.ai.mit.edu>
@@ -25,6 +25,14 @@
#include <stddef.h> /* for size_t */
#include <string.h>
#include <mach/mach.h>
+#include <features.h>
+
+#ifdef PQ_DEFINE_EI
+#define PQ_EI
+#else
+#define PQ_EI __extern_inline
+#endif
+
struct packet
{
@@ -65,15 +73,21 @@ error_t packet_set_ports (struct packet *packet,
/* If PACKET has any ports, deallocates them. */
void packet_dealloc_ports (struct packet *packet);
+extern size_t packet_readable (struct packet *packet);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PQ_DEFINE_EI)
+
/* Returns the number of bytes of data in PACKET. */
-extern inline size_t
+PQ_EI size_t
packet_readable (struct packet *packet)
{
return packet->buf_end - packet->buf_start;
}
+#endif /* Use extern inlines. */
+
/* Append the bytes in DATA, of length DATA_LEN, to what's already in PACKET,
- and return the amount appended in AMOUNT. */
+ and return the amount appended in AMOUNT if that's not the null pointer. */
error_t packet_write (struct packet *packet,
char *data, size_t data_len, size_t *amount);
@@ -89,14 +103,20 @@ error_t packet_read (struct packet *packet,
error_t packet_read_ports (struct packet *packet,
mach_port_t **ports, size_t *num_ports);
+extern void packet_read_source (struct packet *packet, void **source);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PQ_DEFINE_EI)
+
/* Return the source addressd in PACKET in SOURCE, deallocating it from
PACKET. */
-extern inline void
+PQ_EI void
packet_read_source (struct packet *packet, void **source)
{
*source = packet->source;
packet->source = 0;
}
+
+#endif /* Use extern inlines. */
/* The packet size above which we start to do things differently to avoid
copying around data. */
@@ -120,9 +140,17 @@ int packet_extend (struct packet *packet, size_t new_len);
returned. */
error_t packet_realloc (struct packet *packet, size_t new_len);
+extern int packet_fit (struct packet *packet, size_t amount);
+
+extern error_t packet_ensure (struct packet *packet, size_t amount);
+
+extern int packet_ensure_efficiently (struct packet *packet, size_t amount);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PQ_DEFINE_EI)
+
/* Try to make space in PACKET for AMOUNT more bytes without growing the
buffer, returning true if we could do it. */
-extern inline int
+PQ_EI int
packet_fit (struct packet *packet, size_t amount)
{
char *buf = packet->buf, *end = packet->buf_end;
@@ -154,7 +182,7 @@ packet_fit (struct packet *packet, size_t amount)
/* Make sure that PACKET has room for at least AMOUNT more bytes, or return
the reason why not. */
-extern inline error_t
+PQ_EI error_t
packet_ensure (struct packet *packet, size_t amount)
{
if (! packet_fit (packet, amount))
@@ -171,7 +199,7 @@ packet_ensure (struct packet *packet, size_t amount)
it can be done efficiently, e.g., the packet can be grown in place, rather
than moving the contents (or there is little enough data so that copying
it is OK). True is returned if room was made, false otherwise. */
-extern inline int
+PQ_EI int
packet_ensure_efficiently (struct packet *packet, size_t amount)
{
if (! packet_fit (packet, amount))
@@ -184,6 +212,8 @@ packet_ensure_efficiently (struct packet *packet, size_t amount)
}
return 0;
}
+
+#endif /* Use extern inlines. */
struct pq
{
@@ -196,10 +226,14 @@ struct pq
the packet, or deallocated by calling pipe_dealloc_addr. */
struct packet *pq_queue (struct pq *pq, unsigned type, void *source);
+extern struct packet * pq_tail (struct pq *pq, unsigned type, void *source);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PQ_DEFINE_EI)
+
/* Returns the tail of the packet queue PQ, which may mean pushing a new
packet if TYPE and SOURCE do not match the current tail, or this is the
first packet. */
-extern inline struct packet *
+PQ_EI struct packet *
pq_tail (struct pq *pq, unsigned type, void *source)
{
struct packet *tail = pq->tail;
@@ -209,16 +243,24 @@ pq_tail (struct pq *pq, unsigned type, void *source)
return tail;
}
+#endif /* Use extern inlines. */
+
/* Remove the first packet (if any) in PQ, deallocating any resources it
holds. True is returned if a packet was found, false otherwise. */
int pq_dequeue (struct pq *pq);
+extern struct packet * pq_head (struct pq *pq, unsigned type, void *source);
+
+extern struct packet * pq_next (struct pq *pq, unsigned type, void *source);
+
+#if defined(__USE_EXTERN_INLINES) || defined(PQ_DEFINE_EI)
+
/* Returns the next available packet in PQ, without removing it from the
- queue, or NULL if there is none, or the next packet isn't appropiate.
- A packet is inappropiate if SOURCE is non-NULL its source field doesn't
+ queue, or NULL if there is none, or the next packet isn't appropriate.
+ A packet is inappropriate if SOURCE is non-NULL its source field doesn't
match it, or TYPE is non-NULL and the packet's type field doesn't match
it. */
-extern inline struct packet *
+PQ_EI struct packet *
pq_head (struct pq *pq, unsigned type, void *source)
{
struct packet *head = pq->head;
@@ -232,7 +274,7 @@ pq_head (struct pq *pq, unsigned type, void *source)
}
/* The same as pq_head, but first discards the head of the queue. */
-extern inline struct packet *
+PQ_EI struct packet *
pq_next (struct pq *pq, unsigned type, void *source)
{
if (!pq->head)
@@ -241,6 +283,8 @@ pq_next (struct pq *pq, unsigned type, void *source)
return pq_head (pq, type, source);
}
+#endif /* Use extern inlines. */
+
/* Dequeues all packets in PQ. */
void pq_drain (struct pq *pq);