forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.c
2385 lines (2050 loc) · 65.9 KB
/
objects.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the functions of the objects package.
*/
#include "objects.h"
#include "bool.h"
#include "calls.h"
#include "error.h"
#include "gapstate.h"
#include "gvars.h"
#include "io.h"
#include "lists.h"
#include "modules.h"
#include "opers.h"
#include "plist.h"
#include "precord.h"
#include "records.h"
#include "saveload.h"
#include "stringobj.h"
#ifdef HPCGAP
#include "hpc/aobjects.h"
#include "hpc/guards.h"
#include "hpc/thread.h"
#include "hpc/traverse.h"
#endif
#if defined(USE_THREADSAFE_COPYING)
#include "hpc/traverse.h"
#endif
#include <stdio.h>
#include <stdlib.h>
enum {
MAXPRINTDEPTH = 64,
};
static ModuleStateOffset ObjectsStateOffset = -1;
typedef struct {
UInt PrintObjDepth;
#ifdef HPCGAP
Obj PrintObjThissObj;
Obj * PrintObjThiss;
Obj PrintObjIndicesObj;
Int * PrintObjIndices;
#else
Obj PrintObjThiss[MAXPRINTDEPTH];
Int PrintObjIndices[MAXPRINTDEPTH];
#endif
// This variable is used to allow a ViewObj method to call PrintObj on the
// same object without triggering use of '~'. It contains one of the
// values 0, 1 and 2 according to whether ...
// 0: there is no enclosing call to PrintObj or ViewObj still open, or
// 1: the innermost such is PrintObj, or
// 2: the innermost such is ViewObj.
UInt LastPV;
} ObjectsModuleState;
static Int lastFreePackageTNUM = FIRST_PACKAGE_TNUM;
/****************************************************************************
**
*V NameOfType[<type>] . . . . . . . . . . . . . . . . . . . . names of types
**
** 'NameOfType[<type>]' is the name of the type <type>.
*/
static const char * NameOfType[NUM_TYPES];
/****************************************************************************
**
*F RegisterPackageTNUM( <name>, <typeObjFunc> )
**
** Allocates a TNUM for use by a package. The parameters <name> and
** <typeObjFunc> are used to initialize the relevant entries in the
** InfoBags and TypeObjFuncs arrays.
**
** If allocation fails (e.g. because no more TNUMs are available),
** a negative value is returned.
*/
Int RegisterPackageTNUM( const char *name, Obj (*typeObjFunc)(Obj obj) )
{
#ifdef HPCGAP
HashLock(0);
#endif
if (lastFreePackageTNUM > LAST_PACKAGE_TNUM)
return -1;
Int tnum = lastFreePackageTNUM++;
#ifdef HPCGAP
HashUnlock(0);
#endif
SET_TNAM_TNUM(tnum, name);
TypeObjFuncs[tnum] = typeObjFunc;
return tnum;
}
const Char * TNAM_TNUM(UInt tnum)
{
return NameOfType[tnum];
}
void SET_TNAM_TNUM(UInt tnum, const Char *name)
{
GAP_ASSERT(NameOfType[tnum] == 0);
NameOfType[tnum] = name;
}
/****************************************************************************
**
*F FuncFAMILY_TYPE( <self>, <type> ) . . . . . . handler for 'FAMILY_TYPE'
*/
static Obj FuncFAMILY_TYPE(Obj self, Obj type)
{
return FAMILY_TYPE( type );
}
/****************************************************************************
**
*F FuncFAMILY_OBJ( <self>, <obj> ) . . . . . . . handler for 'FAMILY_OBJ'
*/
static Obj FuncFAMILY_OBJ(Obj self, Obj obj)
{
return FAMILY_OBJ( obj );
}
/****************************************************************************
**
*F TYPE_OBJ( <obj> ) . . . . . . . . . . . . . . . . . . . type of an object
**
** 'TYPE_OBJ' returns the type of the object <obj>.
**
** 'TYPE_OBJ' is defined in the declaration part of this package.
*/
Obj (*TypeObjFuncs[LAST_REAL_TNUM+1]) ( Obj obj );
static Obj TypeObjError(Obj obj)
{
ErrorQuit("Panic: basic object of type '%s' is unkind",
(Int)TNAM_OBJ(obj), 0);
return 0;
}
/****************************************************************************
**
*F SET_TYPE_OBJ( <obj>, <type> ) . . . . . . . . . . . set type of an object
**
** 'SET_TYPE_OBJ' sets the type of the object <obj> to <type>; if <obj>
** is not a posobj/comobj/datobj, attempts to first convert it to one; if
** that fails, an error is raised.
*/
void SET_TYPE_OBJ(Obj obj, Obj type)
{
switch (TNUM_OBJ(obj)) {
#ifdef HPCGAP
case T_ALIST:
case T_FIXALIST:
HashLock(obj);
ADDR_OBJ(obj)[1] = type;
CHANGED_BAG(obj);
RetypeBag(obj, T_APOSOBJ);
HashUnlock(obj);
MEMBAR_WRITE();
break;
case T_APOSOBJ:
HashLock(obj);
ADDR_OBJ(obj)[1] = type;
CHANGED_BAG(obj);
HashUnlock(obj);
MEMBAR_WRITE();
break;
case T_AREC:
case T_ACOMOBJ:
ADDR_OBJ(obj)[0] = type;
CHANGED_BAG(obj);
RetypeBag(obj, T_ACOMOBJ);
MEMBAR_WRITE();
break;
#endif
case T_PREC:
#ifdef HPCGAP
MEMBAR_WRITE();
#endif
RetypeBag(obj, T_COMOBJ);
SET_TYPE_COMOBJ(obj, type);
CHANGED_BAG(obj);
break;
case T_COMOBJ:
#ifdef HPCGAP
ReadGuard(obj);
MEMBAR_WRITE();
#endif
SET_TYPE_COMOBJ(obj, type);
CHANGED_BAG(obj);
break;
case T_POSOBJ:
#ifdef HPCGAP
ReadGuard(obj);
MEMBAR_WRITE();
#endif
SET_TYPE_POSOBJ(obj, type);
CHANGED_BAG(obj);
break;
case T_DATOBJ:
SetTypeDatObj(obj, type);
break;
default:
if (IS_STRING_REP(obj)) {
// FIXME/TODO: Hap calls Objectify on a string...
}
else if (IS_PLIST(obj)) {
#ifdef HPCGAP
MEMBAR_WRITE();
#endif
RetypeBag(obj, T_POSOBJ);
SET_TYPE_POSOBJ(obj, type);
CHANGED_BAG(obj);
}
else {
ErrorMayQuit("cannot change type of a %s", (Int)TNAM_OBJ(obj), 0);
}
break;
}
}
/****************************************************************************
**
*F FuncTYPE_OBJ( <self>, <obj> ) . . . . . . . . . handler for 'TYPE_OBJ'
*/
#ifndef WARD_ENABLED
static Obj FuncTYPE_OBJ(Obj self, Obj obj)
{
return TYPE_OBJ( obj );
}
#endif
/****************************************************************************
**
*F FuncSET_TYPE_OBJ( <self>, <obj>, <type> ) . . handler for 'SET_TYPE_OBJ'
*/
static Obj FuncSET_TYPE_OBJ(Obj self, Obj obj, Obj type)
{
SET_TYPE_OBJ( obj, type );
return (Obj) 0;
}
/****************************************************************************
**
*F IS_MUTABLE_OBJ( <obj> ) . . . . . . . . . . . . . . is an object mutable
**
** 'IS_MUTABLE_OBJ' returns 1 if the object <obj> is mutable (i.e., can
** change due to assignments), and 0 otherwise.
**
** 'IS_MUTABLE_OBJ' is defined in the declaration part of this package.
*/
BOOL (*IsMutableObjFuncs[LAST_REAL_TNUM + 1])(Obj obj);
static Obj IsMutableObjFilt;
static BOOL IsMutableObjError(Obj obj)
{
ErrorQuit("Panic: tried to test mutability of unsupported type '%s'",
(Int)TNAM_OBJ(obj), 0);
return FALSE;
}
static BOOL IsMutableObjObject(Obj obj)
{
#ifdef HPCGAP
if (RegionBag(obj) == ReadOnlyRegion)
return FALSE;
#endif
return (DoFilter( IsMutableObjFilt, obj ) == True);
}
/****************************************************************************
**
*F FiltIS_MUTABLE_OBJ( <self>, <obj> ) . . . handler for 'IS_MUTABLE_OBJ'
*/
static Obj FiltIS_MUTABLE_OBJ(Obj self, Obj obj)
{
return (IS_MUTABLE_OBJ( obj ) ? True : False);
}
/****************************************************************************
**
*F FiltIS_INTERNALLY_MUTABLE_OBJ(<self>, <obj>)
*/
#ifdef HPCGAP
static Obj IsInternallyMutableObjFilt;
static Obj FiltIS_INTERNALLY_MUTABLE_OBJ(Obj self, Obj obj)
{
return (TNUM_OBJ(obj) == T_DATOBJ &&
RegionBag(obj) != ReadOnlyRegion &&
DoFilter( IsInternallyMutableObjFilt, obj) == True) ? True : False;
}
BOOL IsInternallyMutableObj(Obj obj)
{
return TNUM_OBJ(obj) == T_DATOBJ &&
RegionBag(obj) != ReadOnlyRegion &&
DoFilter( IsInternallyMutableObjFilt, obj) == True;
}
#endif
/****************************************************************************
**
*F IS_COPYABLE_OBJ(<obj>) . . . . . . . . . . . . . . is an object copyable
**
** 'IS_COPYABLE_OBJ' returns 1 if the object <obj> is copyable (i.e., can be
** copied into a mutable object), and 0 otherwise.
**
** 'IS_COPYABLE_OBJ' is defined in the declaration part of this package.
*/
BOOL (*IsCopyableObjFuncs[LAST_REAL_TNUM + 1])(Obj obj);
static Obj IsCopyableObjFilt;
static BOOL IsCopyableObjError(Obj obj)
{
ErrorQuit("Panic: tried to test copyability of unsupported type '%s'",
(Int)TNAM_OBJ(obj), 0);
return FALSE;
}
static BOOL IsCopyableObjObject(Obj obj)
{
return (DoFilter( IsCopyableObjFilt, obj ) == True);
}
/****************************************************************************
**
*F FiltIS_COPYABLE_OBJ( <self>, <obj> ) . . . handler for 'IS_COPYABLE_OBJ'
*/
static Obj FiltIS_COPYABLE_OBJ(Obj self, Obj obj)
{
return (IS_COPYABLE_OBJ( obj ) ? True : False);
}
/****************************************************************************
**
*V ShallowCopyObjFuncs[<type>] . . . . . . . . . . shallow copier functions
*/
Obj (*ShallowCopyObjFuncs[LAST_REAL_TNUM+1]) ( Obj obj );
static Obj ShallowCopyObjOper;
/****************************************************************************
**
*F ShallowCopyObjError( <obj> ) . . . . . . . . . . . . . . . unknown type
*/
static Obj ShallowCopyObjError(Obj obj)
{
ErrorQuit("Panic: tried to shallow copy object of unsupported type '%s'",
(Int)TNAM_OBJ(obj), 0);
return (Obj)0;
}
/****************************************************************************
**
*F ShallowCopyObjConstant( <obj> ) . . . . . . . . . . . . . . . do nothing
*/
static Obj ShallowCopyObjConstant(Obj obj)
{
return obj;
}
/****************************************************************************
**
*F ShallowCopyObjObject( <obj> ) . . . . . . . . . . . . . . . . call method
*/
static Obj ShallowCopyObjObject(Obj obj)
{
return DoOperation1Args( ShallowCopyObjOper, obj );
}
/****************************************************************************
**
*F ShallowCopyObjDefault( <obj> ) . . . . . . . . . . default shallow copy
*/
static Obj ShallowCopyObjDefault(Obj obj)
{
Obj new;
const Obj * o;
Obj * n;
// make the new object and copy the contents
new = NewBag( MUTABLE_TNUM(TNUM_OBJ(obj)), SIZE_OBJ(obj) );
o = CONST_ADDR_OBJ(obj);
n = ADDR_OBJ( new );
memcpy(n, o, SIZE_OBJ(obj) );
// 'CHANGED_BAG(new);' not needed, <new> is newest object
return new;
}
/****************************************************************************
**
*F FuncSHALLOW_COPY_OBJ( <self>, <obj> ) . . handler for 'SHALLOW_COPY_OBJ'
*/
static Obj FuncSHALLOW_COPY_OBJ(Obj self, Obj obj)
{
return SHALLOW_COPY_OBJ( obj );
}
/****************************************************************************
**
*F CopyObj( <obj>, <mut> ) . . . . . . . make a structural copy of an object
**
** 'CopyObj' only calls 'COPY_OBJ' and then 'CLEAN_OBJ'.
*/
Obj CopyObj (
Obj obj,
Int mut )
{
#ifdef USE_THREADSAFE_COPYING
return CopyReachableObjectsFrom(obj, 0, 0, !mut);
#else
Obj new; // copy of <obj>
// make a copy
new = COPY_OBJ( obj, mut );
// clean up the marks
CLEAN_OBJ( obj );
// return the copy
return new;
#endif
}
#if !defined(USE_THREADSAFE_COPYING)
/****************************************************************************
**
*V CopyObjFuncs[<type>] . . . . . . . . . . . . table of copying functions
*/
Obj (*CopyObjFuncs[ LAST_REAL_TNUM+1 ]) ( Obj obj, Int mut );
/****************************************************************************
**
*V CleanObjFuncs[<type>] . . . . . . . . . . . . table of cleaning functions
*/
void (*CleanObjFuncs[ LAST_REAL_TNUM+1 ]) ( Obj obj );
/****************************************************************************
**
*F PrepareCopy(<obj>,<copy>) . . . helper for use in CopyObjFuncs functions
**
*/
void PrepareCopy(Obj obj, Obj copy)
{
// insert a forwarding pointer into <obj> which contains...
// - the value overwritten by this forwarding pointer,
// - a pointer to <copy>,
// - the TNUM of <obj>.
// Note that we cannot simply restore the overwritten value by copying
// the corresponding value from <copy>, as they may actually differ
// between original and copy (e.g. for objects, they point to the type;
// if making an immutable copy of a mutable object, the types will
// differ).
// Likewise, the TNUM of the copy and the original can and will differ;
// e.g. for a weak pointer list, the copy can be a plist.
Obj tmp = NEW_PLIST(T_PLIST, 3);
SET_LEN_PLIST(tmp, 3);
SET_ELM_PLIST(tmp, 1, CONST_ADDR_OBJ(obj)[0]);
SET_ELM_PLIST(tmp, 2, copy);
SET_ELM_PLIST(tmp, 3, INTOBJ_INT(TNUM_OBJ(obj)));
// insert the forwarding pointer
GAP_ASSERT(SIZE_OBJ(obj) >= sizeof(Obj));
ADDR_OBJ(obj)[0] = tmp;
CHANGED_BAG(obj);
// update the TNUM to indicate the object is being copied
RetypeBag(obj, T_COPYING);
}
/****************************************************************************
**
*F COPY_OBJ(<obj>) . . . . . . . . . . . make a structural copy of an object
**
** 'COPY_OBJ' implements the first pass of 'CopyObj', i.e., it makes the
** structural copy of <obj> and marks <obj> as already copied.
*/
Obj COPY_OBJ(Obj obj, Int mut)
{
UInt tnum = TNUM_OBJ(obj);
Obj copy;
if (tnum == T_COPYING) {
// get the plist reference by the forwarding pointer
Obj fpl = CONST_ADDR_OBJ(obj)[0];
// return pointer to the copy
copy = ELM_PLIST(fpl, 2);
}
else if (!IS_MUTABLE_OBJ(obj)) {
copy = obj;
}
else {
copy = (*CopyObjFuncs[tnum])(obj, mut);
}
return copy;
}
/****************************************************************************
**
*F CLEAN_OBJ(<obj>) . . . . . . . . . . . . . clean up object after copying
**
** 'CLEAN_OBJ' implements the second pass of 'CopyObj', i.e., it removes the
** mark from <obj>.
*/
void CLEAN_OBJ(Obj obj)
{
if (TNUM_OBJ(obj) != T_COPYING)
return;
// get the plist reference by the forwarding pointer
Obj fpl = CONST_ADDR_OBJ(obj)[0];
// remove the forwarding pointer
ADDR_OBJ(obj)[0] = ELM_PLIST(fpl, 1);
CHANGED_BAG(obj);
// restore the tnum
UInt tnum = INT_INTOBJ(ELM_PLIST(fpl, 3));
RetypeBag(obj, tnum);
// invoke type specific cleanup, if any
if (CleanObjFuncs[tnum])
CleanObjFuncs[tnum](obj);
}
#if !defined(USE_THREADSAFE_COPYING) && !defined(USE_BOEHM_GC)
static void MarkCopyingSubBags(Obj obj)
{
Obj fpl = CONST_ADDR_OBJ(obj)[0];
// mark the forwarding pointer
MarkBag(fpl);
// mark the rest as in the non-copied case
UInt tnum = INT_INTOBJ(ELM_PLIST(fpl, 3));
TabMarkFuncBags[tnum](obj);
}
#endif
/****************************************************************************
**
*F CopyObjError( <obj> ) . . . . . . . . . . . . . . . . . . . unknown type
*/
static Obj CopyObjError(Obj obj, Int mut)
{
ErrorQuit("Panic: tried to copy object of unsupported type '%s'",
(Int)TNAM_OBJ(obj), 0);
return (Obj)0;
}
/****************************************************************************
**
*F CleanObjError( <obj> ) . . . . . . . . . . . . . . . . . . unknown type
*/
static void CleanObjError(Obj obj)
{
ErrorQuit("Panic: tried to clean object of unsupported type '%s'",
(Int)TNAM_OBJ(obj), 0);
}
/****************************************************************************
**
*F CopyObjConstant( <obj> ) . . . . . . . . . . . . copy a constant object
*/
static Obj CopyObjConstant(Obj obj, Int mut)
{
return obj;
}
/****************************************************************************
**
*F CopyObjPosObj( <obj>, <mut> ) . . . . . . . . . copy a positional object
*/
static Obj CopyObjPosObj(Obj obj, Int mut)
{
Obj copy; // copy, result
Obj tmp; // temporary variable
UInt i; // loop variable
// immutable input is handled by COPY_OBJ
GAP_ASSERT(IS_MUTABLE_OBJ(obj));
// if the object is not copyable return
if ( ! IS_COPYABLE_OBJ(obj) ) {
ErrorQuit("Panic: encountered mutable, non-copyable object", 0, 0);
}
// make a copy
copy = NewBag( TNUM_OBJ(obj), SIZE_OBJ(obj) );
ADDR_OBJ(copy)[0] = CONST_ADDR_OBJ(obj)[0];
if ( !mut ) {
CALL_2ARGS( RESET_FILTER_OBJ, copy, IsMutableObjFilt );
}
// leave a forwarding pointer
PrepareCopy(obj, copy);
// copy the subvalues
for ( i = 1; i < SIZE_OBJ(obj)/sizeof(Obj); i++ ) {
if (CONST_ADDR_OBJ(obj)[i] != 0) {
tmp = COPY_OBJ(CONST_ADDR_OBJ(obj)[i], mut);
ADDR_OBJ(copy)[i] = tmp;
CHANGED_BAG( copy );
}
}
// return the copy
return copy;
}
/****************************************************************************
**
*F CleanObjPosObj( <obj> ) . . . . . . . . . . . . . . . . . . clean posobj
*/
static void CleanObjPosObj(Obj obj)
{
UInt i; // loop variable
// clean the subvalues
for ( i = 1; i < SIZE_OBJ(obj)/sizeof(Obj); i++ ) {
if (CONST_ADDR_OBJ(obj)[i] != 0)
CLEAN_OBJ(CONST_ADDR_OBJ(obj)[i]);
}
}
/****************************************************************************
**
*F CopyObjComObj( <obj>, <mut> ) . . . . . . . . . . . . . . . copy a comobj
*/
static Obj CopyObjComObj(Obj obj, Int mut)
{
Obj copy; // copy, result
Obj tmp; // temporary variable
// immutable input is handled by COPY_OBJ
GAP_ASSERT(IS_MUTABLE_OBJ(obj));
// if the object is not copyable return
if ( ! IS_COPYABLE_OBJ(obj) ) {
ErrorQuit("Panic: encountered mutable, non-copyable object", 0, 0);
}
// make a copy
copy = NewBag( TNUM_OBJ(obj), SIZE_OBJ(obj) );
memcpy(ADDR_OBJ(copy), CONST_ADDR_OBJ(obj), SIZE_OBJ(obj));
if ( !mut ) {
CALL_2ARGS( RESET_FILTER_OBJ, copy, IsMutableObjFilt );
}
// leave a forwarding pointer
PrepareCopy(obj, copy);
// copy the subvalues; since we used memcpy above, we don't need to worry
// about copying the length or RNAMs; and by working solely inside the
// copy, we avoid triggering tnum assertions in GET_ELM_PREC and
// SET_ELM_PREC
const UInt len = LEN_PREC(copy);
for (UInt i = 1; i <= len; i++) {
tmp = COPY_OBJ(GET_ELM_PREC(copy, i), mut);
SET_ELM_PREC(copy, i, tmp);
CHANGED_BAG(copy);
}
// return the copy
return copy;
}
/****************************************************************************
**
*F CleanObjComObj( <obj> ) . . . . . . . . . . . . . . . . . clean a comobj
*/
static void CleanObjComObj(Obj obj)
{
UInt i; // loop variable
// clean the subvalues
for ( i = 1; i <= LEN_PREC(obj); i++ ) {
CLEAN_OBJ( GET_ELM_PREC(obj,i) );
}
}
/****************************************************************************
**
*F CopyObjDatObj( <obj>, <mut> ) . . . . . . . . . . . . . . . copy a datobj
*/
static Obj CopyObjDatObj(Obj obj, Int mut)
{
Obj copy; // copy, result
// immutable input is handled by COPY_OBJ
GAP_ASSERT(IS_MUTABLE_OBJ(obj));
// if the object is not copyable return
if ( ! IS_COPYABLE_OBJ(obj) ) {
ErrorQuit("Panic: encountered mutable, non-copyable object", 0, 0);
}
// make a copy
copy = NewBag( TNUM_OBJ(obj), SIZE_OBJ(obj) );
memcpy(ADDR_OBJ(copy), CONST_ADDR_OBJ(obj), SIZE_OBJ(obj));
if ( !mut ) {
CALL_2ARGS( RESET_FILTER_OBJ, copy, IsMutableObjFilt );
}
// leave a forwarding pointer
PrepareCopy(obj, copy);
// return the copy
return copy;
}
/****************************************************************************
**
*F CleanObjDatObj( <obj> ) . . . . . . . . . . . . . . . . . clean a datobj
*/
static void CleanObjDatObj(Obj obj)
{
}
#endif // !defined(USE_THREADSAFE_COPYING)
/****************************************************************************
**
*F FuncIMMUTABLE_COPY_OBJ( <self>, <obj> ) . . . . immutable copy of <obj>
*/
static Obj FuncIMMUTABLE_COPY_OBJ(Obj self, Obj obj)
{
return CopyObj( obj, 0 );
}
/****************************************************************************
**
*F FuncDEEP_COPY_OBJ( <self>, <obj> ) . . . . . . mutable copy of <obj>
*/
static Obj FuncDEEP_COPY_OBJ(Obj self, Obj obj)
{
return CopyObj( obj, 1 );
}
/****************************************************************************
**
*F MakeImmutable( <obj> . . . . . . . . . . make an object immutable inplace
**
** Mark an object and all subobjects immutable in-place.
** May cause confusion if there are shared subobjects
**
*/
static Obj PostMakeImmutableOp = 0;
void (*MakeImmutableObjFuncs[LAST_REAL_TNUM+1])( Obj );
void MakeImmutable( Obj obj )
{
if (IS_MUTABLE_OBJ( obj ))
{
(*(MakeImmutableObjFuncs[TNUM_OBJ(obj)]))(obj);
}
}
#ifdef HPCGAP
void CheckedMakeImmutable( Obj obj )
{
if (!PreMakeImmutableCheck(obj))
ErrorMayQuit("MakeImmutable: Argument has inaccessible subobjects", 0, 0);
MakeImmutable(obj);
}
#endif
static void MakeImmutableError(Obj obj)
{
ErrorQuit("No make immutable function installed for a %s",
(Int)TNAM_OBJ(obj), 0);
}
static void MakeImmutableComObj(Obj obj)
{
CALL_2ARGS( RESET_FILTER_OBJ, obj, IsMutableObjFilt );
CALL_1ARGS( PostMakeImmutableOp, obj);
}
static void MakeImmutablePosObj(Obj obj)
{
CALL_2ARGS( RESET_FILTER_OBJ, obj, IsMutableObjFilt );
CALL_1ARGS( PostMakeImmutableOp, obj);
}
#ifdef HPCGAP
// HPCGAP-HACK:
// There is a considerable amount of library code that currently
// relies on being able to modify immutable data objects; in order
// to not break all of that, MakeImmutableDatObj() makes immutable
// data objects public, not read-only if they are not internally
// mutable. Note that this is potentially unsafe if these objects
// are shared between threads and then modified by kernel code.
//
// By setting the environment variable GAP_READONLY_DATOBJS, one
// can restore the old behavior in order to find and debug the
// offending code.
static int ReadOnlyDatObjs = 0;
#endif
static void MakeImmutableDatObj(Obj obj)
{
CALL_2ARGS( RESET_FILTER_OBJ, obj, IsMutableObjFilt );
#ifdef HPCGAP
if (!IsInternallyMutableObj(obj)) {
if (ReadOnlyDatObjs)
MakeBagReadOnly(obj);
else
MakeBagPublic(obj);
}
#endif
}
static Obj FuncMakeImmutable(Obj self, Obj obj)
{
#ifdef HPCGAP
CheckedMakeImmutable(obj);
#else
MakeImmutable(obj);
#endif
return obj;
}
static Obj FuncGET_TNAM_FROM_TNUM(Obj self, Obj obj)
{
UInt tnum = GetBoundedInt(SELF_NAME, obj, 0, NUM_TYPES - 1);
const char * name = TNAM_TNUM(tnum);
return MakeImmString(name ? name : "<unnamed tnum>");
}
// This function is used to keep track of which objects are already
// being printed or viewed to trigger the use of ~ when needed.
static inline BOOL IS_ON_PRINT_STACK(const ObjectsModuleState * os, Obj obj)
{
if (!(FIRST_RECORD_TNUM <= TNUM_OBJ(obj) &&
TNUM_OBJ(obj) <= LAST_LIST_TNUM))
return FALSE;
for (UInt i = 0; i < os->PrintObjDepth; i++)
if (os->PrintObjThiss[i] == obj)
return TRUE;
return FALSE;
}
#ifdef HPCGAP
static void PrintInaccessibleObject(Obj obj)
{
Char buffer[20];
Char *name;
Region *region;
Obj nameobj;
region = REGION(obj);
if (!region)
nameobj = PublicRegionName; // this should not happen, but let's be safe
else
nameobj = GetRegionName(region);
if (nameobj) {
name = CSTR_STRING(nameobj);
} else {
sprintf(buffer, "%p", (void *)region);
name = buffer;
Pr("<protected object in shared region %s (id: %d)>", (Int) name, (Int) obj);
return;
}
Pr("<protected '%s' object (id: %d)>", (Int) name, (Int) obj);
}
#endif
#ifdef HPCGAP
// On-demand creation of the PrintObj stack
static void InitPrintObjStack(ObjectsModuleState * os)
{
if (!os->PrintObjThiss) {
os->PrintObjThissObj = NewBag(T_DATOBJ, MAXPRINTDEPTH*sizeof(Obj)+sizeof(Obj));
os->PrintObjThiss = ADDR_OBJ(os->PrintObjThissObj)+1;
os->PrintObjIndicesObj = NewBag(T_DATOBJ, MAXPRINTDEPTH*sizeof(Int)+sizeof(Obj));
os->PrintObjIndices = (Int *)(ADDR_OBJ(os->PrintObjIndicesObj)+1);
}
}
#endif
/****************************************************************************
**
*F PrintObj( <obj> ) . . . . . . . . . . . . . . . . . . . . print an object
**
** 'PrintObj' prints the object <obj>.
*/
void PrintObj(Obj obj)
{
#if defined(HPCGAP) && !defined(WARD_ENABLED)
if (IS_BAG_REF(obj) && !CheckReadAccess(obj)) {
PrintInaccessibleObject(obj);
return;
}
#endif
ObjectsModuleState * os = &MODULE_STATE(Objects);
#ifdef HPCGAP
InitPrintObjStack(os);
#endif
// First check if <obj> is actually the current object being viewed, since
// ViewObj(<obj>) may result in a call to PrintObj(<obj>); in that case,
// we should not put <obj> on the print stack
if ((os->PrintObjDepth > 0) && (os->LastPV == 2) &&
(obj == os->PrintObjThiss[os->PrintObjDepth - 1])) {
os->LastPV = 1;
PRINT_OBJ(obj);
os->LastPV = 2;
}
// print the path if <obj> is on the stack
else if (IS_ON_PRINT_STACK(os, obj)) {
Pr("~", 0, 0);
for (int i = 0; obj != os->PrintObjThiss[i]; i++) {
PRINT_PATH(os->PrintObjThiss[i], os->PrintObjIndices[i]);
}
}
// dispatch to the appropriate printing function
else if (os->PrintObjDepth < MAXPRINTDEPTH) {
// push obj on the stack
os->PrintObjThiss[os->PrintObjDepth] = obj;
os->PrintObjIndices[os->PrintObjDepth] = 0;