Commit e489743b authored by Niels Möller's avatar Niels Möller
Browse files

* src/resource.c, src/resource.h, src/write_buffer.c,

src/write_buffer.h: Use struct lsh_queue.

* src/write_buffer.h (write_buffer->length): New instance
variable.

* src/write_buffer.c (write_buffer_consume): New function.

Rev: src/resource.c:1.5
Rev: src/resource.h:1.5
Rev: src/write_buffer.c:1.15
Rev: src/write_buffer.h:1.14
parent 9b586874
......@@ -31,38 +31,34 @@
#include "xalloc.h"
/* Prototypes */
void do_mark_resources(struct resource_node *n,
void do_mark_resources(struct lsh_queue *q,
void (*mark)(struct lsh_object *o));
void do_free_resources(struct resource_node *n);
void do_free_resources(struct lsh_queue *q);
void dont_free_live_resource(int alive);
#define CLASS_DEFINE
#include "resource.h.x"
#undef CLASS_DEFINE
void do_mark_resources(struct resource_node *n,
void do_mark_resources(struct lsh_queue *q,
void (*mark)(struct lsh_object *o))
{
for(; n; n = n->next)
FOR_QUEUE(q, struct resource_node *, n)
mark(&n->resource->super);
}
void do_free_resources(struct resource_node *n)
void do_free_resources(struct lsh_queue *q)
{
while(n)
{
struct resource_node *old = n;
n = n->next;
lsh_space_free(old);
}
FOR_QUEUE(q, struct resource_node *, n)
lsh_space_free(n);
}
void dont_free_live_resource(int alive)
{
if (alive)
fatal("dont_free_live_resource: About to garbage collect a live resource!\n");
fatal("dont_free_live_resource: "
"About to garbage collect a live resource!\n");
}
static struct resource_node *do_remember_resource(struct resource_list *self,
......@@ -73,32 +69,24 @@ static struct resource_node *do_remember_resource(struct resource_list *self,
NEW_SPACE(n);
n->resource = r;
/* Add at head of list */
n->next = self->head;
n->prev = NULL;
self->head = n;
if (n->next)
n->next->prev = n;
else
self->tail = n;
lsh_queue_add_head(&self->q, &n->header);
return n;
}
static void do_kill_all(struct resource_list *self)
{
/* FIXME: Doesn't deallocate any nodes */
struct resource_node *n;
/* FIXME: Doesn't deallocate any nodes (but gc should do that
* later). */
for (n = self->head; n; n = n->next)
FOR_QUEUE(&self->q, struct resource_node *, n)
KILL_RESOURCE(n->resource);
}
struct resource_list *empty_resource_list(void)
{
NEW(resource_list, self);
self->head = self->tail = NULL;
lsh_queue_init(&self->q);
self->remember = do_remember_resource;
self->kill_all = do_kill_all;
......
......@@ -28,7 +28,7 @@
#ifndef LSH_RESOURCE_H_INCLUDED
#define LSH_RESOURCE_H_INCLUDED
#include "lsh.h"
#include "queue.h"
#define CLASS_DECLARE
#include "resource.h.x"
......@@ -49,15 +49,10 @@
/* For the resource list. It is doubly linked to make removing
* elements easy. */
/* NOTE: No object header */
/* NOTE: This list is very similar to the packet queue in
* write_buffer.h. It may be a good idea to write a more general
* doubly linked list. */
struct resource_node
{
struct resource_node *next;
struct resource_node *prev;
struct lsh_queue_node header;
struct resource *resource;
};
......@@ -67,9 +62,8 @@ struct resource_node
(class
(name resource_list)
(vars
(head special "struct resource_node *"
do_mark_resources do_free_resources)
(tail simple "struct resource_node *")
(q special-struct "struct lsh_queue"
do_mark_resources do_free_resources)
; Returns the node.
; NOTE: This pointer should only be stored together with
......
......@@ -26,25 +26,24 @@
#include "write_buffer.h"
#include "xalloc.h"
#include "werror.h"
#include <assert.h>
#include <string.h>
/* Prototype */
static void do_free_buffer(struct buffer_node *n);
static void do_free_buffer(struct lsh_queue *q);
#define CLASS_DEFINE
#include "write_buffer.h.x"
#undef CLASS_DEFINE
static void do_free_buffer(struct buffer_node *n)
static void do_free_buffer(struct lsh_queue *q)
{
while(n)
FOR_QUEUE(q, struct buffer_node *, n)
{
struct buffer_node *old = n;
lsh_string_free(n->packet);
n = n->next;
lsh_space_free(old);
lsh_space_free(n);
}
}
......@@ -53,7 +52,9 @@ static int do_write(struct abstract_write *w,
{
CAST(write_buffer, closure, w);
struct buffer_node *new;
debug("write_buffer: do_write length = %i\n",
packet->length);
if (!packet->length)
{
lsh_string_free(packet);
......@@ -69,7 +70,9 @@ static int do_write(struct abstract_write *w,
/* Enqueue packet */
NEW_SPACE(new);
new->packet = packet;
lsh_queue_add_tail(&closure->q, &new->header);
#if 0
new->next = 0;
if (closure->tail)
......@@ -83,7 +86,8 @@ static int do_write(struct abstract_write *w,
closure->head = new;
}
closure->tail = new;
#endif
#if 0
if (closure->try_write)
{
......@@ -92,7 +96,11 @@ static int do_write(struct abstract_write *w,
#endif
closure->empty = 0;
closure->length += packet->length;
debug("write_buffer: do_write closure->length = %i\n",
closure->length);
return LSH_OK | LSH_GOON;
}
......@@ -153,16 +161,14 @@ int write_buffer_pre_write(struct write_buffer *buffer)
else
{
/* Dequeue a packet, if possible */
struct buffer_node *n = buffer->head;
if (n)
{
if (!lsh_queue_is_empty(&buffer->q))
{
struct buffer_node *n =
(struct buffer_node *) lsh_queue_remove_head(&buffer->q);
buffer->partial = n->packet;
buffer->pos = 0;
buffer->head = n->next;
if (buffer->head)
buffer->head->prev = 0;
else
buffer->tail = 0;
lsh_space_free(n);
}
else
......@@ -173,6 +179,13 @@ int write_buffer_pre_write(struct write_buffer *buffer)
return !buffer->empty;
}
void write_buffer_consume(struct write_buffer *buffer, UINT32 size)
{
buffer->start += size;
assert(buffer->start <= buffer->end);
buffer->length -= size;
}
void write_buffer_close(struct write_buffer *buffer)
{
buffer->closed = 1;
......@@ -189,13 +202,15 @@ struct write_buffer *write_buffer_alloc(UINT32 size)
res->buffer = lsh_space_alloc(2 * size);
res->empty = 1;
res->length = 0;
res->closed = 0;
#if 0
res->try_write = try;
#endif
res->head = res->tail = 0;
lsh_queue_init(&res->q);
res->pos = 0;
res->partial = NULL;
......@@ -204,3 +219,5 @@ struct write_buffer *write_buffer_alloc(UINT32 size)
return res;
}
......@@ -27,6 +27,7 @@
#define LSH_WRITE_BUFFER_H_INCLUDED
#include "abstract_io.h"
#include "queue.h"
#define CLASS_DECLARE
#include "write_buffer.h.x"
......@@ -36,8 +37,7 @@
/* NOTE: No object header */
struct buffer_node
{
struct buffer_node *next;
struct buffer_node *prev;
struct lsh_queue_node header;
struct lsh_string *packet;
};
......@@ -50,15 +50,17 @@ struct buffer_node
(buffer space UINT8) ; Size is twice the blocksize
(empty simple int)
; Total amount of data currently in the buffer)
(length . UINT32)
; If non-zero, don't accept any more data. The i/o-channel
; should be closed once the current buffers are flushed.
(closed simple int)
;; (try_write simple int)
(head special "struct buffer_node *"
(q special-struct "struct lsh_queue"
#f do_free_buffer)
(tail simple "struct buffer_node *")
(pos simple UINT32) ; Partial packet
(partial string)
......@@ -69,6 +71,7 @@ struct buffer_node
struct write_buffer *write_buffer_alloc(UINT32 size);
int write_buffer_pre_write(struct write_buffer *buffer);
void write_buffer_consume(struct write_buffer *buffer, UINT32 size);
void write_buffer_close(struct write_buffer *buffer);
#endif /* LSH_WRITE_BUFFER_H_INCLUDED */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment