| 1 | /* |
| 2 | $Header: d:/cvsroot/tads/TADS2/OBJ.H,v 1.3 1999/07/11 00:46:30 MJRoberts Exp $ |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved. |
| 7 | * |
| 8 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 9 | * on using and copying this software. |
| 10 | */ |
| 11 | /* |
| 12 | Name |
| 13 | obj.h - object definitions |
| 14 | Function |
| 15 | Defines interface for object handling routines |
| 16 | Notes |
| 17 | An object is divided into two parts: a "static" part and a "dynamic" |
| 18 | part. The static part contains all of the properties originally |
| 19 | defined in the object; none of these properties can be deleted, but |
| 20 | they can have a flag set to indicate that they are to be ignored, |
| 21 | which effectively deletes them. The dynamic part changes at run-time, |
| 22 | with properties deleted and changed as requested. |
| 23 | |
| 24 | The static part is kept around at all times to allow objects to revert |
| 25 | back to their original state with very little work. For TADS games, |
| 26 | this allows objects to be restored to their pre-play state after game |
| 27 | play, without requiring a separate snapshot of the objects. In |
| 28 | addition, it allows object state to be saved in a very compact format, |
| 29 | because only the object header and dynamic portion need to be written; |
| 30 | the static portion, which may contain a large amount of code, need not |
| 31 | be saved when saving object state. |
| 32 | Modified |
| 33 | 08/11/91 MJRoberts - creation |
| 34 | */ |
| 35 | |
| 36 | #ifndef OBJ_INCLUDED |
| 37 | #define OBJ_INCLUDED |
| 38 | |
| 39 | #ifndef PRP_INCLUDED |
| 40 | #include "prp.h" |
| 41 | #endif |
| 42 | #ifndef DAT_INCLUDED |
| 43 | #include "dat.h" |
| 44 | #endif |
| 45 | #ifndef MCM_INCLUDED |
| 46 | #include "mcm.h" |
| 47 | #endif |
| 48 | #ifndef ERR_INCLUDED |
| 49 | #include "err.h" |
| 50 | #endif |
| 51 | |
| 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
| 56 | /* object number */ |
| 57 | typedef ushort objnum; |
| 58 | |
| 59 | /* |
| 60 | * For non-class objects, we'll leave some space free in the object so |
| 61 | * that a few properties can be added without having to resize the |
| 62 | * object. Class objects will probably never have anything added, so |
| 63 | * there's no need for extra space. |
| 64 | */ |
| 65 | #define OBJEXTRA 64 |
| 66 | |
| 67 | /* object structure */ |
| 68 | /* |
| 69 | struct objdef |
| 70 | { |
| 71 | ushort objws; /o workspace object was defined in o/ |
| 72 | ushort objflg; /o object flags o/ |
| 73 | ushort objnsc; /o number of superclasses o/ |
| 74 | ushort objnprop; /o number of properties defined in object o/ |
| 75 | ushort objfree; /o offset of next free byte in property area o/ |
| 76 | ushort objrst; /o reset point - offset of end of 'static' part o/ |
| 77 | ushort objstat; /o number of 'static' properties o/ |
| 78 | objnum objsc[1]; /o superclasses o/ |
| 79 | }; |
| 80 | typedef struct objdef objdef; |
| 81 | */ |
| 82 | # define OBJFCLS 0x01 /* object is a class */ |
| 83 | |
| 84 | /* |
| 85 | The object structure is actually laid out portably, using unaligned |
| 86 | 2-byte arrays, stored least significant byte first, for each ushort |
| 87 | (including the objnum array for the superclasses). The actual |
| 88 | entries are at these offsets on all machines: |
| 89 | |
| 90 | objws 0 |
| 91 | objflg 2 |
| 92 | objnsc 4 |
| 93 | objnprop 6 |
| 94 | objfree 8 |
| 95 | objrst 10 |
| 96 | objstat 12 |
| 97 | objsc[0] 14 |
| 98 | objsc[1] 16 |
| 99 | etc |
| 100 | |
| 101 | If the OBJFINDEX flag is set, the object has a property index. |
| 102 | The index occurs after the last superclass (so it's where the |
| 103 | property data would go if there were no index), and the property |
| 104 | data follows. Each index entry consists of a pair of two-byte |
| 105 | entries: the first is the property number, and the second is |
| 106 | its offset within the object. For performance reasons, an index |
| 107 | is only built on a class object -- whenever a property is changed |
| 108 | within an object, the entire index must be rebuilt, because the |
| 109 | locations of many properties within the object can be changed by |
| 110 | a single property change in the object. The index is ordered by |
| 111 | property number, so it can be searched using a binary search. |
| 112 | Furthermore, "ignored" properties are excluded from the index; |
| 113 | only the active instance of a particular property is stored. |
| 114 | The index must be maintained by all routines that can change |
| 115 | property information: setp, delp, revert, etc. |
| 116 | |
| 117 | Preceding the index table is a two-byte entry that gives the |
| 118 | offset of the properties. Since the properties immediately |
| 119 | follow the index, this can be used to deduce how large a space |
| 120 | is available for the index itself. |
| 121 | */ |
| 122 | typedef uchar objdef; |
| 123 | #define OBJDEFSIZ 14 /* "sizeof(objdef)" - size of object header w/o sc's */ |
| 124 | |
| 125 | /* object flags */ |
| 126 | #define OBJFCLASS 1 /* object is a class */ |
| 127 | #define OBJFINDEX 2 /* object has a property index */ |
| 128 | #define OBJFMOD 4 /* object has been modified by a newer definition */ |
| 129 | |
| 130 | /* undo context */ |
| 131 | struct objucxdef |
| 132 | { |
| 133 | mcmcxdef *objucxmem; /* cache manager context */ |
| 134 | errcxdef *objucxerr; /* error context */ |
| 135 | ushort objucxsiz; /* size of the undo buffer */ |
| 136 | ushort objucxhead; /* head (position of next write) */ |
| 137 | ushort objucxtail; /* tail (position of oldest record) */ |
| 138 | ushort objucxprv; /* previous head pointer */ |
| 139 | ushort objucxtop; /* highest head value written */ |
| 140 | void (*objucxcun)(void *ctx, uchar *data); |
| 141 | /* apply a client undo record */ |
| 142 | ushort (*objucxcsz)(void *ctx, uchar *data); |
| 143 | /* get size of a client undo record */ |
| 144 | void *objucxccx; /* client undo context */ |
| 145 | uchar objucxbuf[1]; /* undo buffer */ |
| 146 | }; |
| 147 | typedef struct objucxdef objucxdef; |
| 148 | |
| 149 | /* |
| 150 | * Undo records are kept in a circular buffer allocated as part of an |
| 151 | * undo context. Offsets within the buffer are kept for the head, tail, |
| 152 | * and previous head records. The head always points to the byte at |
| 153 | * which the next undo record will be written. The previous head points |
| 154 | * to the most recently written undo record; it contains a back link to |
| 155 | * the undo record before that, and so forth back through the entire |
| 156 | * chain. (These reverse links are necessary because undo records vary |
| 157 | * in size depending on the data contained within.) The tail points to |
| 158 | * the oldest undo record that's still in the buffer. Conceptually, the |
| 159 | * head is always "above" the tail in the buffer; since the buffer is |
| 160 | * circular, the tail may have a higher address, but this just means |
| 161 | * that the buffer wraps around at the top. When the head bumps into |
| 162 | * the tail (i.e., the head address is physically below or equal to the |
| 163 | * tail address, and the head is then advanced so that its address |
| 164 | * becomes higher than the tail's), the tail is advanced by discarding |
| 165 | * as many of the least recent undo records as necessary to make room |
| 166 | * for the new head position. When the head and the previous head point |
| 167 | * to the same place, we have no undo records in the buffer. |
| 168 | */ |
| 169 | /* |
| 170 | * The first byte of an undo record specifies what action is to be |
| 171 | * undone. If a property was added, it is undone merely by deleting the |
| 172 | * property. If a property was changed, it is undone by setting the |
| 173 | * property back to its old value. An additional special flag indicates |
| 174 | * a "savepoint." Normally, all changes back to a savepoint will be |
| 175 | * undone. |
| 176 | */ |
| 177 | #define OBJUADD 1 /* a property was added (undo by deleting) */ |
| 178 | #define OBJUCHG 2 /* a property was changed (change back to old value) */ |
| 179 | #define OBJUSAV 3 /* savepoint marker (no property information) */ |
| 180 | #define OBJUOVR 4 /* override original property (set orig to IGNORE) */ |
| 181 | #define OBJUCLI 5 /* client undo record (any client data) */ |
| 182 | |
| 183 | /* |
| 184 | * After the control byte (OBJUxxx), the object number, property |
| 185 | * number, datatype, and data value will follow; some or all of these |
| 186 | * may be omitted, depending on the control byte. |
| 187 | */ |
| 188 | |
| 189 | /* get object flags */ |
| 190 | #define objflg(o) ((ushort)osrp2(((char *)(o)) + 2)) |
| 191 | |
| 192 | /* get object flags */ |
| 193 | #define objsflg(o, val) oswp2(((char *)(o)) + 2, val) |
| 194 | |
| 195 | /* given an object pointer, get a pointer to the first prpdef */ |
| 196 | /* prpdef *objprp(objdef *objptr); */ |
| 197 | #define objprp(o) ((prpdef *)(objsc(o) + 2*objnsc(o))) |
| 198 | |
| 199 | /* given an object pointer, get number of properties in the prpdef */ |
| 200 | /* int objnprop(objdef *objptr); */ |
| 201 | #define objnprop(o) ((ushort)osrp2(((char *)(o)) + 6)) |
| 202 | |
| 203 | /* set number of properties */ |
| 204 | /* void objsnp(objdef *objptr, int newnum); */ |
| 205 | #define objsnp(o,n) oswp2(((char *)(o)) + 6, n) |
| 206 | |
| 207 | /* given an object pointer, get offset of free space */ |
| 208 | /* int objfree(objdef *objptr); */ |
| 209 | #define objfree(o) ((ushort)osrp2(((char *)(o)) + 8)) |
| 210 | |
| 211 | /* set free space pointer */ |
| 212 | /* void objsfree(objdef *objptr, int newfree); */ |
| 213 | #define objsfree(o,n) oswp2(((char *)(o)) + 8, n) |
| 214 | |
| 215 | /* get number of static properties */ |
| 216 | /* ushort objstat(objdef *objptr); */ |
| 217 | #define objstat(o) ((ushort)osrp2(((char *)(o)) + 10)) |
| 218 | |
| 219 | /* set number of static properties */ |
| 220 | /* void objsetst(objdef *objptr, int newstat); */ |
| 221 | #define objsetst(o,n) oswp2(((char *)(o)) + 10, n) |
| 222 | |
| 223 | /* get reset size (size of static properties) */ |
| 224 | /* ushort objrst(objdef *objptr); */ |
| 225 | #define objrst(o) ((ushort)osrp2(((char *)(o)) + 12)) |
| 226 | |
| 227 | /* set reset size */ |
| 228 | /* void objsetrst(objdef *objptr, uint newrst); */ |
| 229 | #define objsetrst(o,n) oswp2(((char *)(o)) + 12, n) |
| 230 | |
| 231 | /* given an object pointer, get first superclass pointer */ |
| 232 | /* uchar *objsc(objdef *objptr); */ |
| 233 | #define objsc(o) (((uchar *)(o)) + OBJDEFSIZ) |
| 234 | |
| 235 | /* given an object pointer, get number of superclasses */ |
| 236 | /* int objnsc(objdef *objptr); */ |
| 237 | #define objnsc(o) ((ushort)osrp2(((char *)(o)) + 4)) |
| 238 | |
| 239 | /* set number of superclasses */ |
| 240 | /* void objsnsc(objdef *objptr, int num); */ |
| 241 | #define objsnsc(o,n) oswp2(((char *)(o)) + 4, n) |
| 242 | |
| 243 | /* given a prpdef, get the next prpdef */ |
| 244 | /* prpdef *objpnxt(prpdef *p); */ |
| 245 | #define objpnxt(p) \ |
| 246 | ((prpdef *)(((uchar *)(p)) + PRPHDRSIZ + prpsize(p))) |
| 247 | |
| 248 | /* get pointer to free prpdef */ |
| 249 | /* prpdef *objpfre(objdef *objptr); */ |
| 250 | #define objpfre(o) ((prpdef *)(((uchar *)(o)) + objfree(o))) |
| 251 | |
| 252 | /* given a prpdef and an object pointer, compute the prpdef offset */ |
| 253 | /* uint objpofs(objdef *objptr, prpdef *propptr); */ |
| 254 | #define objpofs(o,p) ((uint)((p) ? (((uchar *)(p)) - ((uchar *)(o))) : 0)) |
| 255 | |
| 256 | /* given an object pointer and a property offset, get prpdef pointer */ |
| 257 | /* prpdef *objofsp(objdef *objptr, uint propofs); */ |
| 258 | #define objofsp(o,ofs) ((prpdef *)((ofs) ? (((uchar *)(o)) + (ofs)) : 0)) |
| 259 | |
| 260 | /* |
| 261 | * Get the first superclass of an object. If it doesn't have any |
| 262 | * superclasses, return invalid. |
| 263 | */ |
| 264 | objnum objget1sc(mcmcxdef *ctx, objnum objn); |
| 265 | |
| 266 | /* |
| 267 | * Get an object's property WITHOUT INHERITANCE. If the object has the |
| 268 | * indicated property set, the byte OFFSET of the prpdef within the |
| 269 | * object is returned. The offset will remain valid until any type of |
| 270 | * operation that sets a property in the object (such as objdelp, |
| 271 | * objsetp, or an undo operation). An offset of zero means that the |
| 272 | * property was not set in the object. |
| 273 | */ |
| 274 | uint objgetp(mcmcxdef *ctx, objnum objn, prpnum prop, |
| 275 | dattyp *typptr); |
| 276 | |
| 277 | /* |
| 278 | * Get the *ending* offset of the given property's value, without any |
| 279 | * inheritance. Returns the byte offset one past the end of the |
| 280 | * property's data. |
| 281 | */ |
| 282 | uint objgetp_end(mcmcxdef *ctx, objnum objn, prpnum prop); |
| 283 | |
| 284 | /* |
| 285 | * Get a property of an object, either from the object or from a |
| 286 | * superclass (inherited). If the inh flag is TRUE, we do not look |
| 287 | * at all in the object itself, but restrict our search to inherited |
| 288 | * properties only. We return the byte OFFSET of the prpdef within |
| 289 | * the object in which the prpdef is found; the superclass object |
| 290 | * itself is NOT locked upon return, but we will NOT unlock the |
| 291 | * object passed in. If the offset is zero, the property was not |
| 292 | * found. The offset returned is valid until any operation that |
| 293 | * sets a property in the object (such as objdelp, objsetp, or an |
| 294 | * undo operation). |
| 295 | */ |
| 296 | uint objgetap(mcmcxdef *ctx, noreg objnum objn, prpnum prop, |
| 297 | objnum *orn, int inh); |
| 298 | |
| 299 | /* |
| 300 | * expand an object by a requested amount, returning a pointer to the |
| 301 | * object's new location if it must be moved. The object will be |
| 302 | * unlocked and relocked by this call. On return, the actual amount |
| 303 | * of space ADDED to the object will be returned. |
| 304 | */ |
| 305 | objdef *objexp(mcmcxdef *ctx, objnum obj, ushort *siz); |
| 306 | |
| 307 | /* |
| 308 | * Set an object's property, deleting the original value of the |
| 309 | * property if it existed. If an undo context is provided, write an |
| 310 | * undo record for the change; if the undo context pointer is null, no |
| 311 | * undo information is retained. |
| 312 | */ |
| 313 | void objsetp(mcmcxdef *ctx, objnum obj, prpnum prop, |
| 314 | dattyp typ, void *val, objucxdef *undoctx); |
| 315 | |
| 316 | /* |
| 317 | * Delete a property. If mark_only is true, we'll only mark the |
| 318 | * property as deleted without actually reclaiming its space; this is |
| 319 | * necessary when removing a code property (type DAT_CODE) any time |
| 320 | * other code properties may follow, because p-code is not entirely |
| 321 | * self-relative and thus can't always be relocated within an object. |
| 322 | */ |
| 323 | void objdelp(mcmcxdef *mctx, objnum objn, prpnum prop, int mark_only); |
| 324 | |
| 325 | /* |
| 326 | * Set up for emitting code into an object. Writes a property header |
| 327 | * of type 'code', and returns the offset of the next free byte in the |
| 328 | * object. Call objendemt when done. The datatype argument is |
| 329 | * provided so that list generation can be done through the same |
| 330 | * mechanism, since parser lists must be converted to run-time |
| 331 | * lists via the code generator. |
| 332 | */ |
| 333 | uint objemt(mcmcxdef *ctx, objnum objn, prpnum prop, dattyp typ); |
| 334 | |
| 335 | /* done emitting code into property, finish setting object info */ |
| 336 | void objendemt(mcmcxdef *ctx, objnum objn, prpnum prop, uint endofs); |
| 337 | |
| 338 | /* |
| 339 | * Determine if undo records should be kept. Undo records should be |
| 340 | * kept only if a savepoint is present in the undo log. If no savepoint |
| 341 | * is present, adding undo records would be useless, since it will not |
| 342 | * be possible to apply the undo information. |
| 343 | */ |
| 344 | int objuok(objucxdef *undoctx); |
| 345 | |
| 346 | /* |
| 347 | * Reserve space in an undo buffer, deleting old records as needed. |
| 348 | * Returns a pointer to the reserved space. |
| 349 | */ |
| 350 | uchar *objures(objucxdef *undoctx, uchar cmd, ushort siz); |
| 351 | |
| 352 | /* advance the tail pointer in an undo buffer over the record it points to */ |
| 353 | void objutadv(objucxdef *undoctx); |
| 354 | |
| 355 | /* apply one undo record, and remove it from undo list */ |
| 356 | void obj1undo(mcmcxdef *mctx, objucxdef *undoctx); |
| 357 | |
| 358 | /* |
| 359 | * Undo back to the most recent savepoint. If there is no savepoint in |
| 360 | * the undo list, NOTHING will be undone. This prevents reaching an |
| 361 | * inconsistent state in which some, but not all, of the operations |
| 362 | * between two savepoints are undone: either all operations between two |
| 363 | * savepoints will be undone, or none will. |
| 364 | */ |
| 365 | void objundo(mcmcxdef *mctx, objucxdef *undoctx); |
| 366 | |
| 367 | /* set an undo savepoint */ |
| 368 | void objusav(objucxdef *undoctx); |
| 369 | |
| 370 | /* initialize undo context */ |
| 371 | objucxdef *objuini(mcmcxdef *memctx, ushort undosiz, |
| 372 | void (*undocb)(void *ctx, uchar *data), |
| 373 | ushort (*sizecb)(void *ctx, uchar *data), |
| 374 | void *callctx); |
| 375 | |
| 376 | /* free the undo context - releases memory allocated by objuini() */ |
| 377 | void objuterm(objucxdef *undoctx); |
| 378 | |
| 379 | /* discard all undo context (for times such as restarting) */ |
| 380 | void objulose(objucxdef *undoctx); |
| 381 | |
| 382 | /* |
| 383 | * Allocate and initialize a new object. The caller specifies the |
| 384 | * number of superclasses to be allocated, and the amount of space (in |
| 385 | * bytes) for the object's property data. The caller must fill in the |
| 386 | * superclass array. Upon return, the object is allocated and locked, |
| 387 | * and is initialized with no properties. A pointer to the object's |
| 388 | * memory is returned, and *objnptr receives the object number. |
| 389 | */ |
| 390 | objdef *objnew(mcmcxdef *mctx, int sccnt, ushort propspace, |
| 391 | objnum *objnptr, int classflg); |
| 392 | |
| 393 | /* initialize an already allocated object */ |
| 394 | void objini(mcmcxdef *mctx, int sccnt, objnum objn, int classflg); |
| 395 | |
| 396 | /* |
| 397 | * Add space for additional superclasses to an object. The object can |
| 398 | * already have some properties set (if it doesn't, it can just be |
| 399 | * reinitialized). |
| 400 | */ |
| 401 | void objaddsc(mcmcxdef *mctx, int sccnt, objnum objn); |
| 402 | |
| 403 | /* |
| 404 | * Delete an object's properties and superclasses. The 'mindel' |
| 405 | * parameter specifies the minimum property number to be deleted. |
| 406 | * Properties below this are considered "system" properties that are not |
| 407 | * to be deleted. This could be used by a development environment to |
| 408 | * store the source for an object as a special system property in the |
| 409 | * object; when the object is recompiled, all of the object's properties |
| 410 | * and superclasses must be deleted except the source property, which is |
| 411 | * retained even after recompilation. |
| 412 | */ |
| 413 | void objclr(mcmcxdef *mctx, objnum objn, prpnum mindel); |
| 414 | |
| 415 | /* Build or rebuild an object's property index */ |
| 416 | void objindx(mcmcxdef *mctx, objnum objn); |
| 417 | |
| 418 | /* set up just-compiled object: mark static part and original properties */ |
| 419 | void objcomp(mcmcxdef *mctx, objnum objn, int for_debug); |
| 420 | |
| 421 | /* revert an object to original post-compilation state */ |
| 422 | void objrevert(void *mctx, mcmon objn); |
| 423 | |
| 424 | /* reset 'ignore' flags for a newly reconstructed object */ |
| 425 | void objsetign(mcmcxdef *mctx, objnum objn); |
| 426 | |
| 427 | #ifdef __cplusplus |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | #endif /* OBJ_INCLUDED */ |