/* This program is free software; you can redistribute it and/or modify it
under the terms of the license (GNU LGPL) which comes with this package. */
-#include "DataDesc/datadesc_private.h"
+#include "gras/DataDesc/datadesc_private.h"
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc);
+GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
/**
* gras_ddt_freev:
gras_datadesc_type_t *res;
GRAS_IN1("(%s)",name);
- res=malloc(sizeof(gras_datadesc_type_t));
+ res=gras_new0(gras_datadesc_type_t,1);
if (!res)
RAISE_MALLOC;
- memset(res, 0, sizeof(gras_datadesc_type_t));
res->name = strdup(name);
res->name_len = strlen(name);
for (arch = 0; arch < gras_arch_count; arch ++) {
long int sz;
long int mask;
- res->size[arch] = gras_arches[arch].sizeof_scalars[type];
-
+ res->size[arch] = gras_arches[arch].sizeofs[type];
+ res->alignment[arch] = gras_arches[arch].boundaries[type];
+ res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
+#if 0
sz = res->size[arch];
mask = sz;
res->alignment[arch] = res->size[arch];
res->aligned_size[arch] = res->size[arch];
}
+#endif
}
res->category_code = e_gras_datadesc_type_cat_scalar;
GRAS_IN;
if (field) {
if (field->name)
- free(field->name);
- free(field);
+ gras_free(field->name);
+ gras_free(field);
}
GRAS_OUT;
}
"Cannot add a dynamically sized field in structure %s",
struct_type->name);
- field=malloc(sizeof(gras_dd_cat_field_t));
+ field=gras_new(gras_dd_cat_field_t,1);
if (!field)
RAISE_MALLOC;
field->name = strdup(name);
DEBUG0("----------------");
- DEBUG4("PRE s={size=%ld,align=%ld,asize=%ld} struct_boundary=%d",
+ DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
struct_type->size[GRAS_THISARCH],
struct_type->alignment[GRAS_THISARCH],
- struct_type->aligned_size[GRAS_THISARCH],
- gras_arches[GRAS_THISARCH].struct_boundary);
+ struct_type->aligned_size[GRAS_THISARCH]);
for (arch=0; arch<gras_arch_count; arch ++) {
field->offset[arch] = aligned(struct_type->size[arch],
- min(field_type->alignment[arch],
- gras_arches[arch].struct_boundary));
+ field_type->alignment[arch]);
struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
struct_type->alignment[arch] = max(struct_type->alignment[arch],
return no_error;
}
- field=malloc(sizeof(gras_dd_cat_field_t));
+ field=gras_new0(gras_dd_cat_field_t,1);
if (!field)
RAISE_MALLOC;
field->name = strdup(name);
- for (arch=0; arch<gras_arch_count; arch ++) {
- field->offset[arch] = 0; /* that's the purpose of union ;) */
- }
field->type = field_type;
- field->pre = NULL;
- field->post = NULL;
+ /* All offset are left to 0 in an union */
TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type,
gras_datadesc_type_t **dst) {
gras_error_t errcode;
- char *name=malloc(strlen(element_type->name) + 4);
+ char *name=(char*)gras_malloc(strlen(element_type->name) + 4);
sprintf(name,"%s[]",element_type->name);
sprintf(name,"%s[]*",element_type->name);
TRY(gras_datadesc_ref(name,*dst,dst));
- free(name);
+ gras_free(name);
return no_error;
}
/**
* gras_datadesc_cb_send:
*
- * Add a pre-send callback to this datadexc.
+ * Add a pre-send callback to this datadesc.
* (useful to push the sizes of the upcoming arrays, for example)
*/
void gras_datadesc_cb_send (gras_datadesc_type_t *type,
gras_datadesc_type_cb_void_t recv) {
type->recv = recv;
}
+/**
+ * gras_dd_find_field:
+ *
+ * Returns the type descriptor of the given field. Abort on error.
+ */
+static gras_datadesc_type_t *
+ gras_dd_find_field(gras_datadesc_type_t *type,
+ const char *field_name) {
+ gras_datadesc_type_t *sub_type=NULL;
+ gras_dynar_t *field_array;
+
+ gras_dd_cat_field_t *field=NULL;
+ int field_num;
+
+ if (type->category_code == e_gras_datadesc_type_cat_union) {
+ field_array = type->category.union_data.fields;
+ } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
+ field_array = type->category.struct_data.fields;
+ } else {
+ ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,type);
+ gras_abort();
+ }
+ gras_dynar_foreach(field_array,field_num,field) {
+ if (!strcmp(field_name,field->name)) {
+ return field->type;
+ }
+ }
+ ERROR2("No field nammed %s in %s",field_name,type->name);
+ gras_abort();
+
+}
+
+/**
+ * gras_datadesc_cb_field_send:
+ *
+ * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
+ * (useful to push the sizes of the upcoming arrays, for example)
+ */
+void gras_datadesc_cb_field_send (gras_datadesc_type_t *type,
+ const char *field_name,
+ gras_datadesc_type_cb_void_t send) {
+
+ gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
+ sub_type->send = send;
+}
+
+/**
+ * gras_datadesc_cb_field_push:
+ *
+ * Add a pre-send callback to the given field resulting in its value to be pushed to
+ * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
+ */
+void gras_datadesc_cb_field_push (gras_datadesc_type_t *type,
+ const char *field_name) {
+
+ gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
+ if (!strcmp("int",sub_type->name)) {
+ sub_type->send = gras_datadesc_cb_push_int;
+ } else if (!strcmp("unsigned int",sub_type->name)) {
+ sub_type->send = gras_datadesc_cb_push_uint;
+ } else if (!strcmp("long int",sub_type->name)) {
+ sub_type->send = gras_datadesc_cb_push_lint;
+ } else if (!strcmp("unsigned long int",sub_type->name)) {
+ sub_type->send = gras_datadesc_cb_push_ulint;
+ } else {
+ ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
+ sub_type->name);
+ gras_abort();
+ }
+}
+/**
+ * gras_datadesc_cb_field_recv:
+ *
+ * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
+ * (useful to put the function pointers to the right value, for example)
+ */
+void gras_datadesc_cb_field_recv(gras_datadesc_type_t *type,
+ const char *field_name,
+ gras_datadesc_type_cb_void_t recv) {
+
+ gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
+ sub_type->recv = recv;
+}
/**
- * gras_datadesc_unref:
+ * gras_datadesc_free:
*
- * Removes a reference to the datastruct.
- * ddt will be freed only when the refcount becomes 0.
+ * Free a datadesc. Should only be called at gras_exit.
*/
void gras_datadesc_free(gras_datadesc_type_t *type) {
/* datadesc was invalid. Killing it is like euthanasy, I guess */
break;
}
- free(type->name);
- free(type);
+ gras_free(type->name);
+ gras_free(type);
}