Commit 417a77f2 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Made sample_ref a distinct (opaque) type in C

parent 9d24c494
......@@ -311,7 +311,7 @@ aspect C_Type {
case LABCOMM_DOUBLE: { env.print("double"); } break;
case LABCOMM_STRING: { env.print("char*"); } break;
case LABCOMM_SAMPLE: {
env.print("const struct labcomm2014_signature *");
env.print("const struct labcomm2014_sample_ref *");
} break;
}
env.print(" " + name);
......@@ -528,7 +528,7 @@ aspect C_Decoder {
env.print(env.qualid + " = ");
switch (getToken()) {
case LABCOMM_SAMPLE: {
env.println("r->decoder->index_to_signature(" +
env.println("r->decoder->index_to_sample_ref(" +
"r->decoder, labcomm2014_read_int(r));");
} break;
default: {
......@@ -1062,7 +1062,7 @@ aspect C_Encoder {
switch (getToken()) {
case LABCOMM_SAMPLE: {
env.println("labcomm2014_write_int(w, " +
"w->encoder->signature_to_index(w->encoder, " +
"w->encoder->sample_ref_to_index(w->encoder, " +
env.qualid + "));");
} break;
default: {
......
......@@ -13,7 +13,7 @@ static void handle_test_doavoid(test_doavoid *v,void *context) {
}
static void handle_test_intAndRef(test_intAndRef *v,void *context) {
printf("Got intAndRef. (%d : %s) \n", v->x, v->reference->name);
printf("Got intAndRef. (%d : %p) \n", v->x, v->reference);
}
static void handle_test_twoInts(test_twoInts *v,void *context) {
......
......@@ -112,7 +112,7 @@ int labcomm2014_decoder_ioctl(struct labcomm2014_decoder *decoder,
uint32_t ioctl_action,
...);
const struct labcomm2014_signature *labcomm2014_decoder_get_ref_signature(
const struct labcomm2014_sample_ref *labcomm2014_decoder_get_sample_ref(
struct labcomm2014_decoder *decoder,
const struct labcomm2014_signature *signature);
......@@ -137,7 +137,7 @@ int labcomm2014_encoder_ioctl(struct labcomm2014_encoder *encoder,
uint32_t ioctl_action,
...);
const struct labcomm2014_signature *labcomm2014_encoder_get_sample_ref(
const struct labcomm2014_sample_ref *labcomm2014_encoder_get_sample_ref(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature);
......
......@@ -619,7 +619,7 @@ out:
return local_index;
}
static const struct labcomm2014_signature *do_index_to_signature(
static const struct labcomm2014_sample_ref *do_index_to_sample_ref(
struct labcomm2014_decoder *d, int index)
{
const struct labcomm2014_signature *result = 0;
......@@ -635,14 +635,14 @@ static const struct labcomm2014_signature *do_index_to_signature(
local_index, 0);
}
labcomm2014_scheduler_data_unlock(d->scheduler);
return result;
return labcomm2014_signature_to_sample_ref(result);
}
static const struct labcomm2014_signature *do_get_ref_signature(
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
return signature;
return (const struct labcomm2014_sample_ref *) signature;
}
static void do_free(struct labcomm2014_decoder* d)
......@@ -693,8 +693,8 @@ struct labcomm2014_decoder *labcomm2014_decoder_new(
result->decoder.ref_register = do_ref_register;
result->decoder.sample_register = do_register_sample;
result->decoder.ioctl = do_ioctl;
result->decoder.index_to_signature = do_index_to_signature;
result->decoder.get_ref_signature = do_get_ref_signature;
result->decoder.index_to_sample_ref = do_index_to_sample_ref;
result->decoder.ref_get = do_ref_get;
LABCOMM_SIGNATURE_ARRAY_INIT(result->local, struct sample_entry);
LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->local_ref,
......@@ -705,10 +705,10 @@ struct labcomm2014_decoder *labcomm2014_decoder_new(
}
const struct labcomm2014_signature *labcomm2014_decoder_get_ref_signature(
const struct labcomm2014_sample_ref *labcomm2014_decoder_get_sample_ref(
struct labcomm2014_decoder *decoder,
const struct labcomm2014_signature *signature)
{
return decoder->get_ref_signature(decoder, signature);
return decoder->ref_get(decoder, signature);
}
......@@ -194,14 +194,16 @@ static int do_ioctl(
return result;
}
static int do_signature_to_index(
struct labcomm2014_encoder *e, const struct labcomm2014_signature *signature)
static int do_sample_ref_to_index(
struct labcomm2014_encoder *e,
const struct labcomm2014_sample_ref *sample_ref)
{
/* writer_lock should be held at this point */
struct encoder *ie = e->context;
int index = 0;
if (signature != NULL) {
index = labcomm2014_get_local_index(signature);
if (sample_ref != NULL) {
index = labcomm2014_get_local_index(
(struct labcomm2014_signature *)sample_ref);
if (! LABCOMM_SIGNATURE_ARRAY_GET(ie->sample_ref, int, index, 0)) {
index = 0;
}
......@@ -345,11 +347,11 @@ out:
#endif
}
static const struct labcomm2014_signature *do_ref_get(
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
return signature;
return (const struct labcomm2014_sample_ref *) signature;
}
void labcomm2014_encoder_free(struct labcomm2014_encoder* e)
......@@ -400,7 +402,7 @@ static struct labcomm2014_encoder *internal_encoder_new(
result->encoder.ref_register = do_ref_register;
result->encoder.encode = do_encode;
result->encoder.ioctl = do_ioctl;
result->encoder.signature_to_index = do_signature_to_index;
result->encoder.sample_ref_to_index = do_sample_ref_to_index;
result->encoder.ref_get = do_ref_get;
LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int);
LABCOMM_SIGNATURE_ARRAY_INIT(result->sample_ref, int);
......@@ -432,7 +434,7 @@ struct labcomm2014_encoder *labcomm2014_encoder_new(
}
const struct labcomm2014_signature *labcomm2014_encoder_get_sample_ref(
const struct labcomm2014_sample_ref *labcomm2014_encoder_get_sample_ref(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature)
{
......
......@@ -93,6 +93,25 @@ struct labcomm2014_memory {
void *context;
};
/*
* sample_ref/signature helpers
*/
static const inline
struct labcomm2014_signature *labcomm2014_sample_ref_to_signature(
const struct labcomm2014_sample_ref *sample_ref
)
{
return (struct labcomm2014_signature *)sample_ref;
}
static const inline
struct labcomm2014_sample_ref *labcomm2014_signature_to_sample_ref(
const struct labcomm2014_signature *signature
)
{
return (struct labcomm2014_sample_ref *)signature;
}
/*
* Semi private decoder declarations
*/
......@@ -211,9 +230,9 @@ struct labcomm2014_decoder {
int (*ioctl)(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *s,
uint32_t ioctl_action, va_list args);
const struct labcomm2014_signature *(*index_to_signature)(
const struct labcomm2014_sample_ref *(*index_to_sample_ref)(
struct labcomm2014_decoder *decoder, int index);
const struct labcomm2014_signature *(*get_ref_signature)(
const struct labcomm2014_sample_ref *(*ref_get)(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature);
};
......@@ -247,7 +266,7 @@ struct labcomm2014_decoder {
if (r->error < 0) { \
return 0; \
} \
} \
} > \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
} \
......@@ -392,9 +411,9 @@ struct labcomm2014_encoder {
int (*ioctl)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args);
int (*signature_to_index)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature);
const struct labcomm2014_signature *(*ref_get)(
int (*sample_ref_to_index)(struct labcomm2014_encoder *e,
const struct labcomm2014_sample_ref *s);
const struct labcomm2014_sample_ref *(*ref_get)(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature);
};
......
......@@ -147,15 +147,15 @@ static int do_ioctl(struct labcomm2014_decoder *d,
ioctl_action, args);
}
static const struct labcomm2014_signature *do_index_to_signature(
static const struct labcomm2014_sample_ref *do_index_to_sample_ref(
struct labcomm2014_decoder *d, int index)
{
struct decoder *id = d->context;
return id->next->index_to_signature(id->next, index);
return id->next->index_to_sample_ref(id->next, index);
}
static const struct labcomm2014_signature *do_get_ref_signature(
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
......@@ -164,9 +164,9 @@ static const struct labcomm2014_signature *do_get_ref_signature(
renamed = get_renamed(d, signature);
if (renamed == NULL) {
return id->next->get_ref_signature(id->next, signature);
return id->next->ref_get(id->next, signature);
} else {
return id->next->get_ref_signature(id->next, renamed);
return id->next->ref_get(id->next, renamed);
}
}
......@@ -210,8 +210,8 @@ struct labcomm2014_decoder *labcomm2014_renaming_decoder_new(
result->decoder.sample_register = do_sample_register;
result->decoder.ref_register = do_ref_register;
result->decoder.ioctl = do_ioctl;
result->decoder.index_to_signature = do_index_to_signature;
result->decoder.get_ref_signature = do_get_ref_signature;
result->decoder.index_to_sample_ref = do_index_to_sample_ref;
result->decoder.ref_get = do_ref_get;
result->next = d;
result->rename = rename;
result->context = context;
......
......@@ -171,15 +171,16 @@ static int do_ioctl(struct labcomm2014_encoder *e,
ioctl_action, args);
}
static int do_signature_to_index(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
static int do_sample_ref_to_index(
struct labcomm2014_encoder *e,
const struct labcomm2014_sample_ref *sample_ref)
{
struct encoder *ie = e->context;
return ie->next->signature_to_index(ie->next, get_renamed(e, signature));
return ie->next->sample_ref_to_index(ie->next,sample_ref);
}
static const struct labcomm2014_signature *do_ref_get(
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
......@@ -236,7 +237,7 @@ struct labcomm2014_encoder *labcomm2014_renaming_encoder_new(
result->encoder.ref_register = do_ref_register;
result->encoder.encode = do_encode;
result->encoder.ioctl = do_ioctl;
result->encoder.signature_to_index = do_signature_to_index;
result->encoder.sample_ref_to_index = do_sample_ref_to_index;
result->encoder.ref_get = do_ref_get;
result->next = e;
result->rename = rename;
......
......@@ -46,6 +46,8 @@ struct labcomm2014_signature_data {
};
#endif
struct labcomm2014_sample_ref;
struct labcomm2014_signature {
char *name;
int (*encoded_size)(void *); /* void* refers to sample_data */
......
......@@ -166,10 +166,14 @@ int main(int argc, char **argv)
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_UnusedD);
labcomm2014_encoder_sample_ref_register(encoder, labcomm2014_signature_generated_encoding_R);
r.a[0] = labcomm2014_signature_generated_encoding_V;
r.a[1] = labcomm2014_signature_generated_encoding_UnusedE;
r.a[2] = labcomm2014_signature_generated_encoding_UnusedD;
r.a[3] = labcomm2014_signature_generated_encoding_R;
r.a[0] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_V);
r.a[1] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_UnusedE);
r.a[2] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_UnusedD);
r.a[3] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(encoder, &r);
labcomm2014_encoder_free(encoder);
......
......@@ -266,10 +266,14 @@ int main(void)
EXPECT({VARIABLE(20), 0x01, 1});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
R.a[0] = labcomm2014_signature_generated_encoding_V;
R.a[1] = labcomm2014_signature_generated_encoding_B;
R.a[2] = labcomm2014_signature_generated_encoding_UnusedE;
R.a[3] = labcomm2014_signature_generated_encoding_R;
R.a[0] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_V);
R.a[1] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_B);
R.a[2] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_UnusedE);
R.a[3] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(encoder, &R);
EXPECT({VARIABLE(4), 0x10, 0x00, 0x00, 0x00, VARIABLE(5),
0x00, 0x00, 0x00, VARIABLE(6),
......
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <err.h>
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_fd_writer.h"
#include "labcomm2014_fd_reader.h"
#include "labcomm2014_renaming.h"
#include "labcomm2014_renaming_encoder.h"
#include "labcomm2014_renaming_decoder.h"
#include "test/gen/generated_encoding.h"
#define DATA_FILE "renaming_test.dat"
static void handle_r(generated_encoding_R *v, void *context)
{
labcomm2014_copy_generated_encoding_R(labcomm2014_default_memory, context, v);
}
int main(int argc, char **argv)
{
struct labcomm2014_encoder *encoder, *prefix_encoder, *suffix_encoder;
struct labcomm2014_decoder *decoder, *prefix_decoder, *suffix_decoder;
int fd;
generated_encoding_R r;
generated_encoding_R cache_r, prefix_cache_r, suffix_cache_r;
fd = open(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
err(1, "open()");
}
encoder = labcomm2014_encoder_new(
labcomm2014_fd_writer_new(labcomm2014_default_memory, fd, 0),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix_encoder = labcomm2014_renaming_encoder_new(
encoder, labcomm2014_renaming_prefix, "p.");
suffix_encoder = labcomm2014_renaming_encoder_new(
prefix_encoder, labcomm2014_renaming_suffix, ".s");
labcomm2014_encoder_register_generated_encoding_R(encoder);
labcomm2014_encoder_register_generated_encoding_R(prefix_encoder);
labcomm2014_encoder_register_generated_encoding_R(suffix_encoder);
labcomm2014_encoder_sample_ref_register(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encoder_sample_ref_register(
prefix_encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encoder_sample_ref_register(
suffix_encoder, labcomm2014_signature_generated_encoding_R);
r.a[0] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
r.a[1] = labcomm2014_encoder_get_sample_ref(
prefix_encoder, labcomm2014_signature_generated_encoding_R);
r.a[2] = labcomm2014_encoder_get_sample_ref(
suffix_encoder, labcomm2014_signature_generated_encoding_R);
r.a[3] = labcomm2014_encoder_get_sample_ref(
encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(encoder, &r);
r.a[3] = labcomm2014_encoder_get_sample_ref(
prefix_encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(prefix_encoder, &r);
r.a[3] = labcomm2014_encoder_get_sample_ref(
suffix_encoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_encode_generated_encoding_R(suffix_encoder, &r);
labcomm2014_encoder_free(suffix_encoder);
labcomm2014_encoder_free(prefix_encoder);
labcomm2014_encoder_free(encoder);
encoder = NULL;
lseek(fd, 0, SEEK_SET);
decoder =
labcomm2014_decoder_new(
labcomm2014_fd_reader_new(labcomm2014_default_memory, fd, 0),
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix_decoder = labcomm2014_renaming_decoder_new(
decoder, labcomm2014_renaming_prefix, "p.");
suffix_decoder = labcomm2014_renaming_decoder_new(
prefix_decoder, labcomm2014_renaming_suffix, ".s");
labcomm2014_decoder_register_generated_encoding_R(
decoder, handle_r, &cache_r);
labcomm2014_decoder_register_generated_encoding_R(
prefix_decoder, handle_r, &prefix_cache_r);
labcomm2014_decoder_register_generated_encoding_R(
suffix_decoder, handle_r, &suffix_cache_r);
labcomm2014_decoder_sample_ref_register(
decoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_decoder_sample_ref_register(
prefix_decoder, labcomm2014_signature_generated_encoding_R);
labcomm2014_decoder_sample_ref_register(
suffix_decoder, labcomm2014_signature_generated_encoding_R);
while (labcomm2014_decoder_decode_one(decoder) > 0) ;
fprintf(stderr, "cache_r:\n");
fprintf(stderr, "%p %p %p %p\n",
cache_r.a[0],
cache_r.a[1],
cache_r.a[2],
cache_r.a[3]);
fprintf(stderr, "prefix_cache_r:\n");
fprintf(stderr, "%p %p %p %p\n",
prefix_cache_r.a[0],
prefix_cache_r.a[1],
prefix_cache_r.a[2],
prefix_cache_r.a[3]);
fprintf(stderr, "suffix_cache_r:\n");
fprintf(stderr, "%p %p %p %p\n",
suffix_cache_r.a[0],
suffix_cache_r.a[1],
suffix_cache_r.a[2],
suffix_cache_r.a[3]);
assert(cache_r.a[0] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(cache_r.a[1] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(cache_r.a[2] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
assert(cache_r.a[3] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[0] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[1] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[2] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
assert(prefix_cache_r.a[3] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[0] ==
labcomm2014_decoder_get_sample_ref(
decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[1] ==
labcomm2014_decoder_get_sample_ref(
prefix_decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[2] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
assert(suffix_cache_r.a[3] ==
labcomm2014_decoder_get_sample_ref(
suffix_decoder, labcomm2014_signature_generated_encoding_R));
labcomm2014_decoder_free(decoder);
close(fd);
unlink(DATA_FILE);
}
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