File: Cstruct.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (487 lines) | stat: -rw-r--r-- 18,069 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
 * Copyright (c) 2007 GE Global Research
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


// The central idea is to add a mechanism for passing complex data structures 
// efficiently between C/C++ and FreeMat .m files.  It will consist of these 
// new functions
//   cstruct_define('typename','field1','type1','field2','type2',...)
//                  where 'fieldn' is the name of the field in the data structure
//                  and   'typen' is a typespec for the field (same as used for the import command)
//   s = cstruct_thaw(membuffer, 'typename')
//       where membuffer is a buffer of raw bytes (uint8) containing the data structure as laid out
//       by the C/C++ compiler (may need some extra arguments to handle element alignment, not sure)
//       The output is a first class struct in FreeMat.  Enumerations are mapped to strings.
//   membuffer = cstruct_freeze(s, 'typename')
//       where s is a struct (that should correspond to the info in 'typename', and membuffer is the
//       array of raw bytes that can then be passed to a C/C++ struct and cast in the C code 
//       to a pointer of the appropriate type. 
//
// The data structure to capture the type maps is fairly simple.  The typenames are 
// legal strings, so we can use a SymbolTable or a STL Hash or a QStringHash.  The field/type 
// info are parsed and stored in a data structure to speed access.  The format for the type 
// info is borrowed from import.  It consists of a spec of:
//      typespec = <typename>[array spec]
// where array spec is either a constant or another field in the structure (or substructure) that 
// contains the size of the array.  For example, the following C structure:
//     typedef struct {
//        int len;
//        char p;
//     } simple_string;
// would be mapped to the following cstruct_define call:
//    cstruct_define('simple_string','len','int32','p','int8[len]')
// which allows us to handle the case of "dynamic" structures - used in some notable cases to 
// store variable-sized data structures in structs in C.  Note that the problems of structure 
// alignment and padding are _not_ solved for you.  If padding is required, you must specify 
// it.  For example, suppose your C code converts the following structure:
//     typedef struct {
//        char achar;
//        double d;
//     } t_struct;
// into
//     typedef struct {
//        char achar;
//        char pad1[7];
//        double d;
//     } t_struct;
// where the double entry has to be 8-byte aligned (for example).  There is no way for FreeMat to 
// determine this at run time (indeed, such factors can sometimes be affected by C compiler options
// as well).  It is your responsibility to make sure the alignment is correct by inserting pads as
// necessary.  Sorry, but there is no real way around this problem.
//      

// Need to add arrays of structures

#include "Array.hpp"
#include "Interpreter.hpp"
#include "Algorithms.hpp"
#include <map>

// Base of the type system
class Ctype {
  // Nothing interesting here...
public:
  virtual ~Ctype() {}
  virtual void print(Interpreter *m_eval) = 0;
  virtual void freeze(QByteArray& out, Array s, int length, Interpreter* m_eval) = 0;
  virtual Array thaw(QByteArray& input, int& pos, int length, Interpreter* m_eval) = 0;
  virtual int size(int length) = 0;
};

// Type table
class CTable : public QMap<QString, Ctype*> {
public:
  Ctype* lookup(QString name) {
    if (!contains(name)) 
      throw Exception("Request for lookup of unknown type " + name);
    return value(name);
  }
  void add(QString name, Ctype* val) {
    (*this)[name] = val;
  }
  CTable();
};

static CTable CtypeTable;

// A builtin type
class Cbuiltin : public Ctype {
  DataClass dataClass;
  int t_size;
public:
  Cbuiltin(DataClass i_Class, int i_size) : dataClass(i_Class), t_size(i_size) {}
  DataClass getDataClass() { return dataClass; }
  int getSize() { return t_size;}
  void print(Interpreter *m_eval) {
    m_eval->outputMessage("built in\n");
  }
  void freeze(QByteArray& out, Array s, int length, Interpreter* m_eval) {
    s = s.toClass(dataClass).asDenseArray();
    if ((int) s.length() > length)
      throw Exception("field length overflow");
    const char *cp = (const char*) s.getConstVoidPointer();
    index_t s_len = s.length();
    for (index_t i=0;i<s_len*t_size;i++)
      out.push_back(cp[int(i)]);
    for (index_t i=s_len*t_size;i<length*t_size;i++)
      out.push_back((char) 0);
  }
  Array thaw(QByteArray& input, int& pos, int length, Interpreter* m_eval) {
    int bytecount = length*t_size;
    Array retvec(dataClass,NTuple(1,length));
    char* dp = (char*) retvec.getVoidPointer();
    if (input.size() < (pos+bytecount))
      throw Exception("source buffer is too short");
    for (int i=0;i<bytecount;i++) 
      dp[i] = input.at(pos+i);
    pos += bytecount;
    return retvec;
  }
  int size(int length) {
    return (length*t_size);
  }
};

