Skip to content

Commit

Permalink
Code style change and cleanup for alloc/free macros
Browse files Browse the repository at this point in the history
Code style:
  1. var = RASQAL_CALLOC(type, nmem, size)
  Prefering:
    var = RASQAL_CALLOC(type, 1, sizeof(*var))
  when nmem = 1

  2. var = RASQAL_CALLOC(type, size)

  3. RASQAL_FREE(type, var)

The consequence here is allocs that mostly fit into 1 line without so
much boilerplate and duplication of types.

The RASQAL_MALLOC and RASQAL_CALLOC now do the cast to the return type.

RASQAL_FREE takes the object type too but always casts arg to void
This certainly contains many wrong types to the arg but might be
used later in some kind of smart type-aware debugging allocator.
  • Loading branch information
dajobe committed Jul 31, 2011
1 parent 944e0b4 commit 86678bb
Show file tree
Hide file tree
Showing 62 changed files with 315 additions and 343 deletions.
8 changes: 3 additions & 5 deletions src/rasqal_algebra.c
Expand Up @@ -61,8 +61,7 @@ rasqal_new_algebra_node(rasqal_query* query, rasqal_algebra_node_operator op)
if(!query)
return NULL;

node = (rasqal_algebra_node*)RASQAL_CALLOC(rasqal_algebra, 1,
sizeof(rasqal_algebra_node));
node = RASQAL_CALLOC(rasqal_algebra_node*, 1, sizeof(*node));
if(!node)
return NULL;

