Commit 9ba10d17 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Changed lock structure (still no implementation).

Changed reader/writer interface to simplify stacking (example will 
be forthcoming).
parent 0f17c894
......@@ -56,7 +56,7 @@ int main(int argc, char *argv[]) {
char *filename = argv[1];
printf("C decoder reading from %s\n", filename);
fd = open(filename, O_RDONLY);
decoder = labcomm_decoder_new(labcomm_fd_reader_new(fd, 1), NULL, NULL);
decoder = labcomm_decoder_new(labcomm_fd_reader_new(fd, 1), NULL);
if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return 1;
......
......@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) {
char *filename = argv[1];
printf("C encoder writing to %s\n", filename);
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
encoder = labcomm_encoder_new(labcomm_fd_writer_new(fd, 1), NULL, NULL);
encoder = labcomm_encoder_new(labcomm_fd_writer_new(fd, 1), NULL);
labcomm_encoder_register_simple_theTwoInts(encoder);
labcomm_encoder_register_simple_anotherTwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
......
......@@ -39,10 +39,7 @@
struct labcomm_decoder {
void *context;
struct labcomm_reader *reader;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
struct labcomm_lock *lock;
labcomm_error_handler_callback on_error;
labcomm_handle_new_datatype_callback on_new_datatype;
};
......@@ -50,10 +47,7 @@ struct labcomm_decoder {
struct labcomm_encoder {
void *context;
struct labcomm_writer *writer;
struct {
void *context;
const struct labcomm_lock_action *action;
} lock;
struct labcomm_lock *lock;
labcomm_error_handler_callback on_error;
};
......@@ -240,19 +234,20 @@ static void labcomm_encode_signature(struct labcomm_encoder *e,
int i, index;
index = get_encoder_index(e, signature);
e->writer->action->start(e->writer, e, index, signature, NULL);
e->writer->action->start(e->writer, e->writer->context,
e, index, signature, NULL);
labcomm_write_packed32(e->writer, signature->type);
labcomm_write_packed32(e->writer, index);
labcomm_write_string(e->writer, signature->name);
for (i = 0 ; i < signature->size ; i++) {
if (e->writer->pos >= e->writer->count) {
e->writer->action->flush(e->writer);
e->writer->action->flush(e->writer, e->writer->context);
}
e->writer->data[e->writer->pos] = signature->signature[i];
e->writer->pos++;
}
e->writer->action->end(e->writer);
e->writer->action->end(e->writer, e->writer->context);
}
#ifdef LABCOMM_ENCODER_LINEAR_SEARCH
......@@ -346,8 +341,7 @@ static struct labcomm_sample_entry *encoder_get_sample_by_signature_address(
struct labcomm_encoder *labcomm_encoder_new(
struct labcomm_writer *writer,
const struct labcomm_lock_action *lock,
void *lock_context)
struct labcomm_lock *lock)
{
struct labcomm_encoder *result = malloc(sizeof(*result));
if (result) {
......@@ -367,10 +361,10 @@ struct labcomm_encoder *labcomm_encoder_new(
result->writer->count = 0;
result->writer->pos = 0;
result->writer->error = 0;
result->lock.action = lock;
result->lock.context = lock_context;
result->lock = lock;
result->on_error = on_error_fprintf;
result->writer->action->alloc(result->writer, LABCOMM_VERSION);
result->writer->action->alloc(result->writer,result->writer->context,
LABCOMM_VERSION);
}
return result;
}
......@@ -410,23 +404,25 @@ int labcomm_internal_encode(
int index;
index = get_encoder_index(e, signature);
result = e->writer->action->start(e->writer, e, index, signature, value);
result = e->writer->action->start(e->writer, e->writer->context,
e, index, signature, value);
if (result == -EALREADY) { result = 0; goto no_end; }
if (result != 0) { goto out; }
result = labcomm_write_packed32(e->writer, index);
if (result != 0) { goto out; }
result = encode(e->writer, value);
out:
e->writer->action->end(e->writer);
e->writer->action->end(e->writer, e->writer->context);
no_end:
return result;
}
void labcomm_encoder_free(struct labcomm_encoder* e)
{
e->writer->action->free(e->writer);
struct labcomm_encoder_context *context = (struct labcomm_encoder_context *) e->context;
struct labcomm_encoder_context *context;
context = (struct labcomm_encoder_context *) e->context;
e->writer->action->free(e->writer, e->writer->context);
#ifdef LABCOMM_ENCODER_LINEAR_SEARCH
struct labcomm_sample_entry *entry = context->sample;
struct labcomm_sample_entry *entry_next;
......@@ -459,7 +455,9 @@ int labcomm_encoder_ioctl(struct labcomm_encoder *encoder,
}
va_start(va, action);
result = encoder->writer->action->ioctl(encoder->writer, action, NULL, va);
result = encoder->writer->action->ioctl(encoder->writer,
encoder->writer->context,
action, NULL, va);
va_end(va);
out:
......@@ -483,7 +481,7 @@ static int labcomm_writer_ioctl(struct labcomm_writer *writer,
}
va_start(va, action);
result = writer->action->ioctl(writer, action, NULL, va);
result = writer->action->ioctl(writer, writer->context, action, NULL, va);
va_end(va);
out:
return result;
......@@ -497,8 +495,9 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
int result = -ENOTSUP;
if (encoder->writer->action->ioctl != NULL) {
result = encoder->writer->action->ioctl(encoder->writer, action,
signature, va);
result = encoder->writer->action->ioctl(encoder->writer,
encoder->writer->context,
action, signature, va);
}
return result;
}
......@@ -556,8 +555,7 @@ static void collect_flat_signature(
struct labcomm_decoder *labcomm_decoder_new(
struct labcomm_reader *reader,
const struct labcomm_lock_action *lock,
void *lock_context)
struct labcomm_lock *lock)
{
struct labcomm_decoder *result = malloc(sizeof(*result));
if (result) {
......@@ -570,11 +568,11 @@ struct labcomm_decoder *labcomm_decoder_new(
result->reader->data_size = 0;
result->reader->count = 0;
result->reader->pos = 0;
result->lock.action = lock;
result->lock.context = lock_context;
result->lock = lock;
result->on_error = on_error_fprintf;
result->on_new_datatype = on_new_datatype;
result->reader->action->alloc(result->reader, LABCOMM_VERSION);
result->reader->action->alloc(result->reader, result->reader->context,
LABCOMM_VERSION);
}
return result;
}
......@@ -606,7 +604,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
{
int result;
result = d->reader->action->start(d->reader);
result = d->reader->action->start(d->reader, d->reader->context);
if (result > 0) {
struct labcomm_decoder_context *context = d->context;
......@@ -627,13 +625,13 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
struct labcomm_sample_entry *entry = NULL;
int index, err;
writer.action->alloc(&writer, "");
writer.action->start(&writer, NULL, 0, NULL, NULL);
writer.action->alloc(&writer, writer.context, "");
writer.action->start(&writer, writer.context, NULL, 0, NULL, NULL);
index = labcomm_read_packed32(d->reader); //int
signature.name = labcomm_read_string(d->reader);
signature.type = result;
collect_flat_signature(d, &writer);
writer.action->end(&writer);
writer.action->end(&writer, writer.context);
err = labcomm_writer_ioctl(&writer,
LABCOMM_IOCTL_WRITER_GET_BYTES_WRITTEN,
&signature.size);
......@@ -662,7 +660,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
}
free_signature_name:
free(signature.name);
writer.action->free(&writer);
writer.action->free(&writer, writer.context);
if (!entry) {
// No handler for found type, bail out (after cleanup)
result = -ENOENT;
......@@ -681,7 +679,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
}
}
}
d->reader->action->end(d->reader);
d->reader->action->end(d->reader, d->reader->context);
return result;
}
......@@ -693,11 +691,11 @@ void labcomm_decoder_run(struct labcomm_decoder *d)
void labcomm_decoder_free(struct labcomm_decoder* d)
{
d->reader->action->free(d->reader);
struct labcomm_decoder_context *context = (struct labcomm_decoder_context *) d->context;
struct labcomm_sample_entry *entry = context->sample;
struct labcomm_sample_entry *entry_next;
d->reader->action->free(d->reader, d->reader->context);
while (entry != NULL) {
entry_next = entry->next;
free(entry);
......@@ -718,7 +716,9 @@ int labcomm_decoder_ioctl(struct labcomm_decoder *decoder,
va_list va;
va_start(va, action);
result = decoder->reader->action->ioctl(decoder->reader, action, NULL, va);
result = decoder->reader->action->ioctl(decoder->reader,
decoder->reader->context,
action, NULL, va);
va_end(va);
}
return result;
......@@ -732,7 +732,9 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
int result = -ENOTSUP;
if (decoder->reader->action->ioctl != NULL) {
result = decoder->reader->action->ioctl(decoder->reader, action, NULL, va);
result = decoder->reader->action->ioctl(decoder->reader,
decoder->reader->context,
action, NULL, va);
}
return result;
}
......
......@@ -61,15 +61,6 @@ void labcomm_decoder_register_new_datatype_handler(struct labcomm_decoder *d,
*/
struct labcomm_lock;
struct labcomm_lock_action {
int (*alloc)(void *context);
int (*free)(void *context);
int (*read_lock)(void *context);
int (*read_unlock)(void *context);
int (*write_lock)(void *context);
int (*write_unlock)(void *context);
};
/*
* Decoder
*/
......@@ -77,8 +68,7 @@ struct labcomm_reader;
struct labcomm_decoder *labcomm_decoder_new(
struct labcomm_reader *reader,
const struct labcomm_lock_action *lock,
void *lock_context);
struct labcomm_lock *lock);
int labcomm_decoder_decode_one(
struct labcomm_decoder *decoder);
void labcomm_decoder_run(
......@@ -98,8 +88,7 @@ struct labcomm_writer;
struct labcomm_encoder *labcomm_encoder_new(
struct labcomm_writer *writer,
const struct labcomm_lock_action *lock,
void *lock_context);
struct labcomm_lock *lock);
void labcomm_encoder_free(
struct labcomm_encoder *encoder);
......
......@@ -6,7 +6,8 @@
#include "labcomm_ioctl.h"
#include "labcomm_dynamic_buffer_writer.h"
static int dyn_alloc(struct labcomm_writer *w, char *labcomm_version)
static int dyn_alloc(struct labcomm_writer *w, void *context,
char *labcomm_version)
{
w->data_size = 1000;
w->count = w->data_size;
......@@ -19,7 +20,7 @@ static int dyn_alloc(struct labcomm_writer *w, char *labcomm_version)
return w->error;
}
static int dyn_free(struct labcomm_writer *w)
static int dyn_free(struct labcomm_writer *w, void *context)
{
free(w->data);
w->data = 0;
......@@ -30,7 +31,7 @@ static int dyn_free(struct labcomm_writer *w)
return 0;
}
static int dyn_start(struct labcomm_writer *w,
static int dyn_start(struct labcomm_writer *w, void *context,
struct labcomm_encoder *encoder,
int index,
struct labcomm_signature *signature,
......@@ -52,12 +53,12 @@ static int dyn_start(struct labcomm_writer *w,
return w->error;
}
static int dyn_end(struct labcomm_writer *w)
static int dyn_end(struct labcomm_writer *w, void *context)
{
return 0;
}
static int dyn_flush(struct labcomm_writer *w)
static int dyn_flush(struct labcomm_writer *w, void *context)
{
void *tmp;
......@@ -74,7 +75,7 @@ static int dyn_flush(struct labcomm_writer *w)
return w->error;
}
static int dyn_ioctl(struct labcomm_writer *w,
static int dyn_ioctl(struct labcomm_writer *w, void *context,
int action,
struct labcomm_signature *signature,
va_list arg)
......
......@@ -13,7 +13,7 @@ struct labcomm_fd_reader {
int close_fd_on_free;
};
static int fd_alloc(struct labcomm_reader *r, char *version)
static int fd_alloc(struct labcomm_reader *r, void *context, char *version)
{
int result = 0;
......@@ -42,9 +42,9 @@ static int fd_alloc(struct labcomm_reader *r, char *version)
return result;
}
static int fd_free(struct labcomm_reader *r)
static int fd_free(struct labcomm_reader *r, void *context)
{
struct labcomm_fd_reader *context = r->context;
struct labcomm_fd_reader *fd_context = context;
free(r->data);
r->data = 0;
......@@ -52,18 +52,18 @@ static int fd_free(struct labcomm_reader *r)
r->count = 0;
r->pos = 0;
if (context->close_fd_on_free) {
close(context->fd);
if (fd_context->close_fd_on_free) {
close(fd_context->fd);
}
free(context);
free(fd_context);
return 0;
}
static int fd_fill(struct labcomm_reader *r)
static int fd_fill(struct labcomm_reader *r, void *context)
{
int result = 0;
struct labcomm_fd_reader *context = r->context;
struct labcomm_fd_reader *fd_context = context;
if (r->pos < r->count) {
result = r->count - r->pos;
......@@ -71,7 +71,7 @@ static int fd_fill(struct labcomm_reader *r)
int err;
r->pos = 0;
err = read(context->fd, r->data, r->data_size);
err = read(fd_context->fd, r->data, r->data_size);
if (err <= 0) {
r->count = 0;
result = -EPIPE;
......@@ -83,12 +83,18 @@ static int fd_fill(struct labcomm_reader *r)
return result;
}
static int fd_start(struct labcomm_reader *r)
static int fd_start(struct labcomm_reader *r, void *context)
{
return fd_fill(r);
int available;
available = r->count - r->pos;
if (available == 0) {
available = fd_fill(r, context);
}
return available;
}
static int fd_end(struct labcomm_reader *r)
static int fd_end(struct labcomm_reader *r, void *context)
{
return 0;
}
......
......@@ -14,9 +14,9 @@ struct labcomm_fd_writer {
int close_fd_on_free;
};
static int fd_flush(struct labcomm_writer *w);
static int fd_flush(struct labcomm_writer *w, void *context);
static int fd_alloc(struct labcomm_writer *w, char *version)
static int fd_alloc(struct labcomm_writer *w, void *context, char *version)
{
w->data = malloc(BUFFER_SIZE);
if (! w->data) {
......@@ -30,16 +30,16 @@ static int fd_alloc(struct labcomm_writer *w, char *version)
w->pos = 0;
if (version && version[0]) {
labcomm_write_string(w, version);
fd_flush(w);
fd_flush(w, context);
}
}
return w->error;
}
static int fd_free(struct labcomm_writer *w)
static int fd_free(struct labcomm_writer *w, void *context)
{
struct labcomm_fd_writer *context = w->context;
struct labcomm_fd_writer *fd_context = context;
free(w->data);
w->data = 0;
......@@ -47,13 +47,13 @@ static int fd_free(struct labcomm_writer *w)
w->count = 0;
w->pos = 0;
if (context->close_fd_on_free) {
close(context->fd);
if (fd_context->close_fd_on_free) {
close(fd_context->fd);
}
return 0;
}
static int fd_start(struct labcomm_writer *w,
static int fd_start(struct labcomm_writer *w, void *context,
struct labcomm_encoder *encoder,
int index,
struct labcomm_signature *signature,
......@@ -64,12 +64,20 @@ static int fd_start(struct labcomm_writer *w,
return w->error;
}
static int fd_flush(struct labcomm_writer *w)
static int fd_flush(struct labcomm_writer *w, void *context)
{
struct labcomm_fd_writer *context = w->context;
int err;
err = write(context->fd, w->data, w->pos);
struct labcomm_fd_writer *fd_context = context;
int start, err;
start = 0;
err = 0;
while (start < w->pos) {
err = write(fd_context->fd, &w->data[start], w->pos - start);
if (err <= 0) {
break;
}
start = start + err;
}
if (err < 0) {
w->error = -errno;
} else if (err == 0) {
......
......@@ -47,7 +47,17 @@
/*
* Semi private lock declarations
*/
struct labcomm_lock_action {
int (*alloc)(void *context);
int (*free)(void *context);
int (*read_lock)(void *context);
int (*read_unlock)(void *context);
int (*write_lock)(void *context);
int (*write_unlock)(void *context);
};
struct labcomm_lock {
const struct labcomm_lock_action action;
};
/*
......@@ -61,12 +71,13 @@ typedef void (*labcomm_decoder_function)(
void *context);
struct labcomm_reader_action {
int (*alloc)(struct labcomm_reader *r, char *labcomm_version);
int (*free)(struct labcomm_reader *r);
int (*start)(struct labcomm_reader *r);
int (*end)(struct labcomm_reader *r);
int (*fill)(struct labcomm_reader *r);
int (*ioctl)(struct labcomm_reader *r, int, struct labcomm_signature *, va_list);
int (*alloc)(struct labcomm_reader *r, void *context, char *labcomm_version);
int (*free)(struct labcomm_reader *r, void *context);
int (*start)(struct labcomm_reader *r, void *context);
int (*end)(struct labcomm_reader *r, void *context);
int (*fill)(struct labcomm_reader *r, void *context);
int (*ioctl)(struct labcomm_reader *r, void *context,
int action, struct labcomm_signature *signature, va_list args);
};
struct labcomm_reader {
......@@ -104,7 +115,7 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
type result; int i; \
for (i = sizeof(type) - 1 ; i >= 0 ; i--) { \
if (r->pos >= r->count) { \
r->action->fill(r); \
r->action->fill(r, r->context); \
} \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
......@@ -119,7 +130,7 @@ int labcomm_internal_decoder_ioctl(struct labcomm_decoder *decoder,
type result; int i; \
for (i = 0 ; i < sizeof(type) ; i++) { \
if (r->pos >= r->count) { \
r->action->fill(r); \
r->action->fill(r, r->context); \
} \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
......@@ -145,7 +156,7 @@ static inline unsigned int labcomm_read_packed32(struct labcomm_reader *r)
unsigned char tmp;
if (r->pos >= r->count) {
r->action->fill(r);
r->action->fill(r, r->context);
}
tmp = r->data[r->pos];
r->pos++;
......@@ -166,7 +177,7 @@ static inline char *labcomm_read_string(struct labcomm_reader *r)
result = malloc(length + 1);
for (i = 0 ; i < length ; i++) {
if (r->pos >= r->count) {
r->action->fill(r);
r->action->fill(r, r->context);
}
result[i] = r->data[r->pos];
r->pos++;
......@@ -185,15 +196,15 @@ typedef int (*labcomm_encoder_function)(
struct labcomm_writer;
struct labcomm_writer_action {
int (*alloc)(struct labcomm_writer *w, char *labcomm_version);
int (*free)(struct labcomm_writer *w);
int (*start)(struct labcomm_writer *w,
int (*alloc)(struct labcomm_writer *w, void *context, char *labcomm_version);
int (*free)(struct labcomm_writer *w, void *context);
int (*start)(struct labcomm_writer *w, void *context,
struct labcomm_encoder *encoder,
int index, struct labcomm_signature *signature,
void *value);
int (*end)(struct labcomm_writer *w);
int (*flush)(struct labcomm_writer *w);
int (*ioctl)(struct labcomm_writer *w,
int (*end)(struct labcomm_writer *w, void *context);
int (*flush)(struct labcomm_writer *w, void *context);
int (*ioctl)(struct labcomm_writer *w, void *context,
int index, struct labcomm_signature *,
va_list);
};
......@@ -233,7 +244,7 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
for (i = sizeof(type) - 1 ; i >= 0 ; i--) { \
if (w->pos >= w->count) { /*buffer is full*/ \
int err; \
err = w->action->flush(w); \
err = w->action->flush(w, w->context); \
if (err != 0) { return err; } \
} \
w->data[w->pos] = ((unsigned char*)(&data))[i]; \
......@@ -250,7 +261,7 @@ int labcomm_internal_encoder_ioctl(struct labcomm_encoder *encoder,
for (i = 0 ; i < sizeof(type) ; i++) { \
if (w->pos >= w->count) { \
int err; \
err = w->action->flush(w); \
err = w->action->flush(w, w->context); \
if (err != 0) { return err; } \
} \
w->data[w->pos] = ((unsigned char*)(&data))[i]; \
......@@ -281,7 +292,7 @@ static inline int labcomm_write_packed32(struct labcomm_writer *w,
for (i = i - 1 ; i >= 0 ; i--) {
if (w->pos >= w->count) {
int err;
err = w->action->flush(w);
err = w->action->flush(w, w->context);
if (err != 0) { return err; }
}
w->data[w->pos++] = tmp[i] | (i?0x80:0x00);
......@@ -299,7 +310,7 @@ static inline int labcomm_write_string(struct labcomm_writer *w, char *s)
for (i = 0 ; i < length ; i++) {
if (w->pos >= w->count) {
int err;
err = w->action->flush(w);
err = w->action->flush(w, w->context);
if (err != 0) { return err; }
}
w->data[w->pos] = s[i];
......
......@@ -11,7 +11,8 @@
static unsigned char buffer[128];
struct labcomm_writer *writer;