-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRefpersys.h
1248 lines (1041 loc) · 44.4 KB
/
Refpersys.h
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 Refpersys.h
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
* It is almost its only public C11 header file.
*
*
* © Copyright 2019 - 2022 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* 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/>.
******************************************************************************/
#ifndef REFPERSYS_INCLUDED
#define REFPERSYS_INCLUDED
// A name containing `unsafe` refers to something which should be used
// with great caution.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif /*_GNU_SOURCE*/
#include <argp.h>
#include <ctype.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/syslog.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <dlfcn.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/personality.h>
#include <stddef.h>
#include <stdint.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdalign.h>
#include <math.h>
#include <limits.h>
#include <setjmp.h>
// See also the important comment about pthreads in file agenda_rps.c
// man7.org/linux/man-pages/man3/gnu_get_libc_version.3.html
#include <gnu/libc-version.h>
// GNU libunistring www.gnu.org/software/libunistring/
// we use UTF-8 strings
#include <unicode/unistr.h>
/// Ian Taylor's libbacktrace github.com/ianlancetaylor/libbacktrace
#include "backtrace.h"
/// GTK3 graphical toolkit gtk.org/gtk3/
#include "gtk/gtk.h"
/// CURL library for HTTP web client curl.se/libcurl/
#include "curl/curl.h"
/// libjansson JSON library https://digip.org/jansson/
#include "jansson.h"
/* Adelson-Velsky and Landis generic balanced trees from
http://attractivechaos.github.io/klib/ see also
https://en.wikipedia.org/wiki/AVL_tree */
#include "kavl.h"
/// global variables
extern bool rps_running_in_batch; /* no user interface */
extern bool rps_showing_version;
extern bool rps_with_gui;
/// number of agenda threads
#define RPS_MIN_NB_THREADS 2
#define RPS_MAX_NB_THREADS 16
extern int rps_nb_threads;
extern struct backtrace_state *rps_backtrace_common_state;
extern const char *rps_progname; /* argv[0] of main */
extern void *rps_dlhandle; /* global dlopen handle */
extern const char *rps_load_directory;
extern const char *rps_dump_directory;
/// global variables declared in generated __timestamp.c file:
extern const char _rps_git_id[];
extern const char _rps_git_last_tag[];
extern const char _rps_git_last_commit[];
extern const char _rps_git_short_id[];
extern const char _rps_git_remote_origin_url[];
extern const char rps_timestamp[]; /* some date string */
extern const unsigned long rps_timelong; /* some (time_t) */
extern const char rps_topdirectory[]; /* the source topdirectory */
extern const char rps_md5sum[];
extern const char *const rps_files[];
extern const char rps_makefile[];
extern const char *rps_subdirectories[];
extern const char rps_c_compiler_version[];
extern const char *rps_hostname (void);
//// prime numbers and a table of primes:
extern const unsigned rps_nb_primes_in_tab;
extern int64_t rps_prime_of_index (int ix);
extern int rps_index_of_prime (int64_t n);
extern int64_t rps_prime_above (int64_t n);
extern int64_t rps_prime_below (int64_t n);
extern int rps_randomize_va_space; /// from /proc/sys/kernel/randomize_va_space
//// see also https://man7.org/linux/man-pages/man5/proc.5.html
/////////////////////////////////////////////////////////
///// DEBUGGING SUPPORT
extern void rps_set_debug (const char *dbglev);
/// keep the debug options in alphabetical order
#define RPS_DEBUG_OPTIONS(dbgmacro) \
dbgmacro(CMD) \
dbgmacro(CODEGEN) \
dbgmacro(DUMP) \
dbgmacro(GARBCOLL) \
dbgmacro(GUI) \
dbgmacro(LOAD) \
dbgmacro(LOWREP) \
dbgmacro(MISC) \
dbgmacro(MSGSEND) \
dbgmacro(PARSE) \
dbgmacro(REPL) \
dbgmacro(WEB)
/*end RPS_DEBUG_OPTIONS */
#define RPS_DEBUG_OPTION_DEFINE(dbgopt) RPS_DEBUG_##dbgopt,
extern unsigned rps_debug_flags;
enum Rps_Debug
{
RPS_DEBUG__NONE,
// expands to RPS_DEBUG_CMD, RPS_DEBUG_DUMP, RPS_DEBUG_GARBAGE_COLLECTOR...
RPS_DEBUG_OPTIONS (RPS_DEBUG_OPTION_DEFINE) RPS_DEBUG__LAST
};
// so we could code RPS_DEBUG_PRINTF(NEVER, ....)
#define RPS_DEBUG_NEVER RPS_DEBUG__NONE
#define RPS_DEBUG_ENABLED(dbgopt) (rps_debug_flags & (1 << RPS_DEBUG_##dbgopt))
/// debug print to stderr; if FLINE is negative, print a
/// newline before.... If FMT starts with a |, print the backtrace...
void
rps_debug_printf_at (const char *fname, int fline, enum Rps_Debug dbgopt,
const char *fmt, ...)
__attribute__((format (printf, 4, 5)));
#define RPS_DEBUG_PRINTF_AT(fname, fline, dbgopt, fmt, ...) \
do \
{ \
if (RPS_DEBUG_ENABLED(dbgopt)) \
rps_debug_printf_at(fname, fline, RPS_DEBUG_##dbgopt, fmt, \
##__VA_ARGS__); \
} \
while (0)
#define RPS_DEBUG_PRINTF_AT_BIS(fname, fline, dbgopt, fmt, ...) \
RPS_DEBUG_PRINTF_AT(fname, fline, dbgopt, fmt, ##__VA_ARGS__)
#define RPS_DEBUG_PRINTF(dbgopt, fmt, ...) \
RPS_DEBUG_PRINTF_AT_BIS(__FILE__, __LINE__, dbgopt, fmt, ##__VA_ARGS__)
#define RPS_DEBUG_NLPRINTF(dbgopt, fmt, ...) \
RPS_DEBUG_PRINTF_AT_BIS(__FILE__, -__LINE__, dbgopt, fmt, ##__VA_ARGS__)
/// both backtrace_full and backtrace_simple callbacks are continuing with a 0 return code:
enum RpsBacktrace
{
RPS_BACKTRACE_CONTINUE = 0,
RPS_BACKTRACE_STOP = 1
};
/// value types - prefix is RPS_TYPE
enum RpsType
{
RPS_TYPE__NONE,
RPS_TYPE_INT /*#1 */ , // tagged int, 63 bits, without memory zone */
// the following are in garbage collected memory, our zoned values
RPS_TYPE_DOUBLE /*#2 */ ,
RPS_TYPE_STRING /*#3 */ ,
RPS_TYPE_JSON /*#4 */ ,
RPS_TYPE_GTKWIDGET /*#5 */ , // some GtkWidget* pointer; of course GTK widgets are not persisted
RPS_TYPE_TUPLE /*#6 */ ,
RPS_TYPE_SET /*#7 */ ,
RPS_TYPE_CLOSURE /*#8 */ ,
RPS_TYPE_OBJECT /*#9 */ ,
RPS_TYPE_FILE /*#10 */ , // some opened FILE* handle; of course they are not persisted
RPS_TYPE__LAST
};
const char *rps_type_str (int ty);
/* maximal value depth before encountering objects:*/
#define RPS_MAX_VALUE_DEPTH 64
/// an hash has 32 bits and conventionally is never 0
typedef uint32_t RpsHash_t;
typedef struct RpsZoneObject_st RpsObject_t; ///// forward declaration
typedef struct RpsPayl_AttrTable_st RpsAttrTable_t; //// forward declaration
typedef struct RpsClosure_st RpsClosure_t; //// forward declaration
/// callback function, by convention returning false to "stop" or "fail" e.g. some iteration
typedef bool rps_object_callback_sig_t (RpsObject_t * ob, void *data);
/// a value is a word, sometimes a pointer, sometimes a tagged integer (odd word)
typedef uintptr_t RpsValue_t;
/// callback function, by convention returning false to "stop" or "fail" e.g. some iteration
typedef bool rps_value_callback_sig_t (RpsValue_t val, void *data);
#define RPS_NULL_VALUE ((RpsValue_t)0)
extern enum RpsType rps_value_type (RpsValue_t val);
extern void rps_verify_value (RpsValue_t val, unsigned depth);
extern void rps_verify_object_and_payload (RpsObject_t * ob);
/// the loader internals are in file load_rps.c
typedef struct RpsPayl_Loader_st RpsLoader_t; ///// forward declaration
extern bool rps_is_valid_loader (RpsLoader_t * ld);
extern bool rps_is_valid_filling_loader (RpsLoader_t * ld);
extern bool rps_is_valid_creating_loader (RpsLoader_t * ld);
extern unsigned rps_loader_nb_globals (RpsLoader_t * ld);
extern unsigned rps_loader_nb_constants (RpsLoader_t * ld);
extern RpsValue_t rps_loader_json_to_value (RpsLoader_t * ld, json_t * jv);
extern RpsObject_t *rps_loader_json_to_object (RpsLoader_t * ld, json_t * jv);
//// Signature of extern "C" functions dlsymed for payload loading;
//// their name starts with rpsldpy_ and the object has been locked..
typedef void rpsldpysig_t (RpsObject_t * obz, RpsLoader_t * ld,
const json_t * jv, int spaceindex);
#define RPS_PAYLOADING_PREFIX "rpsldpy_"
/// the dumper internals are in file dump_rps.c
typedef struct RpsPayl_Dumper_st RpsDumper_t; ///// forward declaration
extern bool rps_is_valid_dumper (RpsDumper_t * du);
enum rps_dump_state_en
{
//// this enumeration needs to be in generated C code of course....
rpsdumpstate__NONE,
rpsdumpstate_scanning,
rpsdumpstate_dumpingdata,
rpsdumpstate_emittingcode,
rpsdumpstate__HIGH
};
extern enum rps_dump_state_en rps_dumper_state (RpsDumper_t * du);
extern void rps_dumper_scan_value (RpsDumper_t * du, RpsValue_t val,
unsigned depth);
extern void rps_dumper_scan_object (RpsDumper_t * du, RpsObject_t * ob);
extern void rps_dumper_scan_internal_object (RpsDumper_t * du,
RpsObject_t * ob);
extern json_t *rps_dump_json_for_value (RpsDumper_t * du, RpsValue_t val,
unsigned depth);
extern json_t *rps_dump_json_for_object (RpsDumper_t * du,
const RpsObject_t * ob);
///// hash of strings
extern RpsHash_t rps_hash_cstr (const char *s);
#include "include/oid_rps.h"
struct rps_owned_payload_st;
/// A payload is not a proper value, but garbaged collected as if it was one....
/// payload types - prefix is RpsPyt. In memory we used negative payload indexes...
enum
{
//// this enumeration needs to be in generated C code of course....
RpsPyt__NONE,
RpsPyt_CallFrame, /* #1 call frames */
RpsPyt_Loader, /* #2 the initial loader */
RpsPyt_AttrTable, /* #3 associate objects to values */
RpsPyt_StringBuf, /* #4 mutable string buffer */
RpsPyt_Symbol, /* #5 symbol */
RpsPyt_ClassInfo, /* #6 class information */
RpsPyt_MutableSetOb, /* #7 mutable set of objects */
RpsPyt_DequeOb, /* #8 double ended queue of objects */
RpsPyt_Tasklet, /* #9 tasklet in agenda */
RpsPyt_Agenda, /* #10 the agenda */
RpsPyt_StringDict, /* #11 string dictionary associating names
to values */
RpsPyt_HashTblObj, /* #12 hashtable of objects */
RpsPyt_Space, /* #13 space payload */
RpsPyt_Dumper, /* #14 dumper payload */
RpsPyt__LAST
};
// the maximal index is for internal arrays, and allow for more than
// ten more payload types to be added during a run.
#define RPS_MAX_PAYLOAD_TYPE_INDEX (((RpsPyt__LAST+15)|0xf)+1)
/****************************************************************
* Garbage collected memory zones. Most boxed values or payloads are
* starting with these...
****************************************************************/
/// the fields in every zoned memory -value or payload-; we use macros to mimic C field inheritance
#define RPSFIELDS_ZONED_MEMORY \
atomic_schar zm_atype; /* the type of that zone - value (>0) or payload (<0) */ \
atomic_uchar zm_gcmark; /* the garbage collector mark */ \
uint16_t zm_xtra; /* some short extra data */ \
uint32_t zm_length; /* the size of variable-sized data */ \
volatile void* zm_gclink /* a pointer for our naive GC */
struct RpsZonedMemory_st
{
RPSFIELDS_ZONED_MEMORY;
};
static inline int8_t
rps_zoned_memory_type (const void *ad)
{
if (!ad)
return 0;
return atomic_load (&((struct RpsZonedMemory_st *) ad)->zm_atype);
} /* end rps_zoned_memory_type */
#define RPS_ZONED_MEMORY_TYPE(Ad) rps_zoned_memory_type((const void*)(Ad))
static inline unsigned char
rps_zoned_memory_gcmark (const void *ad)
{
if (!ad)
return 0;
return atomic_load (&((struct RpsZonedMemory_st *) ad)->zm_gcmark);
} /* end rps_zoned_memory_gcmark */
/// for debugging, a routine verifying all the objects in the heap:
extern void rps_verify_heap_at (const char *fil, int lin);
#define RPS_VERIFY_HEAP() do { rps_verify_heap_at(__FILE__,__LINE__); \
} while(0)
/// zoned values all have some non-zero hash
#define RPSFIELDS_ZONED_VALUE \
RPSFIELDS_ZONED_MEMORY; \
RpsHash_t zv_hash
struct RpsZonedValue_st
{
RPSFIELDS_ZONED_VALUE;
};
/****************************************************************
* Boxed double values
****************************************************************/
#define RPSFIELDS_DOUBLE \
RPSFIELDS_ZONED_VALUE; \
double dbl_val
struct RpsZoneDouble_st
{
RPSFIELDS_DOUBLE;
};
typedef struct RpsZoneDouble_st RpsDouble_t; /*for RPS_TYPE_DOUBLE, zm_length is unused */
RpsHash_t rps_hash_double (double x);
// allocate a boxed double which is not NAN, fatal if NAN
const RpsDouble_t *rps_alloc_boxed_double (double x);
// load a boxed double
const RpsDouble_t *rps_load_boxed_double (json_t * js, RpsLoader_t * ld);
// return NAN if not double
double rps_double_value (RpsValue_t val);
/****************************************************************
* Boxed string values; their zm_xtra is an index of a prime allocated
* size.
****************************************************************/
#define RPSFIELDS_STRING \
RPSFIELDS_ZONED_VALUE; \
char cstr[]; /* flexible array zone, zm_length is length in UTF8 characters, not in bytes */
struct RpsZoneString_st
{
RPSFIELDS_STRING;
};
typedef struct RpsZoneString_st RpsString_t; /* for RPS_TYPE_STRING */
// allocate a string
const RpsString_t *rps_alloc_string (const char *str);
// sprintf a string value
const RpsString_t *rps_sprintf_string (const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
// load a string
const RpsString_t *rps_load_string (json_t * js, RpsLoader_t * ld);
// get the UTF8 bytes of a string value, or else NULL
const char *rps_stringv_utf8bytes (RpsValue_t v);
// get the length, in Unicode glyphs, of a string value
unsigned rps_stringv_utf8length (RpsValue_t v);
// get the hashcode of a string value
RpsHash_t rps_stringv_hash (RpsValue_t v);
/****************************************************************
* Boxed JSON values.
****************************************************************/
#define RPSFIELDS_JSON \
RPSFIELDS_ZONED_VALUE; \
const json_t *json
struct RpsZoneJson_st
{
RPSFIELDS_JSON;
};
typedef struct RpsZoneJson_st RpsJson_t; /* for RPS_TYPE_JSON */
const RpsJson_t *rps_alloc_json (const json_t * js);
const RpsJson_t *rps_load_json (const json_t * js, RpsLoader_t * ld);
const json_t *rps_json_value (RpsValue_t val);
/****************************************************************
* Boxed GtkWidget-s values, they cannot be persisted in the heap.
****************************************************************/
/////////////// boxed GtkWidget*
#define RPSFIELDS_GTKWIDGET \
RPSFIELDS_ZONED_VALUE; \
GtkWidget*gtk_widget
struct RpsZoneGtkWidget_st
{
RPSFIELDS_GTKWIDGET;
};
typedef struct RpsZoneGtkWidget_st RpsGtkWidget_t; /* for RPS_GTKWIDGET */
RpsValue_t rps_alloc_gtk_widget (GtkWidget *);
GtkWidget *rps_gtk_widget_value (RpsValue_t val);
// no load routine for GtkWidget
/****************************************************************
* Tuple of objects value.
****************************************************************/
#define RPSFIELDS_TUPLEOB \
RPSFIELDS_ZONED_VALUE; \
RpsObject_t* tuple_comp[] /* zm_length is the number of components */
struct RpsZoneTupleOb_st
{
RPSFIELDS_TUPLEOB;
};
typedef struct RpsZoneTupleOb_st RpsTupleOb_t; /* for RPS_TYPE_TUPLE */
unsigned rps_vtuple_size (const RpsTupleOb_t * tup);
RpsObject_t *rps_vtuple_nth (const RpsTupleOb_t * tup, int rk);
const RpsTupleOb_t *rps_alloc_vtuple (unsigned arity, /*objects */ ...);
const RpsTupleOb_t *rps_alloc_tuple_sized (unsigned arity,
RpsObject_t ** arr);
const RpsTupleOb_t *rps_load_tuple (const json_t * js, RpsLoader_t * ld);
/****************************************************************
* Ordered set of objects value.
****************************************************************/
#define RPSFIELDS_SETOB \
RPSFIELDS_ZONED_VALUE; \
const RpsObject_t* set_elem[] /* zm_length is the number of elements, and they are ordered by oid */
struct RpsZoneSetOb_st
{
RPSFIELDS_SETOB;
};
typedef struct RpsZoneSetOb_st RpsSetOb_t; /* for RPS_TYPE_SET */
const RpsSetOb_t *rps_alloc_vset (unsigned card, /*objects */ ...);
const RpsSetOb_t *rps_alloc_set_sized (unsigned nbcomp,
const RpsObject_t ** arr);
const RpsSetOb_t *rps_load_set (const json_t * js, RpsLoader_t * ld);
// return the index of an element or -1 if non member
int rps_set_index (const RpsSetOb_t * setv, const RpsObject_t * ob);
// test membership
bool rps_set_contains (const RpsSetOb_t * setv, const RpsObject_t * ob);
// cardinal of a set
unsigned rps_set_cardinal (const RpsSetOb_t * setv);
/// get the N-th member of a set; if N<0 count from last.
const RpsObject_t *rps_set_nth_member (const RpsSetOb_t * setv, int n);
/****************************************************************
* Closures. The connective of a closure is an object whose
* ob_routsig and ob_routaddr are set. When the signature is
* appropriate, the function pointed by ob_routaddr is called when
* applying the closure.
****************************************************************/
#define RPSFIELDS_CLOSURE \
RPSFIELDS_ZONED_VALUE; \
RpsObject_t* clos_conn; \
RpsValue_t clos_meta; \
RpsValue_t clos_val[] /*of zm_length */
#define RPS_CLOSURE_MAX_NB_VALUE 1024
struct RpsClosure_st
{
RPSFIELDS_CLOSURE;
};
typedef struct RpsClosure_st RpsClosure_t;
const RpsClosure_t *rps_closure_make (RpsObject_t * conn, unsigned arity,
...);
const RpsClosure_t *rps_closure_meta_make (RpsObject_t * conn,
RpsValue_t meta, unsigned arity,
...);
const RpsClosure_t *rps_closure_array_make (RpsObject_t * conn,
RpsValue_t meta, unsigned arity,
RpsValue_t * cvalarr);
const RpsObject_t *rps_closure_connective (RpsValue_t val);
RpsValue_t rps_closure_get_closed_value (RpsValue_t val, int ix);
RpsValue_t rps_closure_meta (RpsValue_t);
unsigned rps_closure_size (RpsValue_t);
/* TODO: closure application code should be declared. The ob_routaddr
of clos_conn is holding the C function pointer to be called. The
ob_routsig should be checked. Closure application code should
carefully be tail-call friendly. */
typedef struct rps_value_and_int_st
{
RpsValue_t val;
intptr_t num;
} RpsValueAndInt;
typedef struct rps_two_values_st
{
RpsValue_t main_val;
RpsValue_t xtra_val;
} RpsTwoValues;
typedef struct rps_protocallframe_st rps_callframe_t;
/*****************************************************************
* APPLYING FUNCTIONS
*
* Notice that on x86/64 calling conventions only six arguments are
* passed by registers.
*
****************************************************************/
typedef RpsValue_t rps_apply_v_sigt (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsValue_t arg0, RpsValue_t arg1,
RpsValue_t arg2, RpsValue_t arg3);
RpsValue_t rps_closure_apply_v (rps_callframe_t * callerframe,
const RpsClosure_t * clos, RpsValue_t arg0,
RpsValue_t arg1, RpsValue_t arg2,
RpsValue_t arg3);
typedef RpsValueAndInt rps_apply_vi_sigt (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsValue_t arg0, RpsValue_t arg1,
RpsValue_t arg2, RpsValue_t arg3);
RpsValueAndInt rps_closure_apply_vi (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsValue_t arg0, RpsValue_t arg1,
RpsValue_t arg2, RpsValue_t arg3);
typedef RpsTwoValues rps_apply_twov_sigt (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsValue_t arg0, RpsValue_t arg1,
RpsValue_t arg2, RpsValue_t arg3);
RpsTwoValues rps_closure_apply_twov (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsValue_t arg0, RpsValue_t arg1,
RpsValue_t arg2, RpsValue_t arg3);
typedef intptr_t rps_apply_i_sigt (rps_callframe_t * callerframe,
RpsClosure_t * clos, RpsValue_t arg0,
RpsValue_t arg1, RpsValue_t arg2,
RpsValue_t arg3);
intptr_t rps_closure_apply_i (rps_callframe_t * callerframe,
RpsClosure_t * clos, RpsValue_t arg0,
RpsValue_t arg1, RpsValue_t arg2,
RpsValue_t arg3);
/// the dumper needs to apply dumping closures to unboxed and modified
/// json_t... By convention, the returned value is non-null if JS has
/// been filled/modified....
typedef RpsValue_t rps_apply_dumpj_sigt (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsDumper_t * du,
RpsValue_t dumpedval, json_t * js);
RpsValue_t rps_closure_apply_dumpj (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsDumper_t * du,
RpsValue_t dumpedval, json_t * js);
/****************************************************************
* Boxed FILE handle.
****************************************************************/
#define RPSFIELDS_FILE \
RPSFIELDS_ZONED_VALUE; \
/* we probably need more fields here... */ \
FILE*fileh;
struct RpsZoneFile_st
{
RPSFIELDS_FILE;
};
typedef struct RpsZoneFile_st RpsFile_t; /* for RPS_TYPE_FILE */
// allocate a file handle
const RpsFile_t *rps_alloc_plain_file (FILE * f);
FILE *rps_file_of_value (RpsValue_t val);
/// printing oids
extern int rps_fprint_oid (FILE * fil, RpsOid oid);
extern int rps_print_oid (RpsOid oid);
/****************************************************************
* Mutable and mutexed heavy objects.
* ==================================
*
*
* The ob_class is an object representing the RefPerSys class of our
* object. The ob_space is its space. Different spaces are persisted
* in different files. The ob_attrtable is for the attributes of that
* object. The ob_routsig and ob_routaddr is for the routine inside
* that object, e.g. when that object is a closure connective. The
* ob_nbcomp is the used length of the component array ob_comparr,
* whose allocated size is ob_compsize. The ob_payload is the
* optional object payload.
****************************************************************/
#define RPSFIELDS_OBJECT \
RPSFIELDS_ZONED_VALUE; \
RpsOid ob_id; \
double ob_mtime; \
long ob_magic /*should be RPS_OBJ_MAGIC*/; \
pthread_mutex_t ob_mtx; \
RpsObject_t* ob_class; \
RpsObject_t* ob_space; \
RpsAttrTable_t* ob_attrtable /*unowned!*/; \
RpsObject_t* ob_routsig /* signature of routine */; \
void* ob_routaddr; /* dlsymed address of routine */ \
unsigned ob_nbcomp; \
unsigned ob_compsize; \
RpsValue_t*ob_comparr; \
void* ob_payload
#define RPS_OBJ_MAGIC 0x4558ca5921f /*47478392351 */
#define RPS_MAX_NB_OBJECT_COMPONENTS (1U<<20)
struct RpsZoneObject_st
{
RPSFIELDS_OBJECT;
};
struct internal_rootob_node_rps_st
{
RpsObject_t *rootobrps_obj;
KAVL_HEAD (struct internal__rootob_node_rps_st) rootobrps_head;
};
extern void rps_initialize_objects_machinery (void);
extern void rps_initialize_objects_for_loading (RpsLoader_t * ld,
unsigned nbglobroot);
extern bool rps_is_valid_object (RpsObject_t * obj);
extern bool rps_object_less (RpsObject_t * ob1, RpsObject_t * ob2);
extern int rps_object_cmp (const RpsObject_t * ob1, const RpsObject_t * ob2);
extern void rps_object_array_qsort (const RpsObject_t ** arr, int size);
extern RpsObject_t *rps_create_object_of_class (const RpsObject_t * obclass);
extern RpsObject_t *rps_find_object_by_oid (const RpsOid oid);
extern RpsObject_t *rps_get_loaded_object_by_oid (RpsLoader_t * ld,
const RpsOid oid);
extern RpsValue_t rps_get_object_attribute (RpsObject_t * ob,
RpsObject_t * obattr);
extern RpsValue_t rps_get_object_component (RpsObject_t * ob, int ix);
// In a given object, get its payload if it has type paylty; accepts
// any payload if paylty is 0. For example:
// rps_get_object_payload_of_type(obclass, RpsPyt_ClassInfo);
extern void *rps_get_object_payload_of_type (RpsObject_t * ob, int paylty);
extern void rps_check_all_objects_buckets_are_valid (void);
extern void rps_object_reserve_components (RpsObject_t * obj,
unsigned nbcomp);
extern void rps_add_global_root_object (RpsObject_t * obj);
extern void rps_remove_global_root_object (RpsObject_t * obj);
extern unsigned rps_nb_global_root_objects (void);
extern const RpsSetOb_t *rps_set_of_global_root_objects (void);
/* Verify the payload of some already locked object. This might be
called for debugging reasons by some future verifying heap
routine.... */
extern void rps_verify_locked_object_payload (RpsObject_t * ob, int paylty,
void *payl);
/// function to dump object attributes, has a signature compatible with rps_apply_dumpj_sigt
extern RpsValue_t
rpscloj_dump_object_attributes (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsDumper_t * du,
RpsValue_t dumpedobv, json_t * js);
/// function to dump object components, has a signature compatible with rps_apply_dumpj_sigt
extern RpsValue_t
rpscloj_dump_object_components (rps_callframe_t * callerframe,
const RpsClosure_t * clos,
RpsDumper_t * du,
RpsValue_t dumpedobv, json_t * js);
/****************************************************************
* Common superfields of owned payloads
****************************************************************/
#define RPSFIELDS_OWNED_PAYLOAD \
RPSFIELDS_ZONED_MEMORY; \
RpsObject_t* payl_owner
/// By convention, the zm_atype of payload is a small negative index, e.g. some RpsPyt_*
struct rps_owned_payload_st
{
RPSFIELDS_OWNED_PAYLOAD;
};
void rps_object_put_payload (RpsObject_t * ob, void *payl);
typedef void rps_payload_remover_t (RpsObject_t *,
struct rps_owned_payload_st *,
void *data);
extern void rps_register_payload_removal (int paylty,
rps_payload_remover_t * rout,
void *data);
typedef void rps_payload_dump_scanner_t (RpsDumper_t * du,
struct rps_owned_payload_st
*payl, void *data);
extern void rps_register_payload_dump_scanner (int paylty,
rps_payload_dump_scanner_t
* rout, void *data);
typedef void rps_payload_dump_serializer_t (RpsDumper_t * du,
struct rps_owned_payload_st
*payl, json_t * json, void *data);
extern void rps_register_payload_dump_serializer (int paylty, rps_payload_dump_serializer_t * rout, ///
void *data);
typedef void rps_payload_verifier_t (RpsObject_t * ob,
struct rps_owned_payload_st *payl,
void *data);
extern void rps_register_payload_verifier (int paylty,
rps_payload_verifier_t *,
void *data);
extern void rps_dump_scan_object_payload (RpsDumper_t * du, RpsObject_t * ob);
extern void rps_dump_serialize_object_payload (RpsDumper_t * du,
RpsObject_t * ob,
json_t * jsob);
extern bool rps_is_dumpable_value (RpsDumper_t * du, RpsValue_t val);
extern bool rps_is_dumpable_object (RpsDumper_t * du, RpsObject_t * ob);
/****************************************************************
* Payload for table of ordered attributes (objects) associated to
* values.
****************************************************************/
/////////////// table of attributes (objects) with their values
/////////////// entries are either empty or sorted by ascending attributes
struct rps_attrentry_st
{
RpsObject_t *ent_attr;
RpsValue_t ent_val;
};
#define RPS_MAX_NB_ATTRS (1U<<28)
//// the zm_xtra is a prime index for the allocated size
//// the zm_length is the actual number of non-empty entries
#define RPSFIELDS_ATTRTABLE \
RPSFIELDS_OWNED_PAYLOAD; \
struct rps_attrentry_st attr_entries[]
///// for RpsAttrTable_t
struct RpsPayl_AttrTable_st
{
RPSFIELDS_ATTRTABLE;
};
extern RpsAttrTable_t *rps_alloc_empty_attr_table (unsigned size);
extern RpsValue_t rps_attr_table_find (const RpsAttrTable_t * tbl,
RpsObject_t * obattr);
/// These functions could sometimes re-allocate a new table and free the old one ...
extern RpsAttrTable_t *rps_attr_table_put (RpsAttrTable_t * tbl,
RpsObject_t * obattr,
RpsValue_t val);
extern RpsAttrTable_t *rps_attr_table_remove (RpsAttrTable_t * tbl,
RpsObject_t * obattr);
extern unsigned rps_attr_table_size (const RpsAttrTable_t * tbl);
extern unsigned rps_attr_table_iterate (const RpsAttrTable_t * tbl,
rps_object_callback_sig_t * routattr,
rps_value_callback_sig_t * routval,
void *data);
extern const RpsSetOb_t *rps_attr_table_set_of_attributes (const
RpsAttrTable_t
* tbl);
/****************************************************************
* Owned symbol payload
****************************************************************/
typedef struct RpsPayl_Symbol_st RpsSymbol_t;
struct internal_symbol_node_rps_st
{
RpsSymbol_t *synodrps_symbol;
KAVL_HEAD (struct internal_symbol_node_rps_st) synodrps_head;
};
#define RPSFIELDS_PAYLOAD_SYMBOL \
RPSFIELDS_OWNED_PAYLOAD; \
const RpsString_t* symb_name; \
RpsValue_t symb_value
///// for RpsPyt_Symbol
struct RpsPayl_Symbol_st
{
RPSFIELDS_PAYLOAD_SYMBOL;
};
typedef struct RpsPayl_Symbol_st RpsSymbol_t;
extern RpsSymbol_t *rps_find_symbol (const char *name);
extern RpsSymbol_t *rps_register_symbol (const char *name);
extern rps_payload_remover_t rps_symbol_payload_remover;
extern rps_payload_dump_scanner_t rps_symbol_payload_dump_scanner;
extern rps_payload_dump_serializer_t rps_symbol_payload_dump_serializer;
/****************************************************************
* Class information payload for RpsPyt_ClassInfo
****************************************************************/
#define RPSFIELDS_PAYLOAD_CLASSINFO \
RPSFIELDS_OWNED_PAYLOAD; \
uint64_t pclass_magic /*always RPS_CLASSINFO_MAGIC*/; \
RpsObject_t* pclass_super /*:superclass*/; \
RpsAttrTable_t *pclass_methdict/*:method dictionary*/; \
RpsObject_t* pclass_symbol /*:optional symbol */
#define RPS_CLASSINFO_MAGIC 0x3d3c6b284031d237UL
struct RpsPayl_ClassInfo_st
{
RPSFIELDS_PAYLOAD_CLASSINFO;
};
typedef struct RpsPayl_ClassInfo_st RpsClassInfo_t;
extern bool rps_is_valid_classinfo (const RpsClassInfo_t * clinf);
extern RpsObject_t *rps_classinfo_super (const RpsClassInfo_t * clinf);
extern RpsObject_t *rps_classinfo_symbol (const RpsClassInfo_t * clinf);
extern RpsAttrTable_t *rps_classinfo_methdict (const RpsClassInfo_t * clinf);
extern RpsClosure_t *rps_classinfo_get_method (const RpsClassInfo_t *
clinf, RpsObject_t * selob);
extern RpsObject_t *rps_obclass_super (RpsObject_t * obcla);
extern RpsObject_t *rps_obclass_symbol (RpsObject_t * obcla);
extern RpsAttrTable_t *rps_obclass_methdict (RpsObject_t * obcla);
extern RpsClosure_t *rps_obclass_get_method (RpsObject_t * obcla,
RpsObject_t * selob);
//// given some non-nil value, return the closure to send a method of given selector
extern RpsClosure_t *rps_value_compute_method_closure (RpsValue_t val,
const RpsObject_t
* selob);
extern rps_payload_remover_t rps_classinfo_payload_remover;
extern rps_payload_dump_scanner_t rps_classinfo_payload_dump_scanner;
extern rps_payload_dump_serializer_t rps_classinfo_payload_dump_serializer;
/****************************************************************
* Mutable ordered set of objects payload for -RpsPyt_MutableSetOb
****************************************************************/
/* Internally we use "kavl.h" */
struct internal_mutable_set_ob_node_rps_st
{
const RpsObject_t *setobnodrps_obelem;
KAVL_HEAD (struct internal_mutable_set_ob_node_rps_st) setobnodrps_head;
};
#define RPSFIELDS_PAYLOAD_MUTABLESETOB \
RPSFIELDS_OWNED_PAYLOAD; \
unsigned muset_card; \
struct internal_mutable_set_ob_node_rps_st* muset_root
///// for RpsPyt_MutableSetOb
struct RpsPayl_MutableSetOb_st
{
RPSFIELDS_PAYLOAD_MUTABLESETOB;
};
typedef struct RpsPayl_MutableSetOb_st RpsMutableSetOb_t;
/// initialize the payload to an empty mutable set
extern void rps_object_mutable_set_initialize (RpsObject_t *);
/// add an object, all objects of a tuple, a set.... into a mutable set
extern void rps_object_mutable_set_add (RpsObject_t * obset, RpsValue_t val);
/// remove an object, all objects of a tuple, a set.... into a mutable set
extern void rps_object_mutable_set_remove (RpsObject_t * obset,
RpsValue_t val);
/// build the set inside a mutable set
extern const RpsSetOb_t *rps_object_mutable_set_reify (RpsObject_t * obset);
extern bool rps_paylsetob_add_element (RpsMutableSetOb_t * paylmset,
const RpsObject_t * ob);
extern bool rps_paylsetob_remove_element (RpsMutableSetOb_t * paylmset,
const RpsObject_t * ob);
extern rps_payload_remover_t rps_setob_payload_remover;
extern rps_payload_dump_scanner_t rps_setob_payload_dump_scanner;
extern rps_payload_dump_serializer_t rps_setob_payload_dump_serializer;
/****************************************************************
* Double-ended queue/linked-list payload for -RpsPyt_DequeOb
****************************************************************/
struct rps_dequeob_link_st;
#define RPSFIELDS_PAYLOAD_DEQUE \
RPSFIELDS_OWNED_PAYLOAD; \
struct rps_dequeob_link_st *deqob_first; \
struct rps_dequeob_link_st *deqob_last
#define RPS_DEQUE_CHUNKSIZE 6
struct rps_dequeob_link_st
{
RpsObject_t *dequeob_chunk[RPS_DEQUE_CHUNKSIZE];
struct rps_dequeob_link_st *dequeob_prev;
struct rps_dequeob_link_st *dequeob_next;
};
struct RpsPayl_DequeOb_st
{
RPSFIELDS_PAYLOAD_DEQUE;
};
typedef struct RpsPayl_DequeOb_st RpsDequeOb_t;
RpsDequeOb_t *rps_deque_for_dumper (RpsDumper_t * du);
/// initialize the payload to an empty double ended queue
extern void rps_object_deque_ob_initialize (RpsObject_t *);
/// The get functions are accessors; the pop functions are shinking
/// the queue. The push functions are growing it and return false on
/// failure.
extern RpsObject_t *rps_object_deque_get_first (RpsObject_t * obq);
extern RpsObject_t *rps_object_deque_pop_first (RpsObject_t * obq);
extern bool rps_object_deque_push_first (RpsObject_t * obq,
RpsObject_t * obelem);
extern RpsObject_t *rps_object_deque_get_last (RpsObject_t * obq);
extern RpsObject_t *rps_object_deque_pop_last (RpsObject_t * obq);
extern bool rps_object_deque_push_last (RpsObject_t * obq,
RpsObject_t * obelem);
extern int rps_object_deque_length (RpsObject_t * obq);
extern RpsObject_t *rps_payldeque_get_first (RpsDequeOb_t * deq);
extern RpsObject_t *rps_payldeque_pop_first (RpsDequeOb_t * deq);
extern bool rps_payldeque_push_first (RpsDequeOb_t * deq,
RpsObject_t * obelem);
extern RpsObject_t *rps_payldeque_get_last (RpsDequeOb_t * deq);
extern RpsObject_t *rps_payldeque_pop_last (RpsDequeOb_t * deq);
extern bool rps_payldeque_push_last (RpsDequeOb_t * deq,
RpsObject_t * obelem);
extern int rps_payldeque_length (RpsDequeOb_t * deq);
/****************************************************************
* Hashtable of objects payload for -RpsPyt_HashTblObj
****************************************************************/
#define RPS_HTB_EMPTY_SLOT ((RpsObject_t*)(-1L))
#define RPS_HTBOB_MAGIC 0x3210d03f /*839962687 */
/* zm_atype should be -RpsPyt_HashTblObj */
/* zm_length is the total number of objects */
/* zm_extra is the prime index of buckets */