-
Notifications
You must be signed in to change notification settings - Fork 7
/
object_BM.c
1653 lines (1517 loc) · 52.3 KB
/
object_BM.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
// file object_BM.c
// SPDX-License-Identifier: GPL-3.0-or-later
/***
BISMON
Copyright © 2018 CEA (Commissariat à l'énergie atomique et aux énergies alternatives)
contributed by Basile Starynkevitch (working at CEA, LIST, France)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "bismon.h"
/// the predefined have static memory
#define HAS_PREDEF_BM(Id,Hi,Lo,Hash) objectval_tyBM predefdata##Id##_BM = { \
.pA = (typedhead_tyBM){.htyp= tyObject_BM, .hgc=0, .hash= Hash}, \
.ob_id = (rawid_tyBM){.id_hi= Hi, .id_lo= Lo}, \
.ob_space = PredefSp_BM, \
}; \
objectval_tyBM* predefptr##Id##_BM = &predefdata##Id##_BM;
#include "_genbm_predef.h"
/// declare the globals
#define HAS_GLOBAL_BM(Gnam) objectval_tyBM* GLOBAL_BM(Gnam);
#include "_genbm_global.h"
static pthread_mutexattr_t objmutexattr_BM;
static struct hashsetobj_stBM *hashset_predefined_objects_BM;
// for qsort
static int
objptrqcmp_BM (const void *p1, const void *p2)
{
return objectcmp_BM (*(const objectval_tyBM **) p1,
*(const objectval_tyBM **) p2);
} /* end objptrqcmp_BM */
static int
objnamedptrqcmp_BM (const void *p1, const void *p2)
{
return objectnamedcmp_BM (*(const objectval_tyBM **) p1,
*(const objectval_tyBM **) p2);
} /* end objptrqcmp_BM */
void
sortobjarr_BM (objectval_tyBM ** obarr, size_t arrsiz)
{
if (obarr == NULL)
arrsiz = 0;
if (arrsiz > 1)
qsort (obarr, arrsiz, sizeof (const objectval_tyBM *), objptrqcmp_BM);
} /* end sortobjarr_BM */
void
sortnamedobjarr_BM (objectval_tyBM ** obarr, size_t arrsiz)
{
if (obarr == NULL)
arrsiz = 0;
if (arrsiz > 1)
qsort (obarr, arrsiz, sizeof (const objectval_tyBM *),
objnamedptrqcmp_BM);
} /* end sortnamedobjarr_BM */
#define BUCKETMAGIC_BM 125018767 /* 0x773a28f */
struct objbucket_stBM
{
unsigned buckmagic; // always BUCKETMAGIC_BM
unsigned buckrank; /* rank in buckarr_BM */
unsigned bucksize; /* a prime number */
unsigned buckcount; // used count
objectval_tyBM *buckobjs[]; // allocated size is bucksize
};
static struct objbucket_stBM *buckarr_BM[MAXBUCKETS_BM];
#define EMPTYSLOTOB_BM ((objectval_tyBM*)(-1))
static inline bool
validobjbucket_BM (struct objbucket_stBM *buck)
{
if (!buck)
return true;
if (buck->buckmagic != BUCKETMAGIC_BM || buck->buckrank >= MAXBUCKETS_BM
|| buckarr_BM[buck->buckrank] != buck)
return false;
if (buck->buckcount > buck->bucksize)
return false;
return true;
} /* end validobjbucket_BM */
const setval_tyBM *
setobjectsofidprefixed_BM (const char *prefix)
{
const setval_tyBM *setv = NULL;
if (!prefix || prefix[0] != '_' || !isdigit (prefix[1])
|| !isalnum (prefix[2]))
return NULL;
int prefixlen = strlen (prefix);
if (prefixlen > 32)
return NULL;
const char *b62digits = B62DIGITS_BM;
char *p = strchr (b62digits, prefix[2]);
if (!p)
return NULL;
int bucknum = (prefix[1] - '0') * 62 + (p - b62digits);
ASSERT_BM (bucknum >= 0 && bucknum < MAXBUCKETS_BM);
struct objbucket_stBM *curbuck = buckarr_BM[bucknum];
if (!curbuck)
return NULL;
ASSERT_BM (validobjbucket_BM (curbuck));
const objectval_tyBM *tinyarr[TINYSIZE_BM] = { };
const objectval_tyBM **arr = tinyarr;
unsigned siz = TINYSIZE_BM;
unsigned cnt = 0;
unsigned busiz = curbuck->bucksize;
for (unsigned ix = 0; ix < busiz; ix++)
{
objectval_tyBM *curob = curbuck->buckobjs[ix];
if (!curob || curob == EMPTYSLOTOB_BM)
continue;
char curidbuf[32];
memset (curidbuf, 0, sizeof (curidbuf));
idtocbuf32_BM (curob->ob_id, curidbuf);
ASSERT_BM (curidbuf[0] == '_'
&& curidbuf[1] == prefix[1] && curidbuf[2] == prefix[2]);
if (!strncmp (curidbuf, prefix, prefixlen))
{
if (cnt >= siz)
{
unsigned newsiz =
prime_above_BM (4 * cnt / 3 + TINYSIZE_BM + 3);
const objectval_tyBM **newarr =
calloc (newsiz, sizeof (void *));
if (!newarr)
FATAL_BM ("calloc failure for %u", newsiz);
memcpy (newarr, arr, cnt * sizeof (void *));
if (arr != tinyarr)
free (arr), arr = NULL;
arr = newarr;
siz = newsiz;
};
arr[cnt++] = curob;
}
}
setv = makeset_BM (arr, cnt);
if (arr != tinyarr)
free (arr), arr = NULL;
return setv;
} /* end setobjectsofidprefixed_BM */
extern objectval_tyBM *
findobjofid_BM (const rawid_tyBM id)
{
if (!validid_BM (id))
return NULL;
unsigned bucknum = bucknumserial63_BM (id.id_hi);
struct objbucket_stBM *curbuck = buckarr_BM[bucknum];
if (!curbuck)
return NULL;
ASSERT_BM (validobjbucket_BM (curbuck));
unsigned busiz = curbuck->bucksize;
ASSERT_BM (busiz >= 4 && busiz % 2 != 0 && busiz % 3 != 0);
ASSERT_BM (curbuck->buckcount < curbuck->bucksize);
hash_tyBM h = hashid_BM (id);
unsigned startix = h % busiz;
for (unsigned ix = startix; ix < busiz; ix++)
{
objectval_tyBM *curob = curbuck->buckobjs[ix];
if (!curob)
return NULL;
if (curob == EMPTYSLOTOB_BM)
continue;
if (equalid_BM (curob->ob_id, id))
return curob;
};
for (unsigned ix = 0; ix < startix; ix++)
{
objectval_tyBM *curob = curbuck->buckobjs[ix];
if (!curob)
return NULL;
if (curob == EMPTYSLOTOB_BM)
continue;
if (equalid_BM (curob->ob_id, id))
return curob;
};
return NULL;
} /* end of findobjofid_BM */
objectval_tyBM *
findobjofstrid_BM (const char *idstr)
{
const char *end = NULL;
rawid_tyBM oid = parse_rawid_BM (idstr, &end);
if (!end)
return NULL;
if (*end && !isspace (*end))
return NULL;
if (!validid_BM (oid))
return NULL;
return findobjofid_BM (oid);
} /* end findobjofstrid_BM */
static pthread_mutex_t findobmtx_bm = PTHREAD_MUTEX_INITIALIZER;
objectval_tyBM *
lockedfindobjofstrid_BM (const char *idstr)
{
if (!idstr || idstr[0] != '_')
return NULL;
objectval_tyBM *resob = NULL;
pthread_mutex_lock (&findobmtx_bm);
resob = findobjofstrid_BM (idstr);
pthread_mutex_unlock (&findobmtx_bm);
return resob;
} /* end lockedfindobjofstrid_BM */
static void
addtobucket_BM (struct objbucket_stBM *buck, objectval_tyBM * ob)
{
ASSERT_BM (buck != NULL);
ASSERT_BM (validobjbucket_BM (buck));
ASSERT_BM (valtype_BM (ob) == tyObject_BM);
hash_tyBM h = objecthash_BM (ob);
ASSERT_BM (h > 0);
unsigned busiz = buck->bucksize;
ASSERT_BM (busiz % 2 != 0 && busiz % 3 != 0 && busiz % 5 != 0);
ASSERT_BM (buck->buckcount < buck->bucksize);
unsigned startix = h % busiz;
int pos = -1;
for (unsigned ix = startix; ix < busiz; ix++)
{
objectval_tyBM *curob = buck->buckobjs[ix];
if (!curob)
{
if (pos < 0)
pos = (int) ix;
break;
}
else if (curob == EMPTYSLOTOB_BM)
{
if (pos < 0)
pos = (int) ix;
continue;
}
else if (curob == ob)
return;
}
for (unsigned ix = 0; ix < startix; ix++)
{
objectval_tyBM *curob = buck->buckobjs[ix];
if (!curob)
{
if (pos < 0)
pos = (int) ix;
break;
}
else if (curob == EMPTYSLOTOB_BM)
{
if (pos < 0)
pos = (int) ix;
continue;
}
else if (curob == ob)
return;
}
if (pos < 0)
FATAL_BM ("corrupted object bucket@%p", buck);
buck->buckobjs[pos] = ob;
buck->buckcount++;
} /* end addtobucket_BM */
static void
growobucket_BM (unsigned bucknum, unsigned gap)
{
ASSERT_BM (bucknum < MAXBUCKETS_BM);
struct objbucket_stBM *oldbuck = buckarr_BM[bucknum];
unsigned oldsiz = oldbuck ? oldbuck->bucksize : 0;
unsigned oldcnt = oldbuck ? oldbuck->buckcount : 0;
ASSERT_BM (oldbuck == NULL || oldcnt < oldsiz);
ASSERT_BM (validobjbucket_BM (oldbuck));
unsigned long newsiz =
prime_above_BM (4 * (oldcnt + gap) / 3 + gap / 64 + oldcnt / 128 +
ILOG2_BM (oldcnt + 3) + 6);
struct objbucket_stBM *newbuck =
malloc (sizeof (struct objbucket_stBM) + newsiz * sizeof (void *));
if (!newbuck)
FATAL_BM ("out of memory for newbuck#%d of newsiz %lu", bucknum, newsiz);
memset (newbuck, 0,
sizeof (struct objbucket_stBM) + newsiz * sizeof (void *));
newbuck->buckmagic = BUCKETMAGIC_BM;
newbuck->buckrank = bucknum;
newbuck->bucksize = newsiz;
newbuck->buckcount = 0;
buckarr_BM[bucknum] = newbuck;
for (unsigned oix = 0; oix < oldsiz; oix++)
{
objectval_tyBM *oldob = oldbuck->buckobjs[oix];
if (!oldob || oldob == EMPTYSLOTOB_BM)
continue;
ASSERT_BM (isobject_BM (oldob));
addtobucket_BM (newbuck, oldob);
ASSERT_BM (validobjbucket_BM (newbuck));
}
if (oldbuck)
{
oldbuck->buckmagic = 0xffff, free (oldbuck), oldbuck = NULL;
}
ASSERT_BM (newbuck->buckcount == oldcnt);
ASSERT_BM (validobjbucket_BM (newbuck));
} /* end growobucket_BM */
extern objectval_tyBM *
makeobjofid_BM (const rawid_tyBM id)
{
if (!validid_BM (id))
return NULL;
objectval_tyBM *pob = findobjofid_BM (id);
if (pob)
return pob;
unsigned bucknum = bucknumserial63_BM (id.id_hi);
struct objbucket_stBM *curbuck = buckarr_BM[bucknum];
unsigned oldsiz = curbuck ? curbuck->bucksize : 0;
unsigned oldcnt = curbuck ? curbuck->buckcount : 0;
ASSERT_BM (validobjbucket_BM (curbuck));
if (!curbuck || 4 * oldcnt + 10 > 3 * oldsiz)
{
growobucket_BM (bucknum, 7 + ILOG2_BM (oldcnt + 1));
curbuck = buckarr_BM[bucknum];
ASSERT_BM (curbuck && validobjbucket_BM (curbuck));
}
pob = allocgcty_BM (tyObject_BM, sizeof (objectval_tyBM));
pob->ob_id = id;
atomic_init (&pob->ob_aclass, BMP_object);
pthread_mutex_init (&pob->ob_mutex, &objmutexattr_BM);
((typedhead_tyBM *) pob)->hash = hashid_BM (id);
addtobucket_BM (curbuck, pob);
return pob;
} /* end of makeobjofid_BM */
void
objectgcdestroy_BM (struct garbcoll_stBM *gc, objectval_tyBM * obj)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) obj)->htyp == tyObject_BM);
// remove the object name
forgetnamedobject_BM (obj);
if (obj->ob_payl)
objclearpayload_BM (obj);
obj->ob_space = TransientSp_BM;
atomic_store (&obj->ob_aclass, NULL);
obj->ob_compvec = NULL;
obj->ob_attrassoc = NULL;
obj->ob_rout = NULL;
obj->ob_payl = NULL;
// should remove the object from its bucket
const rawid_tyBM id = obj->ob_id;
ASSERT_BM (validid_BM (id));
unsigned bucknum = bucknumserial63_BM (id.id_hi);
struct objbucket_stBM *curbuck = buckarr_BM[bucknum];
ASSERT_BM (curbuck != NULL);
ASSERT_BM (validobjbucket_BM (curbuck));
unsigned busiz = curbuck->bucksize;
unsigned bucnt = curbuck->buckcount;
ASSERT_BM (bucnt < busiz);
hash_tyBM h = objecthash_BM (obj);
ASSERT_BM (h > 0);
unsigned startix = h % busiz;
int pos = -1;
for (unsigned ix = startix; ix < busiz && pos < 0; ix++)
{
objectval_tyBM *curob = curbuck->buckobjs[ix];
if (!curob)
break;
if (curob == obj)
pos = (int) ix;
};
for (unsigned ix = 0; ix < startix && pos < 0; ix++)
{
objectval_tyBM *curob = curbuck->buckobjs[ix];
if (!curob)
break;
if (curob == obj)
pos = (int) ix;
};
ASSERT_BM (pos >= 0);
curbuck->buckobjs[pos] = EMPTYSLOTOB_BM;
curbuck->buckcount--;
memset (obj, 0, sizeof (*obj));
free (obj), obj = NULL;
gc->gc_freedbytes += sizeof (*obj);
gc->gc_nbdestroyedobjects++;
} /* end objectgcdestroy_BM */
void
objectgckeep_BM (struct garbcoll_stBM *gc, objectval_tyBM * obj)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) obj)->htyp == tyObject_BM);
gc->gc_keptbytes += sizeof (*obj);
gc->gc_nbkeptobjects++;
} /* end objectgckeep_BM */
const char *
objectdbg_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg_BM */
const char *
objectdbg1_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg1_BM */
const char *
objectdbg2_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg2_BM */
const char *
objectdbg3_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg3_BM */
const char *
objectdbg4_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg4_BM */
const char *
objectdbg5_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg5_BM */
const char *
objectdbg6_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg6_BM */
const char *
objectdbg7_BM (const objectval_tyBM * obj)
{
if (!obj)
return "__";
if (!isobject_BM ((const value_tyBM) obj))
return "*nonobject*";
const char *n = findobjectname_BM (obj);
if (n)
return n;
static thread_local char idbuf[32];
memset (idbuf, 0, sizeof (idbuf));
idtocbuf32_BM (obj->ob_id, idbuf);
return idbuf;
} /* end objectdbg7_BM */
objectval_tyBM *
makeobj_BM (void)
{
for (;;)
{
rawid_tyBM id = randomid_BM ();
objectval_tyBM *pob = findobjofid_BM (id);
if (pob)
continue;
return makeobjofid_BM (id);
}
} /* end of makeobj_BM */
void
objcompletelyclear_BM (objectval_tyBM * obj)
{
if (!isobject_BM (obj))
return;
objclearpayload_BM (obj);
objputclass_BM (obj, BMP_object);
obj->ob_compvec = NULL;
obj->ob_attrassoc = NULL;
obj->ob_routaddr = NULL;
obj->ob_sig = NULL;
obj->ob_payl = NULL;
obj->ob_space = TransientSp_BM;
objtouchnow_BM (obj);
} /* end of objcompletelyclear_BM */
void
objectinteriorgcmark_BM (struct garbcoll_stBM *gc, objectval_tyBM * obj)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (obj != NULL && isobject_BM (obj));
ASSERT_BM (((typedhead_tyBM *) obj)->hgc == MARKGC_BM);
objectval_tyBM *oclass = objclass_BM (obj);
if (oclass)
gcobjmark_BM (gc, oclass);
if (obj->ob_routaddr && obj->ob_sig)
gcobjmark_BM (gc, obj->ob_sig);
if (obj->ob_compvec)
obj->ob_compvec = datavectgcproc_BM (gc, obj->ob_compvec, obj, 0);
if (obj->ob_attrassoc)
obj->ob_attrassoc = assocgcproc_BM (gc, obj->ob_attrassoc, obj, 0);
if (obj->ob_payl)
obj->ob_payl = extendedgcproc_BM (gc, obj->ob_payl, obj, 0);
} /* end objectinteriorgcmark_BM */
////////////////////////////////////////////////////////////////
value_tyBM
constobjvaluehashed_BM (objectval_tyBM * obconst, hash_tyBM hash)
{
value_tyBM res = NULL;
if (!isobject_BM (obconst))
FATAL_BM ("invalid object to get constant from, of hash %u", hash);
objlock_BM (obconst);
if (!(objectisinstance_BM (obconst, BMP_basiclo_constant_object)))
WARNPRINTF_BM ("object %s is not a basiclo_constant_object",
objectdbg_BM (obconst));
else
res = objgetattr_BM (obconst, BMP_value);
objunlock_BM (obconst);
if (res && valhash_BM (res) != hash)
{
WARNPRINTF_BM ("object %s has value of hash %u, expecting %u",
objectdbg_BM (obconst), valhash_BM (res), hash);
res = NULL;
}
return res;
} /* end constobjvaluehashed_BM */
////////////////
static bool
hashsetobj_insert_BM (struct hashsetobj_stBM *hset,
const objectval_tyBM * obj)
{
if (valtype_BM ((const value_tyBM) obj) != tyObject_BM)
return false;
if (valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
return false;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
ASSERT_BM (ucnt < alsiz && alsiz > 3);
hash_tyBM hob = objecthash_BM (obj);
unsigned startix = hob % alsiz;
int pos = -1;
for (unsigned ix = startix; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj == obj)
return true;
if (!curobj)
{
if (pos < 0)
pos = (int) ix;
hset->hashset_objs[pos] = (objectval_tyBM *) obj;
((typedsize_tyBM *) hset)->size = ucnt + 1;
return true;
};
if (curobj == HASHEMPTYSLOT_BM)
{
if (pos < 0)
pos = (int) ix;
};
}
for (unsigned ix = 0; ix < startix; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj == obj)
return true;
if (!curobj)
{
if (pos < 0)
pos = (int) ix;
hset->hashset_objs[pos] = (objectval_tyBM *) obj;
((typedsize_tyBM *) hset)->size = ucnt + 1;
return true;
};
if (curobj == HASHEMPTYSLOT_BM)
{
if (pos < 0)
pos = (int) ix;
};
}
if (pos >= 0)
{
hset->hashset_objs[pos] = (objectval_tyBM *) obj;
((typedsize_tyBM *) hset)->size = ucnt + 1;
return true;
}
return false;
} /* end hashsetobj_insert_BM */
struct hashsetobj_stBM *
hashsetobj_grow_BM (struct hashsetobj_stBM *hset, unsigned gap)
{
if (!hset || valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
{
unsigned newsiz = prime_above_BM (4 * gap / 3 + 10);
ASSERT_BM (newsiz < MAXSIZE_BM);
hset = //
allocgcty_BM (typayl_hashsetobj_BM,
sizeof (struct hashsetobj_stBM)
+ newsiz * sizeof (void *));
((typedhead_tyBM *) hset)->rlen = newsiz;
((typedsize_tyBM *) hset)->size = 0;
return hset;
}
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
if (4 * (ucnt + gap) + 5 < 3 * alsiz)
return hset;
unsigned newsiz = prime_above_BM ((4 * (ucnt + gap)) / 3 + gap / 32 + 11);
if (alsiz >= newsiz)
return hset;
ASSERT_BM (newsiz < MAXSIZE_BM);
struct hashsetobj_stBM *newhset = //
allocgcty_BM (typayl_hashsetobj_BM,
sizeof (struct hashsetobj_stBM) + newsiz * sizeof (void *));
((typedhead_tyBM *) newhset)->rlen = newsiz;
((typedsize_tyBM *) newhset)->size = 0;
for (unsigned oix = 0; oix < alsiz; oix++)
{
const objectval_tyBM *oldobj = hset->hashset_objs[oix];
if (!oldobj || oldobj == HASHEMPTYSLOT_BM)
continue;
if (!hashsetobj_insert_BM (newhset, oldobj))
FATAL_BM ("corrupted hashset");
}
return newhset;
} /* end hashsetobj_grow_BM */
bool
hashsetobj_contains_BM (struct hashsetobj_stBM *hset,
const objectval_tyBM * obj)
{
if (valtype_BM ((const value_tyBM) obj) != tyObject_BM)
return false;
if (valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
return false;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
ASSERT_BM (ucnt < alsiz && alsiz > 3);
hash_tyBM hob = objecthash_BM (obj);
unsigned startix = hob % alsiz;
for (unsigned ix = startix; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj == obj)
return true;
if (!curobj)
return false;
};
for (unsigned ix = 0; ix < startix; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj == obj)
return true;
if (!curobj)
return false;
};
return false;
} /* end hashsetobj_contains_BM */
struct hashsetobj_stBM *
hashsetobj_add_BM (struct hashsetobj_stBM *hset, const objectval_tyBM * obj)
{
if (valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
{
if (valtype_BM ((const value_tyBM) obj) != tyObject_BM)
return NULL;
hset = hashsetobj_grow_BM (NULL, TINYSIZE_BM / 2);
if (!hashsetobj_insert_BM (hset, obj))
FATAL_BM ("corrupted hashset");
return hset;
};
if (valtype_BM ((const value_tyBM) obj) != tyObject_BM)
return hset;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
ASSERT_BM (ucnt < alsiz && alsiz > 3);
if (4 * ucnt + 8 >= 3 * alsiz)
{
hset = hashsetobj_grow_BM (hset, ucnt / 64 + 4);
};
if (!hashsetobj_insert_BM (hset, obj))
FATAL_BM ("corrupted hashset");
return hset;
} /* end hashsetobj_add_BM */
struct hashsetobj_stBM *
hashsetobj_remove_BM (struct hashsetobj_stBM *hset,
const objectval_tyBM * obj)
{
if (valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
return NULL;
if (valtype_BM ((const value_tyBM) obj) != tyObject_BM)
return hset;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
if (alsiz > TINYSIZE_BM && 3 * ucnt < alsiz)
{
struct hashsetobj_stBM *newhset =
hashsetobj_grow_BM (NULL, (3 * ucnt / 2) + ucnt / 32 + 5);
for (unsigned ix = 0; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj != NULL && curobj != HASHEMPTYSLOT_BM && curobj != obj)
{
if (!hashsetobj_insert_BM (newhset, obj))
FATAL_BM ("corrupted hashset");
}
}
return newhset;
}
hash_tyBM hob = objecthash_BM (obj);
unsigned startix = hob % alsiz;
for (unsigned ix = startix; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj == obj)
{
hset->hashset_objs[ix] = HASHEMPTYSLOT_BM;
((typedsize_tyBM *) hset)->size = ucnt - 1;
return hset;
}
if (!curobj)
return hset;
};
for (unsigned ix = 0; ix < startix; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (curobj == obj)
{
hset->hashset_objs[ix] = HASHEMPTYSLOT_BM;
((typedsize_tyBM *) hset)->size = ucnt - 1;
return hset;
}
if (!curobj)
return hset;
};
return hset;
} /* end hashsetobj_remove_BM */
const setval_tyBM *
hashsetobj_to_set_BM (struct hashsetobj_stBM *hset)
{
if (valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
return NULL;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
if (ucnt == 0)
return makeset_BM (NULL, 0);
const objectval_tyBM *tinyarr[TINYSIZE_BM] = { };
const objectval_tyBM **arr =
(ucnt <
TINYSIZE_BM) ? tinyarr : (calloc (ucnt, sizeof (objectval_tyBM *)));
if (!arr)
FATAL_BM ("calloc %u objptrs failed %m", ucnt);
unsigned elcnt = 0;
for (unsigned ix = 0; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (!curobj || curobj == HASHEMPTYSLOT_BM)
continue;
ASSERT_BM (valtype_BM ((const value_tyBM) curobj) == tyObject_BM);
ASSERT_BM (elcnt < ucnt);
arr[elcnt++] = (const objectval_tyBM *) curobj;
};
ASSERT_BM (elcnt == ucnt);
const setval_tyBM *set = makeset_BM (arr, elcnt);
if (arr != tinyarr)
free (arr), arr = NULL;
return set;
} /* end hashsetobj_to_set_BM */
// return the index of some element, or -1 if none
static int hashsetobj_random_index_BM (struct hashsetobj_stBM *hset);
int
hashsetobj_random_index_BM (struct hashsetobj_stBM *hset)
{
if (!hset)
return -1;
ASSERT_BM (valtype_BM ((const value_tyBM) hset) == typayl_hashsetobj_BM);
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
if (ucnt == 0)
return -1;
ASSERT_BM (alsiz > 2);
unsigned startix = g_random_int () % alsiz;
for (unsigned ix = startix; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (!curobj || curobj == HASHEMPTYSLOT_BM)
continue;
ASSERT_BM (valtype_BM ((const value_tyBM) curobj) == tyObject_BM);
return (int) ix;
};
for (unsigned ix = 0; ix < startix; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (!curobj || curobj == HASHEMPTYSLOT_BM)
continue;
ASSERT_BM (valtype_BM ((const value_tyBM) curobj) == tyObject_BM);
return (int) ix;
}
return -1;
} /* end hashsetobj_random_index_BM */
// pick some random element, and keep it in the hashset
objectval_tyBM *
hashsetobj_pick_random_BM (struct hashsetobj_stBM *hset)
{
if (!hset || valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
return NULL;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
if (ucnt == 0)
return NULL;
ASSERT_BM (alsiz > 2 && ucnt < alsiz);
int ix = hashsetobj_random_index_BM (hset);
if (ix < 0)
return NULL;
ASSERT_BM (ix < (int) alsiz);
objectval_tyBM *curob = hset->hashset_objs[ix];
ASSERT_BM (isobject_BM ((value_tyBM) curob));
return curob;
} /* end hashsetobj_pick_random_BM */
// take some random element, and remove it from the hashset - without reorganizing it
objectval_tyBM *
hashsetobj_take_random_BM (struct hashsetobj_stBM *hset)
{
if (!hset || valtype_BM ((const value_tyBM) hset) != typayl_hashsetobj_BM)
return NULL;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
if (ucnt == 0)
return NULL;
ASSERT_BM (alsiz > 2 && ucnt < alsiz);
int ix = hashsetobj_random_index_BM (hset);
if (ix < 0)
return NULL;
ASSERT_BM (ix < (int) alsiz);
objectval_tyBM *curob = hset->hashset_objs[ix];
ASSERT_BM (isobject_BM ((value_tyBM) curob));
hset->hashset_objs[ix] = HASHEMPTYSLOT_BM;
((typedsize_tyBM *) hset)->size = ucnt - 1;
return curob;
} /* end hashsetobj_take_random_BM */
void
hashsetgcmark_BM (struct garbcoll_stBM *gc, struct hashsetobj_stBM *hset,
objectval_tyBM * fromob)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (valtype_BM ((const value_tyBM) hset) == typayl_hashsetobj_BM);
ASSERT_BM (fromob == NULL || isobject_BM (fromob));
uint8_t oldmark = ((typedhead_tyBM *) hset)->hgc;
if (oldmark)
return;
((typedhead_tyBM *) hset)->hgc = MARKGC_BM;
gc->gc_nbmarks++;
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
unsigned ucnt = ((typedsize_tyBM *) hset)->size;
unsigned elcnt = 0;
for (unsigned ix = 0; ix < alsiz; ix++)
{
objectval_tyBM *curobj = hset->hashset_objs[ix];
if (!curobj || curobj == HASHEMPTYSLOT_BM)
continue;
ASSERT_BM (valtype_BM ((const value_tyBM) curobj) == tyObject_BM);
ASSERT_BM (elcnt < ucnt);
gcobjmark_BM (gc, curobj);
elcnt++;
};
ASSERT_BM (elcnt == ucnt);
} /* end hashsetgcmark_BM */
void
hashsetgcdestroy_BM (struct garbcoll_stBM *gc, struct hashsetobj_stBM *hset)
{
ASSERT_BM (gc && gc->gc_magic == GCMAGIC_BM);
ASSERT_BM (((typedhead_tyBM *) hset)->htyp == typayl_hashsetobj_BM);
unsigned alsiz = ((typedhead_tyBM *) hset)->rlen;
memset (hset, 0, sizeof (*hset) + alsiz * sizeof (void *));
free (hset);
gc->gc_freedbytes += sizeof (*hset) + alsiz * sizeof (void *);
} /* end hashsetgcdestroy_BM */
void
hashsetgckeep_BM (struct garbcoll_stBM *gc, struct hashsetobj_stBM *hset)