From 506eb33bd441bc13217d3633d6e4db9c42395260 Mon Sep 17 00:00:00 2001 From: Sven Robertz <sven@cs.lth.se> Date: Fri, 22 Mar 2013 15:03:54 +0100 Subject: [PATCH] put back varargs in writer --- compiler/C_CodeGen.jrag | 6 +++-- lib/c/labcomm.c | 27 +++++++++++++++----- lib/c/labcomm.h | 10 ++++++-- lib/c/labcomm_dynamic_buffer_reader_writer.c | 2 +- lib/c/labcomm_dynamic_buffer_reader_writer.h | 2 +- lib/c/labcomm_fd_reader_writer.c | 2 +- lib/c/labcomm_fd_reader_writer.h | 2 +- lib/c/labcomm_private.h | 2 +- lib/c/test/labcomm_mem_writer.c | 2 +- lib/c/test/labcomm_mem_writer.h | 2 +- 10 files changed, 39 insertions(+), 18 deletions(-) diff --git a/compiler/C_CodeGen.jrag b/compiler/C_CodeGen.jrag index 1efc0a3..5b04da8 100644 --- a/compiler/C_CodeGen.jrag +++ b/compiler/C_CodeGen.jrag @@ -641,7 +641,8 @@ aspect C_Encoder { env.println(")"); env.println("{"); env.indent(); - env.println("e->writer.write(&e->writer, labcomm_writer_start);"); + env.println("labcomm_encoder_start(e, &labcomm_signature_" + + env.prefix + getName() + ");"); env.println("labcomm_encode_type_index(e, &labcomm_signature_" + env.prefix + getName() + ");"); env.println("{"); @@ -649,7 +650,8 @@ aspect C_Encoder { getType().C_emitEncoder(env); env.unindent(); env.println("}"); - env.println("e->writer.write(&e->writer, labcomm_writer_end);"); + env.println("labcomm_encoder_end(e, &labcomm_signature_" + + env.prefix + getName() + ");"); env.unindent(); env.println("}"); diff --git a/lib/c/labcomm.c b/lib/c/labcomm.c index fd19058..3b790e5 100644 --- a/lib/c/labcomm.c +++ b/lib/c/labcomm.c @@ -183,6 +183,25 @@ static int get_encoder_index( return result; } +void labcomm_encoder_start(struct labcomm_encoder *e, + labcomm_signature_t *s) +{ + int index = get_encoder_index(e, s); + e->writer.write(&e->writer, labcomm_writer_start, index); +} + +void labcomm_encoder_end(struct labcomm_encoder *e, + labcomm_signature_t *s) +{ + e->writer.write(&e->writer, labcomm_writer_end); +} + +void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s) +{ + int index = get_encoder_index(e, s); + labcomm_encode_packed32(e, index); +} + void labcomm_encode_signature(struct labcomm_encoder *e, labcomm_signature_t *signature) { @@ -248,7 +267,7 @@ static void do_encode( } labcomm_encoder_t *labcomm_encoder_new( - int (*writer)(labcomm_writer_t *, labcomm_writer_action_t), + int (*writer)(labcomm_writer_t *, labcomm_writer_action_t, ...), void *writer_context) { labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t)); @@ -342,12 +361,6 @@ int labcomm_encoder_ioctl(struct labcomm_encoder *encoder, } -void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s) -{ - int index = get_encoder_index(e, s); - labcomm_encode_packed32(e, index); -} - static void collect_flat_signature( labcomm_decoder_t *decoder, labcomm_encoder_t *signature_writer) diff --git a/lib/c/labcomm.h b/lib/c/labcomm.h index d99d8fa..1d4740f 100644 --- a/lib/c/labcomm.h +++ b/lib/c/labcomm.h @@ -147,13 +147,13 @@ typedef struct labcomm_writer { int count; int pos; int error; - int (*write)(struct labcomm_writer *, labcomm_writer_action_t); + int (*write)(struct labcomm_writer *, labcomm_writer_action_t, ...); int (*ioctl)(struct labcomm_writer *, int, va_list); labcomm_error_handler_callback on_error; } labcomm_writer_t; struct labcomm_encoder *labcomm_encoder_new( - int (*writer)(labcomm_writer_t *, labcomm_writer_action_t), + int (*writer)(labcomm_writer_t *, labcomm_writer_action_t, ...), void *writer_context); void labcomm_encoder_free( struct labcomm_encoder *encoder); @@ -162,4 +162,10 @@ int labcomm_encoder_ioctl(struct labcomm_encoder *encoder, int ioctl_action, ...); +void labcomm_encoder_start(struct labcomm_encoder *e, + labcomm_signature_t *s) ; + +//HERE BE DRAGONS: is the signature_t* needed here? +void labcomm_encoder_end(struct labcomm_encoder *e, + labcomm_signature_t *s) ; #endif diff --git a/lib/c/labcomm_dynamic_buffer_reader_writer.c b/lib/c/labcomm_dynamic_buffer_reader_writer.c index 1e55c41..b363f17 100644 --- a/lib/c/labcomm_dynamic_buffer_reader_writer.c +++ b/lib/c/labcomm_dynamic_buffer_reader_writer.c @@ -2,7 +2,7 @@ int labcomm_dynamic_buffer_writer( labcomm_writer_t *w, - labcomm_writer_action_t action) + labcomm_writer_action_t action, ...) { switch (action) { case labcomm_writer_alloc: { diff --git a/lib/c/labcomm_dynamic_buffer_reader_writer.h b/lib/c/labcomm_dynamic_buffer_reader_writer.h index d82152c..b03e466 100644 --- a/lib/c/labcomm_dynamic_buffer_reader_writer.h +++ b/lib/c/labcomm_dynamic_buffer_reader_writer.h @@ -9,6 +9,6 @@ extern int labcomm_dynamic_buffer_reader( extern int labcomm_dynamic_buffer_writer( labcomm_writer_t *writer, - labcomm_writer_action_t action); + labcomm_writer_action_t action, ...); #endif diff --git a/lib/c/labcomm_fd_reader_writer.c b/lib/c/labcomm_fd_reader_writer.c index bd81f7b..3ddafc4 100644 --- a/lib/c/labcomm_fd_reader_writer.c +++ b/lib/c/labcomm_fd_reader_writer.c @@ -62,7 +62,7 @@ int labcomm_fd_reader( int labcomm_fd_writer( labcomm_writer_t *w, - labcomm_writer_action_t action) + labcomm_writer_action_t action, ...) { int result = 0; int *fd = w->context; diff --git a/lib/c/labcomm_fd_reader_writer.h b/lib/c/labcomm_fd_reader_writer.h index ef6b637..1de5858 100644 --- a/lib/c/labcomm_fd_reader_writer.h +++ b/lib/c/labcomm_fd_reader_writer.h @@ -9,6 +9,6 @@ extern int labcomm_fd_reader( extern int labcomm_fd_writer( labcomm_writer_t *writer, - labcomm_writer_action_t action); + labcomm_writer_action_t action, ...); #endif diff --git a/lib/c/labcomm_private.h b/lib/c/labcomm_private.h index 7638d73..ed5894c 100644 --- a/lib/c/labcomm_private.h +++ b/lib/c/labcomm_private.h @@ -331,7 +331,7 @@ static inline void labcomm_encode_string(labcomm_encoder_t *e, void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s); static inline int labcomm_buffer_write(struct labcomm_writer *w, - labcomm_writer_action_t action) + labcomm_writer_action_t action, ...) { // If this gets called, it is an error, // so note error and let producer proceed diff --git a/lib/c/test/labcomm_mem_writer.c b/lib/c/test/labcomm_mem_writer.c index 6f26e69..294a471 100644 --- a/lib/c/test/labcomm_mem_writer.c +++ b/lib/c/test/labcomm_mem_writer.c @@ -27,7 +27,7 @@ static void copy_data(labcomm_writer_t *w, labcomm_mem_writer_context_t *mcontex * Write encoded messages to memory. w->context is assumed to be a pointer to a * labcomm_mem_writer_context_t structure. */ -int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action) +int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...) { int result = 0; // Unwrap pointers for easy access. diff --git a/lib/c/test/labcomm_mem_writer.h b/lib/c/test/labcomm_mem_writer.h index 4585903..7506342 100644 --- a/lib/c/test/labcomm_mem_writer.h +++ b/lib/c/test/labcomm_mem_writer.h @@ -11,7 +11,7 @@ struct labcomm_mem_writer_context_t { unsigned char *buf; // Allocated destination buffer. }; -int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action); +int labcomm_mem_writer(labcomm_writer_t *w, labcomm_writer_action_t action, ...); /* Wrapper the internal static function copy_data. This is needed so that the exceptions can be unit tested. */ void test_copy_data(labcomm_writer_t *w, labcomm_mem_writer_context_t *mcontext, unsigned char *mbuf); -- GitLab