Commit 32ba8efc authored by Anders Blomdell's avatar Anders Blomdell
Browse files

First stab at removing the O(n) signature search from labcomm.

parent 61b2291f
...@@ -6,18 +6,12 @@ java -jar ../../compiler/labComm.jar --java=gen --c=gen/simple.c --h=gen/simple. ...@@ -6,18 +6,12 @@ java -jar ../../compiler/labComm.jar --java=gen --c=gen/simple.c --h=gen/simple.
javac -cp ../../lib/java:. gen/*.java Encoder.java Decoder.java javac -cp ../../lib/java:. gen/*.java Encoder.java Decoder.java
gcc -Wall -Werror -I . -I ../../lib/c \ gcc -Wall -Werror -I. -I../../lib/c -L../../lib/c \
-DLABCOMM_ENCODER_LINEAR_SEARCH \ -o example_encoder example_encoder.c gen/simple.c \
gen/simple.c ../../lib/c/labcomm.c \ -llabcomm -Tlabcomm.linkscript
../../lib/c/labcomm_dynamic_buffer_writer.c \ gcc -Wall -Werror -I . -I ../../lib/c -L../../lib/c \
../../lib/c/labcomm_fd_writer.c \ -o example_decoder example_decoder.c gen/simple.c \
-o example_encoder example_encoder.c -llabcomm -Tlabcomm.linkscript
gcc -Wall -Werror -I . -I ../../lib/c \
-DLABCOMM_ENCODER_LINEAR_SEARCH \
gen/simple.c ../../lib/c/labcomm.c \
../../lib/c/labcomm_dynamic_buffer_writer.c \
../../lib/c/labcomm_fd_reader.c \
-o example_decoder example_decoder.c
#gcc -o example_encoder -I . -I ../../lib/c example_encoder.c gen/simple.c ../../lib/c/labcomm.c ../../lib/c/labcomm_fd_reader_writer.c #gcc -o example_encoder -I . -I ../../lib/c example_encoder.c gen/simple.c ../../lib/c/labcomm.c ../../lib/c/labcomm_fd_reader_writer.c
......
## Macros ## Macros
CC=gcc CC=gcc
CFLAGS=-g -Wall -Werror -O3 -I. -Itest -DLABCOMM_ENCODER_LINEAR_SEARCH #CFLAGS=-g -Wall -Werror -O3 -I. -Itest -DLABCOMM_ENCODER_LINEAR_SEARCH
CFLAGS=-g -Wall -Werror -O3 -I. -Itest
LDFLAGS=-L. LDFLAGS=-L.
#LDLIBS_TEST=-Tlabcomm.linkscript -lcunit -llabcomm #LDLIBS_TEST=-Tlabcomm.linkscript -lcunit -llabcomm
LDLIBS_TEST=-lcunit -llabcomm LDLIBS_TEST=-lcunit -llabcomm -Tlabcomm.linkscript
OBJS= labcomm.o \ OBJS= labcomm.o \
labcomm_dynamic_buffer_writer.o labcomm_fd_reader.o labcomm_fd_writer.o \ labcomm_dynamic_buffer_writer.o labcomm_fd_reader.o labcomm_fd_writer.o \
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h>
#include "labcomm.h" #include "labcomm.h"
#include "labcomm_private.h" #include "labcomm_private.h"
...@@ -49,6 +50,7 @@ struct labcomm_encoder { ...@@ -49,6 +50,7 @@ struct labcomm_encoder {
struct labcomm_writer *writer; struct labcomm_writer *writer;
struct labcomm_lock *lock; struct labcomm_lock *lock;
labcomm_error_handler_callback on_error; labcomm_error_handler_callback on_error;
LABCOMM_SIGNATURE_ARRAY_DEF(registered, int);
}; };
struct labcomm_sample_entry { struct labcomm_sample_entry {
...@@ -204,14 +206,13 @@ static int get_encoder_index_by_search( ...@@ -204,14 +206,13 @@ static int get_encoder_index_by_search(
#else #else
static int get_encoder_index_by_section( static int get_local_index(
struct labcomm_encoder *e,
struct labcomm_signature *s) struct labcomm_signature *s)
{ {
int result = -ENOENT; int result = -ENOENT;
if (&labcomm_first_signature <= s && s <= &labcomm_last_signature) {
//fprintf(stderr, "%d\n", (int)(s - &labcomm_start)); if (&labcomm_first_signature <= s && s < &labcomm_last_signature) {
result = s - &labcomm_first_signature + LABCOMM_USER; result = (int)(s - &labcomm_first_signature) + LABCOMM_USER;
} }
return result; return result;
} }
...@@ -224,7 +225,7 @@ static int get_encoder_index( ...@@ -224,7 +225,7 @@ static int get_encoder_index(
#ifdef LABCOMM_ENCODER_LINEAR_SEARCH #ifdef LABCOMM_ENCODER_LINEAR_SEARCH
return get_encoder_index_by_search(e, s); return get_encoder_index_by_search(e, s);
#else #else
return get_encoder_index_by_section(e, s); return get_local_index(s);
#endif #endif
} }
...@@ -275,51 +276,6 @@ static int encoder_add_signature_by_search(struct labcomm_encoder *e, ...@@ -275,51 +276,6 @@ static int encoder_add_signature_by_search(struct labcomm_encoder *e,
} }
#endif #endif
#ifndef LABCOMM_ENCODER_LINEAR_SEARCH
static int encoder_add_signature_by_section(struct labcomm_encoder *e,
struct labcomm_signature *s,
labcomm_encoder_function encode)
{
int result = -ENOENT;
if (&labcomm_first_signature <= s && s <= &labcomm_last_signature) {
/* Signature is in right linker section */
struct labcomm_encoder_context *context = e->context;
int index = s - &labcomm_first_signature;
if (context->by_section == NULL) {
int n = &labcomm_last_signature - &labcomm_first_signature;
context->by_section = malloc(n * sizeof(context->by_section[0]));
}
if (context->by_section == NULL) {
result = -ENOMEM;
goto out;
}
context->by_section[index].next = NULL;
context->by_section[index].index = index + LABCOMM_USER;
context->by_section[index].signature = s;
context->by_section[index].encode = encode;
result = context->by_section[index].index;
}
out:
return result;
}
#endif
static int encoder_add_signature(struct labcomm_encoder *e,
struct labcomm_signature *signature,
labcomm_encoder_function encode)
{
int index = -ENOENT;
#ifdef LABCOMM_ENCODER_LINEAR_SEARCH
index = encoder_add_signature_by_search(e, signature, encode);
#else
index = encoder_add_signature_by_section(e, signature, encode);
#endif
return index;
}
/* /*
static struct labcomm_sample_entry *encoder_get_sample_by_signature_address( static struct labcomm_sample_entry *encoder_get_sample_by_signature_address(
struct labcomm_encoder *encoder, struct labcomm_encoder *encoder,
...@@ -363,6 +319,7 @@ struct labcomm_encoder *labcomm_encoder_new( ...@@ -363,6 +319,7 @@ struct labcomm_encoder *labcomm_encoder_new(
result->writer->error = 0; result->writer->error = 0;
result->lock = lock; result->lock = lock;
result->on_error = on_error_fprintf; result->on_error = on_error_fprintf;
LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int);
result->writer->action->alloc(result->writer,result->writer->context, result->writer->action->alloc(result->writer,result->writer->context,
result, LABCOMM_VERSION); result, LABCOMM_VERSION);
} }
...@@ -375,13 +332,14 @@ void labcomm_internal_encoder_register( ...@@ -375,13 +332,14 @@ void labcomm_internal_encoder_register(
labcomm_encoder_function encode) labcomm_encoder_function encode)
{ {
if (signature->type == LABCOMM_SAMPLE) { if (signature->type == LABCOMM_SAMPLE) {
if (get_encoder_index(e, signature) == 0) { int index = get_local_index(signature);
int index = encoder_add_signature(e, signature, encode); if (index > 0) {
int *registered = LABCOMM_SIGNATURE_ARRAY_REF(e->registered, int, index);
if (index > 0) { if (! *registered) {
struct labcomm_ioctl_register_signature ioctl_data; struct labcomm_ioctl_register_signature ioctl_data;
int err; int err;
*registered = 1;
ioctl_data.index = index; ioctl_data.index = index;
ioctl_data.signature = signature; ioctl_data.signature = signature;
err = labcomm_encoder_ioctl(e, LABCOMM_IOCTL_REGISTER_SIGNATURE, err = labcomm_encoder_ioctl(e, LABCOMM_IOCTL_REGISTER_SIGNATURE,
......
/* Hack to get a link error with (hopefully) useful information if not linked
with -Tlabcomm.linkscript */
"You need to link with '-Tlabcomm.linkscript'"=0;
SECTIONS { SECTIONS {
labcomm : { labcomm : {
labcomm_first_signature = ABSOLUTE(.) ; labcomm_first_signature = ABSOLUTE(.) ;
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
* *
*/ */
#define LABCOMM_DECLARE_SIGNATURE(name) \ #define LABCOMM_DECLARE_SIGNATURE(name) \
struct labcomm_signature __attribute__((section("labcomm"))) name struct labcomm_signature __attribute__((section("labcomm"),aligned(1))) name
/* /*
* Semi private lock declarations * Semi private lock declarations
......
/*INPUT(liblabcomm.so.1 -ltinfo)*/ INPUT(liblabcomm.so.1 -ltinfo)
INPUT(liblabcomm.a) /* Hack to get a link error with (hopefully) useful information if not linked
with -Tlabcomm.linkscript */
"You need to link with '-Tlabcomm.linkscript'"+=1;
/*INPUT(liblabcomm.a)*/
...@@ -150,10 +150,12 @@ int main(void) ...@@ -150,10 +150,12 @@ int main(void)
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm_encoder_register_generated_encoding_V(encoder); labcomm_encoder_register_generated_encoding_V(encoder);
labcomm_encoder_register_generated_encoding_V(encoder);
EXPECT({ 0x02, -1, 0x01, 'V', 0x11, 0x00 }); EXPECT({ 0x02, -1, 0x01, 'V', 0x11, 0x00 });
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
labcomm_encoder_register_generated_encoding_B(encoder); labcomm_encoder_register_generated_encoding_B(encoder);
labcomm_encoder_register_generated_encoding_B(encoder);
EXPECT({0x02, -1, 0x01, 'B', 0x21}); EXPECT({0x02, -1, 0x01, 'B', 0x21});
labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET); labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
......
Markdown is supported
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