| 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: objmgr.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: creation and deletion of objects */ |
| 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: 94/02/21 kb begin */ |
| 19 |
/* 95/11/03 kb complete redesign of objmgr-interface, C++ style */ |
| 20 |
/* 95/11/15 kb arbitrary offset for DDD_HEADER */ |
| 21 |
/* */ |
| 22 |
/* Remarks: */ |
| 23 |
/* */ |
| 24 |
/****************************************************************************/ |
| 25 |
|
| 26 |
/****************************************************************************/ |
| 27 |
/* */ |
| 28 |
/* include files */ |
| 29 |
/* system include files */ |
| 30 |
/* application include files */ |
| 31 |
/* */ |
| 32 |
/****************************************************************************/ |
| 33 |
|
| 34 |
/* standard C library */ |
| 35 |
#include <config.h> |
| 36 |
#include <cstdlib> |
| 37 |
#include <cstdio> |
| 38 |
#include <cstring> |
| 39 |
#include <cassert> |
| 40 |
|
| 41 |
#include <algorithm> |
| 42 |
|
| 43 |
#include <dune/common/exceptions.hh> |
| 44 |
#include <dune/common/stdstreams.hh> |
| 45 |
|
| 46 |
#include <dune/uggrid/parallel/ddd/dddi.h> |
| 47 |
|
| 48 |
USING_UG_NAMESPACES |
| 49 |
|
| 50 |
/* PPIF namespace: */ |
| 51 |
using namespace PPIF; |
| 52 |
|
| 53 |
START_UGDIM_NAMESPACE |
| 54 |
|
| 55 |
/* |
| 56 |
#define DebugCreation |
| 57 |
#define DebugDeletion |
| 58 |
*/ |
| 59 |
|
| 60 |
|
| 61 |
/****************************************************************************/ |
| 62 |
/* */ |
| 63 |
/* defines in the following order */ |
| 64 |
/* */ |
| 65 |
/* compile time constants defining static data size (i.e. arrays) */ |
| 66 |
/* other constants */ |
| 67 |
/* macros */ |
| 68 |
/* */ |
| 69 |
/****************************************************************************/ |
| 70 |
|
| 71 |
|
| 72 |
#define MakeUnique(context, n) (((n)<<MAX_PROCBITS_IN_GID)+(context).me()) |
| 73 |
#define ProcFromId(n) ((n)& ((1<<MAX_PROCBITS_IN_GID)-1)) |
| 74 |
#define CountFromId(n) (((n)-((n)& ((1<<MAX_PROCBITS_IN_GID)-1)))>>MAX_PROCBITS_IN_GID) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
/****************************************************************************/ |
| 80 |
/* */ |
| 81 |
/* routines */ |
| 82 |
/* */ |
| 83 |
/****************************************************************************/ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static bool sort_ObjListGID (const DDD_HDR& a, const DDD_HDR& b) |
| 88 |
{ |
| 89 |
return OBJ_GID(a) < OBJ_GID(b); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
std::vector<DDD_HDR> LocalObjectsList(const DDD::DDDContext& context) |
| 94 |
{ |
| 95 |
const int nObjs = context.nObjs(); |
| 96 |
std::vector<DDD_HDR> locObjs(nObjs); |
| 97 |
|
| 98 |
const auto& objTable = context.objTable(); |
| 99 |
std::copy(objTable.begin(), objTable.begin() + nObjs, locObjs.begin()); |
| 100 |
std::sort(locObjs.begin(), locObjs.end(), sort_ObjListGID); |
| 101 |
|
| 102 |
return locObjs; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
std::vector<DDD_HDR> LocalCoupledObjectsList(const DDD::DDDContext& context) |
| 107 |
{ |
| 108 |
const auto& nCpls = context.couplingContext().nCpls; |
| 109 |
std::vector<DDD_HDR> locObjs(nCpls); |
| 110 |
|
| 111 |
const auto& objTable = context.objTable(); |
| 112 |
std::copy(objTable.begin(), objTable.begin() + nCpls, locObjs.begin()); |
| 113 |
std::sort(locObjs.begin(), locObjs.end(), sort_ObjListGID); |
| 114 |
|
| 115 |
return locObjs; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/****************************************************************************/ |
| 120 |
|
| 121 |
|
| 122 |
void ddd_EnsureObjTabSize(DDD::DDDContext& context, int n) |
| 123 |
{ |
| 124 |
auto& objTable = context.objTable(); |
| 125 |
|
| 126 |
/* if size is large enough, we are already finished. */ |
| 127 |
if (objTable.size() >= n) |
| 128 |
return; |
| 129 |
|
| 130 |
objTable.resize(n); |
| 131 |
|
| 132 |
/* issue a warning in order to inform user */ |
| 133 |
Dune::dwarn << "increased object table, now " << n << " entries\n"; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/****************************************************************************/ |
| 138 |
|
| 139 |
/* |
| 140 |
Description of ObjMgr Interfaces |
| 141 |
|
| 142 |
Raw-Memory-Interface: DDD_ObjNew, DDD_ObjDelete |
| 143 |
Constructor-Interface: DDD_HdrConstructor, DDD_HdrDestructor |
| 144 |
Application-Interface: DDD_Get, DDD_UnGet |
| 145 |
*/ |
| 146 |
|
| 147 |
|
| 148 |
/****************************************************************************/ |
| 149 |
/* */ |
| 150 |
/* Function: DDD_ObjNew */ |
| 151 |
/* */ |
| 152 |
/****************************************************************************/ |
| 153 |
|
| 154 |
/* Purpose: get raw memory for new DDD-object. */ |
| 155 |
/* */ |
| 156 |
/* Input: size: memory size of object */ |
| 157 |
/* typ: DDD_TYPE of object */ |
| 158 |
/* prio: DDD_PRIO of object */ |
| 159 |
/* attr: attribute of distributed object */ |
| 160 |
/* */ |
| 161 |
/* Output: pointer to raw memory */ |
| 162 |
/* */ |
| 163 |
/****************************************************************************/ |
| 164 |
|
| 165 |
/** |
| 166 |
Allocate raw memory for new \ddd{object}. |
| 167 |
This function dynamically creates raw memory for a new \ddd{object}. |
| 168 |
Therefore, the user-supplied memory manager function \memmgrfunk{AllocOMEM} |
| 169 |
is called to allocate the necessary memory. Although the caller must |
| 170 |
supply the object's priority and attribute, its header will not be |
| 171 |
initialized by \funk{ObjNew}; the parameters are used for smart |
| 172 |
memory allocation, only. |
| 173 |
|
| 174 |
The function \funk{ObjNew} and \funk{ObjDelete}, its corresponding |
| 175 |
deletion function, form the ObjManager's {\em raw memory interface}. |
| 176 |
|
| 177 |
DDD users who use the #C_FRONTEND# may prefer the more |
| 178 |
elaborate {\em application interface}, consisting of the functions |
| 179 |
\funk{ObjGet} and \funk{ObjUnGet}. DDD users who use the language |
| 180 |
C++ (object-oriented style) with #CPP_FRONTEND# will use the |
| 181 |
{\em raw memory interface} together with the |
| 182 |
{\em constructor/destructor interface} (\funk{HdrConstructor}, |
| 183 |
\funk{HdrDestructor}, \funk{HdrConstructorMove}) |
| 184 |
in order to integrate DDD into C++ style object management easily. |
| 185 |
|
| 186 |
For variable-sized \ddd{objects}, the parameter {\em aSize} |
| 187 |
may differ from the size specified during the corresponding |
| 188 |
\funk{TypeDefine}-call. |
| 189 |
|
| 190 |
@return pointer to free memory block for the \ddd{object} |
| 191 |
@param aSize memory size of the new object |
| 192 |
@param aType \ddd{type} of the new object |
| 193 |
@param aPrio \ddd{priority} of the new object |
| 194 |
@param aAttr \ddd{attribute} of the new object |
| 195 |
*/ |
| 196 |
|
| 197 |
|
| 198 |
DDD_OBJ DDD_ObjNew (size_t aSize, DDD_TYPE aType, |
| 199 |
DDD_PRIO aPrio, DDD_ATTR aAttr) |
| 200 |
{ |
| 201 |
DDD_OBJ obj; |
| 202 |
|
| 203 |
/* check input parameters */ |
| 204 |
if (aPrio>=MAX_PRIO) |
| 205 |
DUNE_THROW(Dune::Exception, "priority must be less than " << MAX_PRIO); |
| 206 |
if (aType>=MAX_TYPEDESC) |
| 207 |
DUNE_THROW(Dune::Exception, "DDD-type must be less than " << MAX_TYPEDESC); |
| 208 |
|
| 209 |
/* get object memory */ |
| 210 |
obj = (DDD_OBJ) AllocObj(aSize, aType, aPrio, aAttr); |
| 211 |
if (obj==NULL) |
| 212 |
throw std::bad_alloc(); |
| 213 |
|
| 214 |
# ifdef DebugCreation |
| 215 |
Dune::dinfo |
| 216 |
<< "DDD_ObjNew(aSize=" << aSize << ", type=" << aType << ", prio=" << aPrio |
| 217 |
<< ", attr=" << aAttr << ") ADR=" << obj << "\n"; |
| 218 |
# endif |
| 219 |
|
| 220 |
return(obj); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
/****************************************************************************/ |
| 226 |
/* */ |
| 227 |
/* Function: DDD_ObjDelete */ |
| 228 |
/* */ |
| 229 |
/* Purpose: free raw memory from DDD-object */ |
| 230 |
/* */ |
| 231 |
/* Input: obj: object header address */ |
| 232 |
/* size: memory size of object */ |
| 233 |
/* typ: DDD_TYPE of object */ |
| 234 |
/* */ |
| 235 |
/* Output: - */ |
| 236 |
/* */ |
| 237 |
/****************************************************************************/ |
| 238 |
|
| 239 |
|
| 240 |
void DDD_ObjDelete (DDD_OBJ obj, size_t size, DDD_TYPE typ) |
| 241 |
{ |
| 242 |
FreeObj((void *)obj, size, typ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/****************************************************************************/ |
| 247 |
/* */ |
| 248 |
/* Function: DDD_HdrConstructor */ |
| 249 |
/* */ |
| 250 |
/****************************************************************************/ |
| 251 |
|
| 252 |
/** |
| 253 |
Initiate object's \ddd{header}. |
| 254 |
This function registers a \ddd{object} via constructing |
| 255 |
its DDD-header. Each \ddd{object} is given a unique {\em global ID}, |
| 256 |
which is stored in the DDD-header together with the object's |
| 257 |
properties (type\_id, prio, attr) and additional data used by DDD. |
| 258 |
|
| 259 |
The function \funk{HdrConstructor} and its corresponding deletion |
| 260 |
function \funk{HdrDestructor} form the ObjManager's |
| 261 |
{\em constructor/destructor interface}. This interface will be |
| 262 |
useful for C++ users, together with the {\em raw memory interface} |
| 263 |
functions \funk{ObjNew} and \funk{ObjDelete}. |
| 264 |
DDD users who use the language C may prefer the more |
| 265 |
elaborate {\em application interface}, consisting of the functions |
| 266 |
\funk{ObjGet} and \funk{ObjUnGet}. |
| 267 |
|
| 268 |
Due to the construction of global IDs, an overflow error may occur |
| 269 |
after many calls to \funk{HdrConstructor} |
| 270 |
(\MaxUniqueGids\ times in \Version). |
| 271 |
|
| 272 |
@param aHdr pointer to the \ddd{header} which should be constructed |
| 273 |
@param aType \ddd{type} of \ddd{object} |
| 274 |
@param aPrio \ddd{priority} of \ddd{object} |
| 275 |
@param aAttr \ddd{attribute} of \ddd{object} |
| 276 |
*/ |
| 277 |
|
| 278 |
void DDD_HdrConstructor (DDD::DDDContext& context, |
| 279 |
DDD_HDR aHdr, DDD_TYPE aType, |
| 280 |
DDD_PRIO aPrio, DDD_ATTR aAttr) |
| 281 |
{ |
| 282 |
auto& ctx = context.objmgrContext(); |
| 283 |
|
| 284 |
/* check input parameters */ |
| 285 |
if (aPrio>=MAX_PRIO) |
| 286 |
DUNE_THROW(Dune::Exception, "priority must be less than " << MAX_PRIO); |
| 287 |
|
| 288 |
#ifdef WithFullObjectTable |
| 289 |
/* in case of FullObjectTable, we register each header in the |
| 290 |
global ddd_ObjTable. */ |
| 291 |
auto& objTable = context.objTable(); |
| 292 |
|
| 293 |
/* check whether there are available objects */ |
| 294 |
if (context.nObjs() == objTable.size()) |
| 295 |
{ |
| 296 |
/* TODO update docu */ |
| 297 |
/* this is a fatal case. we can't register more objects here */ |
| 298 |
/* TODO one could try to expand the global tables here. */ |
| 299 |
DUNE_THROW(Dune::Exception, "no more objects in DDD_HdrConstructor"); |
| 300 |
} |
| 301 |
|
| 302 |
/* insert into theObj array */ |
| 303 |
objTable[context.nObjs()] = aHdr; |
| 304 |
OBJ_INDEX(aHdr) = context.nObjs(); |
| 305 |
context.nObjs(context.nObjs() + 1); |
| 306 |
#else |
| 307 |
/* if we dont have WithFullObjectTable, pure local objects without |
| 308 |
copies on other processors aren't registered by DDD. Therefore, |
| 309 |
they don't have a valid OBJ_INDEX field. */ |
| 310 |
MarkHdrLocal(aHdr); |
| 311 |
#endif |
| 312 |
|
| 313 |
|
| 314 |
/* init object header with defaults */ |
| 315 |
OBJ_TYPE(aHdr) = aType; |
| 316 |
OBJ_PRIO(aHdr) = aPrio; |
| 317 |
OBJ_ATTR(aHdr) = aAttr; |
| 318 |
OBJ_FLAGS(aHdr) = 0; |
| 319 |
|
| 320 |
/* create unique GID */ |
| 321 |
OBJ_GID(aHdr) = MakeUnique(context, ctx.theIdCount++); |
| 322 |
|
| 323 |
/* check overflow of global id numbering */ |
| 324 |
if (MakeUnique(context, ctx.theIdCount) <= MakeUnique(context, ctx.theIdCount-1)) |
| 325 |
{ |
| 326 |
/* TODO update docu */ |
| 327 |
/* TODO one could try to renumber all objects here. */ |
| 328 |
DUNE_THROW(Dune::Exception, "global ID overflow DDD_HdrConstructor"); |
| 329 |
} |
| 330 |
|
| 331 |
# ifdef DebugCreation |
| 332 |
Dune::dinfo |
| 333 |
<< "DDD_HdrConstructor(adr=" << aHdr << ", type=" << aType |
| 334 |
<< ", prio=" << aPrio << ", attr=" << aAttr << "), GID=" << OBJ_GID(aHdr) |
| 335 |
<< " INDEX=" << OBJ_INDEX(aHdr) << "\n"; |
| 336 |
# endif |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
/****************************************************************************/ |
| 342 |
/* */ |
| 343 |
/* Function: DDD_HdrDestructor */ |
| 344 |
/* */ |
| 345 |
/****************************************************************************/ |
| 346 |
|
| 347 |
/** |
| 348 |
Remove object's header from DDD management. |
| 349 |
This function removes an object from DDD-management |
| 350 |
via destructing its \ddd{header}. |
| 351 |
{\em Note:} The \ddd{object} will be destroyed, but its copies |
| 352 |
on remote processors will not be informed by \funk{HdrDestructor}. |
| 353 |
There are two consistent possibilities to delete \ddd{objects} which |
| 354 |
have copies on remote processors: |
| 355 |
|
| 356 |
\begin{itemize} |
| 357 |
\item In order to delete only this local object copy, use |
| 358 |
\funk{XferDelete} during a DDD Transfer-operation. This will |
| 359 |
inform the remote copies of the deletion. |
| 360 |
\item In order to delete a distributed object (\ie, all its |
| 361 |
object copies), use function \funk{ObjUnGet} (when using the |
| 362 |
{\em application interface}) |
| 363 |
or a combination of functions |
| 364 |
\funk{HdrDestructor} / \funk{ObjDelete} (when not using |
| 365 |
the {\em application interface}) for all copies. |
| 366 |
\end{itemize} |
| 367 |
|
| 368 |
The function \funk{HdrDestructor} and its corresponding creation |
| 369 |
function \funk{HdrConstructor} form the ObjManager's |
| 370 |
{\em constructor/destructor interface}. This interface will be |
| 371 |
useful for C++ users, together with the {\em raw memory interface} |
| 372 |
functions \funk{ObjNew} and \funk{ObjDelete}. |
| 373 |
DDD users who use the language C may prefer the more |
| 374 |
elaborate {\em application interface}, consisting of the functions |
| 375 |
\funk{ObjGet} and \funk{ObjUnGet}. |
| 376 |
|
| 377 |
@param hdr the object's DDD Header |
| 378 |
*/ |
| 379 |
|
| 380 |
void DDD_HdrDestructor(DDD::DDDContext& context, DDD_HDR hdr) |
| 381 |
{ |
| 382 |
auto& objTable = context.objTable(); |
| 383 |
auto& nCpls = context.couplingContext().nCpls; |
| 384 |
COUPLING *cpl; |
| 385 |
int objIndex, xfer_active = ddd_XferActive(context); |
| 386 |
|
| 387 |
# ifdef DebugDeletion |
| 388 |
Dune::dinfo |
| 389 |
<< "DDD_HdrDestructor(adr=" << hdr << ", typ=" << OBJ_TYPE(hdr) |
| 390 |
<< ", prio=" << OBJ_PRIO(hdr) << ", attr=" << OBJ_ATTR(hdr) |
| 391 |
<< "), GID=" << OBJ_GID(hdr) << " INDEX=" << OBJ_INDEX(hdr) << "\n"; |
| 392 |
# endif |
| 393 |
|
| 394 |
|
| 395 |
if (IsHdrInvalid(hdr)) |
| 396 |
{ |
| 397 |
/* DDD_HDR is invalid, so destructor is useless */ |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
/* formally, the object's GID should be returned here */ |
| 402 |
|
| 403 |
|
| 404 |
/* if currently in xfer, register deletion for other processors */ |
| 405 |
if (xfer_active) |
| 406 |
ddd_XferRegisterDelete(context, hdr); |
| 407 |
|
| 408 |
|
| 409 |
objIndex = OBJ_INDEX(hdr); |
| 410 |
|
| 411 |
if (objIndex < nCpls) |
| 412 |
{ |
| 413 |
/* this is an object with couplings */ |
| 414 |
cpl = IdxCplList(context, objIndex); |
| 415 |
|
| 416 |
/* if not during xfer, deletion may be inconsistent */ |
| 417 |
if (!xfer_active) |
| 418 |
{ |
| 419 |
/* deletion is dangerous, distributed object might get |
| 420 |
inconsistent. */ |
| 421 |
if (DDD_GetOption(context, OPT_WARNING_DESTRUCT_HDR)==OPT_ON) |
| 422 |
Dune::dwarn << "DDD_HdrDestructor: inconsistency by deleting gid=" |
| 423 |
<< OBJ_GID(hdr) << "\n"; |
| 424 |
} |
| 425 |
|
| 426 |
nCpls -= 1; |
| 427 |
context.nObjs(context.nObjs() - 1); |
| 428 |
|
| 429 |
/* fill slot of deleted obj with last cpl-obj */ |
| 430 |
objTable[objIndex] = objTable[nCpls]; |
| 431 |
IdxCplList(context, objIndex) = IdxCplList(context, nCpls); |
| 432 |
IdxNCpl(context, objIndex) = IdxNCpl(context, nCpls); |
| 433 |
OBJ_INDEX(objTable[objIndex]) = objIndex; |
| 434 |
|
| 435 |
#ifdef WithFullObjectTable |
| 436 |
/* fill slot of last cpl-obj with last obj */ |
| 437 |
if (nCpls < context.nObjs()) |
| 438 |
{ |
| 439 |
objTable[nCpls] = objTable[context.nObjs()]; |
| 440 |
OBJ_INDEX(objTable[nCpls]) = nCpls; |
| 441 |
} |
| 442 |
#else |
| 443 |
assert(nCpls == context.nObjs()); |
| 444 |
#endif |
| 445 |
|
| 446 |
/* dispose all couplings */ |
| 447 |
DisposeCouplingList(context, cpl); |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
#ifdef WithFullObjectTable |
| 452 |
/* this is an object without couplings */ |
| 453 |
/* deletion is not dangerous (no consistency problem) */ |
| 454 |
context.nObjs(context.nObjs() - 1); |
| 455 |
|
| 456 |
/* fill slot of deleted obj with last obj */ |
| 457 |
objTable[objIndex] = objTable[context.nObjs()]; |
| 458 |
OBJ_INDEX(objTable[objIndex]) = objIndex; |
| 459 |
#endif |
| 460 |
} |
| 461 |
|
| 462 |
/* invalidate this DDD_HDR */ |
| 463 |
MarkHdrInvalid(hdr); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
/****************************************************************************/ |
| 469 |
/* */ |
| 470 |
/* Function: DDD_ObjGet */ |
| 471 |
/* */ |
| 472 |
/* Purpose: get new DDD object for a given DDD_TYPE */ |
| 473 |
/* */ |
| 474 |
/* DDD_ObjGet allows different size for each object instance, */ |
| 475 |
/* differing from the size computed during DDD_TypeDefine */ |
| 476 |
/* */ |
| 477 |
/* Input: typ: DDD_TYPE of object */ |
| 478 |
/* */ |
| 479 |
/* Output: pointer to memory, which is raw except from the */ |
| 480 |
/* constructed DDD_HDR. */ |
| 481 |
/* */ |
| 482 |
/****************************************************************************/ |
| 483 |
|
| 484 |
DDD_OBJ DDD_ObjGet (DDD::DDDContext& context, size_t size, DDD_TYPE typ, DDD_PRIO prio, DDD_ATTR attr) |
| 485 |
{ |
| 486 |
DDD_OBJ obj; |
| 487 |
const TYPE_DESC& desc = context.typeDefs()[typ]; |
| 488 |
|
| 489 |
/* check input parameters */ |
| 490 |
if (prio<0 || prio>=MAX_PRIO) |
| 491 |
DUNE_THROW(Dune::Exception, "priority must be less than " << MAX_PRIO); |
| 492 |
|
| 493 |
/* get raw memory */ |
| 494 |
obj = (DDD_OBJ) DDD_ObjNew(size, typ, prio, attr); |
| 495 |
if (obj==NULL) |
| 496 |
throw std::bad_alloc(); |
| 497 |
|
| 498 |
if ((desc.size != size) && (DDD_GetOption(context, OPT_WARNING_VARSIZE_OBJ)==OPT_ON)) |
| 499 |
{ |
| 500 |
DDD_PrintError('W', 2200, |
| 501 |
"object size differs from declared size in DDD_ObjGet"); |
| 502 |
} |
| 503 |
|
| 504 |
if ((desc.size > size) && (DDD_GetOption(context, OPT_WARNING_SMALLSIZE)==OPT_ON)) |
| 505 |
{ |
| 506 |
DDD_PrintError('W', 2201, |
| 507 |
"object size smaller than declared size in DDD_ObjGet"); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
/* call DDD_HdrConstructor */ |
| 512 |
DDD_HdrConstructor(context, OBJ2HDR(obj, &desc), typ, prio, attr); |
| 513 |
|
| 514 |
return(obj); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
/****************************************************************************/ |
| 520 |
/* */ |
| 521 |
/* Function: DDD_ObjUnGet */ |
| 522 |
/* */ |
| 523 |
/* Purpose: remove object from DDD management and free its memory */ |
| 524 |
/* */ |
| 525 |
/* Input: obj: object header address */ |
| 526 |
/* */ |
| 527 |
/* Output: - */ |
| 528 |
/* */ |
| 529 |
/****************************************************************************/ |
| 530 |
|
| 531 |
void DDD_ObjUnGet (DDD::DDDContext& context, DDD_HDR hdr, size_t size) |
| 532 |
|
| 533 |
{ |
| 534 |
DDD_TYPE typ = OBJ_TYPE(hdr); |
| 535 |
const TYPE_DESC& desc = context.typeDefs()[typ]; |
| 536 |
DDD_OBJ obj = HDR2OBJ(hdr, &desc); |
| 537 |
|
| 538 |
if ((desc.size != size) && (DDD_GetOption(context, OPT_WARNING_VARSIZE_OBJ)==OPT_ON)) |
| 539 |
{ |
| 540 |
DDD_PrintError('W', 2299, |
| 541 |
"object size differs from declared size in DDD_ObjUnGet"); |
| 542 |
} |
| 543 |
|
| 544 |
/* call DDD_HDR-destructor */ |
| 545 |
DDD_HdrDestructor(context, hdr); |
| 546 |
|
| 547 |
/* free raw memory */ |
| 548 |
DDD_ObjDelete(obj, size, typ); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
/****************************************************************************/ |
| 554 |
/* */ |
| 555 |
/* Function: DDD_HdrConstructorCopy */ |
| 556 |
/* */ |
| 557 |
/* Purpose: create DDD_HDR copy from message original */ |
| 558 |
/* */ |
| 559 |
/* Input: newhdr: new DDD_HDR */ |
| 560 |
/* prio: DDD_PRIO of new copy */ |
| 561 |
/* */ |
| 562 |
/* Output: - */ |
| 563 |
/* */ |
| 564 |
/****************************************************************************/ |
| 565 |
|
| 566 |
void DDD_HdrConstructorCopy (DDD::DDDContext& context, DDD_HDR newhdr, DDD_PRIO prio) |
| 567 |
{ |
| 568 |
/* check input parameters */ |
| 569 |
if (prio>=MAX_PRIO) |
| 570 |
DUNE_THROW(Dune::Exception, "priority must be less than " << MAX_PRIO); |
| 571 |
|
| 572 |
#ifdef WithFullObjectTable |
| 573 |
auto& objTable = context.objTable(); |
| 574 |
|
| 575 |
/* check whether there are available objects */ |
| 576 |
if (context.nObjs() == context.objTable().size()) |
| 577 |
{ |
| 578 |
/* TODO update docu */ |
| 579 |
/* this is a fatal case. we can't register more objects here */ |
| 580 |
DDD_PrintError('F', 2220, "no more objects in DDD_HdrConstructorCopy"); |
| 581 |
/* TODO one could try to expand the global tables here. */ |
| 582 |
} |
| 583 |
|
| 584 |
/* insert into theObj array */ |
| 585 |
objTable[context.nObjs()] = newhdr; |
| 586 |
OBJ_INDEX(newhdr) = context.nObjs(); |
| 587 |
context.nObjs(context.nObjs() + 1); |
| 588 |
#else |
| 589 |
MarkHdrLocal(newhdr); |
| 590 |
assert(context.nObjs() == context.couplingContext().nCpls); |
| 591 |
#endif |
| 592 |
|
| 593 |
/* init LDATA components. GDATA components will be copied elsewhere */ |
| 594 |
OBJ_PRIO(newhdr) = prio; |
| 595 |
|
| 596 |
|
| 597 |
# ifdef DebugCreation |
| 598 |
Dune::dinfo |
| 599 |
<< "DDD_HdrConstructorCopy(adr=" << newhdr << ", prio=" << prio |
| 600 |
<< "), GID= " << OBJ_GID(newhdr) << " INDEX=" << OBJ_INDEX(newhdr) |
| 601 |
<< " ATTR=" << OBJ_ATTR(newhdr) << "\n"; |
| 602 |
# endif |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
/****************************************************************************/ |
| 608 |
/* */ |
| 609 |
/* Function: DDD_HdrConstructorMove */ |
| 610 |
/* */ |
| 611 |
/* Purpose: create DDD_HDR copy inside local memory, */ |
| 612 |
/* simultaneously destruct original DDD_HDR */ |
| 613 |
/* */ |
| 614 |
/* Input: newhdr: new DDD_HDR */ |
| 615 |
/* oldhdr: old DDD_HDR */ |
| 616 |
/* */ |
| 617 |
/* Output: - */ |
| 618 |
/* */ |
| 619 |
/****************************************************************************/ |
| 620 |
|
| 621 |
void DDD_HdrConstructorMove (DDD::DDDContext& context, DDD_HDR newhdr, DDD_HDR oldhdr) |
| 622 |
{ |
| 623 |
int objIndex = OBJ_INDEX(oldhdr); |
| 624 |
const auto& nCpls = context.couplingContext().nCpls; |
| 625 |
|
| 626 |
|
| 627 |
/* copy all components */ |
| 628 |
OBJ_INDEX(newhdr) = OBJ_INDEX(oldhdr); |
| 629 |
OBJ_TYPE(newhdr) = OBJ_TYPE(oldhdr); |
| 630 |
OBJ_PRIO(newhdr) = OBJ_PRIO(oldhdr); |
| 631 |
OBJ_ATTR(newhdr) = OBJ_ATTR(oldhdr); |
| 632 |
OBJ_FLAGS(newhdr) = OBJ_FLAGS(oldhdr); |
| 633 |
OBJ_GID(newhdr) = OBJ_GID(oldhdr); |
| 634 |
|
| 635 |
|
| 636 |
/* change all references from DDD to oldhdr */ |
| 637 |
|
| 638 |
/* change entry of theObj array */ |
| 639 |
auto& objTable = context.objTable(); |
| 640 |
#ifdef WithFullObjectTable |
| 641 |
objTable[objIndex] = newhdr; |
| 642 |
#else |
| 643 |
if (objIndex < nCpls) |
| 644 |
objTable[objIndex] = newhdr; |
| 645 |
#endif |
| 646 |
|
| 647 |
/* change pointers from couplings to object */ |
| 648 |
if (objIndex < nCpls) |
| 649 |
{ |
| 650 |
COUPLING *cpl = IdxCplList(context, objIndex); |
| 651 |
|
| 652 |
for(; cpl!=NULL; cpl=CPL_NEXT(cpl)) { |
| 653 |
cpl->obj = newhdr; |
| 654 |
} |
| 655 |
|
| 656 |
/* invalidate update obj-shortcut tables from IF module */ |
| 657 |
IFInvalidateShortcuts(context, OBJ_TYPE(newhdr)); |
| 658 |
} |
| 659 |
|
| 660 |
/* invalidate old DDD_HDR */ |
| 661 |
MarkHdrInvalid(oldhdr); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
/****************************************************************************/ |
| 667 |
/* */ |
| 668 |
/* Function: ObjCopyGlobalData */ |
| 669 |
/* */ |
| 670 |
/* Purpose: copy DDD-object from message to memory. elements marked */ |
| 671 |
/* EL_LDATA (local data) will not be copied. this is done */ |
| 672 |
/* in an efficient way by using the DDD_TYPEs copy-mask (which */ |
| 673 |
/* has been set up during StructRegister()). */ |
| 674 |
/* */ |
| 675 |
/* CopyByMask() is a support function doing the actual work. */ |
| 676 |
/* This function could be more efficient by copying more than */ |
| 677 |
/* one byte at a time. */ |
| 678 |
/* */ |
| 679 |
/* Input: target: DDD_OBJ address of target memory */ |
| 680 |
/* source: DDD_OBJ address of source object */ |
| 681 |
/* size: size of object in bytes (might be != desc->size) */ |
| 682 |
/* */ |
| 683 |
/* Output: - */ |
| 684 |
/* */ |
| 685 |
/****************************************************************************/ |
| 686 |
|
| 687 |
|
| 688 |
static void CopyByMask (TYPE_DESC *desc, DDD_OBJ target, DDD_OBJ source) |
| 689 |
{ |
| 690 |
unsigned char *s=(unsigned char *)source, *t=(unsigned char *)target; |
| 691 |
int i; |
| 692 |
|
| 693 |
# ifdef DebugCreation |
| 694 |
Dune::dinfo << "CopyByMask(" << desc->name << ", size=" << desc->size |
| 695 |
<< ", to=" << target << ", from=" << source << ")\n"; |
| 696 |
# endif |
| 697 |
|
| 698 |
unsigned char* maskp = desc->cmask.get(); |
| 699 |
|
| 700 |
/* copy all bits set in cmask from source to target */ |
| 701 |
for(i=0; i<desc->size; i++) |
| 702 |
{ |
| 703 |
unsigned char negmask = *maskp^0xff; |
| 704 |
*t = (*s & *maskp) | (*t & negmask); |
| 705 |
|
| 706 |
t++; s++; maskp++; /* Paragon didn't manage postfix increment */ |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
void ObjCopyGlobalData (TYPE_DESC *desc, |
| 712 |
DDD_OBJ target, DDD_OBJ source, size_t size) |
| 713 |
{ |
| 714 |
/* |
| 715 |
normally size will be equal to desc->size (for fixed-sized objects). |
| 716 |
for variable-sized objects size depends on what the sender put into |
| 717 |
the message |
| 718 |
*/ |
| 719 |
|
| 720 |
CopyByMask(desc, target, source); |
| 721 |
|
| 722 |
/* copy remainder as EL_GDATA */ |
| 723 |
if (size > desc->size) |
| 724 |
{ |
| 725 |
memcpy(((char *)target)+desc->size, |
| 726 |
((char *)source)+desc->size, size-desc->size); |
| 727 |
} |
| 728 |
|
| 729 |
# ifdef DebugCreation |
| 730 |
const auto& hdr = OBJ2HDR(target, desc); |
| 731 |
Dune::dinfo |
| 732 |
<< "ObjCopyGlobalData(" << hdr << " <- " << source << ", size=" << size |
| 733 |
<< "), TYP=" << OBJ_TYPE(hdr) << " GID=" << OBJ_GID(hdr) |
| 734 |
<< " INDEX=" << OBJ_INDEX(hdr) << "\n"; |
| 735 |
# endif |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
/****************************************************************************/ |
| 741 |
|
| 742 |
DDD_HDR DDD_SearchHdr(DDD::DDDContext& context, DDD_GID gid) |
| 743 |
{ |
| 744 |
auto& objTable = context.objTable(); |
| 745 |
const int nObjs = context.nObjs(); |
| 746 |
int i; |
| 747 |
|
| 748 |
i=0; |
| 749 |
while (i < nObjs && OBJ_GID(objTable[i])!=gid) |
| 750 |
i++; |
| 751 |
|
| 752 |
if (i < nObjs) |
| 753 |
{ |
| 754 |
return(objTable[i]); |
| 755 |
} |
| 756 |
else |
| 757 |
return(NULL); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
/****************************************************************************/ |
| 762 |
|
| 763 |
|
| 764 |
void ddd_ObjMgrInit(DDD::DDDContext& context) |
| 765 |
{ |
| 766 |
auto& ctx = context.objmgrContext(); |
| 767 |
|
| 768 |
/* sanity check: does the DDD_PROC type have enough bits? */ |
| 769 |
if (sizeof(DDD_PROC)*8 < MAX_PROCBITS_IN_GID) |
| 770 |
DDD_PrintError('F', 666, "DDD_PROC isn't large enough for MAX_PROCBITS_IN_GID bits"); |
| 771 |
|
| 772 |
ctx.theIdCount = 1; /* start with 1, for debugging reasons */ |
| 773 |
|
| 774 |
/* allocate first (smallest) object table */ |
| 775 |
context.objTable().resize(MAX_OBJ_START); |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
void ddd_ObjMgrExit(DDD::DDDContext& context) |
| 780 |
{ |
| 781 |
context.objTable().clear(); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
/****************************************************************************/ |
| 786 |
|
| 787 |
END_UGDIM_NAMESPACE |