// An enumerated type
class Cenum : public Ctype {
  map<int,QString> elementsByInt;
  map<QString,int> elementsByName;
public:
  QString lookupByNumber(int n) {
    if (elementsByInt.count(n) == 0) return "unknown";
    return elementsByInt.find(n)->second;
  }
  int lookupByName(QString name) {
    if (elementsByName.count(name) == 0) return 0;
    return elementsByName.find(name)->second;
  }
  void addPair(QString name, int value) {
    elementsByInt[value] = name;
    elementsByName[name] = value;
  }
  void print(Interpreter *m_eval) {
    m_eval->outputMessage("enumeration\n");
    for (map<int,QString>::const_iterator i=elementsByInt.begin();
	 i != elementsByInt.end(); i++) 
      m_eval->outputMessage(QString("  ") + 
			    i->second.leftJustified(30) + 
			    QString("    : ") + 
			    QString("%1\n").arg(i->first));
  }
  void freeze(QByteArray& out, Array s, int length, Interpreter* m_eval) {
    if (IsInteger(s) && !s.isString() && 
	(s.length() == (int)length)) {
      CtypeTable.lookup("int32")->freeze(out,s,length,m_eval);
      return;
    }
    if (length == 1) {
      if (!s.isString()) throw Exception("Expected string for enumerated type");
      CtypeTable.lookup("int32")->freeze(out,Array(int(lookupByName(s.asString()))),1,m_eval);
    } else {
      if (s.dataClass() != CellArray) 
	throw Exception("Expected a cell array of strings for enumerated type");
      if (s.length() != (int)length)
	throw Exception("Length mismatch between cell array of strings and requested enumerated type");
      const BasicArray<Array> &dp(s.constReal<Array>());
      for (index_t i=1;i<=length;i++) {
	if (IsInteger(dp[i]) && !dp[i].isString()) 
	  CtypeTable.lookup("int32")->freeze(out,s,length,m_eval);
	else if (dp[i].isString()) 
	  CtypeTable.lookup("int32")->freeze(out,
					     Array(int(lookupByName(dp[i].asString()))),
					     1,m_eval);
	else 
	  throw Exception("Expected strings as elements of cell array for enumerated type");
      }
    }
  }
  Array thaw(QByteArray& input, int& pos, int length, Interpreter* m_eval) {
    Array values(CtypeTable.lookup("int32")->thaw(input,pos,length,m_eval));
    if (length == 1)
      return Array(lookupByNumber(values.asInteger()));
    const Array &values_int(values.toClass(Int32).asDenseArray());
    const BasicArray<int32> &dp(values_int.constReal<int32>());
    ArrayVector vals;
    for (int i=0;i<length;i++) 
      vals << Array(lookupByNumber(dp[i]));
    return CellConstructor(vals);
  }
  int size(int length) {
    return CtypeTable.lookup("int32")->size(length);
  }
};

// Field in a structure
class CstructField {
  QString name;
  QString type;
  int length;
public:
  CstructField(QString i_name, QString i_type, int i_length) : name(i_name), 
							     type(i_type), 
							     length(i_length) {}
  CstructField() {}
  QString getName() {return name;}
  QString getType() {return type;}
  int getLength() {return length;}
};

// Structure type
class Cstruct : public Ctype {
  QVector<CstructField*> fields;
public:
  ~Cstruct() {
    for (int i=0;i<fields.size();i++) delete fields[i];
  }
  int getFieldCount() {return fields.size();}
  CstructField* getField(int num) {return fields[num];}
  void addField(CstructField* fld) {fields.push_back(fld);}
  void print(Interpreter *m_eval) {
    m_eval->outputMessage("struct\n");
    for (int i=0;i<fields.size();i++) {
      m_eval->outputMessage(QString("  ") + 
			    fields[i]->getName().leftJustified(30) + 
			    QString("    ") + 
			    fields[i]->getType());
      if (fields[i]->getLength() > 1) 
	m_eval->outputMessage(QString("[%1]").arg(fields[i]->getLength()));
      m_eval->outputMessage("\n");
    }
  }
  void freeze(QByteArray& out, Array s, int length, Interpreter* m_eval) {
    int s_count = int(s.length());
    if (s_count != length)
      throw Exception("Length mismatch between expected and actual array count");
    for (int m=0;m<s_count;m++) {
      Array s_el(s.get(Array(index_t(m+1))));
      for (int i=0;i<fields.size();i++) 
	try {
	  CtypeTable.lookup(fields[i]->getType())->freeze(out,s_el.get(fields[i]->getName()).front(),
							  fields[i]->getLength(),m_eval);
	} catch (Exception& e) {
	  throw Exception("Failed during freeze of struct field " + fields[i]->getName() + " reason: " + e.msg());
	}
    }
  }
  Array thaw(QByteArray& input, int& pos, int length, Interpreter* m_eval) {
    // Set up a set of vectors
    ArrayMatrix mat;
    for (int i=0;i<fields.size();i++) 
      mat.push_back(ArrayVector());
    // collect the field names
    StringVector names;
    for (int i=0;i<fields.size();i++) 
      names << fields[i]->getName();
    // Populate the matrix
    for (int m=0;m<length;m++) 
      for (int i=0;i<fields.size();i++) 
	mat[i] << CtypeTable.lookup(fields[i]->getType())->thaw(input,pos,fields[i]->getLength(),m_eval);
    ArrayVector t;
    for (int i=0;i<fields.size();i++)
      t << CellConstructor(mat[i]);
    return StructConstructor(names,t);
  }
  int size(int length) {
    int accum = 0;
    for (int i=0;i<fields.size();i++)
      accum += CtypeTable.lookup(fields[i]->getType())->size(fields[i]->getLength());
    return accum*length;
  }
};

