Commit e6e5f5e8 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Made renaming encoders/decoders use renaming registry

parent a4e342ea
......@@ -23,92 +23,63 @@
#include "labcomm2014_renaming_decoder.h"
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_renaming_private.h"
struct decoder {
struct labcomm2014_decoder decoder;
struct labcomm2014_decoder *next;
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context);
struct labcomm2014_renaming_registry *registry;
char *(*rename_func)(struct labcomm2014_memory *m, char *name, void *context);
void *context;
LABCOMM_SIGNATURE_ARRAY_DEF(renamed, struct labcomm2014_signature *);
LABCOMM_SIGNATURE_ARRAY_DEF(renamed,
struct labcomm2014_renaming_rename *);
};
static struct labcomm2014_signature *get_renamed(
static struct labcomm2014_renaming_rename *get_renamed(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
struct labcomm2014_signature *result;
struct labcomm2014_renaming_rename *result;
struct decoder *id = d->context;
int index;
index = labcomm2014_get_local_index(signature);
labcomm2014_scheduler_data_lock(d->scheduler);
result = LABCOMM_SIGNATURE_ARRAY_GET(id->renamed,
struct labcomm2014_signature *,
struct labcomm2014_renaming_rename *,
index, NULL);
labcomm2014_scheduler_data_unlock(d->scheduler);
return result;
}
static struct labcomm2014_signature *set_renamed(
static struct labcomm2014_renaming_rename *set_renamed(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
struct labcomm2014_signature *result;
struct labcomm2014_renaming_rename *result = NULL;
result = get_renamed(d, signature);
if (result == NULL) {
/* create a renamed sample */
struct decoder *id = d->context;
struct labcomm2014_renaming_rename **renamed;
struct labcomm2014_renaming_rename *entry = NULL;
int index;
struct labcomm2014_signature **renamed;
index = labcomm2014_get_local_index(signature);
if (index <= 0) { goto out; /*result already NULL */}
entry = labcomm2014_renaming_rename_new(id->registry, signature,
id->rename_func, id->context);
if (entry == NULL) { goto out; }
labcomm2014_scheduler_data_lock(d->scheduler);
renamed = LABCOMM_SIGNATURE_ARRAY_REF(d->memory, id->renamed,
struct labcomm2014_signature *,
struct labcomm2014_renaming_rename *,
index);
if (renamed == NULL) {
labcomm2014_error_warning(d->error,
LABCOMM2014_ERROR_MEMORY,
"Could not allocate rename slot: %s\n",
signature->name);
goto unlock;
}
if (*renamed != NULL) {
/* Somebody beat as to allocation, this should never happen */
goto unlock;
}
result = labcomm2014_memory_alloc(d->memory, 0, sizeof(*result));
if (result == NULL) {
labcomm2014_error_warning(d->error,
LABCOMM2014_ERROR_MEMORY,
"Could not allocate rename signature: %s\n",
signature->name);
goto unlock;
}
result->name = id->rename(d->memory, signature->name, id->context);
if (result->name == NULL) {
labcomm2014_error_warning(d->error,
LABCOMM2014_ERROR_MEMORY,
"Could not allocate rename name: %s\n",
signature->name);
goto unlock_free_result;
}
result->encoded_size = signature->encoded_size;
result->size = signature->size;
result->signature = signature->signature;
result->index = 0;
#ifndef LABCOMM_NO_TYPEDECL
result->tdsize = signature->tdsize;
result->treedata = signature->treedata;
#endif
labcomm2014_set_local_index(result);
*renamed = result;
if (renamed == NULL) { goto free_unlock; }
if (*renamed != NULL) { result = *renamed; goto free_unlock; }
*renamed = entry;
result = entry;
goto unlock;
unlock_free_result:
labcomm2014_memory_free(d->memory, 0, result);
result = NULL;
free_unlock:
labcomm2014_renaming_rename_free(id->registry, entry);
unlock:
labcomm2014_scheduler_data_unlock(d->scheduler);
out:
......@@ -118,33 +89,42 @@ static struct labcomm2014_signature *set_renamed(
}
static int do_sample_register(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *s,
const struct labcomm2014_signature *signature,
labcomm2014_decoder_function decoder,
labcomm2014_handler_function handler,
void *context)
{
const struct labcomm2014_renaming_rename *renamed;
struct decoder *id = d->context;
return id->next->sample_register(id->next, set_renamed(d, s), decoder,
handler, context);
renamed = set_renamed(d, signature);
return id->next->sample_register(
id->next, labcomm2014_renaming_rename_signature(renamed),
decoder, handler, context);
}
static int do_ref_register(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
const struct labcomm2014_renaming_rename *renamed;
struct decoder *id = d->context;
return id->next->ref_register(id->next, set_renamed(d, signature));
renamed = set_renamed(d, signature);
return id->next->ref_register(
id->next, labcomm2014_renaming_rename_signature(renamed));
}
static int do_ioctl(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args)
{
const struct labcomm2014_renaming_rename *renamed;
struct decoder *id = d->context;
return id->next->ioctl(id->next, get_renamed(d, signature),
ioctl_action, args);
renamed = get_renamed(d, signature);
return id->next->ioctl(
id->next, labcomm2014_renaming_rename_signature(renamed),
ioctl_action, args);
}
static int do_decode_one(struct labcomm2014_decoder *d)
......@@ -166,14 +146,15 @@ static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
const struct labcomm2014_signature *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct decoder *id = d->context;
renamed = get_renamed(d, signature);
if (renamed == NULL) {
return id->next->ref_get(id->next, signature);
} else {
return id->next->ref_get(id->next, renamed);
return id->next->ref_get(id->next,
labcomm2014_renaming_rename_signature(renamed));
}
}
......@@ -182,23 +163,25 @@ static void do_free(struct labcomm2014_decoder *d)
struct decoder *id = d->context;
int i;
LABCOMM_SIGNATURE_ARRAY_FOREACH(id->renamed, struct labcomm2014_signature *,
LABCOMM_SIGNATURE_ARRAY_FOREACH(id->renamed,
struct labcomm2014_renaming_rename *,
i) {
struct labcomm2014_signature *s;
s = LABCOMM_SIGNATURE_ARRAY_GET(id->renamed,
struct labcomm2014_signature *, i, NULL);
if (s) {
labcomm2014_memory_free(d->memory, 0, s->name);
labcomm2014_memory_free(d->memory, 0, s);
struct labcomm2014_renaming_rename *r;
r = LABCOMM_SIGNATURE_ARRAY_GET(id->renamed,
struct labcomm2014_renaming_rename *,
i, NULL);
if (r) {
labcomm2014_renaming_rename_free(id->registry, r);
}
}
LABCOMM_SIGNATURE_ARRAY_FREE(d->memory, id->renamed,
struct labcomm2014_signature *);
struct labcomm2014_renaming_rename *);
labcomm2014_memory_free(d->memory, 0, id);
}
struct labcomm2014_decoder *labcomm2014_renaming_decoder_new(
struct labcomm2014_decoder *d,
struct labcomm2014_renaming_registry *registry,
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
void *context)
{
......@@ -220,10 +203,11 @@ struct labcomm2014_decoder *labcomm2014_renaming_decoder_new(
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->registry = registry;
result->rename_func = rename;
result->context = context;
LABCOMM_SIGNATURE_ARRAY_INIT(result->renamed,
struct labcomm2014_signature *);
struct labcomm2014_renaming_rename *);
return &(result->decoder);
}
}
......@@ -23,9 +23,11 @@
#define __LABCOMM2014_RENAMING_DECODER_H__
#include "labcomm2014.h"
#include "labcomm2014_renaming.h"
struct labcomm2014_decoder *labcomm2014_renaming_decoder_new(
struct labcomm2014_decoder *d,
struct labcomm2014_renaming_registry *r,
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
void *context);
......
......@@ -23,104 +23,63 @@
#include "labcomm2014_renaming_encoder.h"
#include "labcomm2014.h"
#include "labcomm2014_private.h"
struct renamed {
struct labcomm2014_signature signature;
struct labcomm2014_signature_data s_treedata[2];
};
#include "labcomm2014_renaming_private.h"
struct encoder {
struct labcomm2014_encoder encoder;
struct labcomm2014_encoder *next;
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context);
struct labcomm2014_renaming_registry *registry;
char *(*rename_func)(struct labcomm2014_memory *m, char *name, void *context);
void *context;
LABCOMM_SIGNATURE_ARRAY_DEF(renamed, struct renamed *);
LABCOMM_SIGNATURE_ARRAY_DEF(renamed,
struct labcomm2014_renaming_rename *);
};
static struct renamed *get_renamed(
static struct labcomm2014_renaming_rename *get_renamed(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
struct renamed *result;
struct labcomm2014_renaming_rename *result;
struct encoder *ie = e->context;
int index;
index = labcomm2014_get_local_index(signature);
labcomm2014_scheduler_writer_lock(e->scheduler);
result = LABCOMM_SIGNATURE_ARRAY_GET(ie->renamed,
struct renamed *,
struct labcomm2014_renaming_rename *,
index, NULL);
labcomm2014_scheduler_writer_unlock(e->scheduler);
return result;
}
static struct renamed *set_renamed(
static struct labcomm2014_renaming_rename *set_renamed(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
struct renamed *result;
struct labcomm2014_renaming_rename *result = NULL;
result = get_renamed(e, signature);
if (result == NULL) {
/* create a renamed sample */
struct encoder *ie = e->context;
struct labcomm2014_renaming_rename **renamed;
struct labcomm2014_renaming_rename *entry = NULL;
int index;
struct renamed **renamed;
index = labcomm2014_get_local_index(signature);
if (index <= 0) { goto out; /*result already NULL */}
entry = labcomm2014_renaming_rename_new(ie->registry, signature,
ie->rename_func, ie->context);
if (entry == NULL) { goto out; }
labcomm2014_scheduler_writer_lock(e->scheduler);
renamed = LABCOMM_SIGNATURE_ARRAY_REF(e->memory, ie->renamed,
struct renamed *,
struct labcomm2014_renaming_rename *,
index);
if (renamed == NULL) {
labcomm2014_error_warning(e->error,
LABCOMM2014_ERROR_MEMORY,
"Could not allocate rename slot: %s\n",
signature->name);
goto unlock;
}
if (*renamed != NULL) {
/* Somebody beat as to allocation, this should never happen */
goto unlock;
}
result = labcomm2014_memory_alloc(e->memory, 0, sizeof(*result));
if (result == NULL) {
labcomm2014_error_warning(e->error,
LABCOMM2014_ERROR_MEMORY,
"Could not allocate rename signature: %s\n",
signature->name);
goto unlock;
}
result->signature.name = ie->rename(
e->memory, signature->name, ie->context);
if (result->signature.name == NULL) {
labcomm2014_error_warning(e->error,
LABCOMM2014_ERROR_MEMORY,
"Could not allocate rename name: %s\n",
signature->name);
goto unlock_free_result;
}
result->signature.encoded_size = signature->encoded_size;
result->signature.size = signature->size;
result->signature.signature = signature->signature;
result->signature.index = 0;
#ifndef LABCOMM_NO_TYPEDECL
struct labcomm2014_signature_data s_treedata[2] = {
LABCOMM_SIGDEF_SIGNATURE(*signature),
LABCOMM_SIGDEF_END
};
result->s_treedata[0] = s_treedata[0];
result->s_treedata[1] = s_treedata[1];
result->signature.tdsize = sizeof(result->s_treedata);
result->signature.treedata = result->s_treedata;
#endif
labcomm2014_set_local_index(&result->signature);
*renamed = result;
if (renamed == NULL) { goto free_unlock; }
if (*renamed != NULL) { result = *renamed; goto free_unlock; }
*renamed = entry;
result = entry;
goto unlock;
unlock_free_result:
labcomm2014_memory_free(e->memory, 0, result);
result = NULL;
free_unlock:
labcomm2014_renaming_rename_free(ie->registry, entry);
unlock:
labcomm2014_scheduler_writer_unlock(e->scheduler);
out:
......@@ -132,14 +91,15 @@ static struct renamed *set_renamed(
static int do_type_register(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = get_renamed(e, signature);
if (renamed) {
/* Register base type and renamed type */
ie->next->type_register(ie->next, signature);
return ie->next->type_register(ie->next, &renamed->signature);
return ie->next->type_register(
ie->next, labcomm2014_renaming_rename_signature(renamed));
} else {
return ie->next->type_register(ie->next, signature);
}
......@@ -149,12 +109,13 @@ static int do_type_bind(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
char has_deps)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = get_renamed(e, signature);
if (renamed) {
return ie->next->type_bind(ie->next, &renamed->signature, 1);
return ie->next->type_bind(
ie->next, labcomm2014_renaming_rename_signature(renamed), 1);
} else {
return ie->next->type_bind(ie->next, signature, has_deps);
}
......@@ -164,21 +125,23 @@ static int do_sample_register(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = set_renamed(e, signature);
return ie->next->sample_register(ie->next, &renamed->signature, encode);
return ie->next->sample_register(
ie->next, labcomm2014_renaming_rename_signature(renamed), encode);
}
static int do_ref_register(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = set_renamed(e, signature);
return ie->next->ref_register(ie->next, &renamed->signature);
return ie->next->ref_register(ie->next,
labcomm2014_renaming_rename_signature(renamed));
}
static int do_encode(struct labcomm2014_encoder *e,
......@@ -186,14 +149,16 @@ static int do_encode(struct labcomm2014_encoder *e,
labcomm2014_encoder_function encode,
void *value)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = get_renamed(e, signature);
if (renamed == NULL) {
return -EINVAL;
} else {
return ie->next->encode(ie->next, &renamed->signature, encode, value);
return ie->next->encode(ie->next,
labcomm2014_renaming_rename_signature(renamed),
encode, value);
}
}
......@@ -201,12 +166,12 @@ static int do_ioctl(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = get_renamed(e, signature);
if (renamed != NULL) {
signature = &renamed->signature;
signature = labcomm2014_renaming_rename_signature(renamed);
}
return ie->next->ioctl(ie->next, signature, ioctl_action, args);
}
......@@ -224,14 +189,15 @@ static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
const struct renamed *renamed;
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = get_renamed(e, signature);
if (renamed == NULL) {
return ie->next->ref_get(ie->next, signature);
} else {
return ie->next->ref_get(ie->next, &renamed->signature);
return ie->next->ref_get(
ie->next, labcomm2014_renaming_rename_signature(renamed));
}
}
......@@ -240,20 +206,23 @@ static void do_free(struct labcomm2014_encoder *e)
struct encoder *ie = e->context;
int i;
LABCOMM_SIGNATURE_ARRAY_FOREACH(ie->renamed, struct renamed *, i) {
struct renamed *r;
r = LABCOMM_SIGNATURE_ARRAY_GET(ie->renamed, struct renamed *, i, NULL);
LABCOMM_SIGNATURE_ARRAY_FOREACH(ie->renamed,
struct labcomm2014_renaming_rename *, i) {
struct labcomm2014_renaming_rename *r;
r = LABCOMM_SIGNATURE_ARRAY_GET(ie->renamed,
struct labcomm2014_renaming_rename *, i,
NULL);
if (r) {
labcomm2014_memory_free(e->memory, 0, r->signature.name);
labcomm2014_memory_free(e->memory, 0, r);
labcomm2014_renaming_rename_free(ie->registry, r);
}
}
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->renamed, struct renamed *);
LABCOMM_SIGNATURE_ARRAY_FREE(e->memory, ie->renamed, struct labcomm2014_renaming_rename *);
labcomm2014_memory_free(e->memory, 0, ie);
}
struct labcomm2014_encoder *labcomm2014_renaming_encoder_new(
struct labcomm2014_encoder *e,
struct labcomm2014_renaming_registry *registry,
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
void *context)
{
......@@ -277,10 +246,11 @@ struct labcomm2014_encoder *labcomm2014_renaming_encoder_new(
result->encoder.sample_ref_to_index = do_sample_ref_to_index;
result->encoder.ref_get = do_ref_get;
result->next = e;
result->rename = rename;
result->registry = registry;
result->rename_func = rename;
result->context = context;
LABCOMM_SIGNATURE_ARRAY_INIT(result->renamed,
struct renamed *);
struct labcomm2014_renaming_rename *);
return &(result->encoder);
}
}
......@@ -23,9 +23,11 @@
#define __LABCOMM2014_RENAMING_ENCODER_H__
#include "labcomm2014.h"
#include "labcomm2014_renaming.h"
struct labcomm2014_encoder *labcomm2014_renaming_encoder_new(
struct labcomm2014_encoder *e,
struct labcomm2014_renaming_registry *r,
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
void *context);
......
......@@ -148,18 +148,18 @@ struct labcomm2014_renaming_rename *labcomm2014_renaming_rename_new(
if (entry == NULL) { goto out; }
entry->signature.name = new_name;
new_name = NULL;
entry->signature.encoded_size = signature->encoded_size;
entry->signature.size = signature->size;
entry->signature.signature = signature->signature;
entry->signature.encoded_size = base->signature->encoded_size;
entry->signature.size = base->signature->size;
entry->signature.signature = base->signature->signature;
entry->signature.index = 0;
#ifndef LABCOMM_NO_TYPEDECL
struct labcomm2014_signature_data s_treedata[2] = {
LABCOMM_SIGDEF_SIGNATURE(*signature),
LABCOMM_SIGDEF_SIGNATURE(*base->signature),
LABCOMM_SIGDEF_END
};
entry->s_treedata[0] = s_treedata[0];
entry->s_treedata[1] = s_treedata[1];
entry->signature.tdsize = sizeof(result->s_treedata);
entry->signature.tdsize = sizeof(entry->s_treedata);
entry->signature.treedata = entry->s_treedata;
#endif
labcomm2014_set_local_index(&entry->signature);
......
......@@ -28,6 +28,7 @@ static void handle_r(generated_encoding_R *v, void *context)
int main(int argc, char **argv)
{
struct labcomm2014_renaming_registry *registry;
struct labcomm2014_encoder *encoder, *prefix_encoder, *suffix_encoder;
struct labcomm2014_decoder *decoder, *prefix_decoder, *suffix_decoder;
int fd;
......@@ -38,15 +39,19 @@ int main(int argc, char **argv)
if (fd == -1) {
err(1, "open()");
}
registry = labcomm2014_renaming_registry_new(
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
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.");
encoder, registry, labcomm2014_renaming_prefix, "p.");
suffix_encoder = labcomm2014_renaming_encoder_new(
prefix_encoder, labcomm2014_renaming_suffix, ".s");
prefix_encoder, registry, labcomm2014_renaming_suffix, ".s");
labcomm2014_encoder_register_generated_encoding_R(encoder);
labcomm2014_encoder_register_generated_encoding_R(prefix_encoder);
......@@ -90,9 +95,9 @@ int main(int argc, char **argv)
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix_decoder = labcomm2014_renaming_decoder_new(