-
Notifications
You must be signed in to change notification settings - Fork 3k
/
global.h
1599 lines (1361 loc) · 48.8 KB
/
global.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifndef __GLOBAL_H__
#define __GLOBAL_H__
#include "sys.h"
#include <stddef.h> /* offsetof() */
#include "erl_alloc.h"
#include "erl_vm.h"
#include "erl_node_container_utils.h"
#include "hash.h"
#include "index.h"
#include "atom.h"
#include "code_ix.h"
#include "export.h"
#include "module.h"
#include "register.h"
#include "erl_fun.h"
#include "erl_node_tables.h"
#include "benchmark.h"
#include "erl_process.h"
#include "erl_sys_driver.h"
#include "erl_debug.h"
#include "error.h"
#include "erl_utils.h"
#include "erl_port.h"
#include "erl_gc.h"
struct enif_func_t;
struct enif_environment_t /* ErlNifEnv */
{
struct erl_module_nif* mod_nif;
Process* proc;
Eterm* hp;
Eterm* hp_end;
ErlHeapFragment* heap_frag;
int fpe_was_unmasked;
struct enif_tmp_obj_t* tmp_obj_list;
int exception_thrown; /* boolean */
Process *tracee;
int exiting; /* boolean (dirty nifs might return in exiting state) */
};
extern void erts_pre_nif(struct enif_environment_t*, Process*,
struct erl_module_nif*, Process* tracee);
extern void erts_post_nif(struct enif_environment_t* env);
extern void erts_pre_dirty_nif(ErtsSchedulerData *,
struct enif_environment_t*, Process*,
struct erl_module_nif*, Process* tracee);
extern Eterm erts_nif_taints(Process* p);
extern void erts_print_nif_taints(int to, void* to_arg);
void erts_unload_nif(struct erl_module_nif* nif);
extern void erl_nif_init(void);
extern int erts_nif_get_funcs(struct erl_module_nif*,
struct enif_func_t **funcs);
extern Eterm erts_nif_call_function(Process *p, Process *tracee,
struct erl_module_nif*,
struct enif_func_t *,
int argc, Eterm *argv);
/* Driver handle (wrapper for old plain handle) */
#define ERL_DE_OK 0
#define ERL_DE_UNLOAD 1
#define ERL_DE_FORCE_UNLOAD 2
#define ERL_DE_RELOAD 3
#define ERL_DE_FORCE_RELOAD 4
#define ERL_DE_PERMANENT 5
#define ERL_DE_PROC_LOADED 0
#define ERL_DE_PROC_AWAIT_UNLOAD 1
#define ERL_DE_PROC_AWAIT_UNLOAD_ONLY 2
#define ERL_DE_PROC_AWAIT_LOAD 3
/* Flags for process entries */
#define ERL_DE_FL_DEREFERENCED 1
/* Flags for drivers, put locking policy here /PaN */
#define ERL_DE_FL_KILL_PORTS 1
#define ERL_FL_CONSISTENT_MASK ( ERL_DE_FL_KILL_PORTS )
/* System specific load errors are returned as positive values */
#define ERL_DE_NO_ERROR 0
#define ERL_DE_LOAD_ERROR_NO_INIT -1
#define ERL_DE_LOAD_ERROR_FAILED_INIT -2
#define ERL_DE_LOAD_ERROR_BAD_NAME -3
#define ERL_DE_LOAD_ERROR_NAME_TO_LONG -4
#define ERL_DE_LOAD_ERROR_INCORRECT_VERSION -5
#define ERL_DE_ERROR_NO_DDLL_FUNCTIONALITY -6
#define ERL_DE_ERROR_UNSPECIFIED -7
#define ERL_DE_LOOKUP_ERROR_NOT_FOUND -8
#define ERL_DE_DYNAMIC_ERROR_OFFSET -10
typedef struct de_proc_entry {
Process *proc; /* The process... */
Uint awaiting_status; /* PROC_LOADED == Have loaded the driver
PROC_AWAIT_UNLOAD == Wants to be notified
when we have unloaded the driver (was locked)
PROC_AWAIT_LOAD == Wants to be notified when we
reloaded the driver (old was locked) */
Uint flags; /* ERL_FL_DE_DEREFERENCED when reload in progress */
Eterm heap[REF_THING_SIZE]; /* "ref heap" */
struct de_proc_entry *next;
} DE_ProcEntry;
typedef struct {
void *handle; /* Handle for DLL or SO (for dyn. drivers). */
DE_ProcEntry *procs; /* List of pids that have loaded this driver,
or that wait for it to change state */
erts_refc_t refc; /* Number of ports/processes having
references to the driver */
erts_smp_atomic32_t port_count; /* Number of ports using the driver */
Uint flags; /* ERL_DE_FL_KILL_PORTS */
int status; /* ERL_DE_xxx */
char *full_path; /* Full path of the driver */
char *reload_full_path; /* If status == ERL_DE_RELOAD, this contains
full name of driver (path) */
char *reload_driver_name; /* ... and this contains the driver name */
Uint reload_flags; /* flags for reloaded driver */
} DE_Handle;
/*
* This structure represents a link to the next driver.
*/
struct erts_driver_t_ {
erts_driver_t *next;
erts_driver_t *prev;
char *name;
struct {
int major;
int minor;
} version;
int flags;
DE_Handle *handle;
#ifdef ERTS_SMP
erts_smp_mtx_t *lock;
#endif
ErlDrvEntry *entry;
ErlDrvData (*start)(ErlDrvPort port, char *command, SysDriverOpts* opts);
void (*stop)(ErlDrvData drv_data);
void (*finish)(void);
void (*flush)(ErlDrvData drv_data);
void (*output)(ErlDrvData drv_data, char *buf, ErlDrvSizeT len);
void (*outputv)(ErlDrvData drv_data, ErlIOVec *ev); /* Might be NULL */
ErlDrvSSizeT (*control)(ErlDrvData drv_data, unsigned int command,
char *buf, ErlDrvSizeT len,
char **rbuf, ErlDrvSizeT rlen); /* Might be NULL */
ErlDrvSSizeT (*call)(ErlDrvData drv_data, unsigned int command,
char *buf, ErlDrvSizeT len,
char **rbuf, ErlDrvSizeT rlen, /* Might be NULL */
unsigned int *flags);
void (*event)(ErlDrvData drv_data, ErlDrvEvent event,
ErlDrvEventData event_data);
void (*ready_input)(ErlDrvData drv_data, ErlDrvEvent event);
void (*ready_output)(ErlDrvData drv_data, ErlDrvEvent event);
void (*timeout)(ErlDrvData drv_data);
void (*ready_async)(ErlDrvData drv_data, ErlDrvThreadData thread_data); /* Might be NULL */
void (*process_exit)(ErlDrvData drv_data, ErlDrvMonitor *monitor);
void (*stop_select)(ErlDrvEvent event, void*); /* Might be NULL */
void (*emergency_close)(ErlDrvData drv_data); /* Might be NULL */
};
extern erts_driver_t *driver_list;
extern erts_smp_rwmtx_t erts_driver_list_lock;
extern void erts_ddll_init(void);
extern void erts_ddll_lock_driver(DE_Handle *dh, char *name);
/* These are for bookkeeping */
extern void erts_ddll_increment_port_count(DE_Handle *dh);
extern void erts_ddll_decrement_port_count(DE_Handle *dh);
/* These makes things happen, drivers may be scheduled for unload etc */
extern void erts_ddll_reference_driver(DE_Handle *dh);
extern void erts_ddll_reference_referenced_driver(DE_Handle *dh);
extern void erts_ddll_dereference_driver(DE_Handle *dh);
extern char *erts_ddll_error(int code);
extern void erts_ddll_proc_dead(Process *p, ErtsProcLocks plocks);
extern int erts_ddll_driver_ok(DE_Handle *dh);
extern void erts_ddll_remove_monitor(Process *p,
Eterm ref,
ErtsProcLocks plocks);
extern Eterm erts_ddll_monitor_driver(Process *p,
Eterm description,
ErtsProcLocks plocks);
/*
** Just like the driver binary but with initial flags
** Note that the two structures Binary and ErlDrvBinary HAVE to
** be equal except for extra fields in the beginning of the struct.
** ErlDrvBinary is defined in erl_driver.h.
** When driver_alloc_binary is called, a Binary is allocated, but
** the pointer returned is to the address of the first element that
** also occurs in the ErlDrvBinary struct (driver.*binary takes care if this).
** The driver need never know about additions to the internal Binary of the
** emulator. One should however NEVER be sloppy when mixing ErlDrvBinary
** and Binary, the macros below can convert one type to the other, as they both
** in reality are equal.
*/
#ifdef ARCH_32
/* *DO NOT USE* only for alignment. */
#define ERTS_BINARY_STRUCT_ALIGNMENT Uint32 align__;
#else
#define ERTS_BINARY_STRUCT_ALIGNMENT
#endif
/* Add fields in ERTS_INTERNAL_BINARY_FIELDS, otherwise the drivers crash */
#define ERTS_INTERNAL_BINARY_FIELDS \
UWord flags; \
erts_refc_t refc; \
ERTS_BINARY_STRUCT_ALIGNMENT
typedef struct binary {
ERTS_INTERNAL_BINARY_FIELDS
SWord orig_size;
char orig_bytes[1]; /* to be continued */
} Binary;
#define ERTS_SIZEOF_Binary(Sz) \
(offsetof(Binary,orig_bytes) + (Sz))
typedef struct {
ERTS_INTERNAL_BINARY_FIELDS
SWord orig_size;
void (*destructor)(Binary *);
union {
struct {
ERTS_BINARY_STRUCT_ALIGNMENT
char data[1];
} aligned;
struct {
char data[1];
} unaligned;
} u;
} ErtsMagicBinary;
#ifdef ARCH_32
#define ERTS_MAGIC_BIN_BYTES_TO_ALIGN 4
#else
#define ERTS_MAGIC_BIN_BYTES_TO_ALIGN 0
#endif
typedef union {
Binary binary;
ErtsMagicBinary magic_binary;
struct {
ERTS_INTERNAL_BINARY_FIELDS
ErlDrvBinary binary;
} driver;
} ErtsBinary;
/*
* 'Binary' alignment:
* Address of orig_bytes[0] of a Binary should always be 8-byte aligned.
* It is assumed that the flags, refc, and orig_size fields are 4 bytes on
* 32-bits architectures and 8 bytes on 64-bits architectures.
*/
#define ERTS_MAGIC_BIN_DESTRUCTOR(BP) \
((ErtsBinary *) (BP))->magic_binary.destructor
#define ERTS_MAGIC_BIN_DATA(BP) \
((void *) ((ErtsBinary *) (BP))->magic_binary.u.aligned.data)
#define ERTS_MAGIC_DATA_OFFSET \
(offsetof(ErtsMagicBinary,u.aligned.data) - offsetof(Binary,orig_bytes))
#define ERTS_MAGIC_BIN_ORIG_SIZE(Sz) \
(ERTS_MAGIC_DATA_OFFSET + (Sz))
#define ERTS_MAGIC_BIN_SIZE(Sz) \
(offsetof(ErtsMagicBinary,u.aligned.data) + (Sz))
/* On 32-bit arch these macro variants will save memory
by not forcing 8-byte alignment for the magic payload.
*/
#define ERTS_MAGIC_BIN_UNALIGNED_DATA(BP) \
((void *) ((ErtsBinary *) (BP))->magic_binary.u.unaligned.data)
#define ERTS_MAGIC_UNALIGNED_DATA_OFFSET \
(offsetof(ErtsMagicBinary,u.unaligned.data) - offsetof(Binary,orig_bytes))
#define ERTS_MAGIC_BIN_UNALIGNED_DATA_SIZE(BP) \
((BP)->orig_size - ERTS_MAGIC_UNALIGNED_DATA_OFFSET)
#define ERTS_MAGIC_BIN_UNALIGNED_ORIG_SIZE(Sz) \
(ERTS_MAGIC_UNALIGNED_DATA_OFFSET + (Sz))
#define ERTS_MAGIC_BIN_UNALIGNED_SIZE(Sz) \
(offsetof(ErtsMagicBinary,u.unaligned.data) + (Sz))
#define ERTS_MAGIC_BIN_FROM_UNALIGNED_DATA(DATA) \
((ErtsBinary*)((char*)(DATA) - offsetof(ErtsMagicBinary,u.unaligned.data)))
#define Binary2ErlDrvBinary(B) (&((ErtsBinary *) (B))->driver.binary)
#define ErlDrvBinary2Binary(D) ((Binary *) \
(((char *) (D)) \
- offsetof(ErtsBinary, driver.binary)))
/* A "magic" binary flag */
#define BIN_FLAG_MAGIC 1
#define BIN_FLAG_USR1 2 /* Reserved for use by different modules too mark */
#define BIN_FLAG_USR2 4 /* certain binaries as special (used by ets) */
#define BIN_FLAG_DRV 8
/*
* This structure represents one type of a binary in a process.
*/
typedef struct proc_bin {
Eterm thing_word; /* Subtag REFC_BINARY_SUBTAG. */
Uint size; /* Binary size in bytes. */
struct erl_off_heap_header *next;
Binary *val; /* Pointer to Binary structure. */
byte *bytes; /* Pointer to the actual data bytes. */
Uint flags; /* Flag word. */
} ProcBin;
#define PB_IS_WRITABLE 1 /* Writable (only one reference to ProcBin) */
#define PB_ACTIVE_WRITER 2 /* There is an active writer */
/*
* ProcBin size in Eterm words.
*/
#define PROC_BIN_SIZE (sizeof(ProcBin)/sizeof(Eterm))
ERTS_GLB_INLINE Eterm erts_mk_magic_binary_term(Eterm **hpp,
ErlOffHeap *ohp,
Binary *mbp);
#if ERTS_GLB_INLINE_INCL_FUNC_DEF
ERTS_GLB_INLINE Eterm
erts_mk_magic_binary_term(Eterm **hpp, ErlOffHeap *ohp, Binary *mbp)
{
ProcBin *pb = (ProcBin *) *hpp;
*hpp += PROC_BIN_SIZE;
ASSERT(mbp->flags & BIN_FLAG_MAGIC);
pb->thing_word = HEADER_PROC_BIN;
pb->size = 0;
pb->next = ohp->first;
ohp->first = (struct erl_off_heap_header*) pb;
pb->val = mbp;
pb->bytes = (byte *) mbp->orig_bytes;
pb->flags = 0;
erts_refc_inc(&mbp->refc, 1);
return make_binary(pb);
}
#endif
#define ERTS_TERM_IS_MAGIC_BINARY(T) \
(is_binary((T)) \
&& (thing_subtag(*binary_val((T))) == REFC_BINARY_SUBTAG) \
&& (((ProcBin *) binary_val((T)))->val->flags & BIN_FLAG_MAGIC))
union erl_off_heap_ptr {
struct erl_off_heap_header* hdr;
ProcBin *pb;
struct erl_fun_thing* fun;
struct external_thing_* ext;
Eterm* ep;
void* voidp;
};
/* controls warning mapping in error_logger */
extern Eterm node_cookie;
extern Uint display_items; /* no of items to display in traces etc */
extern int erts_backtrace_depth;
extern erts_smp_atomic32_t erts_max_gen_gcs;
extern int bif_reductions; /* reductions + fcalls (when doing call_bif) */
extern int stackdump_on_exit;
/*
* Here is an implementation of a lightweight stack.
*
* Use it like this:
*
* DECLARE_ESTACK(Stack) (At the start of a block)
* ...
* ESTACK_PUSH(Stack, Term)
* ...
* if (ESTACK_ISEMPTY(Stack)) {
* Stack is empty
* } else {
* Term = ESTACK_POP(Stack);
* Process popped Term here
* }
* ...
* DESTROY_ESTACK(Stack)
*/
typedef struct ErtsEStack_ {
Eterm* start;
Eterm* sp;
Eterm* end;
Eterm* edefault;
ErtsAlcType_t alloc_type;
}ErtsEStack;
#define DEF_ESTACK_SIZE (16)
void erl_grow_estack(ErtsEStack*, Uint need);
#define ESTK_CONCAT(a,b) a##b
#define ESTK_DEF_STACK(s) ESTK_CONCAT(s,_default_estack)
#define DECLARE_ESTACK(s) \
Eterm ESTK_DEF_STACK(s)[DEF_ESTACK_SIZE]; \
ErtsEStack s = { \
ESTK_DEF_STACK(s), /* start */ \
ESTK_DEF_STACK(s), /* sp */ \
ESTK_DEF_STACK(s) + DEF_ESTACK_SIZE, /* end */ \
ESTK_DEF_STACK(s), /* default */ \
ERTS_ALC_T_ESTACK /* alloc_type */ \
}
#define ESTACK_CHANGE_ALLOCATOR(s,t) \
do { \
if ((s).start != ESTK_DEF_STACK(s)) { \
erts_exit(ERTS_ERROR_EXIT, "Internal error - trying to change allocator " \
"type of active estack\n"); \
} \
(s).alloc_type = (t); \
} while (0)
#define DESTROY_ESTACK(s) \
do { \
if ((s).start != ESTK_DEF_STACK(s)) { \
erts_free((s).alloc_type, (s).start); \
} \
} while(0)
/*
* Do not free the stack after this, it may have pointers into what
* was saved in 'dst'.
*/
#define ESTACK_SAVE(s,dst)\
do {\
if ((s).start == ESTK_DEF_STACK(s)) {\
UWord _wsz = ESTACK_COUNT(s);\
(dst)->start = erts_alloc((s).alloc_type,\
DEF_ESTACK_SIZE * sizeof(Eterm));\
memcpy((dst)->start, (s).start,_wsz*sizeof(Eterm));\
(dst)->sp = (dst)->start + _wsz;\
(dst)->end = (dst)->start + DEF_ESTACK_SIZE;\
(dst)->edefault = NULL;\
(dst)->alloc_type = (s).alloc_type;\
} else\
*(dst) = (s);\
} while (0)
#define DESTROY_SAVED_ESTACK(estack)\
do {\
if ((estack)->start) {\
erts_free((estack)->alloc_type, (estack)->start);\
(estack)->start = NULL;\
}\
} while(0)
#define CLEAR_SAVED_ESTACK(estack) ((void) ((estack)->start = NULL))
/*
* Use on empty stack, only the allocator can be changed before this.
* The src stack is reset to NULL.
*/
#define ESTACK_RESTORE(s, src) \
do { \
ASSERT((s).start == ESTK_DEF_STACK(s)); \
(s) = *(src); /* struct copy */ \
(src)->start = NULL; \
ASSERT((s).sp >= (s).start); \
ASSERT((s).sp <= (s).end); \
} while (0)
#define ESTACK_IS_STATIC(s) ((s).start == ESTK_DEF_STACK(s))
#define ESTACK_PUSH(s, x) \
do { \
if ((s).sp == (s).end) { \
erl_grow_estack(&(s), 1); \
} \
*(s).sp++ = (x); \
} while(0)
#define ESTACK_PUSH2(s, x, y) \
do { \
if ((s).sp > (s).end - 2) { \
erl_grow_estack(&(s), 2); \
} \
*(s).sp++ = (x); \
*(s).sp++ = (y); \
} while(0)
#define ESTACK_PUSH3(s, x, y, z) \
do { \
if ((s).sp > (s).end - 3) { \
erl_grow_estack(&s, 3); \
} \
*(s).sp++ = (x); \
*(s).sp++ = (y); \
*(s).sp++ = (z); \
} while(0)
#define ESTACK_PUSH4(s, E1, E2, E3, E4) \
do { \
if ((s).sp > (s).end - 4) { \
erl_grow_estack(&s, 4); \
} \
*(s).sp++ = (E1); \
*(s).sp++ = (E2); \
*(s).sp++ = (E3); \
*(s).sp++ = (E4); \
} while(0)
#define ESTACK_RESERVE(s, push_cnt) \
do { \
if ((s).sp > (s).end - (push_cnt)) { \
erl_grow_estack(&(s), (push_cnt)); \
} \
} while(0)
/* Must be preceded by ESTACK_RESERVE */
#define ESTACK_FAST_PUSH(s, x) \
do { \
ASSERT((s).sp < (s).end); \
*s.sp++ = (x); \
} while(0)
#define ESTACK_COUNT(s) ((s).sp - (s).start)
#define ESTACK_ISEMPTY(s) ((s).sp == (s).start)
#define ESTACK_POP(s) (*(--(s).sp))
/*
* WSTACK: same as ESTACK but with UWord instead of Eterm
*/
typedef struct ErtsWStack_ {
UWord* wstart;
UWord* wsp;
UWord* wend;
UWord* wdefault;
ErtsAlcType_t alloc_type;
}ErtsWStack;
#define DEF_WSTACK_SIZE (16)
void erl_grow_wstack(ErtsWStack*, Uint need);
#define WSTK_CONCAT(a,b) a##b
#define WSTK_DEF_STACK(s) WSTK_CONCAT(s,_default_wstack)
#define WSTACK_DECLARE(s) \
UWord WSTK_DEF_STACK(s)[DEF_WSTACK_SIZE]; \
ErtsWStack s = { \
WSTK_DEF_STACK(s), /* wstart */ \
WSTK_DEF_STACK(s), /* wsp */ \
WSTK_DEF_STACK(s) + DEF_WSTACK_SIZE, /* wend */ \
WSTK_DEF_STACK(s), /* wdflt */ \
ERTS_ALC_T_ESTACK /* alloc_type */ \
}
#define DECLARE_WSTACK WSTACK_DECLARE
typedef struct ErtsDynamicWStack_ {
UWord default_stack[DEF_WSTACK_SIZE];
ErtsWStack ws;
}ErtsDynamicWStack;
#define WSTACK_INIT(dwsp, ALC_TYPE) \
do { \
(dwsp)->ws.wstart = (dwsp)->default_stack; \
(dwsp)->ws.wsp = (dwsp)->default_stack; \
(dwsp)->ws.wend = (dwsp)->default_stack + DEF_WSTACK_SIZE;\
(dwsp)->ws.wdefault = (dwsp)->default_stack; \
(dwsp)->ws.alloc_type = ALC_TYPE; \
} while (0)
#define WSTACK_CHANGE_ALLOCATOR(s,t) \
do { \
if (s.wstart != WSTK_DEF_STACK(s)) { \
erts_exit(ERTS_ERROR_EXIT, "Internal error - trying to change allocator " \
"type of active wstack\n"); \
} \
s.alloc_type = (t); \
} while (0)
#define WSTACK_DESTROY(s) \
do { \
if (s.wstart != s.wdefault) { \
erts_free(s.alloc_type, s.wstart); \
} \
} while(0)
#define DESTROY_WSTACK WSTACK_DESTROY
#define WSTACK_DEBUG(s) \
do { \
fprintf(stderr, "wstack size = %ld\r\n", s.wsp - s.wstart); \
fprintf(stderr, "wstack wstart = %p\r\n", s.wstart); \
fprintf(stderr, "wstack wsp = %p\r\n", s.wsp); \
} while(0)
/*
* Do not free the stack after this, it may have pointers into what
* was saved in 'dst'.
*/
#define WSTACK_SAVE(s,dst)\
do {\
if (s.wstart == WSTK_DEF_STACK(s)) {\
UWord _wsz = WSTACK_COUNT(s);\
(dst)->wstart = erts_alloc(s.alloc_type,\
DEF_WSTACK_SIZE * sizeof(UWord));\
memcpy((dst)->wstart, s.wstart,_wsz*sizeof(UWord));\
(dst)->wsp = (dst)->wstart + _wsz;\
(dst)->wend = (dst)->wstart + DEF_WSTACK_SIZE;\
(dst)->wdefault = NULL;\
(dst)->alloc_type = s.alloc_type;\
} else\
*(dst) = s;\
} while (0)
#define DESTROY_SAVED_WSTACK(wstack)\
do {\
if ((wstack)->wstart) {\
erts_free((wstack)->alloc_type, (wstack)->wstart);\
(wstack)->wstart = NULL;\
}\
} while(0)
#define CLEAR_SAVED_WSTACK(wstack) ((void) ((wstack)->wstart = NULL))
/*
* Use on empty stack, only the allocator can be changed before this.
* The src stack is reset to NULL.
*/
#define WSTACK_RESTORE(s, src) \
do { \
ASSERT(s.wstart == WSTK_DEF_STACK(s)); \
s = *(src); /* struct copy */ \
(src)->wstart = NULL; \
ASSERT(s.wsp >= s.wstart); \
ASSERT(s.wsp <= s.wend); \
} while (0)
#define WSTACK_IS_STATIC(s) (s.wstart == WSTK_DEF_STACK(s))
#define WSTACK_PUSH(s, x) \
do { \
if (s.wsp == s.wend) { \
erl_grow_wstack(&s, 1); \
} \
*s.wsp++ = (x); \
} while(0)
#define WSTACK_PUSH2(s, x, y) \
do { \
if (s.wsp > s.wend - 2) { \
erl_grow_wstack(&s, 2); \
} \
*s.wsp++ = (x); \
*s.wsp++ = (y); \
} while(0)
#define WSTACK_PUSH3(s, x, y, z) \
do { \
if (s.wsp > s.wend - 3) { \
erl_grow_wstack(&s, 3); \
} \
*s.wsp++ = (x); \
*s.wsp++ = (y); \
*s.wsp++ = (z); \
} while(0)
#define WSTACK_PUSH4(s, A1, A2, A3, A4) \
do { \
if (s.wsp > s.wend - 4) { \
erl_grow_wstack(&s, 4); \
} \
*s.wsp++ = (A1); \
*s.wsp++ = (A2); \
*s.wsp++ = (A3); \
*s.wsp++ = (A4); \
} while(0)
#define WSTACK_PUSH5(s, A1, A2, A3, A4, A5) \
do { \
if (s.wsp > s.wend - 5) { \
erl_grow_wstack(&s, 5); \
} \
*s.wsp++ = (A1); \
*s.wsp++ = (A2); \
*s.wsp++ = (A3); \
*s.wsp++ = (A4); \
*s.wsp++ = (A5); \
} while(0)
#define WSTACK_PUSH6(s, A1, A2, A3, A4, A5, A6) \
do { \
if (s.wsp > s.wend - 6) { \
erl_grow_wstack(&s, 6); \
} \
*s.wsp++ = (A1); \
*s.wsp++ = (A2); \
*s.wsp++ = (A3); \
*s.wsp++ = (A4); \
*s.wsp++ = (A5); \
*s.wsp++ = (A6); \
} while(0)
#define WSTACK_RESERVE(s, push_cnt) \
do { \
if (s.wsp > s.wend - (push_cnt)) { \
erl_grow_wstack(&s, (push_cnt)); \
} \
} while(0)
/* Must be preceded by WSTACK_RESERVE */
#define WSTACK_FAST_PUSH(s, x) \
do { \
ASSERT(s.wsp < s.wend); \
*s.wsp++ = (x); \
} while(0)
#define WSTACK_COUNT(s) (s.wsp - s.wstart)
#define WSTACK_ISEMPTY(s) (s.wsp == s.wstart)
#define WSTACK_POP(s) ((ASSERT(s.wsp > s.wstart)),*(--s.wsp))
#define WSTACK_ROLLBACK(s, count) (ASSERT(WSTACK_COUNT(s) >= (count)), \
s.wsp = s.wstart + (count))
/* PSTACK - Stack of any type.
* Usage:
* {
* #define PSTACK_TYPE MyType
* PSTACK_DECLARE(s,16);
* MyType *sp = PSTACK_PUSH(s);
*
* sp->x = ....
* sp->y = ....
* sp = PSTACK_PUSH(s);
* ...
* sp = PSTACK_POP(s);
* if (PSTACK_IS_EMPTY(s)) {
* // sp is invalid when stack is empty after pop
* }
*
* PSTACK_DESTROY(s);
* }
*/
typedef struct ErtsPStack_ {
byte* pstart;
byte* psp;
byte* pend;
ErtsAlcType_t alloc_type;
}ErtsPStack;
void erl_grow_pstack(ErtsPStack* s, void* default_pstack, unsigned need_bytes);
#define PSTK_CONCAT(a,b) a##b
#define PSTK_DEF_STACK(s) PSTK_CONCAT(s,_default_pstack)
#define PSTACK_DECLARE(s, DEF_PSTACK_SIZE) \
PSTACK_TYPE PSTK_DEF_STACK(s)[DEF_PSTACK_SIZE]; \
ErtsPStack s = { (byte*)PSTK_DEF_STACK(s), /* pstart */ \
(byte*)(PSTK_DEF_STACK(s) - 1), /* psp */ \
(byte*)(PSTK_DEF_STACK(s) + (DEF_PSTACK_SIZE)), /* pend */\
ERTS_ALC_T_ESTACK /* alloc_type */ \
}
#define PSTACK_CHANGE_ALLOCATOR(s,t) \
do { \
if (s.pstart != (byte*)PSTK_DEF_STACK(s)) { \
erts_exit(ERTS_ERROR_EXIT, "Internal error - trying to change allocator " \
"type of active pstack\n"); \
} \
s.alloc_type = (t); \
} while (0)
#define PSTACK_DESTROY(s) \
do { \
if (s.pstart != (byte*)PSTK_DEF_STACK(s)) { \
erts_free(s.alloc_type, s.pstart); \
} \
} while(0)
#define PSTACK_IS_EMPTY(s) (s.psp < s.pstart)
#define PSTACK_COUNT(s) (((PSTACK_TYPE*)s.psp + 1) - (PSTACK_TYPE*)s.pstart)
#define PSTACK_TOP(s) (ASSERT(!PSTACK_IS_EMPTY(s)), (PSTACK_TYPE*)(s.psp))
#define PSTACK_PUSH(s) \
(s.psp += sizeof(PSTACK_TYPE), \
((s.psp == s.pend) ? erl_grow_pstack(&s, PSTK_DEF_STACK(s), \
sizeof(PSTACK_TYPE)) : (void)0), \
((PSTACK_TYPE*) s.psp))
#define PSTACK_POP(s) ((PSTACK_TYPE*) (s.psp -= sizeof(PSTACK_TYPE)))
/*
* Do not free the stack after this, it may have pointers into what
* was saved in 'dst'.
*/
#define PSTACK_SAVE(s,dst)\
do {\
if (s.pstart == (byte*)PSTK_DEF_STACK(s)) {\
UWord _pbytes = PSTACK_COUNT(s) * sizeof(PSTACK_TYPE);\
(dst)->pstart = erts_alloc(s.alloc_type,\
sizeof(PSTK_DEF_STACK(s)));\
sys_memcpy((dst)->pstart, s.pstart, _pbytes);\
(dst)->psp = (dst)->pstart + _pbytes - sizeof(PSTACK_TYPE);\
(dst)->pend = (dst)->pstart + sizeof(PSTK_DEF_STACK(s));\
(dst)->alloc_type = s.alloc_type;\
} else\
*(dst) = s;\
} while (0)
/*
* Use on empty stack, only the allocator can be changed before this.
* The src stack is reset to NULL.
*/
#define PSTACK_RESTORE(s, src) \
do { \
ASSERT(s.pstart == (byte*)PSTK_DEF_STACK(s)); \
s = *(src); /* struct copy */ \
(src)->pstart = NULL; \
ASSERT(s.psp >= (s.pstart - sizeof(PSTACK_TYPE))); \
ASSERT(s.psp < s.pend); \
} while (0)
#define PSTACK_DESTROY_SAVED(pstack)\
do {\
if ((pstack)->pstart) {\
erts_free((pstack)->alloc_type, (pstack)->pstart);\
(pstack)->pstart = NULL;\
}\
} while(0)
/*
* An implementation of lightweight unbounded queues,
* using a circular dynamic array.
* It does not include support for change_allocator.
*
* Use it like this:
*
* DECLARE_EQUEUE(Queue) (At the start of a block)
* ...
* EQUEUE_PUT(Queue, Term)
* ...
* if (EQUEUE_ISEMPTY(Queue)) {
* Queue is empty
* } else {
* Term = EQUEUE_GET(Stack);
* Process popped Term here
* }
* ...
* DESTROY_EQUEUE(Queue)
*/
typedef struct {
Eterm* start;
Eterm* front;
Eterm* back;
int possibly_empty;
Eterm* end;
ErtsAlcType_t alloc_type;
} ErtsEQueue;
#define DEF_EQUEUE_SIZE (16)
void erl_grow_equeue(ErtsEQueue*, Eterm* def_queue);
#define EQUE_CONCAT(a,b) a##b
#define EQUE_DEF_QUEUE(q) EQUE_CONCAT(q,_default_equeue)
#define DECLARE_EQUEUE(q) \
UWord EQUE_DEF_QUEUE(q)[DEF_EQUEUE_SIZE]; \
ErtsEQueue q = { \
EQUE_DEF_QUEUE(q), /* start */ \
EQUE_DEF_QUEUE(q), /* front */ \
EQUE_DEF_QUEUE(q), /* back */ \
1, /* possibly_empty */ \
EQUE_DEF_QUEUE(q) + DEF_EQUEUE_SIZE, /* end */ \
ERTS_ALC_T_ESTACK /* alloc_type */ \
}
#define DESTROY_EQUEUE(q) \
do { \
if (q.start != EQUE_DEF_QUEUE(q)) { \
erts_free(q.alloc_type, q.start); \
} \
} while(0)
#define EQUEUE_PUT_UNCHECKED(q, x) \
do { \
q.possibly_empty = 0; \
*(q.back) = (x); \
if (++(q.back) == q.end) { \
q.back = q.start; \
} \
} while(0)
#define EQUEUE_PUT(q, x) \
do { \
if (q.back == q.front && !q.possibly_empty) { \
erl_grow_equeue(&q, EQUE_DEF_QUEUE(q)); \
} \
EQUEUE_PUT_UNCHECKED(q, x); \
} while(0)
#define EQUEUE_ISEMPTY(q) (q.back == q.front && q.possibly_empty)
ERTS_GLB_INLINE Eterm erts_equeue_get(ErtsEQueue *q);
#if ERTS_GLB_INLINE_INCL_FUNC_DEF
ERTS_GLB_INLINE Eterm erts_equeue_get(ErtsEQueue *q) {
Eterm x;
q->possibly_empty = 1;
x = *(q->front);
if (++(q->front) == q->end) {
q->front = q->start;
}
return x;
}
#endif
#define EQUEUE_GET(q) erts_equeue_get(&(q));
/* binary.c */
void erts_emasculate_writable_binary(ProcBin* pb);
Eterm erts_new_heap_binary(Process *p, byte *buf, int len, byte** datap);
Eterm erts_new_mso_binary(Process*, byte*, Uint);
Eterm new_binary(Process*, byte*, Uint);
Eterm erts_realloc_binary(Eterm bin, size_t size);
/* erl_bif_info.c */
Eterm
erts_bld_port_info(Eterm **hpp,
ErlOffHeap *ohp,
Uint *szp,
Port *prt,
Eterm item);
void erts_bif_info_init(void);
/* bif.c */
ERTS_GLB_INLINE Eterm
erts_proc_store_ref(Process *c_p, Uint32 ref[ERTS_MAX_REF_NUMBERS]);
#if ERTS_GLB_INLINE_INCL_FUNC_DEF
ERTS_GLB_INLINE Eterm
erts_proc_store_ref(Process *c_p, Uint32 ref[ERTS_MAX_REF_NUMBERS])
{
Eterm *hp = HAlloc(c_p, REF_THING_SIZE);
write_ref_thing(hp, ref[0], ref[1], ref[2]);
return make_internal_ref(hp);
}
#endif
void erts_queue_monitor_message(Process *,
ErtsProcLocks*,
Eterm,
Eterm,
Eterm,
Eterm);
void erts_init_trap_export(Export* ep, Eterm m, Eterm f, Uint a,
Eterm (*bif)(Process*,Eterm*));
void erts_init_bif(void);
Eterm erl_send(Process *p, Eterm to, Eterm msg);
/* erl_bif_op.c */
Eterm erl_is_function(Process* p, Eterm arg1, Eterm arg2);
/* beam_bif_load.c */