| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: typemgr.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: declaring and defining DDD_TYPEs */ |
| 10 |
/* */ |
| 11 |
/* Author: Klaus Birken */ |
| 12 |
/* Rechenzentrum Uni Stuttgart */ |
| 13 |
/* Universitaet Stuttgart */ |
| 14 |
/* Allmandring 30 */ |
| 15 |
/* 70550 Stuttgart */ |
| 16 |
/* internet: [email protected] */ |
| 17 |
/* */ |
| 18 |
/* History: 93/11/22 kb begin */ |
| 19 |
/* 94/09/13 kb added DDD_Status */ |
| 20 |
/* 95/11/03 kb complete rewrite of StructRegister code */ |
| 21 |
/* 95/11/16 kb copied from main.c, introduced clean type concept*/ |
| 22 |
/* 95/12/07 jb added fortran frontend */ |
| 23 |
/* 96/04/02 kb added EL_CONTINUE feature to TypeDefine() */ |
| 24 |
/* 97/02/12 kb added CPP_FRONTEND */ |
| 25 |
/* */ |
| 26 |
/* Remarks: */ |
| 27 |
/* */ |
| 28 |
/****************************************************************************/ |
| 29 |
|
| 30 |
/****************************************************************************/ |
| 31 |
/* */ |
| 32 |
/* include files */ |
| 33 |
/* system include files */ |
| 34 |
/* application include files */ |
| 35 |
/* */ |
| 36 |
/****************************************************************************/ |
| 37 |
|
| 38 |
/* standard C library */ |
| 39 |
#include <config.h> |
| 40 |
|
| 41 |
#include <algorithm> |
| 42 |
#include <cstddef> |
| 43 |
#include <cstdlib> |
| 44 |
#include <cstdio> |
| 45 |
#include <stdarg.h> |
| 46 |
#include <cstring> |
| 47 |
|
| 48 |
#include <iomanip> |
| 49 |
#include <iostream> |
| 50 |
|
| 51 |
#include <dune/common/exceptions.hh> |
| 52 |
#include <dune/common/stdstreams.hh> |
| 53 |
|
| 54 |
#include <dune/uggrid/parallel/ddd/dddi.h> |
| 55 |
|
| 56 |
#include <dune/uggrid/parallel/ddd/dddcontext.hh> |
| 57 |
|
| 58 |
USING_UG_NAMESPACES |
| 59 |
|
| 60 |
/* PPIF namespace: */ |
| 61 |
using namespace PPIF; |
| 62 |
|
| 63 |
START_UGDIM_NAMESPACE |
| 64 |
|
| 65 |
/* |
| 66 |
#define DebugTypeDefine |
| 67 |
#define DebugCopyMask |
| 68 |
#define DebugNoStructCompress |
| 69 |
*/ |
| 70 |
|
| 71 |
|
| 72 |
/****************************************************************************/ |
| 73 |
/* */ |
| 74 |
/* definition of constants */ |
| 75 |
/* */ |
| 76 |
/****************************************************************************/ |
| 77 |
|
| 78 |
enum DDD_TypeModes |
| 79 |
{ |
| 80 |
DDD_TYPE_INVALID = 0, /* DDD_TYPE not declared, not defined */ |
| 81 |
DDD_TYPE_DECLARED, /* DDD_TYPE declared, but not defined */ |
| 82 |
DDD_TYPE_CONTDEF, /* DDD_TYPE declared and partially defined */ |
| 83 |
DDD_TYPE_DEFINED /* DDD_TYPE declared and defined */ |
| 84 |
}; |
| 85 |
|
| 86 |
/****************************************************************************/ |
| 87 |
/* */ |
| 88 |
/* routines */ |
| 89 |
/* */ |
| 90 |
/****************************************************************************/ |
| 91 |
|
| 92 |
static void InitHandlers (TYPE_DESC *); |
| 93 |
|
| 94 |
|
| 95 |
int ddd_TypeDefined (TYPE_DESC *desc) |
| 96 |
{ |
| 97 |
return(desc->mode==DDD_TYPE_DEFINED); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/****************************************************************************/ |
| 102 |
|
| 103 |
|
| 104 |
/* |
| 105 |
print out trailing part of error message during TypeDefine process |
| 106 |
|
| 107 |
error occurred during TypeDefine for desc, with argument argno |
| 108 |
*/ |
| 109 |
class RegisterError |
| 110 |
{ |
| 111 |
public: |
| 112 |
RegisterError(TYPE_DESC *desc, int argno) |
| 113 |
: desc(desc) |
| 114 |
, argno(argno) |
| 115 |
{} |
| 116 |
|
| 117 |
friend std::ostream& operator<<(std::ostream& out, const RegisterError& e) |
| 118 |
{ |
| 119 |
if (e.argno != 0) |
| 120 |
out << ", arg " << e.argno << " of "; |
| 121 |
else |
| 122 |
out << " in "; |
| 123 |
out << "DDD_TypeDefine(\"" << e.desc->name << "/" << e.desc->currTypeDefCall << "\")"; |
| 124 |
return out; |
| 125 |
} |
| 126 |
|
| 127 |
private: |
| 128 |
TYPE_DESC* desc; |
| 129 |
int argno; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
/* |
| 136 |
check ELEM_DESC for plausibility |
| 137 |
*/ |
| 138 |
|
| 139 |
static int CheckBounds (TYPE_DESC *desc, ELEM_DESC *el, int argno) |
| 140 |
{ |
| 141 |
if (el->offset < 0) |
| 142 |
{ |
| 143 |
Dune::dwarn << "negative offset" << RegisterError(desc, argno) << "\n"; |
| 144 |
return(ERROR); |
| 145 |
} |
| 146 |
|
| 147 |
if (el->size<=0) |
| 148 |
{ |
| 149 |
Dune::dwarn << "illegal element size" << RegisterError(desc, argno) << "\n"; |
| 150 |
return (ERROR); |
| 151 |
} |
| 152 |
|
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
/* |
| 159 |
check ELEM_DESC list of given TYPE_DESC for bad overlapping |
| 160 |
*/ |
| 161 |
|
| 162 |
static int CheckOverlapEls (TYPE_DESC *desc) |
| 163 |
{ |
| 164 |
int i; |
| 165 |
int ok = true; |
| 166 |
|
| 167 |
for(i=0; i<desc->nElements; i++) |
| 168 |
{ |
| 169 |
ELEM_DESC *e1 = &desc->element[i]; |
| 170 |
|
| 171 |
if (i<desc->nElements-1) |
| 172 |
{ |
| 173 |
ELEM_DESC *e2 = &desc->element[i+1]; |
| 174 |
|
| 175 |
if (e1->offset+e1->size > e2->offset) |
| 176 |
{ |
| 177 |
ok = false; |
| 178 |
Dune::dwarn << "element too big (offset=" << e1->offset << ")" |
| 179 |
<< RegisterError(desc, 0) << "\n"; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
else |
| 184 |
{ |
| 185 |
if (e1->offset+e1->size > desc->size) |
| 186 |
{ |
| 187 |
ok = false; |
| 188 |
Dune::dwarn << "element too big (offset=" << e1->offset << ")" |
| 189 |
<< RegisterError(desc, 0) << "\n"; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
return ok; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
/* |
| 199 |
constructor for ELEM_DESC |
| 200 |
*/ |
| 201 |
|
| 202 |
static void ConstructEl (ELEM_DESC *elem, int t, int o, size_t s, DDD_TYPE rt) |
| 203 |
{ |
| 204 |
elem->type = t; |
| 205 |
elem->offset = o; |
| 206 |
elem->size = s; |
| 207 |
|
| 208 |
/* |
| 209 |
for OBJPTR elements, store referenced DDD_TYPE here. |
| 210 |
the default is EL_DDDHDR, i.e., if this feature is |
| 211 |
not used, the DDD_HDR will be assumed to be at the |
| 212 |
beginning of each structure (offsetHeader==0). |
| 213 |
*/ |
| 214 |
EDESC_SET_REFTYPE(elem, rt); |
| 215 |
elem->reftypeHandler = NULL; |
| 216 |
|
| 217 |
/* |
| 218 |
for GBITS elements, store array of bits. 1=GDATA, |
| 219 |
0=LDATA. |
| 220 |
*/ |
| 221 |
if (t==EL_GBITS) |
| 222 |
{ |
| 223 |
elem->gbits = std::make_unique<unsigned char[]>(s); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/* |
| 229 |
register previously defined TYPE_DESC during TypeDefine |
| 230 |
*/ |
| 231 |
|
| 232 |
static int RecursiveRegister (DDD::DDDContext& context, TYPE_DESC *desc, |
| 233 |
int i, DDD_TYPE typ, int offs, int argno) |
| 234 |
{ |
| 235 |
TYPE_DESC *d2 = &context.typeDefs()[typ]; |
| 236 |
int j; |
| 237 |
|
| 238 |
/* inherit elements of other ddd-type */ |
| 239 |
for(j=0; j<d2->nElements && i < TYPE_DESC::MAX_ELEMDESC; j++, i++) |
| 240 |
{ |
| 241 |
ConstructEl(&desc->element[i], |
| 242 |
d2->element[j].type, |
| 243 |
d2->element[j].offset + offs, |
| 244 |
d2->element[j].size, |
| 245 |
EDESC_REFTYPE(&(d2->element[j]))); |
| 246 |
if (CheckBounds(desc, &desc->element[i], argno) == ERROR) |
| 247 |
return(ERROR); |
| 248 |
} |
| 249 |
|
| 250 |
/* inherit other properties */ |
| 251 |
desc->nPointers += d2->nPointers; |
| 252 |
if (d2->hasHeader) |
| 253 |
{ |
| 254 |
if (!desc->hasHeader) |
| 255 |
{ |
| 256 |
desc->hasHeader = true; |
| 257 |
desc->offsetHeader = d2->offsetHeader + offs; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
if (desc->offsetHeader == d2->offsetHeader+offs) |
| 262 |
{ |
| 263 |
Dune::dwarn << "two DDD_HDRs, same offset" |
| 264 |
<< RegisterError(desc, argno) << "\n"; |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
Dune::dwarn << "only one DDD_HDR allowed" |
| 269 |
<< RegisterError(desc, argno) << "\n"; |
| 270 |
return(ERROR); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return i; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/* |
| 280 |
constructor for TYPE_DESC |
| 281 |
*/ |
| 282 |
|
| 283 |
static void ConstructDesc (TYPE_DESC *desc) |
| 284 |
{ |
| 285 |
InitHandlers(desc); |
| 286 |
|
| 287 |
desc->nPointers = 0; |
| 288 |
desc->nElements = 0; |
| 289 |
desc->cmask = nullptr; |
| 290 |
desc->hasHeader = false; |
| 291 |
desc->offsetHeader = 0; |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
#ifndef DebugNoStructCompress |
| 298 |
|
| 299 |
/* |
| 300 |
remove first element in given ELEM_DESC-list, |
| 301 |
adjust remaining elements |
| 302 |
*/ |
| 303 |
|
| 304 |
static void DeleteFirstEl (ELEM_DESC *elarray, int n) |
| 305 |
{ |
| 306 |
int i; |
| 307 |
|
| 308 |
for(i=1; i<n; i++) |
| 309 |
{ |
| 310 |
elarray[i-1] = std::move(elarray[i]); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
#endif |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
/* |
| 319 |
normalize ELEM_DESC-list of given TYPE_DESC |
| 320 |
|
| 321 |
this consists of two parts: |
| 322 |
1) sort ELEM_DESC-list by offset (necessary!!) |
| 323 |
2) compress ELEM_DESC-list according to a set of rules |
| 324 |
*/ |
| 325 |
|
| 326 |
static int NormalizeDesc (TYPE_DESC *desc) |
| 327 |
{ |
| 328 |
ELEM_DESC *elems = desc->element; |
| 329 |
|
| 330 |
/* sort element array by offset */ |
| 331 |
std::sort( |
| 332 |
elems, elems + desc->nElements, |
| 333 |
[](const ELEM_DESC& a, const ELEM_DESC& b) { return a.offset < b.offset; } |
| 334 |
); |
| 335 |
|
| 336 |
/* check for overlapping elements */ |
| 337 |
if (! CheckOverlapEls(desc)) |
| 338 |
return false; |
| 339 |
|
| 340 |
|
| 341 |
# ifndef DebugNoStructCompress |
| 342 |
{ |
| 343 |
/* compile this only if Debug-flag not set */ |
| 344 |
int i; |
| 345 |
|
| 346 |
/* compress element description */ |
| 347 |
for(i=0; i<desc->nElements-1; i++) |
| 348 |
{ |
| 349 |
size_t realsize; |
| 350 |
|
| 351 |
/* 1) type must be equal */ |
| 352 |
if (elems[i].type != elems[i+1].type) |
| 353 |
continue; |
| 354 |
|
| 355 |
/* 2) nothing can melt into DDD_HEADER */ |
| 356 |
if (desc->hasHeader && elems[i+1].offset==desc->offsetHeader) |
| 357 |
continue; |
| 358 |
|
| 359 |
/* 3) gap between elements is allowed only for EL_LDATA */ |
| 360 |
if ((elems[i].offset+elems[i].size != elems[i+1].offset) && |
| 361 |
(elems[i].type!=EL_LDATA) ) |
| 362 |
continue; |
| 363 |
|
| 364 |
/* 4) EL_OBJPTRs with different reftypes can't be compressed */ |
| 365 |
if (elems[i].type==EL_OBJPTR && |
| 366 |
((EDESC_REFTYPE(elems+i)!= EDESC_REFTYPE(elems+(i+1))) || |
| 367 |
(EDESC_REFTYPE(elems+i)==DDD_TYPE_BY_HANDLER)) ) |
| 368 |
continue; |
| 369 |
|
| 370 |
/* 5) EL_GBITS can't be compressed */ |
| 371 |
if (elems[i].type == EL_GBITS) |
| 372 |
continue; |
| 373 |
|
| 374 |
/* all conditions fit: compress elements */ |
| 375 |
realsize = elems[i+1].offset - elems[i].offset; |
| 376 |
elems[i].size = realsize + elems[i+1].size; |
| 377 |
|
| 378 |
desc->nElements--; |
| 379 |
DeleteFirstEl(&elems[i+1], desc->nElements - i); |
| 380 |
|
| 381 |
i--; /* skip one element back and try again */ |
| 382 |
} |
| 383 |
} |
| 384 |
# endif |
| 385 |
|
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
/* |
| 391 |
compute copy-mask (for efficiency) and attach it to TYPE_DESC |
| 392 |
*/ |
| 393 |
|
| 394 |
static void AttachMask(const DDD::DDDContext& context, TYPE_DESC *desc) |
| 395 |
{ |
| 396 |
unsigned char mask; |
| 397 |
|
| 398 |
/* get storage for mask */ |
| 399 |
desc->cmask = std::make_unique<unsigned char[]>(desc->size); |
| 400 |
|
| 401 |
/* set default: EL_LDATA for unspecified regions (gaps) */ |
| 402 |
for (std::size_t i=0; i<desc->size; i++) |
| 403 |
{ |
| 404 |
desc->cmask[i] = 0x00; /* dont-copy-flag */ |
| 405 |
} |
| 406 |
|
| 407 |
/* create mask from element list */ |
| 408 |
for (int i=0; i<desc->nElements; i++) |
| 409 |
{ |
| 410 |
ELEM_DESC *e = &desc->element[i]; |
| 411 |
unsigned char* mp = desc->cmask.get() + e->offset; |
| 412 |
|
| 413 |
switch (e->type) |
| 414 |
{ |
| 415 |
case EL_LDATA : |
| 416 |
case EL_OBJPTR : /* OBJPTR are LDATA!!! */ |
| 417 |
mask = 0x00; /* dont-copy-flag */ |
| 418 |
break; |
| 419 |
|
| 420 |
case EL_GDATA : |
| 421 |
case EL_DATAPTR : |
| 422 |
mask = 0xff; /* copy-flag */ |
| 423 |
break; |
| 424 |
} |
| 425 |
|
| 426 |
for (std::size_t k=0; k<e->size; k++) |
| 427 |
{ |
| 428 |
if (e->type==EL_GBITS) |
| 429 |
{ |
| 430 |
mp[k] = e->gbits[k]; /* copy bitwise */ |
| 431 |
} |
| 432 |
else |
| 433 |
{ |
| 434 |
mp[k] = mask; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
# ifdef DebugCopyMask |
| 440 |
if (context.isMaster()) |
| 441 |
{ |
| 442 |
using std::setfill; |
| 443 |
using std::setw; |
| 444 |
|
| 445 |
Dune::dinfo << "AttachMask for " << desc->name ":"; |
| 446 |
for(i=0; i<desc->size; i++) |
| 447 |
{ |
| 448 |
if (i%8==0) |
| 449 |
Dune::dinfo << "\n " << setw(4) << i << ": "; |
| 450 |
Dune::dinfo << setfill('0') << setw(2) << desc->cmask[i] << setfill(' ') << " "; |
| 451 |
} |
| 452 |
Dune::dinfo << "\n"; |
| 453 |
} |
| 454 |
# endif |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
/****************************************************************************/ |
| 462 |
/* */ |
| 463 |
/* Function: DDD_TypeDefine */ |
| 464 |
/* */ |
| 465 |
/* Purpose: define object structure at runtime */ |
| 466 |
/* */ |
| 467 |
/* Input: context typ, t0, p0, s0, [r0 [, rh0]], t1, p1, s1 ... */ |
| 468 |
/* with typ: previously declared DDD_TYPE */ |
| 469 |
/* t: element type */ |
| 470 |
/* p: offset of member in data structure */ |
| 471 |
/* s: size of element in byte */ |
| 472 |
/* r: (only for EL_OBJPTR): referenced DDD_TYPE */ |
| 473 |
/* rt: (only for EL_OBJPTR and DDD_TYPE_BY_HANDLER): */ |
| 474 |
/* handler for getting reftype on-the-fly. */ |
| 475 |
/* */ |
| 476 |
/* Output: - */ |
| 477 |
/* */ |
| 478 |
/****************************************************************************/ |
| 479 |
|
| 480 |
void DDD_TypeDefine(DDD::DDDContext& context, DDD_TYPE typ, ...) |
| 481 |
{ |
| 482 |
auto& nDescr = context.typemgrContext().nDescr; |
| 483 |
|
| 484 |
size_t argsize; |
| 485 |
size_t argoffset; |
| 486 |
int argtyp, argno; |
| 487 |
DDD_TYPE argrefs; |
| 488 |
int i, nPtr; |
| 489 |
va_list ap; |
| 490 |
char *gbits; |
| 491 |
|
| 492 |
/* TODO: only master should be able to define types, other |
| 493 |
procs should receive the correct definition from master. |
| 494 |
(with the current implementation inconsistencies might occur) |
| 495 |
*/ |
| 496 |
|
| 497 |
/* test whether typ is valid */ |
| 498 |
if (typ>=nDescr) |
| 499 |
DUNE_THROW(Dune::Exception, "invalid DDD_TYPE"); |
| 500 |
|
| 501 |
/* get object description */ |
| 502 |
TYPE_DESC* desc = &context.typeDefs()[typ]; |
| 503 |
desc->currTypeDefCall++; |
| 504 |
|
| 505 |
if (desc->mode!=DDD_TYPE_DECLARED && desc->mode!=DDD_TYPE_CONTDEF) |
| 506 |
{ |
| 507 |
if (desc->mode==DDD_TYPE_DEFINED) |
| 508 |
DUNE_THROW(Dune::Exception, "DDD_TYPE already defined"); |
| 509 |
else |
| 510 |
DUNE_THROW(Dune::Exception, "undeclared DDD_TYPE"); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
/* initialize TYPE_DESC struct, only on first call */ |
| 515 |
if (desc->currTypeDefCall==1) |
| 516 |
ConstructDesc(desc); |
| 517 |
|
| 518 |
|
| 519 |
if (typ==0) /* i.e. typ==EL_DDDHDR */ |
| 520 |
{ |
| 521 |
/* DDD_HDR also contains a DDD_HDR (sic!) */ |
| 522 |
desc->hasHeader = true; |
| 523 |
} |
| 524 |
|
| 525 |
# ifdef DebugTypeDefine |
| 526 |
Dune::dinfo << " DDD_TypeDefine(" << desc->name |
| 527 |
<< "/" << desc->currTypeDefCall << ")\n"; |
| 528 |
# endif |
| 529 |
|
| 530 |
|
| 531 |
/* start variable arguments after "typ"-parameter */ |
| 532 |
va_start(ap, typ); |
| 533 |
|
| 534 |
argno = 1; |
| 535 |
|
| 536 |
|
| 537 |
/* loop over variable argument list */ |
| 538 |
|
| 539 |
i = desc->nElements; |
| 540 |
while ((i < TYPE_DESC::MAX_ELEMDESC) && |
| 541 |
((argtyp= va_arg(ap, int))!=EL_END) && (argtyp!=EL_CONTINUE)) |
| 542 |
{ |
| 543 |
HandlerGetRefType arg_rt_handler = NULL; |
| 544 |
|
| 545 |
/* get the offset of the member in the object */ |
| 546 |
argoffset = va_arg(ap, size_t); |
| 547 |
argno+=2; |
| 548 |
|
| 549 |
/* handle several types of ELEM_DESCs */ |
| 550 |
switch (argtyp) |
| 551 |
{ |
| 552 |
/* 1) ELEM_DESC is a pointer [array] */ |
| 553 |
case EL_OBJPTR : |
| 554 |
case EL_DATAPTR : |
| 555 |
/* get third argument of this definition line */ |
| 556 |
argsize = va_arg(ap, size_t); argno++; |
| 557 |
|
| 558 |
/* EL_OBJPTR have to be specified with target type */ |
| 559 |
if (argtyp==EL_OBJPTR) |
| 560 |
{ |
| 561 |
|
| 562 |
/* get fourth argument: referenced DDD_TYPE */ |
| 563 |
argrefs = (DDD_TYPE) va_arg(ap, int); argno++; |
| 564 |
|
| 565 |
/* check whether target type is DDD_TYPE_BY_HANDLER */ |
| 566 |
/* this is currently supported only by C_FRONTEND */ |
| 567 |
if (argrefs==DDD_TYPE_BY_HANDLER) |
| 568 |
{ |
| 569 |
arg_rt_handler = va_arg(ap, HandlerGetRefType); |
| 570 |
argno++; |
| 571 |
} |
| 572 |
else |
| 573 |
{ |
| 574 |
/* check whether target type is valid */ |
| 575 |
if (argrefs>=nDescr || |
| 576 |
context.typeDefs()[argrefs].mode==DDD_TYPE_INVALID) |
| 577 |
{ |
| 578 |
DUNE_THROW(Dune::Exception, |
| 579 |
"referencing invalid DDD_TYPE" << RegisterError(desc,argno)); |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
else |
| 584 |
{ |
| 585 |
/* to target type for EL_DATAPTR */ |
| 586 |
argrefs = EL_DDDHDR; |
| 587 |
} |
| 588 |
|
| 589 |
/* compute #pointers (in array) */ |
| 590 |
nPtr = argsize / sizeof(void *); |
| 591 |
|
| 592 |
/* check for plausibility */ |
| 593 |
if (nPtr*sizeof(void *) != argsize) |
| 594 |
{ |
| 595 |
DUNE_THROW(Dune::Exception, |
| 596 |
"invalid sizeof" << RegisterError(desc,argno)); |
| 597 |
} |
| 598 |
|
| 599 |
/* remember #pointers */ |
| 600 |
desc->nPointers += nPtr; |
| 601 |
|
| 602 |
|
| 603 |
/* initialize ELEM_DESC */ |
| 604 |
ConstructEl(&desc->element[i], |
| 605 |
argtyp, argoffset, argsize, argrefs); |
| 606 |
|
| 607 |
/* set reftype-handler function pointer, if any */ |
| 608 |
if (argrefs==DDD_TYPE_BY_HANDLER) |
| 609 |
desc->element[i].reftypeHandler = arg_rt_handler; |
| 610 |
|
| 611 |
if (CheckBounds(desc, &desc->element[i], argno) == ERROR) |
| 612 |
return; |
| 613 |
i++; |
| 614 |
|
| 615 |
# ifdef DebugTypeDefine |
| 616 |
Dune::dinfo << " PTR, " << std::setw(5) << argoffset |
| 617 |
<< ", " << std::setw(6) << argsize << "\n"; |
| 618 |
# endif |
| 619 |
|
| 620 |
break; |
| 621 |
|
| 622 |
|
| 623 |
/* 2) ELEM_DESC is global or local data */ |
| 624 |
case EL_GDATA : |
| 625 |
case EL_LDATA : |
| 626 |
/* get third argument of this definition line */ |
| 627 |
argsize = va_arg(ap, size_t); argno++; |
| 628 |
|
| 629 |
/* initialize ELEM_DESC */ |
| 630 |
ConstructEl(&desc->element[i], |
| 631 |
argtyp, argoffset, argsize, 0); |
| 632 |
|
| 633 |
if (CheckBounds(desc, &desc->element[i], argno) == ERROR) |
| 634 |
return; |
| 635 |
i++; |
| 636 |
|
| 637 |
# ifdef DebugTypeDefine |
| 638 |
Dune::dinfo << " DAT, " << std::setw(5) << argoffset |
| 639 |
<< ", " << std::setw(6) << argsize << "\n"; |
| 640 |
# endif |
| 641 |
|
| 642 |
break; |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
/* 3) ELEM_DESC is bitwise global or local */ |
| 647 |
case EL_GBITS : |
| 648 |
/* get third argument of this definition line */ |
| 649 |
argsize = va_arg(ap, size_t); argno++; |
| 650 |
|
| 651 |
/* initialize ELEM_DESC */ |
| 652 |
ConstructEl(&desc->element[i], |
| 653 |
argtyp, argoffset, argsize, 0); |
| 654 |
|
| 655 |
/* read forth arg from cmdline */ |
| 656 |
gbits = va_arg(ap, char *); argno++; |
| 657 |
|
| 658 |
/* fill gbits array, read forth arg from cmdline */ |
| 659 |
memcpy(desc->element[i].gbits.get(), gbits, argsize); |
| 660 |
|
| 661 |
if (CheckBounds(desc, &desc->element[i], argno) == ERROR) |
| 662 |
return; |
| 663 |
|
| 664 |
# ifdef DebugTypeDefine |
| 665 |
Dune::dinfo << " BITS, " << std::setw(5) << argoffset |
| 666 |
<< ", " << std::setw(6) << argsize << ", "; |
| 667 |
Dune::dinfo << std::setfill('0') << std::hex; |
| 668 |
for(int ii=0; ii<argsize; ii++) |
| 669 |
Dune::dinfo << std::setw(2) << int(desc->element[i].gbits[ii]) << " "; |
| 670 |
Dune::dinfo << std::setfill(' ') << std::dec << "\n"; |
| 671 |
# endif |
| 672 |
|
| 673 |
i++; |
| 674 |
|
| 675 |
break; |
| 676 |
|
| 677 |
|
| 678 |
/* 4) ELEM_DESC is a recursively defined DDD_TYPE */ |
| 679 |
default : |
| 680 |
/* hierarchical registering of known ddd_types */ |
| 681 |
/* no third argument here */ |
| 682 |
|
| 683 |
/* check for plausibility of given DDD_TYPE */ |
| 684 |
if (argtyp<0 || argtyp>=nDescr || argtyp==typ) |
| 685 |
{ |
| 686 |
DUNE_THROW(Dune::Exception, |
| 687 |
"undefined DDD_TYPE=" << argtyp |
| 688 |
<< RegisterError(desc,argno-1)); |
| 689 |
} |
| 690 |
|
| 691 |
/* check whether given DDD_TYPE has been defined already */ |
| 692 |
if (context.typeDefs()[argtyp].mode==DDD_TYPE_DEFINED) |
| 693 |
{ |
| 694 |
/* do recursive TypeDefine */ |
| 695 |
i = RecursiveRegister(context, desc, |
| 696 |
i, argtyp, argoffset, argno); |
| 697 |
if (i==ERROR) HARD_EXIT; /* return; */ |
| 698 |
|
| 699 |
#ifdef DebugTypeDefine |
| 700 |
Dune::dinfo |
| 701 |
<< " " << std::setw(3) << argtyp |
| 702 |
<< ", " << std::setw(5) << argoffset |
| 703 |
<< ", " << std::setw(6) << context.typeDefs()[argtyp].size << "\n"; |
| 704 |
#endif |
| 705 |
} |
| 706 |
else |
| 707 |
{ |
| 708 |
DUNE_THROW(Dune::Exception, |
| 709 |
"undefined DDD_TYPE " << context.typeDefs()[argtyp].name |
| 710 |
<< RegisterError(desc,argno-1)); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
break; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
/* check whether loop has come to a correct end */ |
| 720 |
if (i >= TYPE_DESC::MAX_ELEMDESC && argtyp!=EL_END && argtyp!=EL_CONTINUE) |
| 721 |
DUNE_THROW(Dune::Exception, "too many elements" << RegisterError(desc, 0)); |
| 722 |
|
| 723 |
|
| 724 |
/* remember #elements in TYPE_DESC */ |
| 725 |
desc->nElements = i; |
| 726 |
|
| 727 |
|
| 728 |
if (argtyp==EL_END) /* and not EL_CONTINUE */ |
| 729 |
{ |
| 730 |
/* compute aligned object length */ |
| 731 |
desc->size = va_arg(ap, size_t); |
| 732 |
desc->size = CEIL(desc->size); |
| 733 |
|
| 734 |
/* do normalization */ |
| 735 |
if (! NormalizeDesc(desc)) |
| 736 |
HARD_EXIT; /*return;*/ |
| 737 |
|
| 738 |
/* attach copy-mask for efficient copying */ |
| 739 |
AttachMask(context, desc); |
| 740 |
|
| 741 |
/* change TYPE_DESC state to DEFINED */ |
| 742 |
desc->mode = DDD_TYPE_DEFINED; |
| 743 |
} |
| 744 |
else /* argtyp==EL_CONTINUE */ |
| 745 |
{ |
| 746 |
/* change TYPE_DESC state to CONTDEF */ |
| 747 |
desc->mode = DDD_TYPE_CONTDEF; |
| 748 |
} |
| 749 |
|
| 750 |
va_end(ap); |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
/****************************************************************************/ |
| 757 |
/* */ |
| 758 |
/* Function: DDD_TypeDeclare */ |
| 759 |
/* */ |
| 760 |
/* Purpose: declare DDD_TYPE at runtime */ |
| 761 |
/* */ |
| 762 |
/* Input: name: object name string */ |
| 763 |
/* */ |
| 764 |
/* Output: id of object-description */ |
| 765 |
/* -1 on error */ |
| 766 |
/* */ |
| 767 |
/****************************************************************************/ |
| 768 |
|
| 769 |
DDD_TYPE DDD_TypeDeclare(DDD::DDDContext& context, const char *name) |
| 770 |
{ |
| 771 |
auto& nDescr = context.typemgrContext().nDescr; |
| 772 |
TYPE_DESC* desc = &context.typeDefs()[nDescr]; |
| 773 |
|
| 774 |
/* check whether there is one more DDD_TYPE */ |
| 775 |
if (nDescr==MAX_TYPEDESC) |
| 776 |
DUNE_THROW(Dune::Exception, "no more free DDD_TYPEs"); |
| 777 |
|
| 778 |
/* set status to DECLARED and remember textual type name */ |
| 779 |
desc->mode = DDD_TYPE_DECLARED; |
| 780 |
desc->name = name; |
| 781 |
|
| 782 |
desc->prioMatrix = nullptr; |
| 783 |
desc->prioDefault = PRIOMERGE_DEFAULT; |
| 784 |
|
| 785 |
/* increase #DDD_TYPEs, but return previously defined one */ |
| 786 |
nDescr++; return(nDescr-1); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
/****************************************************************************/ |
| 792 |
/* */ |
| 793 |
/* Function: DDD_TypeDisplay */ |
| 794 |
/* */ |
| 795 |
/* Purpose: show defined DDD_TYPE */ |
| 796 |
/* */ |
| 797 |
/* Input: id: DDD_TYPE which will be shown */ |
| 798 |
/* */ |
| 799 |
/* Output: - */ |
| 800 |
/* */ |
| 801 |
/****************************************************************************/ |
| 802 |
|
| 803 |
void DDD_TypeDisplay(const DDD::DDDContext& context, DDD_TYPE id) |
| 804 |
{ |
| 805 |
using std::setw; |
| 806 |
|
| 807 |
/* only master should display DDD_TYPEs */ |
| 808 |
if (context.isMaster()) |
| 809 |
{ |
| 810 |
/* check for plausibility */ |
| 811 |
if (id >= context.typemgrContext().nDescr) |
| 812 |
DUNE_THROW(Dune::Exception, "invalid DDD_TYPE " << id); |
| 813 |
|
| 814 |
const TYPE_DESC* desc = &context.typeDefs()[id]; |
| 815 |
if (desc->mode != DDD_TYPE_DEFINED) |
| 816 |
DUNE_THROW(Dune::Exception, "undefined DDD_TYPE " << id); |
| 817 |
|
| 818 |
/* print header */ |
| 819 |
std::ostream& out = std::cout; |
| 820 |
out << "/ Structure of " << (desc->hasHeader ? "DDD" : "data") |
| 821 |
<< "--object '" << desc->name <<"', id " << id |
| 822 |
<< ", " << desc->size << " byte\n" |
| 823 |
<< "|--------------------------------------------------------------\n"; |
| 824 |
|
| 825 |
/* print one line for each element */ |
| 826 |
for (int i = 0; i < desc->nElements; i++) |
| 827 |
{ |
| 828 |
const ELEM_DESC *e = &desc->element[i]; |
| 829 |
|
| 830 |
int realnext = (i==desc->nElements-1) ? desc->size : (e+1)->offset; |
| 831 |
int estinext = e->offset+e->size; |
| 832 |
|
| 833 |
/* handle gap at the beginning */ |
| 834 |
if (i==0 && e->offset!=0) |
| 835 |
out << "|" << setw(5) << 0 |
| 836 |
<< " " << setw(5) << e->offset |
| 837 |
<< " gap (local data)\n"; |
| 838 |
|
| 839 |
/* do visual compression of elems inherited from DDD_HDR */ |
| 840 |
if (id==EL_DDDHDR || |
| 841 |
(!desc->hasHeader) || |
| 842 |
e->offset < desc->offsetHeader || |
| 843 |
e->offset >= desc->offsetHeader + context.typeDefs()[EL_DDDHDR].size) |
| 844 |
{ |
| 845 |
out << "|" << setw(5) << e->offset |
| 846 |
<< " " << setw(5) << e->size << " "; |
| 847 |
|
| 848 |
/* print one line according to type */ |
| 849 |
switch (e->type) |
| 850 |
{ |
| 851 |
case EL_GDATA : out << "global data\n"; break; |
| 852 |
case EL_LDATA : out << "local data\n"; break; |
| 853 |
case EL_DATAPTR : out << "data pointer\n"; break; |
| 854 |
case EL_OBJPTR : |
| 855 |
if (EDESC_REFTYPE(e)!=DDD_TYPE_BY_HANDLER) |
| 856 |
out << "obj pointer (refs " |
| 857 |
<< context.typeDefs()[EDESC_REFTYPE(e)].name << ")\n"; |
| 858 |
else |
| 859 |
out << "obj pointer (reftype on-the-fly)\n"; |
| 860 |
break; |
| 861 |
case EL_GBITS : |
| 862 |
out << "bitwise global: "; |
| 863 |
out << std::setfill('0') << std::hex; |
| 864 |
for (size_t ii=0; ii<e->size; ii++) |
| 865 |
out << setw(2) << int(e->gbits[ii]) << " "; |
| 866 |
out << std::setfill(' ') << std::dec << "\n"; |
| 867 |
break; |
| 868 |
} |
| 869 |
|
| 870 |
/* handle gap */ |
| 871 |
if (estinext != realnext) |
| 872 |
out << "|" << setw(5) << estinext << " " |
| 873 |
<< setw(5) << (realnext-estinext) |
| 874 |
<< " gap (local data)\n"; |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
/* handle included DDD_HDR */ |
| 879 |
if (e->offset == desc->offsetHeader) |
| 880 |
out << "|" << setw(5) << e->offset |
| 881 |
<< " " << setw(5) << context.typeDefs()[EL_DDDHDR].size |
| 882 |
<< " ddd-header\n"; |
| 883 |
} |
| 884 |
|
| 885 |
} |
| 886 |
out << "\\--------------------------------------------------------------\n"; |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
static void InitHandlers (TYPE_DESC *desc) |
| 894 |
{ |
| 895 |
/* set all handler functions to default (=none) */ |
| 896 |
desc->handlerLDATACONSTRUCTOR = NULL; |
| 897 |
desc->handlerDESTRUCTOR = NULL; |
| 898 |
desc->handlerDELETE = NULL; |
| 899 |
desc->handlerUPDATE = NULL; |
| 900 |
desc->handlerOBJMKCONS = NULL; |
| 901 |
desc->handlerSETPRIORITY = NULL; |
| 902 |
desc->handlerXFERCOPY = NULL; |
| 903 |
desc->handlerXFERDELETE = NULL; |
| 904 |
desc->handlerXFERGATHER = NULL; |
| 905 |
desc->handlerXFERSCATTER = NULL; |
| 906 |
desc->handlerXFERGATHERX = NULL; |
| 907 |
desc->handlerXFERSCATTERX = NULL; |
| 908 |
desc->handlerXFERCOPYMANIP = NULL; |
| 909 |
} |
| 910 |
|
| 911 |
#define DEFINE_DDD_SETHANDLER(name) \ |
| 912 |
void DDD_SetHandler##name(DDD::DDDContext& context, DDD_TYPE type_id, Handler##name funcptr) { \ |
| 913 |
TYPE_DESC& desc = context.typeDefs()[type_id]; \ |
| 914 |
assert(desc.mode == DDD_TYPE_DEFINED); \ |
| 915 |
desc.handler##name = funcptr; \ |
| 916 |
} |
| 917 |
|
| 918 |
DEFINE_DDD_SETHANDLER(LDATACONSTRUCTOR) |
| 919 |
DEFINE_DDD_SETHANDLER(DESTRUCTOR) |
| 920 |
DEFINE_DDD_SETHANDLER(DELETE) |
| 921 |
DEFINE_DDD_SETHANDLER(UPDATE) |
| 922 |
DEFINE_DDD_SETHANDLER(OBJMKCONS) |
| 923 |
DEFINE_DDD_SETHANDLER(SETPRIORITY) |
| 924 |
DEFINE_DDD_SETHANDLER(XFERCOPY) |
| 925 |
DEFINE_DDD_SETHANDLER(XFERDELETE) |
| 926 |
DEFINE_DDD_SETHANDLER(XFERGATHER) |
| 927 |
DEFINE_DDD_SETHANDLER(XFERSCATTER) |
| 928 |
DEFINE_DDD_SETHANDLER(XFERGATHERX) |
| 929 |
DEFINE_DDD_SETHANDLER(XFERSCATTERX) |
| 930 |
DEFINE_DDD_SETHANDLER(XFERCOPYMANIP) |
| 931 |
|
| 932 |
#undef DEFINE_DDD_SETHANDLER |
| 933 |
|
| 934 |
/****************************************************************************/ |
| 935 |
/* */ |
| 936 |
/* Function: DDD_InfoTypes */ |
| 937 |
/* */ |
| 938 |
/* Purpose: returns number of declared types */ |
| 939 |
/* */ |
| 940 |
/* Input: - */ |
| 941 |
/* */ |
| 942 |
/* Output: number of declared types */ |
| 943 |
/* */ |
| 944 |
/****************************************************************************/ |
| 945 |
|
| 946 |
int DDD_InfoTypes(const DDD::DDDContext& context) |
| 947 |
{ |
| 948 |
return context.typemgrContext().nDescr; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
/****************************************************************************/ |
| 954 |
/* */ |
| 955 |
/* Function: DDD_InfoHdrOffset */ |
| 956 |
/* */ |
| 957 |
/* Purpose: returns offset of DDD_HEADER for a given DDD_TYPE in bytes */ |
| 958 |
/* NOTE: output will be invalid for DDD_TYPEs without header! */ |
| 959 |
/* */ |
| 960 |
/* Input: typeId: DDD_TYPE for which header offset is returned */ |
| 961 |
/* */ |
| 962 |
/* Output: nheader offset for given type */ |
| 963 |
/* */ |
| 964 |
/****************************************************************************/ |
| 965 |
|
| 966 |
|
| 967 |
int DDD_InfoHdrOffset(const DDD::DDDContext& context, DDD_TYPE typeId) |
| 968 |
{ |
| 969 |
const TYPE_DESC& desc = context.typeDefs()[typeId]; |
| 970 |
|
| 971 |
return desc.offsetHeader; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
/****************************************************************************/ |
| 976 |
/* */ |
| 977 |
/* Function: ddd_TypeMgrInit */ |
| 978 |
/* */ |
| 979 |
/* Purpose: init TypeMgr module */ |
| 980 |
/* (DDD_HEADER is declared and defined as first DDD_TYPE) */ |
| 981 |
/* */ |
| 982 |
/* Input: - */ |
| 983 |
/* */ |
| 984 |
/* Output: - */ |
| 985 |
/* */ |
| 986 |
/****************************************************************************/ |
| 987 |
|
| 988 |
void ddd_TypeMgrInit(DDD::DDDContext& context) |
| 989 |
{ |
| 990 |
/* set all theTypeDefs to INVALID, i.e., no DDD_TYPE has been defined */ |
| 991 |
for (TYPE_DESC& typeDef : context.typeDefs()) { |
| 992 |
typeDef.mode = DDD_TYPE_INVALID; |
| 993 |
typeDef.currTypeDefCall = 0; |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
/* reset declared types */ |
| 998 |
context.typemgrContext().nDescr = 0; |
| 999 |
|
| 1000 |
|
| 1001 |
/* init DDD_HEADER as first type, with DDD_TYPE=0 */ |
| 1002 |
{ |
| 1003 |
DDD_TYPE hdr_type; |
| 1004 |
|
| 1005 |
/* hdr_type will be EL_DDDHDR (=0) per default */ |
| 1006 |
hdr_type = DDD_TypeDeclare(context, "DDD_HDR"); |
| 1007 |
DDD_TypeDefine(context, hdr_type, |
| 1008 |
|
| 1009 |
EL_GDATA, offsetof(DDD_HEADER,typ), sizeof(DDD_HEADER::typ), |
| 1010 |
EL_LDATA, offsetof(DDD_HEADER,prio), sizeof(DDD_HEADER::prio), |
| 1011 |
EL_GDATA, offsetof(DDD_HEADER,attr), sizeof(DDD_HEADER::attr), |
| 1012 |
EL_LDATA, offsetof(DDD_HEADER,flags), sizeof(DDD_HEADER::flags), |
| 1013 |
EL_LDATA, offsetof(DDD_HEADER,myIndex), sizeof(DDD_HEADER::myIndex), |
| 1014 |
EL_GDATA, offsetof(DDD_HEADER,gid), sizeof(DDD_HEADER::gid), |
| 1015 |
|
| 1016 |
EL_END, sizeof(DDD_HEADER) ); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
/****************************************************************************/ |
| 1022 |
/* */ |
| 1023 |
/* Function: ddd_TypeMgrExit */ |
| 1024 |
/* */ |
| 1025 |
/* Purpose: exit and clean up TypeMgr module */ |
| 1026 |
/* */ |
| 1027 |
/* Input: - */ |
| 1028 |
/* */ |
| 1029 |
/* Output: - */ |
| 1030 |
/* */ |
| 1031 |
/****************************************************************************/ |
| 1032 |
|
| 1033 |
void ddd_TypeMgrExit(DDD::DDDContext& context) |
| 1034 |
{ |
| 1035 |
/* free memory */ |
| 1036 |
for (auto& typeDef : context.typeDefs()) { |
| 1037 |
typeDef.cmask = nullptr; |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
/****************************************************************************/ |
| 1042 |
|
| 1043 |
END_UGDIM_NAMESPACE |