Debian Codegraph @

terps/tads/tads2/obj.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/OBJ.C,v 1.2 1999/05/17 02:52:12 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 obj.c - object manipulation
15 Function
16 Operations on TADS objects
17 Notes
18 A TADS object is stored in a single cache object. The object is laid
19 out in memory as follows:
20
21 {
22 ushort workspace - workspace in which object is defined
23 ushort flags - object flags
24 ushort sc_count - number of superclasses
25 ushort prop_used - number of entries actually used
26 ushort free_offset - offset of next free byte in prop area
27 objnum sclist[sc_count] - array of superclass object numbers
28 prpdef proplist[prop_max] - array of properties of the object
29 }
30
31 The number of superclasses is defined when the object is compiled,
32 and does not change at runtime. Properties can be added and replaced
33 at runtime; the object's cache object may need to be resized from
34 time to time to accomdate added or expanded property values.
35
36 Objects are stored in a portable format. Each ushort (including
37 the objnum) in the object header is stored as a 2-byte array,
38 least significant byte first; the array is stored with no padding
39 for alignment.
40
41 Each property is defined by a prpdef structure (see prp.h).
42
43 The meaning of propval varies by datatype. For DAT_NUMBER,
44 the value is the actual number in question. For DAT_OBJECT, it
45 is the object number of the object. For DAT_NIL and DAT_TRUE,
46 there is no value. For DAT_SSTRING, DAT_CODE, DAT_LIST, and
47 DAT_DSTRING, the value is stored immediately following the prpdef,
48 and takes up the number of bytes given in the prpsize field.
49 Modified
50 08/11/91 MJRoberts - creation
51 */
52
53 #include "os.h"
54 #include "obj.h"
55 #include "prp.h"
56 #include "dat.h"
57 #include "mch.h"
58 #include "mcm.h"
59 #include "err.h"
60
61 /*
62 * Get a property WITHOUT INHERITANCE. The offset of the property's
63 * prpdef is returned. An offset of zero means the property wasn't
64 * found.
65 */
66 uint objgetp(mcmcxdef *mctx, objnum objn, prpnum prop, dattyp *typptr)
67 {
68 objdef *objptr;
69 prpdef *p;
70 int cnt;
71 uint retval; /* property offset, if we find it */
72 uint ignprop; /* ignored property - use if real property isn't found */
73 uchar pbuf[2]; /* property number in portable format */
74 uchar *indp;
75 uchar *indbase;
76 int last;
77 int first;
78 int cur;
79
80 oswp2(pbuf, prop); /* get property number in portable foramt */
81 objptr = (objdef *)mcmlck(mctx, objn); /* get a lock on the object */
82 ignprop = 0; /* assume we won't find ignored property */
83 cnt = objnprop(objptr); /* get number of properties defined */
84 retval = 0; /* presume failure */
85
86 if (objflg(objptr) & OBJFINDEX)
87 {
88 /* there's an index -> do a binary search through the index */
89 indbase = (uchar *)objpfre(objptr); /* find index */
90 first = 0;
91 last = cnt - 1;
92 for (;;)
93 {
94 if (first > last) break; /* crossed over -> not found */
95 cur = first + (last - first)/2; /* split the difference */
96 indp = indbase + cur*4; /* get pointer to this entry */
97 if (indp[0] == pbuf[0] && indp[1] == pbuf[1])
98 {
99 retval = osrp2(indp + 2);
100 break;
101 }
102 else if (indp[0] < pbuf[0]
103 || (indp[0] == pbuf[0] && indp[1] < pbuf[1]))
104 first = (cur == first ? first + 1 : cur);
105 else
106 last = (cur == last ? last - 1 : cur);
107 }
108
109 /* ignore ignored and deleted properties if possible */
110 while (retval
111 && ((prpflg(objptr + retval) & PRPFIGN) != 0
112 || ((prpflg(objptr + retval) & PRPFDEL) != 0
113 && (mctx->mcmcxflg & MCMCXF_NO_PRP_DEL) == 0))
114 && cur < cnt && indp[0] == indp[4] && indp[1] == indp[5])
115 {
116 indp += 4;
117 retval = osrp2(indp + 2);
118 }
119 if (retval && osrp2(objptr + retval) != prop)
120 assert(FALSE);
121 }
122 else
123 {
124 /* there's no index -> do sequential search through properties */
125 for (p = objprp(objptr) ; cnt ; p = objpnxt(p), --cnt)
126 {
127 /* if this is the property, and it's not being ignored, use it */
128 if (*(uchar *)p == pbuf[0] && *(((uchar *)p) + 1) == pbuf[1])
129 {
130 if (prpflg(p) & PRPFIGN) /* this is ignored */
131 ignprop = objpofs(objptr, p); /* ... make a note of it */
132 else if ((prpflg(p) & PRPFDEL) != 0 /* it's deleted */
133 && (mctx->mcmcxflg & MCMCXF_NO_PRP_DEL) == 0)
134 /* simply skip it */ ;
135 else
136 {
137 retval = objpofs(objptr, p); /* this is the one */
138 break; /* we're done */
139 }
140 }
141 }
142 }
143
144 if (!retval) retval = ignprop; /* use ignored value if nothing else */
145 if (retval && typptr) *typptr = prptype(objofsp(objptr, retval));
146
147 mcmunlck(mctx, objn); /* done with object, so unlock it */
148 return(retval);
149 }
150
151 /* get the offset of the end of a property in an object */
152 uint objgetp_end(mcmcxdef *ctx, objnum objn, prpnum prop)
153 {
154 objdef *objp;
155 prpdef *propptr;
156 uint ofs;
157 uint valsiz;
158
159 /* get the start of the object */
160 ofs = objgetp(ctx, objn, prop, 0);
161 if (ofs == 0)
162 return 0;
163
164 /* get the object */
165 objp = mcmlck(ctx, (mcmon)objn);
166
167 /* get the property */
168 propptr = objofsp(objp, ofs);
169
170 /* get the data size */
171 valsiz = prpsize(propptr);
172
173 /* done with the object */
174 mcmunlck(ctx, (mcmon)objn);
175
176 /*
177 * return the ending offset - it's the starting offset plus the
178 * property header size plus the size of the property data
179 */
180 return ofs + PRPHDRSIZ + valsiz;
181 }
182
183 /* determine whether an object is a descendant of another object */
184 static int objisd(mcmcxdef *ctx, objdef *objptr, objnum parentnum)
185 {
186 uchar *sc;
187 int cnt;
188
189 for (sc = objsc(objptr), cnt = objnsc(objptr) ; cnt ;
190 sc += 2, --cnt)
191 {
192 int cursc = osrp2(sc);
193 int ret;
194 objdef *curptr;
195
196 if (cursc == parentnum) return(TRUE);
197
198 curptr = (objdef *)mcmlck(ctx, (mcmon)cursc);
199 ret = objisd(ctx, curptr, parentnum);
200 mcmunlck(ctx, (mcmon)cursc);
201 if (ret) return(TRUE);
202 }
203 return(FALSE);
204 }
205
206 /*
207 * Get a property of an object, either from the object or from a
208 * superclass (inherited). If the inh flag is TRUE, we do not look at
209 * all in the object itself, but restrict our search to inherited
210 * properties only. We return the byte offset of the prpdef within the
211 * object in which the prpdef is found; the superclass object itself is
212 * NOT locked upon return, but we will NOT unlock the object passed in
213 * (in other words, all object locking status is the same as it was on
214 * entry). If the offset is zero, the property was not found.
215 *
216 * This is an internal helper routine - it's not meant to be called
217 * except by objgetap().
218 */
219 static uint objgetap0(mcmcxdef *ctx, noreg objnum obj, prpnum prop,
220 objnum *orn, int inh, dattyp *ortyp)
221 {
222 uchar *sc;
223 ushort sccnt;
224 ushort psav;
225 dattyp typsav = DAT_NIL;
226 objnum osavn = MCMONINV;
227 uchar *o1;
228 objnum o1n;
229 ushort poff;
230 int found;
231 uint retval;
232 dattyp typ;
233 uchar sclist[100]; /* up to 50 superclasses */
234 objdef *objptr;
235
236 NOREG((&obj))
237
238 /* see if the property is in the current object first */
239 if (!inh && (retval = objgetp(ctx, obj, prop, &typ)) != 0)
240 {
241 /*
242 * tell the caller which object this came from, if the caller
243 * wants to know
244 */
245 if (orn != 0)
246 *orn = obj;
247
248 /* if the caller wants to know the type, return it */
249 if (ortyp != 0)
250 *ortyp = typ;
251
252 /* return the property offset */
253 return retval;
254 }
255
256 /* lock the object, cache its superclass list, and unlock it */
257 objptr = (objdef *)mcmlck(ctx, (mcmon)obj);
258 sccnt = objnsc(objptr);
259 memcpy(sclist, objsc(objptr), (size_t)(sccnt << 1));
260 sc = sclist;
261 mcmunlck(ctx, (mcmon)obj);
262
263 /* try to inherit the property */
264 for (found = FALSE ; sccnt != 0 ; sc += 2, --sccnt)
265 {
266 /* recursively look up the property in this superclass */
267 poff = objgetap0(ctx, (objnum)osrp2(sc), prop, &o1n, FALSE, &typ);
268
269 /* if we found the property, remember it */
270 if (poff != 0)
271 {
272 int isdesc;
273
274 /* if we have a previous object, determine lineage */
275 if (found)
276 {
277 o1 = mcmlck(ctx, o1n);
278 isdesc = objisd(ctx, o1, osavn);
279 mcmunlck(ctx, o1n);
280 }
281
282 /*
283 * if we don't already have a property, or the new object
284 * is a descendant of the previously found object (meaning
285 * that the new object's property should override the
286 * previously found object's property), use this new
287 * property
288 */
289 if (!found || isdesc)
290 {
291 psav = poff;
292 osavn = o1n;
293 typsav = typ;
294 found = TRUE;
295 }
296 }
297 }
298
299 /* set return pointer and return the offset of what we found */
300 if (orn != 0)
301 *orn = osavn;
302
303 /* return the object type if the caller wanted it */
304 if (ortyp != 0)
305 *ortyp = typsav;
306
307 /* return the offset of the property if we found one, or zero if not */
308 return (found ? psav : 0);
309 }
310
311 /*
312 * Get a property of an object, either from the object or from a
313 * superclass (inherited). If the inh flag is TRUE, we do not look at
314 * all in the object itself, but restrict our search to inherited
315 * properties only. We return the byte offset of the prpdef within the
316 * object in which the prpdef is found; the superclass object itself is
317 * NOT locked upon return, but we will NOT unlock the object passed in
318 * (in other words, all object locking status is the same as it was on
319 * entry). If the offset is zero, the property was not found.
320 */
321 uint objgetap(mcmcxdef *ctx, noreg objnum obj, prpnum prop,
322 objnum *ornp, int inh)
323 {
324 uint retval;
325 dattyp typ;
326 objnum orn;
327
328 /*
329 * even if the caller doesn't care about the original object number,
330 * we do, so provide our own location to store it if necessary
331 */
332 if (ornp == 0)
333 ornp = &orn;
334
335 /* keep going until we've finished translating synonyms */
336 for (;;)
337 {
338 /* look up the property */
339 retval = objgetap0(ctx, obj, prop, ornp, inh, &typ);
340
341 /*
342 * If we found something (i.e., retval != 0), check to see if we
343 * have a synonym; if so, synonym translation is required
344 */
345 if (retval != 0 && typ == DAT_SYN)
346 {
347 prpnum prvprop;
348 objdef *objptr;
349 prpdef *p;
350
351 /*
352 * Translation is required - get new property and try again.
353 * First, remember the original property, so we can make
354 * sure we're not going to loop (at least, not in this one
355 * synonym definition).
356 */
357 prvprop = prop;
358
359 objptr = (objdef *)mcmlck(ctx, (mcmon)*ornp);
360 p = objofsp(objptr, retval);
361 prop = osrp2(prpvalp(p));
362 mcmunlck(ctx, (mcmon)*ornp);
363
364 /* check for direct circularity */
365 if (prop == prvprop)
366 errsig(ctx->mcmcxgl->mcmcxerr, ERR_CIRCSYN);
367
368 /* go back for another try with the new property */
369 continue;
370 }
371
372 /* we don't have to perform a translation; return the result */
373 return retval;
374 }
375 }
376
377
378 /*
379 * Expand an object by a requested size, and return a pointer to the
380 * object's location. The object will be unlocked and relocked by this
381 * call. The new size is written to the *siz argument.
382 */
383 objdef *objexp(mcmcxdef *ctx, objnum obj, ushort *siz)
384 {
385 ushort oldsiz;
386 uchar *p;
387
388 oldsiz = mcmobjsiz(ctx, (mcmon)obj);
389 p = mcmrealo(ctx, (mcmon)obj, (ushort)(oldsiz + *siz));
390 *siz = mcmobjsiz(ctx, (mcmon)obj) - oldsiz;
391 return((objdef *)p);
392 }
393
394 /*
395 * Delete a property in an object. Note that we never actually remove
396 * anything marked as an original property, but just mark it 'ignore'.
397 * This way, it's easy to restore the entire original state of the
398 * objects, simply by deleting everything not marked original and
399 * clearing the 'ignore' flag on the remaining properties. If
400 * 'mark_only' is true, we'll only mark the property as deleted without
401 * actually reclaiming the space; this is necessary when deleting a
402 * method when other methods may follow, since p-code is not entirely
403 * self-relative and thus can't always be relocated within an object.
404 */
405 void objdelp(mcmcxdef *mctx, objnum objn, prpnum prop, int mark_only)
406 {
407 objdef *objptr;
408 uint pofs;
409 prpdef *p;
410 prpdef *nxt;
411 size_t movsiz;
412
413 pofs = objgetp(mctx, objn, prop, (dattyp *)0); /* try to find property */
414 if (!pofs) return; /* not defined - nothing to delete */
415
416 objptr = (objdef *)mcmlck(mctx, objn); /* get lock on object */
417 p = objofsp(objptr, pofs); /* get actual prpdef pointer */
418 nxt = objpnxt(p); /* find next prpdef after this one */
419
420 /* if this is original, just mark 'ignore' */
421 if (prpflg(p) & PRPFORG)
422 {
423 prpflg(p) |= PRPFIGN; /* mark this as overridden */
424 }
425 else if (mark_only)
426 {
427 prpflg(p) |= PRPFDEL; /* mark as deleted without removing space */
428 }
429 else
430 {
431 /* move prpdef's after current one down over current one */
432 movsiz = (uchar *)objptr + objfree(objptr) - (uchar *)nxt;
433 memmove(p, nxt, movsiz);
434
435 objsnp(objptr, objnprop(objptr)-1);
436 objsfree(objptr, objfree(objptr) - (((uchar *)nxt) - ((uchar *)p)));
437 }
438
439 /* tell cache manager this object has been changed, and unlock it */
440 mcmtch(mctx, objn);
441 mcmunlck(mctx, objn);
442 }
443
444 /*
445 * Set a property of an object to a new value, overwriting the original
446 * value (if any); the object must be unlocked coming in. If an undo
447 * context is provided, an undo record is written; if the undo context
448 * pointer is null, no undo information is kept.
449 */
450 void objsetp(mcmcxdef *ctx, objnum objn, prpnum prop, dattyp typ,
451 void *val, objucxdef *undoctx)
452 {
453 objdef *objptr;
454 prpdef *p;
455 uint pofs;
456 uint siz;
457 ushort newsiz;
458 int indexed;
459 int prop_was_set;
460
461 /* get a lock on the object */
462 objptr = (objdef *)mcmlck(ctx, objn);
463 indexed = objflg(objptr) & OBJFINDEX;
464
465 /* catch any errors so we can unlock the object */
466 ERRBEGIN(ctx->mcmcxgl->mcmcxerr)
467 {
468 /* get the previous value of the property, if any */
469 pofs = objgetp(ctx, objn, prop, (dattyp *)0);
470 p = objofsp(objptr, pofs);
471 prop_was_set = (p != 0);
472
473 /* start the undo record if we are keeping undo information */
474 if (undoctx && objuok(undoctx))
475 {
476 uchar *up;
477 uchar cmd;
478
479 if (p)
480 {
481 if (prpflg(p) & PRPFORG)
482 {
483 cmd = OBJUOVR; /* override original */
484 p = (prpdef *)0; /* pretend it doesn't even exist */
485 }
486 else cmd = OBJUCHG; /* change property */
487 }
488 else cmd = OBJUADD; /* prop didn't exist - adding it */
489
490 /* write header, reserve space, and get a pointer to the space */
491 up = objures(undoctx, cmd,
492 (ushort)(sizeof(mcmon) + sizeof(prpnum)
493 + (p ? PRPHDRSIZ + prpsize(p) : 0)));
494
495 /* write the object and property numbers */
496 memcpy(up, &objn, (size_t)sizeof(objn));
497 up += sizeof(mcmon);
498 memcpy(up, &prop, (size_t)sizeof(prop));
499 up += sizeof(prop);
500
501 /* if there's existing data, write it */
502 if (p)
503 {
504 memcpy(up, p, (size_t)(PRPHDRSIZ + prpsize(p)));
505 up += PRPHDRSIZ + prpsize(p);
506 }
507
508 /* update the undo context's head offset for the new value */
509 undoctx->objucxhead = up - undoctx->objucxbuf;
510 }
511
512 /* get the size of the data */
513 siz = datsiz(typ, val);
514
515 /*
516 * If the property is already set, and the new data fits, use the
517 * existing slot. However, do not use existing slot if it's
518 * in the non-mutable portion of the object.
519 */
520 if (!p || (uint)prpsize(p) < siz || pofs < (uint)objrst(objptr))
521 {
522 uint avail;
523
524 /* delete any existing value */
525 if (prop_was_set)
526 objdelp(ctx, objn, prop, FALSE);
527
528 /* get the top of the property area */
529 p = objpfre(objptr);
530
531 /* make sure there's room at the top */
532 avail = mcmobjsiz(ctx, (mcmon)objn) - objfree(objptr);
533 if (avail < siz + PRPHDRSIZ)
534 {
535 newsiz = 64 + ((objfree(objptr) + siz + PRPHDRSIZ) -
536 mcmobjsiz(ctx, (mcmon)objn));
537 objptr = objexp(ctx, objn, &newsiz);
538 p = objpfre(objptr); /* reset pointer if object moved */
539 /* NOTE! Index (if present) is now invalid! */
540 }
541
542 prpsetsize(p, siz); /* set the new property size */
543 prpsetprop(p, prop); /* ... and property id */
544 prpflg(p) = 0; /* no property flags yet */
545 objsnp(objptr, objnprop(objptr) + 1); /* one more prop */
546 objsfree(objptr, objfree(objptr) + siz + PRPHDRSIZ);
547 }
548
549 /* copy the new data to top of object's free space */
550 prptype(p) = typ;
551 if (siz != 0) memcpy(prpvalp(p), val, (size_t)siz);
552 }
553 ERRCLEAN(ctx->mcmcxgl->mcmcxerr)
554 {
555 mcmunlck(ctx, objn); /* unlock the object */
556 }
557 ERRENDCLN(ctx->mcmcxgl->mcmcxerr)
558
559 /* dirty the object, and release lock on object before return */
560 mcmtch(ctx, objn); /* mark the object as changed */
561 mcmunlck(ctx, objn); /* unlock it */
562
563 /* if necessary, rebuild the property index */
564 if (indexed) objindx(ctx, objn);
565 }
566
567 /* set an undo savepoint */
568 void objusav(objucxdef *undoctx)
569 {
570 /* the only thing in this record is the OBJUSAV header */
571 objures(undoctx, OBJUSAV, (ushort)0);
572 }
573
574 /* reserve space in an undo buffer, and write header */
575 uchar *objures(objucxdef *undoctx, uchar cmd, ushort siz)
576 {
577 ushort prv;
578 uchar *p;
579
580 /* adjust size to include header information */
581 siz += 1 + sizeof(ushort);
582
583 /* make sure there's enough room overall for the record */
584 if (siz > undoctx->objucxsiz) errsig(undoctx->objucxerr, ERR_UNDOVF);
585
586 /* if there's no information, reset buffers */
587 if (undoctx->objucxhead == undoctx->objucxprv)
588 {
589 undoctx->objucxhead = undoctx->objucxprv = undoctx->objucxtail = 0;
590 undoctx->objucxtop = 0;
591 goto done;
592 }
593
594 /* if tail is below head, we can use to top of entire buffer */
595 if (undoctx->objucxtail < undoctx->objucxhead)
596 {
597 /* if there's enough space left after head, we're done */
598 if (undoctx->objucxsiz - undoctx->objucxhead >= siz)
599 goto done;
600
601 /* insufficient space: wrap head down to bottom of buffer */
602 undoctx->objucxtop = undoctx->objucxprv; /* last was top */
603 undoctx->objucxhead = 0;
604 }
605
606 /* head is below tail: delete records until we have enough room */
607 while (undoctx->objucxtail - undoctx->objucxhead < siz)
608 {
609 objutadv(undoctx);
610
611 /* if the tail wrapped, advancing won't do any more good */
612 if (undoctx->objucxtail <= undoctx->objucxhead)
613 {
614 /* if there's enough room at the top, we're done */
615 if (undoctx->objucxsiz - undoctx->objucxhead >= siz)
616 goto done;
617
618 /* still not enough room; wrap the head this time */
619 undoctx->objucxtop = undoctx->objucxprv; /* last was top */
620 undoctx->objucxhead = 0;
621 }
622 }
623
624 done:
625 /* save back-link, and set objucxprv pointer to the new record */
626 prv = undoctx->objucxprv;
627 undoctx->objucxprv = undoctx->objucxhead;
628
629 /* write the header: command byte, back-link to previous record */
630 p = &undoctx->objucxbuf[undoctx->objucxhead];
631 *p++ = cmd;
632 memcpy(p, &prv, sizeof(prv));
633
634 /* advance the head pointer past the header */
635 undoctx->objucxhead += 1 + sizeof(prv);
636
637 /* set the high-water mark if we've exceeded the old one */
638 if (undoctx->objucxprv > undoctx->objucxtop)
639 undoctx->objucxtop = undoctx->objucxprv;
640
641 /* return the reserved space */
642 return &undoctx->objucxbuf[undoctx->objucxhead];
643 }
644
645 /* advance the undo tail pointer over the record it points to */
646 void objutadv(objucxdef *undoctx)
647 {
648 uchar *p;
649 ushort siz;
650 uchar pr[PRPHDRSIZ]; /* space for a property header */
651 uchar cmd;
652
653 /* if we're at the most recently written record, flush buffer */
654 if (undoctx->objucxtail == undoctx->objucxprv)
655 {
656 undoctx->objucxtail = 0;
657 undoctx->objucxprv = 0;
658 undoctx->objucxhead = 0;
659 undoctx->objucxtop = 0;
660 }
661
662 /* if we've reached high water mark, wrap back to bottom */
663 if (undoctx->objucxtail == undoctx->objucxtop)
664 {
665 undoctx->objucxtail = 0;
666 return;
667 }
668
669 /* determine size by inspecting current record */
670 p = undoctx->objucxbuf + undoctx->objucxtail;
671 siz = 1 + sizeof(ushort); /* basic header size */
672
673 cmd = *p++;
674 p += sizeof(ushort); /* skip the previous pointer */
675
676 switch(cmd)
677 {
678 case OBJUCHG:
679 /* change: property header (added below) plus data value */
680 memcpy(pr, p + sizeof(mcmon) + sizeof(prpnum), (size_t)PRPHDRSIZ);
681 siz += PRPHDRSIZ + prpsize(pr);
682 /* FALLTHROUGH */
683
684 case OBJUADD:
685 case OBJUOVR:
686 /* add/override: property header only */
687 siz += sizeof(mcmon) + sizeof(prpnum);
688 break;
689
690 case OBJUCLI:
691 siz += (*undoctx->objucxcsz)(undoctx->objucxccx, p);
692 break;
693
694 case OBJUSAV:
695 break;
696 }
697
698 undoctx->objucxtail += siz;
699 }
700
701 /* undo one undo record, and remove it from the undo list */
702 void obj1undo(mcmcxdef *mctx, objucxdef *undoctx)
703 {
704 uchar *p;
705 prpnum prop;
706 objnum objn;
707 uchar cmd;
708 uchar pr[PRPHDRSIZ]; /* space for property header */
709 ushort prv;
710 ushort pofs;
711 objdef *objptr;
712 int indexed;
713
714 /* if there's no more undo, signal an error */
715 if (undoctx->objucxprv == undoctx->objucxhead)
716 errsig(undoctx->objucxerr, ERR_NOUNDO);
717
718 /* move back to previous record */
719 undoctx->objucxhead = undoctx->objucxprv;
720 p = &undoctx->objucxbuf[undoctx->objucxprv];
721
722 /* get command, and set undocxprv to previous record */
723 cmd = *p++;
724 memcpy(&prv, p, sizeof(prv));
725 p += sizeof(prv);
726
727 /* if we're at the tail, no more undo; otherwise, use back link */
728 if (undoctx->objucxprv == undoctx->objucxtail)
729 undoctx->objucxprv = undoctx->objucxhead;
730 else
731 undoctx->objucxprv = prv;
732
733 if (cmd == OBJUSAV) return; /* savepointer marker - nothing to do */
734
735 /* get object/property information for property-changing undo */
736 if (cmd != OBJUCLI)
737 {
738 memcpy(&objn, p, (size_t)sizeof(objn));
739 p += sizeof(objn);
740 memcpy(&prop, p, (size_t)sizeof(prop));
741 p += sizeof(prop);
742 objptr = mcmlck(mctx, objn);
743 indexed = (objflg(objptr) & OBJFINDEX);
744 mcmunlck(mctx, objn);
745 }
746
747 switch(cmd)
748 {
749 case OBJUADD:
750 objdelp(mctx, objn, prop, FALSE);
751 if (indexed) objindx(mctx, objn);
752 break;
753
754 case OBJUOVR:
755 objdelp(mctx, objn, prop, FALSE); /* delete the non-original value */
756 pofs = objgetp(mctx, objn, prop, (dattyp *)0); /* get ignored prop */
757 objptr = (objdef *)mcmlck(mctx, objn); /* lock the object */
758 prpflg(objofsp(objptr, pofs)) &= ~PRPFIGN; /* no longer ignored */
759 mcmunlck(mctx, objn); /* unlock the object */
760 break;
761
762 case OBJUCHG:
763 memcpy(pr, p, (size_t)PRPHDRSIZ);
764 p += PRPHDRSIZ;
765 objsetp(mctx, objn, prop, prptype(pr), (void *)p, (objucxdef *)0);
766 break;
767
768 case OBJUCLI:
769 (*undoctx->objucxcun)(undoctx->objucxccx, p);
770 break;
771 }
772 }
773
774 /*
775 * Determine if it's ok to add undo records - returns TRUE if a
776 * savepoint has been stored in the undo log, FALSE if not.
777 */
778 int objuok(objucxdef *undoctx)
779 {
780 ushort prv;
781
782 /* see if there's any more undo information */
783 if (undoctx->objucxprv == undoctx->objucxhead)
784 return(FALSE);
785
786 /* look for most recent savepoint marker */
787 for (prv = undoctx->objucxprv ;; )
788 {
789 if (undoctx->objucxbuf[prv] == OBJUSAV)
790 return(TRUE); /* found a savepoint - can add undo */
791
792 /* if we've reached the tail, there are no more undo records */
793 if (prv == undoctx->objucxtail)
794 return(FALSE); /* no savepoints - can't add undo */
795
796 /* get previous record */
797 memcpy(&prv, &undoctx->objucxbuf[prv+1], sizeof(prv));
798 }
799 }
800
801 /*
802 * Undo back to the most recent savepoint. If there is no savepoint in
803 * the undo list, NOTHING will be undone. This prevents reaching an
804 * inconsistent state in which some, but not all, of the operations
805 * between two savepoints are undone: either all operations between two
806 * savepoints will be undone, or none will.
807 */
808 void objundo(mcmcxdef *mctx, objucxdef *undoctx)
809 {
810 ushort prv;
811 ushort sav;
812
813 /* see if there's any more undo information */
814 if (undoctx->objucxprv == undoctx->objucxhead)
815 errsig(undoctx->objucxerr, ERR_NOUNDO);
816
817 /* look for most recent savepoint marker */
818 for (prv = undoctx->objucxprv ;; )
819 {
820 if (undoctx->objucxbuf[prv] == OBJUSAV)
821 {
822 sav = prv;
823 break;
824 }
825
826 /* if we've reached the tail, there are no more undo records */
827 if (prv == undoctx->objucxtail)
828 errsig(undoctx->objucxerr, ERR_ICUNDO);
829
830 /* get previous record */
831 memcpy(&prv, &undoctx->objucxbuf[prv+1], sizeof(prv));
832 }
833
834 /* now undo everything until we get to the savepoint */
835 do { obj1undo(mctx, undoctx); } while (undoctx->objucxhead != sav);
836 }
837
838 /* initialize undo context */
839 objucxdef *objuini(mcmcxdef *ctx, ushort siz,
840 void (*undocb)(void *, uchar *),
841 ushort (*sizecb)(void *, uchar *),
842 void *callctx)
843 {
844 objucxdef *ret;
845 long totsiz;
846
847 /* force size into valid range */
848 totsiz = (long)siz + sizeof(objucxdef) - 1;
849 if (totsiz > 0xff00)
850 siz = 0xff00 - sizeof(objucxdef) + 1;
851
852 ret = (objucxdef *)mchalo(ctx->mcmcxgl->mcmcxerr,
853 (sizeof(objucxdef) + siz - 1),
854 "objuini");
855
856 ret->objucxmem = ctx;
857 ret->objucxerr = ctx->mcmcxgl->mcmcxerr;
858 ret->objucxsiz = siz;
859 ret->objucxhead = ret->objucxprv = ret->objucxtail = ret->objucxtop = 0;
860
861 /* set client callback functions */
862 ret->objucxcun = undocb; /* callback to apply client undo */
863 ret->objucxcsz = sizecb; /* callback to get size of client undo */
864 ret->objucxccx = callctx; /* context for client callback functions */
865
866 return(ret);
867 }
868
869 /* discard all undo records */
870 void objulose(objucxdef *ctx)
871 {
872 if (ctx)
873 ctx->objucxhead =
874 ctx->objucxprv =
875 ctx->objucxtail =
876 ctx->objucxtop = 0;
877 }
878
879 /* uninitialize the undo context - release allocated memory */
880 void objuterm(objucxdef *uctx)
881 {
882 /* free the undo memory block */
883 mchfre(uctx);
884 }
885
886 /* revert object to original (post-compilation) values */
887 void objrevert(void *ctx0, mcmon objn)
888 {
889 mcmcxdef *mctx = (mcmcxdef *)ctx0;
890 uchar *p;
891 prpdef *pr;
892 int cnt;
893 int indexed;
894
895 p = mcmlck(mctx, objn);
896 pr = objprp(p);
897 indexed = objflg(p) & OBJFINDEX;
898
899 /* restore original settings */
900 objsfree(p, objrst(p));
901 objsnp(p, objstat(p));
902
903 /* go through original properties and remove 'ignore' flag if set */
904 for (cnt = objnprop(p) ; cnt ; pr = objpnxt(pr), --cnt)
905 prpflg(pr) &= ~PRPFIGN;
906
907 /* touch object and unlock it */
908 mcmtch(mctx, objn);
909 mcmunlck(mctx, objn);
910
911 /* if it's indexed, rebuild the index */
912 if (indexed) objindx(mctx, objn);
913 }
914
915 /* set 'ignore' flag for original properties set in mutable part */
916 void objsetign(mcmcxdef *mctx, objnum objn)
917 {
918 objdef *objptr;
919 prpdef *mut;
920 prpdef *p;
921 int statcnt;
922 int cnt;
923 int indexed;
924 prpdef *p1;
925
926 objptr = (objdef *)mcmlck(mctx, (mcmon)objn);
927 p1 = objprp(objptr);
928 indexed = objflg(objptr) & OBJFINDEX;
929
930 /* go through mutables, and set ignore on duplicates in non-mutables */
931 for (mut = (prpdef *)(objptr + objrst(objptr)),
932 cnt = objnprop(objptr) - objstat(objptr) ; cnt ;
933 mut = objpnxt(mut), --cnt)
934 {
935 for (p = p1, statcnt = objstat(objptr) ; statcnt ;
936 p = objpnxt(p), --statcnt)
937 {
938 /* if this static prop matches a mutable prop, ignore it */
939 if (prpprop(p) == prpprop(mut))
940 {
941 prpflg(p) |= PRPFIGN;
942 break;
943 }
944 }
945 }
946
947 mcmtch(mctx, (mcmon)objn);
948 mcmunlck(mctx, (mcmon)objn);
949 if (indexed) objindx(mctx, objn);
950 }
951
952 /*
953 * Build or rebuild a property index for an object.
954 */
955 void objindx(mcmcxdef *mctx, objnum objn)
956 {
957 uint newsiz;
958 uint avail;
959 objdef *objptr;
960 uint cnt;
961 prpdef *p;
962 uchar *indp;
963 uchar *indbase;
964 uint icnt;
965 uint first;
966 uint last;
967 uint cur;
968
969 objptr = (objdef *)mcmlck(mctx, objn); /* get object pointer */
970 cnt = objnprop(objptr); /* get number of properties */
971 p = objprp(objptr); /* get pointer to properties (or old index) */
972 newsiz = 2 + 4*cnt; /* figure size needed for the index */
973
974 avail = mcmobjsiz(mctx, objn) - objfree(objptr);
975
976 /* insert space for the index; expand the object if necessary */
977 if (avail < newsiz)
978 {
979 ushort need;
980
981 newsiz += 10*4; /* add some extra space for later */
982 need = newsiz - avail; /* compute amount of space needed */
983 objptr = objexp(mctx, objn, &need);
984 p = objprp(objptr);
985 }
986
987 /* now build the index */
988 indbase = objpfre(objptr);
989 for (icnt = 0 ; cnt ; p = objpnxt(p), --cnt, ++icnt)
990 {
991 uint ofs = (uchar *)p - (uchar *)objptr;
992
993 if (icnt)
994 {
995 /* figure out where to insert this property */
996 first = 0;
997 last = icnt - 1;
998 for (;;)
999 {
1000 if (first > last) break;
1001 cur = first + (last - first)/2;
1002 indp = indbase + cur*4;
1003 if (indp[0] == p[0] && indp[1] == p[1])
1004 break;
1005 else if (indp[0] < p[0]
1006 || (indp[0] == p[0] && indp[1] < p[1]))
1007 first = (cur == first ? first + 1 : cur);
1008 else
1009 last = (cur == last ? last - 1 : cur);
1010 }
1011
1012 /* make sure we're positioned just before insertion point */
1013 while (cur < icnt
1014 && (indp[0] <= p[0]
1015 || (indp[0] == p[0] && indp[1] <= p[1])))
1016 {
1017 indp += 4;
1018 ++cur;
1019 }
1020
1021 /* move elements above if any */
1022 if (cur < icnt)
1023 memmove(indp + 4, indp, (size_t)((icnt - cur) * 4));
1024 }
1025 else
1026 indp = indbase;
1027
1028 /* insert property into index */
1029 indp[0] = p[0];
1030 indp[1] = p[1];
1031 oswp2(indp+2, ofs);
1032 }
1033
1034 /* set the index flag, and dirty and free the object */
1035 objsflg(objptr, objflg(objptr) | OBJFINDEX);
1036 mcmtch(mctx, (mcmon)objn);
1037 mcmunlck(mctx, (mcmon)objn);
1038 }
1039
1040 /* allocate and initialize an object */
1041 objdef *objnew(mcmcxdef *mctx, int sccnt, ushort propspace,
1042 objnum *objnptr, int classflg)
1043 {
1044 objdef *o;
1045 mcmon objn;
1046
1047 /* allocate cache object */
1048 o = (objdef *)mcmalo(mctx, (ushort)(OBJDEFSIZ + sccnt * 2 + propspace),
1049 &objn);
1050
1051 /* set up object descriptor for the new object */
1052 objini(mctx, sccnt, (objnum)objn, classflg);
1053
1054 *objnptr = (objnum)objn;
1055 return(o);
1056 }
1057
1058 /* initialize an already allocated object */
1059 void objini(mcmcxdef *mctx, int sccnt, objnum objn, int classflg)
1060 {
1061 objdef *o;
1062 uint flags = 0;
1063
1064 /* get a lock on the object */
1065 o = (objdef *)mcmlck(mctx, objn);
1066
1067 memset(o, 0, (size_t)10);
1068 objsnsc(o, sccnt);
1069 objsfree(o, ((uchar *)objsc(o) + 2*sccnt) - (uchar *)o);
1070
1071 /* set up flags */
1072 if (classflg) flags |= OBJFCLASS;
1073 objsflg(o, flags);
1074
1075 /* tell cache manager that this object has been modified */
1076 mcmtch(mctx, objn);
1077 mcmunlck(mctx, objn);
1078 }
1079
1080 /*
1081 * Get the first superclass of an object. If it doesn't have any
1082 * superclasses, return invalid.
1083 */
1084 objnum objget1sc(mcmcxdef *ctx, objnum objn)
1085 {
1086 objdef *p;
1087 objnum retval;
1088
1089 /* lock the object */
1090 p = mcmlck(ctx, (mcmon)objn);
1091
1092 /* get the first superclass if it has any */
1093 if (objnsc(p) == 0)
1094 retval = MCMONINV;
1095 else
1096 retval = osrp2(objsc(p));
1097
1098 /* unlock the object and return the superclass value */
1099 mcmunlck(ctx, (mcmon)objn);
1100 return retval;
1101 }