Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Damn idiot, double where declared being as big as float (not more); do dump structure...
[simgrid.git] / src / gras / DataDesc / datadesc.c
1 /* $Id$ */
2
3 /* datadesc - data description in order to send/recv it in GRAS             */
4
5 /* Authors: Olivier Aumage, Martin Quinson                                  */
6 /* Copyright (C) 2003, 2004 the GRAS posse.                                 */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "DataDesc/datadesc_private.h"
12
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(datadesc,GRAS);
14 /* FIXME: make this host-dependent using a trick such as UserData*/
15 gras_set_t *gras_datadesc_set_local=NULL;
16
17
18 /* callback for array size when sending strings */
19 static int
20 _strlen_cb(void                 *vars,
21            gras_datadesc_type_t *type,
22            void                 *data) {
23
24   return 1+(long int)strlen(data);
25 }
26
27
28 /**
29  * gras_datadesc_init:
30  *
31  * Initialize the datadesc module.
32  * FIXME: We assume that when neither signed nor unsigned is given, 
33  *    that means signed. To be checked by configure.
34  **/
35 void
36 gras_datadesc_init(void) {
37   gras_error_t errcode;
38   gras_datadesc_type_t *ddt; /* What to add */
39
40   /* only initialize once */
41   if (gras_datadesc_set_local != NULL)
42     return;
43   
44   VERB0("Initializing DataDesc");
45   
46   TRYFAIL(gras_set_new(&gras_datadesc_set_local));
47   
48   TRYFAIL(gras_datadesc_declare_scalar("signed char", 
49                                        gras_ddt_scalar_char, 
50                                        e_gras_dd_scalar_encoding_sint, 
51                                        NULL, &ddt));
52   TRYFAIL(gras_datadesc_declare_scalar("char", 
53                                        gras_ddt_scalar_char, 
54                                        e_gras_dd_scalar_encoding_sint, 
55                                        NULL, &ddt));
56   TRYFAIL(gras_datadesc_declare_scalar("unsigned char", 
57                                        gras_ddt_scalar_char, 
58                                        e_gras_dd_scalar_encoding_uint, 
59                                        NULL, &ddt));
60   
61   TRYFAIL(gras_datadesc_declare_scalar("signed short int", 
62                                        gras_ddt_scalar_short, 
63                                        e_gras_dd_scalar_encoding_sint, 
64                                        NULL, &ddt));
65   TRYFAIL(gras_datadesc_declare_scalar("short int", 
66                                        gras_ddt_scalar_short, 
67                                        e_gras_dd_scalar_encoding_sint, 
68                                        NULL, &ddt));
69   TRYFAIL(gras_datadesc_declare_scalar("unsigned short int", 
70                                        gras_ddt_scalar_short, 
71                                        e_gras_dd_scalar_encoding_uint, 
72                                        NULL, &ddt));
73   
74   TRYFAIL(gras_datadesc_declare_scalar("signed int", 
75                                        gras_ddt_scalar_int, 
76                                        e_gras_dd_scalar_encoding_sint, 
77                                        NULL, &ddt));
78   TRYFAIL(gras_datadesc_declare_scalar("int", 
79                                        gras_ddt_scalar_int, 
80                                        e_gras_dd_scalar_encoding_sint, 
81                                        NULL, &ddt));
82   TRYFAIL(gras_datadesc_declare_scalar("unsigned int", 
83                                        gras_ddt_scalar_int, 
84                                        e_gras_dd_scalar_encoding_uint, 
85                                        NULL, &ddt));
86   
87   TRYFAIL(gras_datadesc_declare_scalar("signed long int", 
88                                        gras_ddt_scalar_long, 
89                                        e_gras_dd_scalar_encoding_sint, 
90                                        NULL, &ddt));
91   TRYFAIL(gras_datadesc_declare_scalar("long int", 
92                                        gras_ddt_scalar_long, 
93                                        e_gras_dd_scalar_encoding_sint, 
94                                        NULL, &ddt));
95   TRYFAIL(gras_datadesc_declare_scalar("unsigned long int", 
96                                        gras_ddt_scalar_long, 
97                                        e_gras_dd_scalar_encoding_uint, 
98                                        NULL, &ddt));
99   
100   TRYFAIL(gras_datadesc_declare_scalar("signed long long int", 
101                                        gras_ddt_scalar_long_long, 
102                                        e_gras_dd_scalar_encoding_sint, 
103                                        NULL, &ddt));
104   TRYFAIL(gras_datadesc_declare_scalar("long long int", 
105                                        gras_ddt_scalar_long_long, 
106                                        e_gras_dd_scalar_encoding_sint, 
107                                        NULL, &ddt));
108   TRYFAIL(gras_datadesc_declare_scalar("unsigned long long int", 
109                                        gras_ddt_scalar_long_long, 
110                                        e_gras_dd_scalar_encoding_uint, 
111                                        NULL, &ddt));
112   
113   TRYFAIL(gras_datadesc_declare_scalar("data pointer", 
114                                        gras_ddt_scalar_pdata, 
115                                        e_gras_dd_scalar_encoding_uint, 
116                                        NULL, &ddt));
117   TRYFAIL(gras_datadesc_declare_scalar("function pointer", 
118                                        gras_ddt_scalar_pfunc, 
119                                        e_gras_dd_scalar_encoding_uint, 
120                                        NULL, &ddt));
121   
122   TRYFAIL(gras_datadesc_declare_scalar("float", 
123                                        gras_ddt_scalar_float, 
124                                        e_gras_dd_scalar_encoding_float, 
125                                        NULL, &ddt));
126   TRYFAIL(gras_datadesc_declare_scalar("double", 
127                                        gras_ddt_scalar_double, 
128                                        e_gras_dd_scalar_encoding_float, 
129                                        NULL,&ddt));
130
131   TRYFAIL(gras_datadesc_declare_array_dyn("char[]", 
132                                           gras_datadesc_by_name("char"), 
133                                           _strlen_cb,&ddt));
134   TRYFAIL(gras_datadesc_declare_ref("string", 
135                                     gras_datadesc_by_name("char[]"), 
136                                     &ddt));
137
138 }
139
140 /**
141  * gras_datadesc_exit:
142  *
143  * Finalize the datadesc module
144  **/
145 void
146 gras_datadesc_exit(void) {
147   VERB0("Exiting DataDesc");
148   gras_set_free(&gras_datadesc_set_local);
149   gras_datadesc_set_local = NULL;
150   DEBUG0("Exited DataDesc");
151 }
152
153 /**
154  * gras_datadesc_get_name:
155  *
156  * Returns the name of a datadescription (to ease the debug)
157  */
158 char *
159 gras_datadesc_get_name(gras_datadesc_type_t *ddt) {
160   return ddt->name;
161 }
162 /**
163  * gras_datadesc_get_id:
164  *
165  * Returns the name of a datadescription (to ease the debug)
166  */
167 int
168 gras_datadesc_get_id(gras_datadesc_type_t *ddt) {
169   return ddt->code;
170 }
171
172 /**
173  * gras_datadesc_size: 
174  *
175  * Returns the size occuped by data of this type (on the current arch).
176  *
177  */
178 int gras_datadesc_size(gras_datadesc_type_t *type) {
179   return type->size[GRAS_THISARCH];
180 }
181
182 /**
183  * gras_datadesc_type_dump:
184  *
185  * For debugging purpose
186  */
187 void gras_datadesc_type_dump(const gras_datadesc_type_t *ddt){
188   int cpt;
189   gras_error_t errcode;
190
191   printf("DataDesc dump:");
192   if(!ddt) {
193     printf("(null)\n");
194     return;
195   }
196   printf ("%s (ID:%d)\n",ddt->name,ddt->code);
197   printf ("  refcounter=%d\n",ddt->refcounter);
198   printf ("  category: %s\n",gras_datadesc_cat_names[ddt->category_code]);
199
200   printf ("  size[");
201   for (cpt=0; cpt<gras_arch_count; cpt++) {
202     printf("%s%s%ld%s",
203            cpt>0?", ":"",
204            cpt == GRAS_THISARCH ? "*":"",
205            ddt->size[cpt],
206            cpt == GRAS_THISARCH ? "*":"");
207   }
208   printf ("]\n");
209
210   printf ("  alignment[");
211   for (cpt=0; cpt<gras_arch_count; cpt++) {
212     printf("%s%s%ld%s",
213            cpt>0?", ":"",
214            cpt == GRAS_THISARCH ? "*":"",
215            ddt->alignment[cpt],
216            cpt == GRAS_THISARCH ? "*":"");
217   }
218   printf ("]\n");
219
220   printf ("  aligned_size[");
221   for (cpt=0; cpt<gras_arch_count; cpt++) {
222     printf("%s%s%ld%s",
223            cpt>0?", ":"",
224            cpt == GRAS_THISARCH ? "*":"",
225            ddt->aligned_size[cpt],
226            cpt == GRAS_THISARCH ? "*":"");
227   }
228   printf ("]\n");
229   if (ddt->category_code == e_gras_datadesc_type_cat_struct) {
230     gras_dd_cat_struct_t  struct_data;
231     gras_dd_cat_field_t  *field;
232     gras_datadesc_type_t *sub_type;
233
234     struct_data = ddt->category.struct_data;
235     gras_dynar_foreach(struct_data.fields, cpt, field) {
236     printf(">>> Dump field #%d (%s) (offset=%ld)\n",
237            cpt,field->name,field->offset[GRAS_THISARCH]);
238     errcode=gras_datadesc_by_id(field->code, &sub_type);
239     if (errcode != no_error) {
240       ERROR4("Got %s while searching for the sub type %d, #%d of %s",
241              gras_error_name(errcode),field->code,cpt,ddt->name);
242       gras_abort();
243     }
244     gras_datadesc_type_dump(sub_type);
245     printf("<<< end dump field #%d (%s)\n",cpt,field->name);
246     }
247  }
248   fflush(stdout);
249 }