Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • anders_blomdell/labcomm
  • klaren/labcomm
  • tommyo/labcomm
  • erikj/labcomm
  • sven/labcomm
5 results
Select Git revision
  • anders.blomdell
  • compiler-refactoring
  • labcomm2006
  • labcomm2013
  • master
  • pragma
  • typedefs
  • typeref
  • v2006.0
  • v2013.0
  • v2014.0
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.4
15 results
Show changes
Showing
with 1613 additions and 71 deletions
......@@ -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
*/
......@@ -185,23 +204,38 @@ int labcomm2014_reader_ioctl(struct labcomm2014_reader *r,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args);
struct labcomm2014_decoder {
void *context;
struct labcomm2014_reader *reader;
struct labcomm2014_error_handler *error;
struct labcomm2014_memory *memory;
struct labcomm2014_scheduler *scheduler;
int reader_allocated;
int version_ok;
labcomm2014_error_handler_callback on_error;
labcomm2014_handle_new_datatype_callback on_new_datatype;
void (*free)(struct labcomm2014_decoder *d);
int (*decode_one)(struct labcomm2014_decoder *d);
int (*ref_register)(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *s);
/*
* Non typesafe registration function to be called from
* generated labcomm2014_decoder_register_* functions.
*/
int labcomm2014_internal_decoder_register(
struct labcomm2014_decoder *d,
int (*sample_register)(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *s,
labcomm2014_decoder_function decoder,
labcomm2014_handler_function handler,
void *context);
int labcomm2014_internal_decoder_ioctl(struct labcomm2014_decoder *decoder,
const struct labcomm2014_signature *signature,
int (*ioctl)(struct labcomm2014_decoder *d,
const struct labcomm2014_signature *s,
uint32_t ioctl_action, va_list args);
const struct labcomm2014_signature *labcomm2014_internal_decoder_index_to_signature(
const struct labcomm2014_sample_ref *(*index_to_sample_ref)(
struct labcomm2014_decoder *decoder, int index);
const struct labcomm2014_sample_ref *(*ref_get)(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature);
};
#if __BYTE_ORDER == __LITTLE_ENDIAN
......@@ -232,7 +266,7 @@ const struct labcomm2014_signature *labcomm2014_internal_decoder_index_to_signat
if (r->error < 0) { \
return 0; \
} \
} \
} > \
((unsigned char*)(&result))[i] = r->data[r->pos]; \
r->pos++; \
} \
......@@ -353,6 +387,38 @@ struct labcomm2014_writer {
int error;
};
struct labcomm2014_encoder {
void *context;
struct labcomm2014_writer *writer;
struct labcomm2014_error_handler *error;
struct labcomm2014_memory *memory;
struct labcomm2014_scheduler *scheduler;
void (*free)(struct labcomm2014_encoder *e);
int (*type_register)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature);
int (*type_bind)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
char has_deps);
int (*sample_register)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode);
int (*ref_register)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature);
int (*encode)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode,
void *value);
int (*ioctl)(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args);
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);
};
int labcomm2014_writer_alloc(struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context);
int labcomm2014_writer_free(struct labcomm2014_writer *w,
......@@ -370,33 +436,6 @@ int labcomm2014_writer_ioctl(struct labcomm2014_writer *w,
int index, const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args);
int labcomm2014_internal_encoder_type_register(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature);
int labcomm2014_internal_encoder_type_bind(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
char has_deps);
int labcomm2014_internal_encoder_register(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode);
int labcomm2014_internal_encode(
struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode,
void *value);
int labcomm2014_internal_encoder_ioctl(struct labcomm2014_encoder *encoder,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args);
int labcomm2014_internal_encoder_signature_to_index(
struct labcomm2014_encoder *encoder, const struct labcomm2014_signature *signature);
int labcomm2014_internal_sizeof(const struct labcomm2014_signature *signature,
void *v);
......
/*
labcomm2014_renaming.c -- functions intended for renaming
encoders and decoders
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "labcomm2014_renaming.h"
#include <string.h>
char *labcomm2014_renaming_prefix(struct labcomm2014_memory *m,
char *name, void *context)
{
char *result, *prefix = context;
int length;
length = strlen(name) + strlen(prefix) + 1;
result = labcomm2014_memory_alloc(m, 0, length);
if (result != NULL) {
strcpy(result, prefix);
strcat(result, name);
}
return result;
}
char *labcomm2014_renaming_suffix(struct labcomm2014_memory *m,
char *name, void *context)
{
return labcomm2014_renaming_prefix(m, context, name);
}
/*
labcomm2014_renaming.h -- functions intended for renaming
encoders and decoders
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LABCOMM2014_RENAMING_H__
#define __LABCOMM2014_RENAMING_H__
#include "labcomm2014.h"
char *labcomm2014_renaming_prefix(struct labcomm2014_memory *m,
char *name, void *context);
char *labcomm2014_renaming_suffix(struct labcomm2014_memory *m,
char *name, void *context);
struct labcomm2014_renaming_registry *labcomm2014_renaming_registry_new(
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler);
void labcomm2014_renaming_registry_free(
struct labcomm2014_renaming_registry *r);
#endif
/*
labcomm2014_renaming_decoder.c -- a stacked decoder that renames samples
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#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;
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_renaming_rename *);
};
static struct labcomm2014_renaming_rename *get_renamed(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
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_renaming_rename *,
index, NULL);
labcomm2014_scheduler_data_unlock(d->scheduler);
return result;
}
static struct labcomm2014_renaming_rename *set_renamed(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
struct labcomm2014_renaming_rename *result = NULL;
result = get_renamed(d, signature);
if (result == NULL) {
struct decoder *id = d->context;
struct labcomm2014_renaming_rename **renamed;
struct labcomm2014_renaming_rename *entry = NULL;
int index;
index = labcomm2014_get_local_index(signature);
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_renaming_rename *,
index);
if (renamed == NULL) { goto free_unlock; }
if (*renamed != NULL) { result = *renamed; goto free_unlock; }
*renamed = entry;
result = entry;
goto unlock;
free_unlock:
labcomm2014_renaming_rename_free(id->registry, entry);
unlock:
labcomm2014_scheduler_data_unlock(d->scheduler);
out:
;
}
return result;
}
static int do_sample_register(struct labcomm2014_decoder *d,
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;
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;
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;
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)
{
struct decoder *id = d->context;
return id->next->decode_one(id->next);
}
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_sample_ref(id->next, index);
}
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_decoder *d,
const struct labcomm2014_signature *signature)
{
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,
labcomm2014_renaming_rename_signature(renamed));
}
}
static void do_free(struct labcomm2014_decoder *d)
{
struct decoder *id = d->context;
int i;
LABCOMM_SIGNATURE_ARRAY_FOREACH(id->renamed,
struct labcomm2014_renaming_rename *,
i) {
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_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)
{
struct decoder *result;
result = labcomm2014_memory_alloc(d->memory, 0, sizeof(*result));
if (!result) {
return NULL;
} else {
result->decoder.context = result;
result->decoder.reader = d->reader;
result->decoder.error = d->error;
result->decoder.memory = d->memory;
result->decoder.scheduler = d->scheduler;
result->decoder.free = do_free;
result->decoder.decode_one = do_decode_one;
result->decoder.sample_register = do_sample_register;
result->decoder.ref_register = do_ref_register;
result->decoder.ioctl = do_ioctl;
result->decoder.index_to_sample_ref = do_index_to_sample_ref;
result->decoder.ref_get = do_ref_get;
result->next = d;
result->registry = registry;
result->rename_func = rename;
result->context = context;
LABCOMM_SIGNATURE_ARRAY_INIT(result->renamed,
struct labcomm2014_renaming_rename *);
return &(result->decoder);
}
}
/*
labcomm2014_renaming_decoder.h -- a stacked decoder that renames samples
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LABCOMM2014_RENAMING_DECODER_H__
#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);
#endif
/*
labcomm2014_renaming_encoder.c -- a stacked encoder that renames samples
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include "labcomm2014_renaming_encoder.h"
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_renaming_private.h"
struct encoder {
struct labcomm2014_encoder encoder;
struct labcomm2014_encoder *next;
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_renaming_rename *);
};
static struct labcomm2014_renaming_rename *get_renamed(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
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 labcomm2014_renaming_rename *,
index, NULL);
labcomm2014_scheduler_writer_unlock(e->scheduler);
return result;
}
static struct labcomm2014_renaming_rename *set_renamed(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
struct labcomm2014_renaming_rename *result = NULL;
result = get_renamed(e, signature);
if (result == NULL) {
struct encoder *ie = e->context;
struct labcomm2014_renaming_rename **renamed;
struct labcomm2014_renaming_rename *entry = NULL;
int index;
index = labcomm2014_get_local_index(signature);
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 labcomm2014_renaming_rename *,
index);
if (renamed == NULL) { goto free_unlock; }
if (*renamed != NULL) { result = *renamed; goto free_unlock; }
*renamed = entry;
result = entry;
goto unlock;
free_unlock:
labcomm2014_renaming_rename_free(ie->registry, entry);
unlock:
labcomm2014_scheduler_writer_unlock(e->scheduler);
out:
;
}
return result;
}
static int do_type_register(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
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, labcomm2014_renaming_rename_signature(renamed));
} else {
return ie->next->type_register(ie->next, signature);
}
}
static int do_type_bind(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
char has_deps)
{
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, labcomm2014_renaming_rename_signature(renamed), 1);
} else {
return ie->next->type_bind(ie->next, signature, has_deps);
}
}
static int do_sample_register(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode)
{
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = set_renamed(e, signature);
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 labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = set_renamed(e, signature);
return ie->next->ref_register(ie->next,
labcomm2014_renaming_rename_signature(renamed));
}
static int do_encode(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
labcomm2014_encoder_function encode,
void *value)
{
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,
labcomm2014_renaming_rename_signature(renamed),
encode, value);
}
}
static int do_ioctl(struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature,
uint32_t ioctl_action, va_list args)
{
const struct labcomm2014_renaming_rename *renamed;
struct encoder *ie = e->context;
renamed = get_renamed(e, signature);
if (renamed != NULL) {
signature = labcomm2014_renaming_rename_signature(renamed);
}
return ie->next->ioctl(ie->next, signature, ioctl_action, args);
}
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->sample_ref_to_index(ie->next,sample_ref);
}
static const struct labcomm2014_sample_ref *do_ref_get(
struct labcomm2014_encoder *e,
const struct labcomm2014_signature *signature)
{
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, labcomm2014_renaming_rename_signature(renamed));
}
}
static void do_free(struct labcomm2014_encoder *e)
{
struct encoder *ie = e->context;
int i;
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_renaming_rename_free(ie->registry, r);
}
}
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)
{
struct encoder *result;
result = labcomm2014_memory_alloc(e->memory, 0, sizeof(*result));
if (!result) {
return NULL;
} else {
result->encoder.context = result;
result->encoder.writer = e->writer;
result->encoder.error = e->error;
result->encoder.memory = e->memory;
result->encoder.scheduler = e->scheduler;
result->encoder.free = do_free;
result->encoder.type_register = do_type_register;
result->encoder.type_bind = do_type_bind;
result->encoder.sample_register = do_sample_register;
result->encoder.ref_register = do_ref_register;
result->encoder.encode = do_encode;
result->encoder.ioctl = do_ioctl;
result->encoder.sample_ref_to_index = do_sample_ref_to_index;
result->encoder.ref_get = do_ref_get;
result->next = e;
result->registry = registry;
result->rename_func = rename;
result->context = context;
LABCOMM_SIGNATURE_ARRAY_INIT(result->renamed,
struct labcomm2014_renaming_rename *);
return &(result->encoder);
}
}
/*
labcomm2014_renaming_encoder.h -- a stacked encoder that renames samples
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LABCOMM2014_RENAMING_ENCODER_H__
#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);
#endif
/*
labcomm2014_renaming_private.h -- functions intended for renaming
encoders and decoders
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LABCOMM2014_RENAMING__PRIVATE_H__
#define __LABCOMM2014_RENAMING_PRIVATE_H__
#include "labcomm2014.h"
#include "labcomm2014_renaming.h"
struct labcomm2014_renaming_rename *labcomm2014_renaming_rename_new(
struct labcomm2014_renaming_registry *r,
const struct labcomm2014_signature *signature,
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
void *context);
void labcomm2014_renaming_rename_free(
struct labcomm2014_renaming_registry *r,
struct labcomm2014_renaming_rename *rename);
const struct labcomm2014_signature *labcomm2014_renaming_rename_signature(
const struct labcomm2014_renaming_rename *rename);
#endif
/*
labcomm2014_renaming_registry.c -- renaming registry
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_renaming_private.h"
struct alias {
int usecount;
struct alias *next;
char *name;
};
struct registry {
struct registry *base; /* NULL if this is the base type */
const struct labcomm2014_signature *signature;
struct labcomm2014_renaming_rename *rename;
};
struct labcomm2014_renaming_rename {
struct labcomm2014_renaming_rename *next;
int use_count;
struct registry *base;
struct labcomm2014_signature signature;
struct labcomm2014_signature_data s_treedata[2];
};
struct labcomm2014_renaming_registry {
struct labcomm2014_error_handler *error;
struct labcomm2014_memory *memory;
struct labcomm2014_scheduler *scheduler;
LABCOMM_SIGNATURE_ARRAY_DEF(registry, struct registry *);
};
struct labcomm2014_renaming_registry *labcomm2014_renaming_registry_new(
struct labcomm2014_error_handler *error,
struct labcomm2014_memory *memory,
struct labcomm2014_scheduler *scheduler)
{
struct labcomm2014_renaming_registry *result;
result = labcomm2014_memory_alloc(memory, 0, sizeof(*result));
if (! result) {
return NULL;
} else {
result->error = error;
result->memory = memory;
result->scheduler = scheduler;
LABCOMM_SIGNATURE_ARRAY_INIT(result->registry, struct registry *);
return result;
}
}
void labcomm2014_renaming_registry_free(
struct labcomm2014_renaming_registry *r)
{
LABCOMM_SIGNATURE_ARRAY_FREE(r->memory, r->registry, struct registry *);
labcomm2014_memory_free(r->memory, 0, r);
}
static struct registry *registry_new(
struct labcomm2014_renaming_registry *r,
const struct labcomm2014_signature *signature,
struct registry *base)
{
/* Called with registry locked */
struct registry *result = NULL;
struct registry **registry;
int index;
index = labcomm2014_get_local_index(signature);
if (index <= 0) {
labcomm2014_error_warning(r->error,
LABCOMM2014_ERROR_MEMORY,
"Signature has no index: %s\n",
signature->name);
goto out;
}
registry = LABCOMM_SIGNATURE_ARRAY_REF(r->memory, r->registry,
struct registry *, index);
if (registry == NULL) { goto out; }
if (*registry != NULL) {
result = *registry;
} else {
/* Add previosly unknown sample to registry */
registry = LABCOMM_SIGNATURE_ARRAY_REF(r->memory, r->registry,
struct registry *, index);
if (registry == NULL) { goto out; }
result = labcomm2014_memory_alloc(r->memory, 0, sizeof(*result));
if (result == NULL) { goto out; }
result->base = base;
result->signature = signature;
result->rename = NULL;
*registry = result;
}
out:
return result;
}
struct labcomm2014_renaming_rename *labcomm2014_renaming_rename_new(
struct labcomm2014_renaming_registry *r,
const struct labcomm2014_signature *signature,
char *(*rename)(struct labcomm2014_memory *m, char *name, void *context),
void *context)
{
struct labcomm2014_renaming_rename *result = NULL;
labcomm2014_scheduler_data_lock(r->scheduler);
{
char *new_name = NULL;
static struct registry *base, *renamed;
struct labcomm2014_renaming_rename **l;
base = registry_new(r, signature, NULL);
if (base == NULL) { goto out; }
if (base->base) {
base = base->base;
}
/* Find if renamed entry already exists */
new_name = rename(r->memory, signature->name, context);
if (new_name == NULL) { goto out; }
for (l = &base->rename ; *l ; l = &(*l)->next) {
if (strcmp((*l)->signature.name, new_name) == 0) { break; }
}
if ((*l) == NULL) {
/* Create a new rename entry */
struct labcomm2014_renaming_rename *entry = NULL;
entry = labcomm2014_memory_alloc(r->memory, 0, sizeof(*entry));
if (entry == NULL) { goto out; }
entry->signature.name = new_name;
new_name = NULL;
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(*base->signature),
LABCOMM_SIGDEF_END
};
entry->s_treedata[0] = s_treedata[0];
entry->s_treedata[1] = s_treedata[1];
entry->signature.tdsize = sizeof(entry->s_treedata);
entry->signature.treedata = entry->s_treedata;
#endif
labcomm2014_set_local_index(&entry->signature);
renamed = registry_new(r, &entry->signature, base);
if (renamed == NULL) {
/* Failed to create registry entry */
labcomm2014_memory_free(r->memory, 0, entry);
goto out;
} else {
entry->next = NULL;
entry->use_count = 0;
entry->base = base;
(*l) = entry;
}
}
result = *l;
if (result) {
result->use_count++;
}
out:
if (new_name != NULL) {
labcomm2014_memory_free(r->memory, 0, new_name);
}
}
labcomm2014_scheduler_data_unlock(r->scheduler);
return result;
}
void labcomm2014_renaming_rename_free(
struct labcomm2014_renaming_registry *r,
struct labcomm2014_renaming_rename *rename)
{
labcomm2014_scheduler_data_lock(r->scheduler);
rename->use_count--;
if (rename->use_count == 0) {
int index;
struct labcomm2014_renaming_rename **l;
struct registry **registry;
for (l = &rename->base->rename ; *l ; l = &(*l)->next) {
if (*l == rename) { break; }
}
*l = rename->next;
if (rename->base->rename == NULL) {
/* Last use of base signature */
index = labcomm2014_get_local_index(rename->base->signature);
registry = LABCOMM_SIGNATURE_ARRAY_REF(r->memory, r->registry,
struct registry *, index);
labcomm2014_memory_free(r->memory, 0, *registry);
*registry = NULL;
}
index = labcomm2014_get_local_index(&rename->signature);
registry = LABCOMM_SIGNATURE_ARRAY_REF(r->memory, r->registry,
struct registry *, index);
labcomm2014_memory_free(r->memory, 0, *registry);
*registry = NULL;
/* TODO: We should return the index to the pool*/
labcomm2014_memory_free(r->memory, 0, rename->signature.name);
labcomm2014_memory_free(r->memory, 0, rename);
}
labcomm2014_scheduler_data_unlock(r->scheduler);
}
const struct labcomm2014_signature *labcomm2014_renaming_rename_signature(
const struct labcomm2014_renaming_rename *rename)
{
return &rename->signature;
}
......@@ -46,9 +46,11 @@ struct labcomm2014_signature_data {
};
#endif
struct labcomm2014_sample_ref;
struct labcomm2014_signature {
char *name;
int (*encoded_size)(void *); /* void* refers to sample_data */
int (*encoded_size)(const struct labcomm2014_signature *sig, void *); /* void* refers to sample_data */
int size;
unsigned char *signature;
int index;
......
......@@ -171,8 +171,10 @@ static test_sample_test_var decoder_var = {
void handle_test_var(test_sample_test_var *v, void *ctx)
{
if (v->n_0 * v->n_2 > 0) {
decoder_var.a[0] = v->a[0];
}
}
int test_decode_one(struct labcomm2014_decoder *decoder)
{
......@@ -215,10 +217,10 @@ static void test_encode_decode(struct labcomm2014_encoder *encoder,
err = test_decode_one(decoder);
fprintf(stderr, "decode of sample %u * 2 * %u -> size=%d err=%d\n",
n_0, n_2, writer.pos, err);
if (writer.pos != labcomm2014_sizeof_test_sample_test_var(&encoder_var)) {
if (writer.pos != labcomm2014_sizeof_test_sample_test_var(NULL, &encoder_var)) {
fprintf(stderr, "Incorrect sizeof %u * 2 * %u (%d != %d)\n",
n_0, n_2,
writer.pos, labcomm2014_sizeof_test_sample_test_var(&encoder_var));
writer.pos, labcomm2014_sizeof_test_sample_test_var(NULL, &encoder_var));
exit(1);
}
if (writer.pos != expected) {
......@@ -229,7 +231,7 @@ static void test_encode_decode(struct labcomm2014_encoder *encoder,
}
}
int main(void)
static int do_test(int argc, char *argv[])
{
int i;
struct labcomm2014_decoder *decoder = labcomm2014_decoder_new(
......@@ -273,5 +275,13 @@ int main(void)
test_encode_decode(encoder, decoder, 4+i, 0, (1<<(7*i)));
}
test_encode_decode(encoder, decoder, 8, 0, 4294967295);
labcomm2014_encoder_free(encoder);
labcomm2014_decoder_free(decoder);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
......@@ -91,7 +91,7 @@ static void writer_assert(char *type,
}
}
int main(void)
static int do_test(int argc, char *argv[])
{
TEST_WRITE_READ(packed32, packed32, "%d", 0x0, 1, "\x00");
TEST_WRITE_READ(packed32, packed32, "%d", 0x7f, 1, "\x7f");
......@@ -141,3 +141,7 @@ int main(void)
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
......@@ -70,7 +70,7 @@ static void handle_as(more_types_AS *v, void *context)
labcomm2014_copy_more_types_AS(labcomm2014_default_memory, context, v);
}
int main(int argc, char **argv)
int do_test(int argc, char **argv)
{
struct labcomm2014_encoder *encoder;
struct labcomm2014_decoder *decoder;
......@@ -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);
......@@ -280,4 +284,15 @@ int main(int argc, char **argv)
puts("NS deallocated ok");
labcomm2014_copy_free_more_types_AS(labcomm2014_default_memory, &cache_as);
puts("AS deallocated ok");
labcomm2014_copy_free_generated_encoding_R(labcomm2014_default_memory,
&cache_r);
puts("R deallocated ok");
return 0;
}
int main(int argc, char **argv)
{
return do_test(argc, argv);
}
......@@ -213,23 +213,23 @@ int main(void)
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_V(encoder);
labcomm2014_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, 0x06, VARIABLE(0), 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x08, VARIABLE(1), 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
0x04, 0x05, VARIABLE(2), 0x01, 'V', 0x01, VARIABLE(1),
EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x0a, VARIABLE(1), 0x01, 0x00, 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
0x04, 0x07, VARIABLE(2), 0x01, 0x00, 0x01, 'V', 0x01, VARIABLE(1),
0x05, 0x02, VARIABLE(0), VARIABLE(2) });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_B(encoder);
labcomm2014_encoder_register_generated_encoding_B(encoder);
EXPECT({ 0x02, 0x05, VARIABLE(3), 0x01, 'B', 0x01, 0x21,
EXPECT({ 0x02, 0x07, VARIABLE(3),0x01, 0x00, 0x01, 'B', 0x01, 0x21,
0x05, 0x02, VARIABLE(3), LABCOMM_BIND_SELF });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_R(encoder);
labcomm2014_encoder_register_generated_encoding_R(encoder);
EXPECT({ 0x02, 0x08, VARIABLE(4), 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28,
EXPECT({ 0x02, 0x0a, VARIABLE(4), 0x01, 0x00, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28,
0x05, 0x02, VARIABLE(4), LABCOMM_BIND_SELF });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
......@@ -238,7 +238,7 @@ int main(void)
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x06, VARIABLE(5), 0x01, 'V', 0x02, 0x11, 0x00});
EXPECT({0x03, 0x08, VARIABLE(5), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
......@@ -246,7 +246,7 @@ int main(void)
labcomm2014_signature_generated_encoding_B);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_B);
EXPECT({0x03, 0x05, VARIABLE(6), 0x01, 'B', 0x01, 0x21});
EXPECT({0x03, 0x07, VARIABLE(6), 0x01, 0x00, 0x01, 'B', 0x01, 0x21});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
......@@ -254,7 +254,7 @@ int main(void)
labcomm2014_signature_generated_encoding_R);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_R);
EXPECT({0x03, 0x08, VARIABLE(7), 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28});
EXPECT({0x03, 0x0a, VARIABLE(7), 0x01, 0x00, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
// was: labcomm2014_encode_generated_encoding_V(encoder, &V);
......@@ -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),
......
......@@ -40,6 +40,7 @@ static void func(void *arg)
if (func_arg->i == 999) {
labcomm2014_scheduler_wakeup(func_arg->scheduler);
}
free(func_arg);
}
void enqueue(struct labcomm2014_scheduler *scheduler,
......@@ -56,7 +57,7 @@ void enqueue(struct labcomm2014_scheduler *scheduler,
}
}
int main(int argc, char *argv[])
static int do_test(int argc, char *argv[])
{
struct labcomm2014_scheduler *scheduler;
struct labcomm2014_time *time;
......@@ -78,5 +79,12 @@ int main(int argc, char *argv[])
labcomm2014_scheduler_sleep(scheduler, NULL);
labcomm2014_scheduler_sleep(scheduler, time);
labcomm2014_time_free(time);
labcomm2014_scheduler_free(scheduler);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
#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_renaming_registry *registry;
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()");
}
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, registry, labcomm2014_renaming_prefix, "p.");
suffix_encoder = labcomm2014_renaming_encoder_new(
prefix_encoder, registry, 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, registry, labcomm2014_renaming_prefix, "p.");
suffix_decoder = labcomm2014_renaming_decoder_new(
prefix_decoder, registry, 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(suffix_decoder);
labcomm2014_decoder_free(prefix_decoder);
labcomm2014_decoder_free(decoder);
labcomm2014_renaming_registry_free(registry);
close(fd);
unlink(DATA_FILE);
}
/*
test_labcomm2014_renaming.c -- LabComm tests of renaming
Copyright 2015 Anders Blomdell <anders.blomdell@control.lth.se>
This file is part of LabComm.
LabComm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LabComm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_renaming.h"
#include "labcomm2014_renaming_encoder.h"
#include "test/gen/generated_encoding.h"
#define IOCTL_WRITER_ASSERT_BYTES 4096
#define IOCTL_WRITER_RESET 4097
#define EXPECT(...) \
{ \
int expected[] = __VA_ARGS__; \
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, \
__LINE__, \
sizeof(expected)/sizeof(expected[0]), \
expected); \
}
#define VARIABLE(i) -(i + 1)
#define IS_VARIABLE(i) (i < 0)
static unsigned char buffer[128];
struct labcomm2014_writer *writer;
static int seen_variable[1024];
static int buf_writer_alloc(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
writer = w; /* Hack */
w->data_size = sizeof(buffer);
w->count = w->data_size;
w->data = buffer;
w->pos = 0;
return 0;
}
static int buf_writer_free(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
return 0;
}
static int buf_writer_start(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int index,
const struct labcomm2014_signature *signature,
void *value)
{
return 0;
}
static int buf_writer_end(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
return 0;
}
static int buf_writer_flush(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context)
{
fprintf(stderr, "Should not come here %s:%d\n", __FILE__, __LINE__);
exit(1);
return 0;
}
static int buf_writer_ioctl(
struct labcomm2014_writer *w,
struct labcomm2014_writer_action_context *action_context,
int signature_index, const struct labcomm2014_signature *signature,
uint32_t action, va_list arg)
{
int result = -ENOTSUP;
switch (action) {
case IOCTL_WRITER_ASSERT_BYTES: {
int line = va_arg(arg, int);
int count = va_arg(arg, int);
int *expected = va_arg(arg, int *);
int i, mismatch;
mismatch = 0;
if (w->pos != count) {
fprintf(stderr, "Invalid length detected %d != %d (%s:%d)\n",
w->pos, count, __FILE__, line);
mismatch = 1;
}
for (i = 0 ; i < count ; i++) {
if (IS_VARIABLE(expected[i])) {
if (seen_variable[VARIABLE(expected[i])] == -1) {
seen_variable[VARIABLE(expected[i])] = buffer[i];
}
if (seen_variable[VARIABLE(expected[i])] != buffer[i]) {
fprintf(stderr, "Unexpected variable v%d (%02x: != %02x)\n",
VARIABLE(expected[i]),
seen_variable[VARIABLE(expected[i])], buffer[i]);
mismatch = 1;
}
} else if (expected[i] != buffer[i]) {
mismatch = 1;
}
}
if (mismatch) {
fprintf(stderr, "Encoder mismatch (%s:%d)\n",
__FILE__, line);
for (i = 0 ; i < w->pos ; i++) {
if (32 <= w->data[i] && w->data[i] < 127) {
printf("%2c ", w->data[i]);
} else {
printf("%2.2x ", w->data[i]);
}
}
printf("\n");
for (i = 0 ; i < w->pos ; i++) {
printf("%2.2x ", w->data[i]);
}
printf("\n");
for (i = 0 ; i < count ; i++) {
if (expected[i] < 0) {
printf("v%d ", VARIABLE(expected[i]));
} else {
printf("%2.2x ", expected[i] );
}
}
printf("\n");
if (mismatch) { exit(1); }
}
result = 0;
} break;
case IOCTL_WRITER_RESET: {
w->pos = 0;
result = 0;
}
}
return result;
}
const struct labcomm2014_writer_action writer_action = {
.alloc = buf_writer_alloc,
.free = buf_writer_free,
.start = buf_writer_start,
.end = buf_writer_end,
.flush = buf_writer_flush,
.ioctl = buf_writer_ioctl
};
static struct labcomm2014_writer_action_context action_context = {
.next = NULL,
.action = &writer_action,
.context = NULL
};
static struct labcomm2014_writer buffer_writer = {
.action_context = &action_context,
.data = buffer,
.data_size = sizeof(buffer),
.count = sizeof(buffer),
.pos = 0,
.error = 0,
};
void dump_encoder(struct labcomm2014_encoder *encoder)
{
int i;
for (i = 0 ; i < writer->pos ; i++) {
printf("%2.2x ", writer->data[i]);
}
printf("\n");
}
static int do_test(int argc, char *argv[])
{
struct labcomm2014_renaming_registry *registry;
struct labcomm2014_encoder *encoder, *prefix, *suffix;
int i;
for (i = 0 ; i < sizeof(seen_variable)/sizeof(seen_variable[0]) ; i++) {
seen_variable[i] = -1;
}
registry = labcomm2014_renaming_registry_new(
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
encoder = labcomm2014_encoder_new(
&buffer_writer,
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
prefix = labcomm2014_renaming_encoder_new(encoder,
registry,
labcomm2014_renaming_prefix,
"p.");
suffix = labcomm2014_renaming_encoder_new(prefix,
registry,
labcomm2014_renaming_suffix,
".s");
EXPECT({ 0x01, 0x0c, 0x0b,
'L', 'a', 'b', 'C', 'o', 'm', 'm', '2','0', '1', '4' });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_register_generated_encoding_V(encoder);
labcomm2014_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00,
0x04, 0x0a, VARIABLE(1), 0x01, 0x00, 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
0x04, 0x07, VARIABLE(2), 0x01, 0x00, 0x01, 'V', 0x01, VARIABLE(1),
0x05, 0x02, VARIABLE(0), VARIABLE(2) });
labcomm2014_encoder_ioctl(prefix, IOCTL_WRITER_RESET);
labcomm2014_encoder_register_generated_encoding_V(prefix);
labcomm2014_encoder_register_generated_encoding_V(prefix);
EXPECT({ 0x02, 0x0a, VARIABLE(3), 0x01, 0x00, 0x03, 'p', '.', 'V', 0x02, 0x11, 0x00,
0x04, 0x09, VARIABLE(4), 0x01, 0x00, 0x03, 'p', '.', 'V', 0x01, VARIABLE(2),
0x05, 0x02, VARIABLE(3), VARIABLE(4) });
labcomm2014_encoder_ioctl(suffix, IOCTL_WRITER_RESET);
labcomm2014_encoder_register_generated_encoding_V(suffix);
labcomm2014_encoder_register_generated_encoding_V(suffix);
// XXX HERE BE DRAGONS! What does "BOGUS" man in the below renaming test?
// previous version (from merge conflict): VARIABLE(6) vs VARIABLE(2)
// <<<<<<< ours
// EXPECT({ 0x02, 0x0c, VARIABLE(5), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x02, 0x11, 0x00,
// 0x04, 0x09, VARIABLE(6), 0x01, 0x00, 0x03, 'V', '.', 's', 0x01, VARIABLE(2),
// 0x04, 0x0b, VARIABLE(7), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x01, VARIABLE(6),
// =======
EXPECT({ 0x02, 0x0c, VARIABLE(5), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x02, 0x11, 0x00,
0x04, 0x09, VARIABLE(6), 0x01, 0x00, 0x03, 'V', '.', 's', 0x01, VARIABLE(2), /* BOGUS */
0x04, 0x0b, VARIABLE(7), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x01, VARIABLE(2),
0x05, 0x02, VARIABLE(5), VARIABLE(7) });
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(encoder,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x08, VARIABLE(8), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(prefix, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(prefix,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(prefix,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x0a, VARIABLE(9), 0x01, 0x00, 0x03, 'p', '.', 'V', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(suffix, IOCTL_WRITER_RESET);
/* Register twice to make sure that only one registration gets encoded */
labcomm2014_encoder_sample_ref_register(suffix,
labcomm2014_signature_generated_encoding_V);
labcomm2014_encoder_sample_ref_register(suffix,
labcomm2014_signature_generated_encoding_V);
EXPECT({0x03, 0x0c, VARIABLE(10), 0x01, 0x00, 0x05, 'p', '.', 'V', '.', 's', 0x02, 0x11, 0x00});
labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_V(encoder);
EXPECT({VARIABLE(0), 0x00 });
labcomm2014_encoder_ioctl(prefix, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_V(prefix);
EXPECT({VARIABLE(3), 0x00 });
labcomm2014_encoder_ioctl(suffix, IOCTL_WRITER_RESET);
labcomm2014_encode_generated_encoding_V(suffix);
EXPECT({VARIABLE(5), 0x00 });
labcomm2014_encoder_free(suffix);
labcomm2014_encoder_free(prefix);
labcomm2014_encoder_free(encoder);
labcomm2014_renaming_registry_free(registry);
return 0;
}
int main(int argc, char *argv[])
{
return do_test(argc, argv);
}
#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_renaming.h"
#include "labcomm2014_renaming_private.h"
#include "test/gen/generated_encoding.h"
static int do_test(int argc, char **argv)
{
struct labcomm2014_renaming_registry *registry;
struct labcomm2014_renaming_rename *r1, *r2, *r3, *r4;
registry = labcomm2014_renaming_registry_new(
labcomm2014_default_error_handler,
labcomm2014_default_memory,
labcomm2014_default_scheduler);
r1 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_signature_generated_encoding_R,
labcomm2014_renaming_prefix, "p:");
r2 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_renaming_rename_signature(r1),
labcomm2014_renaming_suffix, ":s");
r3 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_signature_generated_encoding_R,
labcomm2014_renaming_suffix, ":s");
r4 = labcomm2014_renaming_rename_new(
registry,
labcomm2014_renaming_rename_signature(r3),
labcomm2014_renaming_prefix, "p:");
assert(r2 == r4);
labcomm2014_renaming_rename_free(registry, r1);
labcomm2014_renaming_rename_free(registry, r2);
labcomm2014_renaming_rename_free(registry, r3);
labcomm2014_renaming_rename_free(registry, r4);
labcomm2014_renaming_registry_free(registry);
return 0;
}
int main(int argc, char **argv)
{
return do_test(argc, argv);
}
......@@ -10,6 +10,7 @@ ifeq ($(UNAME_S),Linux)
CFLAGS_TEST=$(CFLAGS) -Itest
LDFLAGS=-L..
LDLIBS=-llabcomm$(LIBVERSION) -lrt
LD_LIBRARY_PATH_NAME=LD_LIBRARY_PATH
MAKESHARED=gcc -o $1 -shared -Wl,-soname,$2 $3 -lc -lrt
else ifeq ($(UNAME_S),Darwin)
#CC=$(CROSS_COMPILE)clang
......@@ -24,6 +25,7 @@ else ifeq ($(UNAME_S),Darwin)
CFLAGS+=-std=c99
LDFLAGS=-L..
LDLIBS=-llabcomm$(LIBVERSION)
LD_LIBRARY_PATH_NAME=DYLD_LIBRARY_PATH
MAKESHARED=clang -o $1 -shared -Wl,-install_name,$2 $3 -lc
else ifneq ($(findstring CYGWIN,$(UNAME_S)),)
CC=$(CROSS_COMPILE)gcc
......
......@@ -5,10 +5,15 @@ MODULES=Constant\
Encoder \
EncoderChannel \
EncoderRegistry \
RenamingDecoder \
RenamingEncoder \
RenamingRegistry \
Sample \
SampleDispatcher \
SampleHandler \
SampleType
SampleType \
WrappingDecoder \
WrappingEncoder
.PHONY: all
all: labcomm2014.dll
......