aboutsummaryrefslogtreecommitdiff
path: root/libpipe/pq.c
diff options
context:
space:
mode:
Diffstat (limited to 'libpipe/pq.c')
-rw-r--r--libpipe/pq.c64
1 files changed, 37 insertions, 27 deletions
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;