// Alias type
class Calias : public Ctype {
  QString alias;
public:
  Calias(QString i_alias) : alias(i_alias) {}
  QString getAlias() {return alias;}
  void freeze(QByteArray& out, Array s, int length, Interpreter* m_eval) {
    CtypeTable.lookup(alias)->freeze(out,s,length,m_eval);
  }
  Array thaw(QByteArray& input, int& pos, int length, Interpreter* m_eval) {
    return CtypeTable.lookup(alias)->thaw(input,pos,length,m_eval);
  }
  int size(int length) {
    return CtypeTable.lookup(alias)->size(length);
  }
  void print(Interpreter *m_eval) {
    m_eval->outputMessage("alias to " + alias + "\n");
  }
};

CTable::CTable() {
  add("uint8",new Cbuiltin(UInt8,sizeof(uint8)));
  add("uint16",new Cbuiltin(UInt16,sizeof(uint16)));
  add("uint32",new Cbuiltin(UInt32,sizeof(uint32)));
  add("uint64",new Cbuiltin(UInt64,sizeof(uint64)));
  add("int8",new Cbuiltin(Int8,sizeof(int8)));
  add("int16",new Cbuiltin(Int16,sizeof(int16)));
  add("int32",new Cbuiltin(Int32,sizeof(int32)));
  add("int64",new Cbuiltin(Int64,sizeof(int64)));
  add("float",new Cbuiltin(Float,sizeof(float)));
  add("double",new Cbuiltin(Double,sizeof(double)));
}


//@@Signature
//function cenum CenumFunction
//inputs enumtype enumint
//outputs enumstring
//DOCBLOCK external_cenum
ArrayVector CenumFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 2) throw Exception("cenum requires at least two arguments");
  QString enumname = arg[0].asString();
  if (!CtypeTable.contains(enumname))
    throw Exception("type " + enumname + " is not defined");
  Ctype* p = CtypeTable.lookup(enumname);
  Cenum* q = dynamic_cast<Cenum*>(p);
  if (!q)
    throw Exception("type " + enumname + " is not an enumerated type");
  if (arg[1].isString()) {
    return ArrayVector() << Array(double(q->lookupByName(arg[1].asString())));
  } else {
    return ArrayVector() << Array(q->lookupByNumber(arg[1].asInteger()));
  }
  return ArrayVector();
}