Expand Down Expand Up @@ -1552,7 +1551,7 @@ rasqal_algebra_extract_aggregate_expression_visit(void *user_data,
/* If not an error, create a new internal variable name for it
* $$agg{id}$$ and add it to the map.
*/
var_name = (char*)RASQAL_MALLOC(cstring, 20);
var_name = RASQAL_MALLOC(char*, 20);
if(!var_name) {
ae->error = 1;
return 1;
Expand Down Expand Up @@ -1787,8 +1786,7 @@ rasqal_algebra_query_prepare_aggregates(rasqal_query* query,
{
rasqal_algebra_aggregate* ae;

ae = (rasqal_algebra_aggregate*)RASQAL_CALLOC(rasqal_algebra_aggregate,
sizeof(*ae), 1);
ae = RASQAL_CALLOC(rasqal_algebra_aggregate*, sizeof(*ae), 1);
if(!ae)
return NULL;

Expand Down
3 changes: 1 addition & 2 deletions src/rasqal_bindings.c
Expand Up @@ -62,8 +62,7 @@ rasqal_new_bindings(rasqal_query* query,
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query, rasqal_query, NULL);
RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(variables, raptor_sequence, NULL);

bindings = (rasqal_bindings*)RASQAL_CALLOC(rasqal_bindings,
1, sizeof(*bindings));
bindings = RASQAL_CALLOC(rasqal_bindings*, 1, sizeof(*bindings));
if(!bindings)
return NULL;

Expand Down
10 changes: 5 additions & 5 deletions src/rasqal_data_graph.c
Expand Up @@ -52,7 +52,7 @@ rasqal_new_data_graph_common(rasqal_world* world,
{
rasqal_data_graph* dg;

dg = (rasqal_data_graph*)RASQAL_CALLOC(rasqal_data_graph, 1, sizeof(*dg));
dg = RASQAL_CALLOC(rasqal_data_graph*, 1, sizeof(*dg));
if(dg) {
dg->world = world;

Expand All @@ -70,7 +70,7 @@ rasqal_new_data_graph_common(rasqal_world* world,

if(format_type) {
size_t len = strlen(format_type);
dg->format_type = (char*)RASQAL_MALLOC(string, len + 1);
dg->format_type = RASQAL_MALLOC(char*, len + 1);
if(!dg->format_type)
goto error;

Expand All @@ -79,7 +79,7 @@ rasqal_new_data_graph_common(rasqal_world* world,

if(format_name) {
size_t len = strlen(format_name);
dg->format_name = (char*)RASQAL_MALLOC(string, len + 1);
dg->format_name = RASQAL_MALLOC(char*, len + 1);
if(!dg->format_name)
goto error;

Expand Down Expand Up @@ -213,9 +213,9 @@ rasqal_free_data_graph(rasqal_data_graph* dg)
if(dg->name_uri)
raptor_free_uri(dg->name_uri);
if(dg->format_type)
RASQAL_FREE(cstring, dg->format_type);
RASQAL_FREE(char*, dg->format_type);
if(dg->format_name)
RASQAL_FREE(cstring, dg->format_name);
RASQAL_FREE(char*, dg->format_name);
if(dg->format_uri)
raptor_free_uri(dg->format_uri);
if(dg->base_uri)
Expand Down
8 changes: 3 additions & 5 deletions src/rasqal_dataset.c
Expand Up @@ -101,7 +101,7 @@ rasqal_new_dataset(rasqal_world* world)
if(!world)
return NULL;

ds = (rasqal_dataset*)RASQAL_CALLOC(rasqal_dataset, 1, sizeof(*ds));
ds = RASQAL_CALLOC(rasqal_dataset*, 1, sizeof(*ds));
if(!ds)
return NULL;

Expand Down Expand Up @@ -153,8 +153,7 @@ rasqal_dataset_statement_handler(void *user_data,

ds = (rasqal_dataset*)user_data;

triple = (rasqal_dataset_triple*)RASQAL_MALLOC(rasqal_dataset_triple,
sizeof(*triple));
triple = RASQAL_MALLOC(rasqal_dataset_triple*, sizeof(*triple));
triple->next = NULL;
triple->triple = raptor_statement_as_rasqal_triple(ds->world,
statement);
Expand Down Expand Up @@ -242,8 +241,7 @@ rasqal_dataset_init_match_internal(rasqal_dataset* ds,
if(!ds)
return NULL;

iter = (rasqal_dataset_term_iterator*)RASQAL_CALLOC(rasqal_dataset_term_iterator,
1, sizeof(*iter));
iter = RASQAL_CALLOC(rasqal_dataset_term_iterator*, 1, sizeof(*iter));

if(!iter)
return NULL;
Expand Down
26 changes: 13 additions & 13 deletions src/rasqal_datetime.c
Expand Up @@ -532,7 +532,7 @@ rasqal_new_xsd_datetime(rasqal_world* world, const char *datetime_string)
rasqal_xsd_datetime* dt;
int rc = 0;

dt = (rasqal_xsd_datetime*)RASQAL_MALLOC(datetime, sizeof(*dt));
dt = RASQAL_MALLOC(rasqal_xsd_datetime*, sizeof(*dt));
if(!dt)
return NULL;

Expand Down Expand Up @@ -563,7 +563,7 @@ rasqal_new_xsd_datetime_from_unixtime(rasqal_world* world, time_t secs)
rasqal_xsd_datetime* dt;
int rc = 0;

dt = (rasqal_xsd_datetime*)RASQAL_MALLOC(datetime, sizeof(*dt));
dt = RASQAL_MALLOC(rasqal_xsd_datetime*, sizeof(*dt));
if(!dt)
return NULL;

Expand Down Expand Up @@ -592,7 +592,7 @@ rasqal_new_xsd_datetime_from_timeval(rasqal_world* world, struct timeval *tv)
rasqal_xsd_datetime* dt;
int rc = 0;

dt = (rasqal_xsd_datetime*)RASQAL_MALLOC(datetime, sizeof(*dt));
dt = RASQAL_MALLOC(rasqal_xsd_datetime*, sizeof(*dt));
if(!dt)
return NULL;

Expand Down Expand Up @@ -727,7 +727,7 @@ rasqal_xsd_datetime_to_counted_string(const rasqal_xsd_datetime *dt,
/* error? */
if(r < 0) {
if(ret)
RASQAL_FREE(cstring, ret);
RASQAL_FREE(char*, ret);
return NULL;
}

Expand All @@ -736,7 +736,7 @@ rasqal_xsd_datetime_to_counted_string(const rasqal_xsd_datetime *dt,
if(len_p)
*len_p = r;

ret = (char*)RASQAL_MALLOC(cstring, ++r);
ret = RASQAL_MALLOC(char*, ++r);
if(!ret)
return NULL;
}
Expand Down Expand Up @@ -953,13 +953,13 @@ rasqal_xsd_date_to_string(const rasqal_xsd_date *d)
/* error? */
if(r < 0) {
if(ret)
RASQAL_FREE(cstring, ret);
RASQAL_FREE(char*, ret);
return NULL;
}

/* alloc return buffer on first pass */
if(!i) {
ret = (char*)RASQAL_MALLOC(cstring, ++r);
ret = RASQAL_MALLOC(char*, ++r);
if(!ret)
return NULL;
}
Expand Down Expand Up @@ -1210,7 +1210,7 @@ rasqal_xsd_datetime_get_as_timeval(rasqal_xsd_datetime *dt)
if(!dt)
return NULL;

tv =(struct timeval*) RASQAL_CALLOC(timeval, 1, sizeof(*tv));
tv = RASQAL_CALLOC(struct timeval*, 1, sizeof(*tv));
if(!tv)
return NULL;

Expand Down Expand Up @@ -1255,7 +1255,7 @@ rasqal_xsd_datetime_get_timezone_as_counted_string(rasqal_xsd_datetime *dt,
if(!dt)
return NULL;

tz_str = (char*)RASQAL_MALLOC(cstring, TZ_STR_SIZE + 1);
tz_str = RASQAL_MALLOC(char*, TZ_STR_SIZE + 1);
if(!tz_str)
return NULL;

Expand Down Expand Up @@ -1330,7 +1330,7 @@ rasqal_xsd_datetime_get_tz_as_counted_string(rasqal_xsd_datetime* dt,
{
char* s;

s = (char*)RASQAL_MALLOC(cstring, TIMEZONE_BUFFER_LEN);
s = RASQAL_MALLOC(char*, TIMEZONE_BUFFER_LEN);
if(!s)
return NULL;

Expand All @@ -1343,7 +1343,7 @@ rasqal_xsd_datetime_get_tz_as_counted_string(rasqal_xsd_datetime* dt,
return s;

failed:
RASQAL_FREE(cstring, s);
RASQAL_FREE(char*, s);
return NULL;
}

Expand Down Expand Up @@ -1373,7 +1373,7 @@ static int test_datetime_parser_tostring(const char *in_str, const char *out_exp
r = strcmp((char*)s, out_expected);
if(r)
fprintf(stderr, "input dateTime \"%s\" converted to canonical \"%s\", expected \"%s\"\n", in_str, s, out_expected);
RASQAL_FREE(cstring, (void*)s);
RASQAL_FREE(char*, s);
} else
fprintf(stderr, "input dateTime \"%s\" converted to canonical (null), expected \"%s\"\n", in_str, out_expected);
return r;
Expand All @@ -1389,7 +1389,7 @@ static int test_date_parser_tostring(const char *in_str, const char *out_expecte
r = strcmp((char*)s, out_expected);
if(r)
fprintf(stderr, "input date \"%s\" converted to canonical \"%s\", expected \"%s\"\n", in_str, s, out_expected);
RASQAL_FREE(cstring, (void*)s);
RASQAL_FREE(char*, s);
} else
fprintf(stderr, "input date \"%s\" converted to canonical (null), expected \"%s\"\n", in_str, out_expected);
return r;
Expand Down
9 changes: 5 additions & 4 deletions src/rasqal_decimal.c
Expand Up @@ -113,7 +113,8 @@ rasqal_xsd_decimal*
rasqal_new_xsd_decimal(rasqal_world* world)
{
rasqal_xsd_decimal* dec;
dec = (rasqal_xsd_decimal*)RASQAL_MALLOC(decimal, sizeof(rasqal_xsd_decimal));

dec = RASQAL_MALLOC(rasqal_xsd_decimal*, sizeof(*dec));
if(dec)
rasqal_xsd_decimal_init(dec);
return dec;
Expand Down Expand Up @@ -178,7 +179,7 @@ static void
rasqal_xsd_decimal_clear_string(rasqal_xsd_decimal* dec)
{
if(dec->string) {
RASQAL_FREE(cstring, dec->string);
RASQAL_FREE(char*, dec->string);
dec->string=NULL;
}
dec->string_len=0;
Expand Down Expand Up @@ -224,7 +225,7 @@ rasqal_xsd_decimal_set_string(rasqal_xsd_decimal* dec, const char* string)
rasqal_xsd_decimal_clear_string(dec);

len = strlen(string);
dec->string = (char*)RASQAL_MALLOC(cstring, len+1);
dec->string = RASQAL_MALLOC(char*, len + 1);
if(!dec->string)
return 1;

Expand Down Expand Up @@ -428,7 +429,7 @@ rasqal_xsd_decimal_as_string(rasqal_xsd_decimal* dec)
/* decimal snprintf with no buffer to get buffer length */
len = DECIMAL_SNPRINTF(NULL, 0, fmt, dec->raw);

s = (char*)RASQAL_MALLOC(cstring, len + 1);
s = RASQAL_MALLOC(char*, len + 1);
if(!s)
return NULL;

Expand Down
5 changes: 2 additions & 3 deletions src/rasqal_engine_sort.c
Expand Up @@ -57,7 +57,7 @@ rasqal_engine_rowsort_free_compare_data(const void* user_data)
{
rowsort_compare_data* rcd = (rowsort_compare_data*)user_data;

RASQAL_FREE(rowsort_compare_data, rcd);
RASQAL_FREE(rowsort_compare_data, rcd);
}


Expand Down Expand Up @@ -137,8 +137,7 @@ rasqal_engine_new_rowsort_map(int is_distinct, int compare_flags,
{
rowsort_compare_data* rcd;

rcd = (rowsort_compare_data*)RASQAL_MALLOC(rowsort_compare_data,
sizeof(rowsort_compare_data));
rcd = RASQAL_MALLOC(rowsort_compare_data*, sizeof(*rcd));
if(!rcd)
return NULL;

Expand Down
27 changes: 13 additions & 14 deletions src/rasqal_expr.c
Expand Up @@ -78,7 +78,7 @@ rasqal_new_0op_expression(rasqal_world* world, rasqal_op op)

RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -126,7 +126,7 @@ rasqal_new_1op_expression(rasqal_world* world, rasqal_op op,
goto tidy;
}

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -175,7 +175,7 @@ rasqal_new_2op_expression(rasqal_world* world,
if(!world || !arg1 || !arg2)
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -222,7 +222,7 @@ rasqal_new_3op_expression(rasqal_world* world,
if(!world || !arg1 || !arg2) /* arg3 may be NULL */
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -271,7 +271,7 @@ rasqal_new_string_op_expression(rasqal_world* world,
if(!world || !arg1 || !literal)
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -308,7 +308,7 @@ rasqal_new_literal_expression(rasqal_world* world, rasqal_literal *literal)
if(!world || !literal)
return NULL;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand All @@ -335,7 +335,7 @@ rasqal_new_function_expression_common(rasqal_world* world,
if(!world || (arg1 && args) || (name && !args)|| (!name && args))
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -437,7 +437,7 @@ rasqal_new_cast_expression(rasqal_world* world, raptor_uri* name,
if(!world || !name || !value)
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -478,7 +478,7 @@ rasqal_new_expr_seq_expression(rasqal_world* world,
if(!world || !args)
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -517,7 +517,7 @@ rasqal_new_set_expression(rasqal_world* world, rasqal_op op,
if(!world || !arg1 || !args)
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -562,7 +562,7 @@ rasqal_new_group_concat_expression(rasqal_world* world,
if(!world || !args)
goto tidy;

e = (rasqal_expression*)RASQAL_CALLOC(rasqal_expression, 1, sizeof(*e));
e = RASQAL_CALLOC(rasqal_expression*, 1, sizeof(*e));
if(e) {
e->usage = 1;
e->world = world;
Expand Down Expand Up @@ -2187,8 +2187,7 @@ rasqal_expression_convert_aggregate_to_variable(rasqal_expression* e_in,
world = e_in->world;

if(e_out) {
*e_out = (rasqal_expression*)RASQAL_MALLOC(rasqal_expression,
sizeof(**e_out));
*e_out = RASQAL_MALLOC(rasqal_expression*, sizeof(**e_out));
if(!*e_out)
goto tidy;
}
Expand Down Expand Up @@ -2243,7 +2242,7 @@ rasqal_new_evaluation_context(rasqal_world* world,

RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);

eval_context = (rasqal_evaluation_context*)RASQAL_CALLOC(rasqal_evaluation_context, 1, sizeof(*eval_context));
eval_context = RASQAL_CALLOC(rasqal_evaluation_context*, 1, sizeof(*eval_context));
if(!eval_context)
return NULL;

Expand Down

0 comments on commit 86678bb

Please sign in to comment.