Commit a13f1f9f authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Use controller_block_alloc everywhere.

Also allocate as many interms and outterms with helper functions as possible.
parent 967b7dd5
......@@ -73,43 +73,30 @@ static struct controller_block_param_list params[] = {
{ NULL },
};
static struct controller_block_interm_list interms[] = {
{ "clk", CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, clk) },
{ NULL }
};
static struct controller_block_outterm_list outterms[] = {
{ "counter", CONTROLLER_BLOCK_TERM_UINT32, offsetof(struct controller_block_private, count) },
{ NULL }
};
struct controller_block * block_counter_create(char *name)
{
struct controller_block *counter;
counter = malloc(sizeof(struct controller_block));
if (!counter)
if (!(counter = controller_block_alloc("counter", name, sizeof(struct controller_block_private))))
return NULL;
counter->type = "counter";
counter->name = malloc(strlen(name)+1);
if (!counter->name)
goto err_name;
strcpy(counter->name, name);
counter->private = malloc(sizeof(struct controller_block_private));
if (!counter->private)
goto err_private;
counter->private->count = 0;
counter->inputs = 1;
counter->input = malloc(sizeof(struct controller_block_interm));
if (!counter->input)
if (controller_block_interm_list_init(counter, interms))
goto err_inputs;
counter->input[0].name = "clk";
counter->input[0].type = CONTROLLER_BLOCK_TERM_BOOL;
counter->input[0].value.b = &counter->private->clk;
counter->input[0].ghostof = NULL;
counter->outputs = 1;
counter->output = malloc(sizeof(struct controller_block_outterm) * 1);
if (!counter->output)
if (controller_block_outterm_list_init(counter, outterms))
goto err_outputs;
counter->output[0].name = "counter";
counter->output[0].type = CONTROLLER_BLOCK_TERM_UINT32;
counter->output[0].value.u32 = &counter->private->count;
counter->output[0].source = counter;
counter->calculate = calculate;
......@@ -124,10 +111,6 @@ err_paramlist:
err_outputs:
free(counter->input);
err_inputs:
free(counter->private);
err_private:
free(counter->name);
err_name:
free(counter);
controller_block_free(counter);
return NULL;
}
......@@ -45,20 +45,9 @@ struct controller_block * block_debug_create(char *name)
{
struct controller_block *debug;
debug = malloc(sizeof(struct controller_block));
if (!debug)
if (!(debug = controller_block_alloc("debug", name, sizeof(struct controller_block_private))))
return NULL;
debug->private = malloc(sizeof(struct controller_block_private));
if (!debug->private)
goto err_debug;
debug->type = "debug";
debug->name = malloc(strlen(name)+1);
if (!debug->name)
goto err_debugpriv;
strcpy(debug->name, name);
debug->inputs = 1;
debug->input = malloc(sizeof(struct controller_block_interm) * 1);
if (!debug->input)
......@@ -78,10 +67,6 @@ struct controller_block * block_debug_create(char *name)
return debug;
err_name:
free(debug->name);
err_debugpriv:
free(debug->private);
err_debug:
free(debug);
controller_block_free(debug);
return NULL;
}
......@@ -52,6 +52,10 @@ static void calculate(struct controller_block *dec)
priv->out[*priv->in] = true;
}
static struct controller_block_interm_list interms[] = {
{ "in", CONTROLLER_BLOCK_TERM_UINT32, offsetof(struct controller_block_private, in) },
{ NULL }
};
struct controller_block * block_decoder_uint32_bool_create(char *name, va_list ap)
{
......@@ -65,33 +69,16 @@ struct controller_block * block_decoder_uint32_bool_create(char *name, va_list a
return NULL;
}
dec = malloc(sizeof(struct controller_block));
if (!dec)
if (!(dec = controller_block_alloc("decoder_uint32_bool", name, sizeof(struct controller_block_private))))
return NULL;
dec->type = "decoder_uint32_bool";
dec->name = malloc(strlen(name)+1);
if (!dec->name)
goto err_name;
strcpy(dec->name, name);
dec->private = malloc(sizeof(struct controller_block_private));
if (!dec->private)
goto err_private;
dec->private->n = n;
dec->private->out = calloc(sizeof(bool), n);
if (!dec->private->out)
goto err_out;
dec->inputs = 1;
dec->input = malloc(sizeof(struct controller_block_interm)*n);
if (!dec->input)
if (controller_block_interm_list_init(dec, interms))
goto err_input;
dec->input[0].name = "in";
dec->input[0].type = CONTROLLER_BLOCK_TERM_UINT32;
dec->input[0].value.u32 = &dec->private->in;
dec->input[0].ghostof = NULL;
dec->outputs = n;
dec->output = malloc(sizeof(struct controller_block_outterm) * n);
......@@ -117,10 +104,6 @@ err_output:
err_input:
free(dec->private->out);
err_out:
free(dec->private);
err_private:
free(dec->name);
err_name:
free(dec);
controller_block_free(dec);
return NULL;
}
......@@ -149,9 +149,6 @@ struct controller_block * block_joystick_create(char *name, va_list ap)
block->private->device = strdup(device);
block->inputs = 0;
block->input = NULL;
/* Inputs on the hardware are outputs on this software block */
block->outputs = nr_axes + nr_buttons;
block->output = malloc(sizeof(struct controller_block_outterm) *
......
......@@ -59,6 +59,11 @@ static void calculate(struct controller_block *mul)
priv->out = *priv->in[select];
}
static struct controller_block_outterm_list outterms[] = {
{ "out", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, out) },
{ NULL }
};
struct controller_block * block_multiplexer_create(char *name, va_list ap)
{
......@@ -94,14 +99,8 @@ struct controller_block * block_multiplexer_create(char *name, va_list ap)
mul->input[1+i].ghostof = NULL;
}
mul->outputs = 1;
mul->output = malloc(sizeof(struct controller_block_outterm) * 1);
if (!mul->output)
if (controller_block_outterm_list_init(mul, outterms))
goto err_output;
mul->output[0].name = "out";
mul->output[0].type = CONTROLLER_BLOCK_TERM_FLOAT;
mul->output[0].value.f = &mul->private->out;
mul->output[0].source = mul;
mul->calculate = calculate;
......
......@@ -56,11 +56,11 @@ static struct controller_block_outterm_list outterms[] = {
{ NULL }
};
struct controller_block * block_value_create(char *name)
struct controller_block * block_value_float_create(char *name)
{
struct controller_block *value;
if (!(value = controller_block_alloc("value", name, sizeof(struct controller_block_private))))
if (!(value = controller_block_alloc("value_float", name, sizeof(struct controller_block_private))))
return NULL;
value->private->value = 0.0;
......@@ -78,3 +78,9 @@ err_block:
controller_block_free(value);
return NULL;
}
/* old name of this block, left for compatiblity */
struct controller_block * block_value_create(char *name)
{
return block_value_float_create(name);
}
......@@ -50,6 +50,11 @@ static struct controller_block_param_list params[] = {
{ NULL },
};
static struct controller_block_outterm_list outterms[] = {
{ "value", CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, value) },
{ NULL }
};
struct controller_block * block_value_bool_create(char *name)
{
struct controller_block *value;
......@@ -60,33 +65,16 @@ struct controller_block * block_value_bool_create(char *name)
value->private->value = 0.0;
value->inputs = 0;
value->input = NULL;
value->outputs = 1;
value->output = malloc(sizeof(struct controller_block_outterm));
if (!value->output)
goto err_private;
value->output[0].name = "value";
value->output[0].type = CONTROLLER_BLOCK_TERM_BOOL;
value->output[0].value.b = &value->private->value;
value->output[0].source = value;
value->calculate = NULL;
if (controller_block_outterm_list_init(value, outterms))
goto err_block;
if (controller_block_param_list_add(value, params))
goto err_output;
goto err_block;
controller_block_add(value);
return value;
err_output:
free(value->output);
err_private:
free(value->private);
err_name:
free(value->name);
err_value:
free(value);
err_block:
controller_block_free(value);
return NULL;
}
......@@ -50,52 +50,31 @@ static struct controller_block_param_list params[] = {
{ NULL },
};
static struct controller_block_outterm_list outterms[] = {
{ "value", CONTROLLER_BLOCK_TERM_UINT32, offsetof(struct controller_block_private, value) },
{ NULL }
};
struct controller_block * block_value_uint32_create(char *name)
{
struct controller_block *value;
value = malloc(sizeof(struct controller_block));
if (!value)
if (!(value = controller_block_alloc("value_uint32", name, sizeof(struct controller_block_private))))
return NULL;
value->type = "value_uint32";
value->name = malloc(strlen(name)+1);
if (!value->name)
goto err_value;
strcpy(value->name, name);
value->private = malloc(sizeof(struct controller_block_private));
if (!value->private)
goto err_name;
value->private->value = 0.0;
value->inputs = 0;
value->input = NULL;
value->outputs = 1;
value->output = malloc(sizeof(struct controller_block_outterm));
if (!value->output)
goto err_private;
value->output[0].name = "value";
value->output[0].type = CONTROLLER_BLOCK_TERM_UINT32;
value->output[0].value.u32 = &value->private->value;
value->output[0].source = value;
value->calculate = NULL;
if (controller_block_outterm_list_init(value, outterms))
goto err_block;
if (controller_block_param_list_add(value, params))
goto err_output;
goto err_block;
controller_block_add(value);
return value;
err_output:
free(value->output);
err_private:
free(value->private);
err_name:
free(value->name);
err_value:
free(value);
err_block:
controller_block_free(value);
return NULL;
}
......@@ -59,23 +59,10 @@ static struct ghost_term ghost_inputs[] = {
struct controller_block *block_dt_az_stoeber_sim_create(char *name, va_list ap)
{
struct controller_block *stoeber;
struct controller_block_private *private;
int i;
stoeber = malloc(sizeof(struct controller_block));
if (!stoeber)
if (!(stoeber = controller_block_alloc("dt_az_stoeber_sim", name, sizeof(struct controller_block_private))))
goto err_malloc;
stoeber->type = "dt_az_stoeber_sim";
stoeber->name = strdup(name);
if (!stoeber->name)
goto err_name;
private = calloc(1, sizeof(struct controller_block_private));
if (!private)
goto err_private;
stoeber->private = private;
for (i = 0; ghost_outputs[i].name; i++);
stoeber->outputs = i;
......@@ -150,11 +137,7 @@ struct controller_block *block_dt_az_stoeber_sim_create(char *name, va_list ap)
err_input:
free(stoeber->output);
err_output:
free(private);
err_private:
free(stoeber->name);
err_name:
free(stoeber);
controller_block_free(stoeber);
err_malloc:
return NULL;
}
......@@ -61,24 +61,10 @@ static struct ghost_term ghost_inputs[] = {
struct controller_block *block_dt_el_stoeber_l_sim_create(char *name, va_list ap)
{
struct controller_block *stoeber;
struct controller_block_private *private;
int i;
stoeber = malloc(sizeof(struct controller_block));
if (!stoeber)
if (!(stoeber = controller_block_alloc("dt_el_stoeber_l_sim", name, sizeof(struct controller_block_private))))
goto err_malloc;
stoeber->type = "dt_el_stoeber_l_sim";
stoeber->name = strdup(name);
if (!stoeber->name)
goto err_name;
private = calloc(1, sizeof(struct controller_block_private));
if (!private)
goto err_private;
stoeber->private = private;
for (i = 0; ghost_outputs[i].name; i++);
stoeber->outputs = i;
......@@ -165,11 +151,7 @@ err_input:
err_ghost_output:
free(stoeber->output);
err_output:
free(stoeber->private);
err_private:
free(stoeber->name);
err_name:
free(stoeber);
controller_block_free(stoeber);
err_malloc:
return NULL;
}
......@@ -61,23 +61,10 @@ static struct ghost_term ghost_inputs[] = {
struct controller_block *block_dt_el_stoeber_r_sim_create(char *name, va_list ap)
{
struct controller_block *stoeber;
struct controller_block_private *private;
int i;
stoeber = malloc(sizeof(struct controller_block));
if (!stoeber)
if (!(stoeber = controller_block_alloc("dt_el_stoeber_r_sim", name, sizeof(struct controller_block_private))))
goto err_malloc;
stoeber->type = "dt_el_stoeber_r_sim";
stoeber->name = strdup(name);
if (!stoeber->name)
goto err_name;
private = calloc(1, sizeof(struct controller_block_private));
if (!private)
goto err_private;
stoeber->private = private;
for (i = 0; ghost_outputs[i].name; i++);
stoeber->outputs = i;
......@@ -158,11 +145,7 @@ err_input:
err_ghost_output:
free(stoeber->output);
err_output:
free(stoeber->private);
err_private:
free(stoeber->name);
err_name:
free(stoeber);
controller_block_free(stoeber);
err_malloc:
return NULL;
}
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