//@@Signature
//function ctypedefine CtypeDefineFunction
//inputs varargin
//outputs none
//DOCBLOCK external_ctypedefine
ArrayVector CtypeDefineFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 1) return ArrayVector();
  if (arg.size() < 3) throw Exception("ctypedefine requires at least three arguments - the typeclass ('struct','alias','enum'), the typename, and some type definition information");
  QString tclass = arg[0].asString();
  if (tclass == "enum") {
    QString tname = arg[1].asString();
    int cnt = 2;
    Cenum *enumDef = new Cenum;
    while (cnt < arg.size()) {
      QString ftype = arg[cnt].asString();
      if (arg.size() < (cnt+1))
	throw Exception("Expecting value for enum name " + ftype);
      int fvalue = arg[cnt+1].asInteger();
      enumDef->addPair(ftype,fvalue);
      cnt += 2;
    }
    CtypeTable.add(tname,enumDef);
  } else if (tclass == "struct") {
    QString tname = arg[1].asString();
    int cnt = 2;
    Cstruct *new_struct = new Cstruct;
    while (cnt < arg.size()) {
      QString ftype = arg[cnt].asString();
      if (arg.size() < (cnt+1))
	throw Exception("Expecting typespec for fieldname " + ftype);
      QString ttypespec = arg[cnt+1].asString();
      QString ttype;
      int tlength;
      if (ttypespec.indexOf('[') != -1) {
	int left_brace = ttypespec.indexOf('[');
	int right_brace = ttypespec.indexOf(']');
	ttype = ttypespec.left(left_brace);
	tlength = ttypespec.mid(left_brace+1,right_brace-left_brace-1).toInt();
	if (tlength <= 0)
	  throw Exception(QString("illegal length of %d in defining type ").arg(tlength) + tname);
      } else {
	ttype = ttypespec;
	tlength = 1;
      }
      if (!CtypeTable.contains(ttype)) 
	throw Exception("type " + ttype + " is not defined");
      new_struct->addField(new CstructField(ftype,ttype,tlength));
      cnt += 2;
    }
    CtypeTable.add(tname,new_struct);
  } else if (tclass == "alias") {
    QString tname = arg[1].asString();
    QString aname = arg[2].asString();
    if (tname == aname)
      throw Exception("self-referencing aliases not allowed: " + tname);
    if (!CtypeTable.contains(aname))
      throw Exception("type " + aname + " is not defined");
    CtypeTable.add(tname,new Calias(aname));
  }
  return ArrayVector();
}

//@@Signature
//sfunction ctypeprint CtypePrintFunction
//inputs typename
//outputs none
//DOCBLOCK external_ctypeprint
ArrayVector CtypePrintFunction(int nargout, const ArrayVector& arg, Interpreter *m_eval) {
  if (arg.size() < 1) return ArrayVector();
  QString tname = arg[0].asString();
  if (!CtypeTable.contains(tname)) {
    m_eval->outputMessage("ctype " + tname + " not in table");
    return ArrayVector();
  }
  m_eval->outputMessage("ctype: " + tname + " ");
  CtypeTable.lookup(tname)->print(m_eval);
  return ArrayVector();  
}

//@@Signature
//sfunction ctypefreeze CtypeFreezeFunction
//inputs mystruct typename
//outputs bytearray
//DOCBLOCK external_ctypefreeze
ArrayVector CtypeFreezeFunction(int nargout, const ArrayVector& arg, Interpreter* m_eval) {
  if (arg.size() != 2) 
    throw Exception("ctypefreeze requires two arguments - the structure to freeze and the typename to use");
  Array s(arg[0]);
  QString ttype(arg[1].asString());
  if (!CtypeTable.contains(ttype))
    throw Exception("unable to find a C type definition for " + ttype);
  QByteArray output;
  CtypeTable.lookup(ttype)->freeze(output,s,int(s.length()),m_eval);
  Array retvec(UInt8,NTuple(1,output.length()));
  memcpy(retvec.real<uint8>().data(),output.constData(),output.length());
  return ArrayVector() << retvec;
}

//@@Signature
//function ctypesize CtypeSizeFunction
//inputs type count
//outputs size
//DOCBLOCK external_ctypesize
ArrayVector CtypeSizeFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 1)
    throw Exception("ctypesize requires an argument - the structure name");
  int count = 1;
  if (arg.size() > 1)
    count = arg[1].asInteger();
  QString ttype(arg[0].asString());
  if (!CtypeTable.contains(ttype))
    throw Exception("unable to find a C struct definition for type " + ttype);
  return ArrayVector() << Array(double(CtypeTable.lookup(ttype)->size(count)));
}

//@@Signature
//sfunction ctypethaw CtypeThawFunction
//inputs x type count
//outputs mystruct byarray
//DOCBLOCK external_ctypethaw
ArrayVector CtypeThawFunction(int nargout, const ArrayVector& arg, Interpreter* m_eval) {
  if (arg.size() < 2)
    throw Exception("ctypethaw requires two arguments - the uint8 array to thaw the structure from, and the typename to use");
  Array s(arg[0].asDenseArray());
  QString ttype(arg[1].asString());
  int count = 1;
  if (arg.size() > 2) count = arg[2].asInteger();
  if (!CtypeTable.contains(ttype))
    throw Exception("unable to find a C struct definition for type " + ttype);
  if (s.dataClass() != UInt8)
    throw Exception("first argument to ctypethaw must be a uint8 array");
  QByteArray input((const char*)(s.constReal<uint8>().constData()),int(s.length()));
  int pos = 0;
  ArrayVector outputs;
  outputs << CtypeTable.lookup(ttype)->thaw(input,pos,count,m_eval);
  if (nargout > 1) {
    input.remove(0,pos);
    Array retvec(UInt8,NTuple(1,int(input.length())));
    memcpy(retvec.real<uint8>().data(),input.constData(),input.length());
    outputs << retvec;
  }
  return outputs;
}