Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial revision
[simgrid.git] / src / gras / Common / gras_datadesc.c
1 /* $Id$ */
2
3 /* gras_datadesc.c - manipulating datadescriptors                           */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
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 "gras_private.h"
12
13 void *gras_datadesc_copy_data(const DataDescriptor *dd, unsigned int c, 
14                               void *data) {
15   size_t s=DataSize(dd,c,HOST_FORMAT);
16   void *res=malloc(s);
17   
18   if (!res) {
19     fprintf(stderr,"grasDataDescCpyData: malloc of %d bytes failed.\n",s);
20     return NULL;
21   }
22
23   memcpy(res,data,s);
24
25   return data; 
26 }
27
28 int gras_datadesc_cmp(const DataDescriptor *dd1, unsigned int c1,
29                       const DataDescriptor *dd2, unsigned int c2) {
30   unsigned int i;
31   int subcmp;
32
33   if (c1 != c2) 
34     return c1 < c2;
35
36   if (!dd1 && !dd2) return 0;
37   if (!dd1 || !dd2) return 1;
38
39   for (i=0;i<c1;i++) {
40     if (dd1[i].type != dd2[i].type) 
41       return  dd1[i].type < dd2[i].type;
42     if (dd1[i].repetitions != dd2[i].repetitions) 
43       return dd1[i].repetitions < dd2[i].repetitions;
44     if (dd1[i].type == STRUCT_TYPE) {
45       subcmp = gras_datadesc_cmp(dd1[i].members, dd1[i].length,
46                                  dd2[i].members, dd2[i].length);
47       if (subcmp)
48         return subcmp;
49     }
50   }
51
52   return 0;
53 }
54
55 void gras_datadesc_dump(const DataDescriptor *dd, unsigned int c) {
56   unsigned int i;
57
58   if (!dd) return;
59
60   for (i=0;i<c;i++) {
61     fprintf(stderr,"DD[%d] = {%s,repetitions=%d,offset=%d,members=%p,len=%d,pad=%d}\n",
62             (int)i,
63             (dd[i].type == CHAR_TYPE ? "char" :
64              (dd[i].type == DOUBLE_TYPE ? "double" :
65               (dd[i].type == FLOAT_TYPE ? "float" :
66                (dd[i].type == INT_TYPE ? "int" :
67                 (dd[i].type == LONG_TYPE ? "long" :
68                  (dd[i].type == SHORT_TYPE ? "short" :
69                   (dd[i].type == UNSIGNED_INT_TYPE ? "uint" :
70                    (dd[i].type == UNSIGNED_LONG_TYPE ? "ulong" :
71                     (dd[i].type == UNSIGNED_SHORT_TYPE ? "ushort":
72                      (dd[i].type == STRUCT_TYPE ? "struct" : "UNKNOWN")))))))))),
73             dd[i].repetitions, dd[i].offset,
74             dd[i].members, dd[i].length,
75             dd[i].tailPadding);
76     if (dd[i].type == STRUCT_TYPE) {
77       gras_datadesc_dump(dd[i].members, dd[i].length);
78     }
79   }
80 }
81
82