forked from Samsung/CAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibflat.h
1597 lines (1456 loc) · 60.3 KB
/
libflat.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
#ifndef __LIBFLAT_H__
#define __LIBFLAT_H__
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <memory.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#ifdef __linux__
#include <sys/time.h>
#else
#ifdef _WIN32
#include "wintime.h"
#endif
#endif
#include "rbtree.h"
#include "utils.h"
struct interval_tree_node {
struct rb_node rb;
uintptr_t start; /* Start of interval */
uintptr_t last; /* Last location _in_ interval */
uintptr_t __subtree_last;
struct blstream* storage;
};
struct flatten_pointer {
struct interval_tree_node* node;
size_t offset;
};
struct flatten_header {
size_t memory_size;
size_t ptr_count;
size_t fptr_count;
size_t root_addr_count;
uintptr_t fix_base;
uint64_t magic;
};
#include "interval_tree.h"
#define CONFIG_DEBUG_LEVEL 3
#define __LIBFLAT_VERSION__ "0.95"
static const volatile char version[] = __LIBFLAT_VERSION__;
#define DBGL(n,...) do { if (n<=CONFIG_DEBUG_LEVEL) printf(__VA_ARGS__); } while(0)
#define FLATTEN_MAGIC 0x464c415454454e00ULL
/* Private debugging facilities. In order to use them include their prototypes in the source file */
/* 0001 : debug flattening stage
* 0002 : debug memory fixing
*/
void flatten_debug_info();
void flatten_set_debug_flag(int flag);
void flatten_debug_memory();
/* Binary stream doubly linked list implementation */
struct blstream* binary_stream_append_reserve(size_t size);
struct blstream* binary_stream_insert_front_reserve(size_t size, struct blstream* where);
struct blstream* binary_stream_insert_back_reserve(size_t size, struct blstream* where);
struct blstream* binary_stream_update(const void* data, size_t size, struct blstream* where);
void binary_stream_calculate_index();
void binary_stream_update_pointers();
void binary_stream_destroy();
void binary_stream_print();
size_t binary_stream_size();
size_t binary_stream_write(FILE* f);
/* Set of memory fixup elements implemented using red-black tree */
struct fixup_set_node {
struct rb_node node;
/* Storage area and offset where the original address to be fixed is stored */
struct interval_tree_node* inode;
size_t offset;
/* Storage area and offset where the original address points to */
struct flatten_pointer* ptr;
};
struct fixup_set_node* fixup_set_search(uintptr_t v);
void fixup_set_print();
size_t fixup_set_count();
void fixup_set_destroy();
size_t fixup_set_write(FILE* f);
/* Root address list */
struct root_addrnode {
struct root_addrnode* next;
uintptr_t root_addr;
};
size_t root_addr_count();
void fix_unflatten_memory(struct flatten_header* hdr, void* memory);
struct interval_nodelist {
struct interval_nodelist* next;
struct interval_tree_node* node;
};
/* Main interface functions */
void flatten_init();
int flatten_write(FILE* f);
void flatten_fini();
void unflatten_init();
int unflatten_read(FILE* f);
int unflatten_map(int fd, off_t offset);
int unflatten_map_rdonly(int fd, off_t offset);
void unflatten_fini();
enum flatten_option {
option_silent = 0x01,
};
void flatten_set_option(int option);
void flatten_clear_option(int option);
/* Implementation */
#ifdef __linux__
#define _ALIGNAS(n) __attribute__((aligned(n)))
#define RB_NODE_ALIGN (sizeof(long))
#else
#ifdef _WIN32
#define _ALIGNAS(n) __declspec(align(n))
#ifdef _M_IX86
#define RB_NODE_ALIGN 4
#elif defined _M_X64
#define RB_NODE_ALIGN 8
#endif
#endif /* _WIN32 */
#endif /* __linux__ */
#ifdef __linux__
#include <alloca.h>
#define ALLOCA(x) alloca(x)
#else
#ifdef _WIN32
#include <malloc.h>
#define ALLOCA(x) _malloca(x)
#endif
#endif
#ifdef __linux__
#if defined __cplusplus
#define container_of(ptr, type, member) ({ \
const decltype( ((type *)0)->member ) *__mptr = (decltype( ((type *)0)->member )*)(ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#else
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (typeof( ((type *)0)->member )*)(ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif
#else
#ifdef _WIN32
#define container_of(ptr, type, member) (type *)( (char *)(ptr) - offsetof(type,member) )
#endif
#endif
#define MAX_FLCTRL_RECURSION_DEPTH 16
#define FLCTRL (FLCTRL_ARR[FLCTRL_INDEX])
struct FLCONTROL {
struct blstream* bhead;
struct blstream* btail;
struct rb_root fixup_set_root;
struct rb_root imap_root;
struct flatten_header HDR;
struct root_addrnode* rhead;
struct root_addrnode* rtail;
struct root_addrnode* last_accessed_root;
int debug_flag;
unsigned long option;
void* mem;
size_t map_size;
void* map_mem;
};
extern struct FLCONTROL FLCTRL_ARR[MAX_FLCTRL_RECURSION_DEPTH];
extern int FLCTRL_INDEX;
extern struct flatten_pointer* flatten_plain_type(const void* _ptr, size_t _sz);
extern int fixup_set_insert(struct interval_tree_node* node, size_t offset, struct flatten_pointer* ptr);
extern int fixup_set_insert_force_update(struct interval_tree_node* node, size_t offset, struct flatten_pointer* ptr);
extern struct fixup_set_node *fixup_set_search(uintptr_t v);
extern int fixup_set_reserve(struct interval_tree_node* node, size_t offset);
extern int fixup_set_reserve_address(uintptr_t addr);
extern int fixup_set_update(struct interval_tree_node* node, size_t offset, struct flatten_pointer* ptr);
extern struct flatten_pointer* get_pointer_node(const void* _ptr);
extern void root_addr_append(size_t root_addr);
void* root_pointer_next();
void* root_pointer_seq(size_t index);
extern struct interval_tree_node * interval_tree_iter_first(struct rb_root *root, uintptr_t start, uintptr_t last);
struct interval_tree_node * interval_tree_iter_next(struct interval_tree_node *node, uintptr_t start, uintptr_t last);
extern struct rb_node* interval_tree_insert(struct interval_tree_node *node, struct rb_root *root);
struct blstream {
struct blstream* next;
struct blstream* prev;
void* data;
size_t size;
size_t index;
size_t alignment;
size_t align_offset;
};
struct blstream* binary_stream_insert_back(const void* data, size_t size, struct blstream* where);
struct blstream* binary_stream_insert_front(const void* data, size_t size, struct blstream* where);
struct blstream* binary_stream_append(const void* data, size_t size);
struct rb_node *rb_next(const struct rb_node *node);
struct rb_node *rb_prev(const struct rb_node *node);
#define DEFAULT_ITER_QUEUE_SIZE (1024*1024*8)
struct queue_block {
struct queue_block* next;
unsigned char data[];
};
struct bqueue {
size_t block_size;
size_t size;
struct queue_block* front_block;
size_t front_index;
struct queue_block* back_block;
size_t back_index;
};
void bqueue_init(struct bqueue* q, size_t block_size);
void bqueue_destroy(struct bqueue* q);
int bqueue_empty(struct bqueue* q);
void bqueue_push_back(struct bqueue* q, const void* m, size_t s);
void bqueue_pop_front(struct bqueue* q, void* m, size_t s);
struct flatten_base {};
typedef struct flatten_pointer* (*flatten_struct_t)(const struct flatten_base*, size_t n, struct bqueue*);
typedef struct flatten_pointer* (*flatten_struct_mixed_convert_t)(struct flatten_pointer*, const struct flatten_base*);
struct flatten_job {
struct interval_tree_node* node;
size_t offset;
size_t size;
struct flatten_base* ptr;
flatten_struct_t fun;
/* Mixed pointer support */
const struct flatten_base* fp;
flatten_struct_mixed_convert_t convert;
};
static inline struct flatten_pointer* make_flatten_pointer(struct interval_tree_node* node, size_t offset) {
struct flatten_pointer* v = malloc(sizeof(struct flatten_pointer));
assert(v!=0);
v->node = node;
v->offset = offset;
return v;
}
static inline size_t strmemlen(const char* s) {
return strlen(s)+1;
}
static inline size_t ptrarrmemlen(const void* const* m) {
size_t count=1;
while(*m) {
count++;
m++;
}
return count;
}
#define LIBFLAT_DBGM(...) do { if (FLCTRL.debug_flag&1) printf(__VA_ARGS__); } while(0)
#define LIBFLAT_DBGM1(name,a1) do { if (FLCTRL.debug_flag&1) printf(#name "(" #a1 ")\n"); } while(0)
#define LIBFLAT_DBGF(name,F,FMT,P) do { if (FLCTRL.debug_flag&1) printf(#name "(" #F "[" FMT "])\n",P); } while(0)
#define LIBFLAT_DBGM2(name,a1,a2) do { if (FLCTRL.debug_flag&1) printf(#name "(" #a1 "," #a2 ")\n"); } while(0)
#define LIBFLAT_DBGTF(name,T,F,FMT,P) do { if (FLCTRL.debug_flag&1) printf(#name "(" #T "," #F "[" FMT "])\n",P); } while(0)
#define LIBFLAT_DBGTFMF(name,T,F,FMT,P,PF,FF) do { if (FLCTRL.debug_flag&1) printf(#name "(" #T "," #F "[" FMT "]," #PF "," #FF ")\n",P); } while(0)
#define LIBFLAT_DBGTP(name,T,P) do { if (FLCTRL.debug_flag&1) printf(#name "(" #T "," #P "[%p])\n",P); } while(0)
#define LIBFLAT_DBGM3(name,a1,a2,a3) do { if (FLCTRL.debug_flag&1) printf(#name "(" #a1 "," #a2 "," #a3 ")\n"); } while(0)
#define LIBFLAT_DBGM4(name,a1,a2,a3,a4) do { if (FLCTRL.debug_flag&1) printf(#name "(" #a1 "," #a2 "," #a3 "," #a4 ")\n"); } while(0)
#define ATTR(f) ((_ptr)->f)
#define STRUCT_ALIGN(n) do { _alignment=n; } while(0)
#define FUNCTION_DEFINE_FLATTEN_STRUCT_ARRAY(FLTYPE) \
struct flatten_pointer* flatten_struct_##FLTYPE##_array(const struct FLTYPE* _ptr, size_t n) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct_##FLTYPE(_ptr+_i); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT_ARRAY(FLTYPE) \
extern struct flatten_pointer* flatten_struct_##FLTYPE##_array(const struct FLTYPE* _ptr, size_t n);
#define FUNCTION_DEFINE_FLATTEN_STRUCT2_ARRAY(FLTYPE) \
struct flatten_pointer* flatten_struct2_##FLTYPE##_array(const struct FLTYPE* _ptr, size_t n) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct2_##FLTYPE(_ptr+_i); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT2_ARRAY(FLTYPE) \
extern struct flatten_pointer* flatten_struct2_##FLTYPE##_array(const struct FLTYPE* _ptr, size_t n);
#define FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE_ARRAY(FLTYPE) \
struct flatten_pointer* flatten_struct_type_##FLTYPE##_array(const FLTYPE* _ptr, size_t n) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct_type_##FLTYPE(_ptr+_i); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE_ARRAY(FLTYPE) \
extern struct flatten_pointer* flatten_struct_type_##FLTYPE##_array(const FLTYPE* _ptr, size_t n);
#define FUNCTION_DEFINE_FLATTEN_STRUCT_ARRAY_ITER(FLTYPE) \
struct flatten_pointer* flatten_struct_##FLTYPE##_array_iter(const struct FLTYPE* _ptr, size_t n, struct bqueue* __q) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct_##FLTYPE##_iter(_ptr+_i,__q); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT_ARRAY_ITER(FLTYPE) \
struct flatten_pointer* flatten_struct_##FLTYPE##_array_iter(const struct FLTYPE* _ptr, size_t n, struct bqueue* __q);
#define FUNCTION_DEFINE_FLATTEN_STRUCT2_ARRAY_ITER(FLTYPE) \
struct flatten_pointer* flatten_struct2_##FLTYPE##_array_iter(const struct FLTYPE* _ptr, size_t n, struct bqueue* __q) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct2_##FLTYPE##_iter(_ptr+_i,__q); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE2_ARRAY_ITER(FLTYPE) \
struct flatten_pointer* flatten_struct_type2_##FLTYPE##_array_iter(const FLTYPE* _ptr, size_t n, struct bqueue* __q) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct_type2_##FLTYPE##_iter(_ptr+_i,__q); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT2_ARRAY_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct2_##FLTYPE##_array_iter(const struct FLTYPE* _ptr, size_t n, struct bqueue* __q);
#define FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE2_ARRAY_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct_type2_##FLTYPE##_array_iter(const FLTYPE* _ptr, size_t n, struct bqueue* __q);
#define FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE_ARRAY_ITER(FLTYPE) \
struct flatten_pointer* flatten_struct_type_iter_##FLTYPE##_array(const struct FLTYPE* _ptr, size_t n, struct bqueue* __q) { \
size_t _i; \
void* _fp_first=0; \
for (_i=0; _i<n; ++_i) { \
void* _fp = (void*)flatten_struct_type_##FLTYPE##_iter(_ptr+_i,__q); \
if (!_fp_first) _fp_first=_fp; \
else free(_fp); \
} \
return _fp_first; \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE_ARRAY_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct_type_iter_##FLTYPE##_array(const struct FLTYPE* _ptr, size_t n, struct bqueue* __q);
#define FUNCTION_DEFINE_FLATTEN_STRUCT(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct_##FLTYPE(const struct FLTYPE* _ptr) { \
\
typedef struct FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(struct FLTYPE)-1); \
LIBFLAT_DBGM("flatten_struct_" #FLTYPE "(%lx): [%zu]\n",(uintptr_t)_ptr,sizeof(struct FLTYPE)); \
if (__node) { \
assert(__node->start==(uint64_t)_ptr); \
assert(__node->last==(uint64_t)_ptr+sizeof(struct FLTYPE)-1); \
return make_flatten_pointer(__node,0); \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(struct FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(struct FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node->storage->alignment = _alignment; \
return make_flatten_pointer(__node,0); \
} \
\
FUNCTION_DEFINE_FLATTEN_STRUCT_ARRAY(FLTYPE)
#define FUNCTION_DECLARE_FLATTEN_STRUCT(FLTYPE) \
extern struct flatten_pointer* flatten_struct_##FLTYPE(const struct FLTYPE*); \
FUNCTION_DECLARE_FLATTEN_STRUCT_ARRAY(FLTYPE)
#define FUNCTION_DEFINE_FLATTEN_STRUCT2(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct2_##FLTYPE(const struct FLTYPE* _ptr) { \
\
typedef struct FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(struct FLTYPE)-1); \
LIBFLAT_DBGM("flatten_struct_" #FLTYPE "(%lx): [%zu]\n",(uintptr_t)_ptr,sizeof(struct FLTYPE)); \
if (__node) { \
uintptr_t p = (uintptr_t)_ptr; \
struct interval_tree_node *prev; \
while(__node) { \
if (__node->start>p) { \
assert(__node->storage!=0); \
struct interval_tree_node* nn = calloc(1,sizeof(struct interval_tree_node)); \
assert(nn!=0); \
nn->start = p; \
nn->last = __node->start-1; \
nn->storage = binary_stream_insert_front((void*)p,__node->start-p,__node->storage); \
interval_tree_insert(nn, &FLCTRL.imap_root); \
} \
p = __node->last+1; \
prev = __node; \
__node = interval_tree_iter_next(__node, (uintptr_t)_ptr, (uintptr_t)_ptr+sizeof(struct FLTYPE)-1); \
} \
if ((uintptr_t)_ptr+sizeof(struct FLTYPE)>p) { \
assert(prev->storage!=0); \
struct interval_tree_node* nn = calloc(1,sizeof(struct interval_tree_node)); \
assert(nn!=0); \
nn->start = p; \
nn->last = (uintptr_t)_ptr+sizeof(struct FLTYPE)-1; \
nn->storage = binary_stream_insert_back((void*)p,(uintptr_t)_ptr+sizeof(struct FLTYPE)-p,prev->storage); \
interval_tree_insert(nn, &FLCTRL.imap_root); \
} \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(struct FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(struct FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(struct FLTYPE*)-1); \
assert(__node!=0); \
size_t _node_offset = (uint64_t)_ptr-__node->start; \
__node->storage->alignment = _alignment; \
__node->storage->align_offset = _node_offset; \
return make_flatten_pointer(__node,_node_offset); \
}
#define FUNCTION_DECLARE_FLATTEN_STRUCT2(FLTYPE) \
extern struct flatten_pointer* flatten_struct2_##FLTYPE(const struct FLTYPE*); \
FUNCTION_DECLARE_FLATTEN_STRUCT2_ARRAY(FLTYPE)
#define FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct_type_##FLTYPE(const FLTYPE* _ptr) { \
\
typedef FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(FLTYPE)-1); \
if (__node) { \
assert(__node->start==(uint64_t)_ptr); \
assert(__node->last==(uint64_t)_ptr+sizeof(FLTYPE)-1); \
return make_flatten_pointer(__node,0); \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node->storage->alignment = _alignment; \
return make_flatten_pointer(__node,0); \
} \
\
FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE_ARRAY(FLTYPE)
#define FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE(FLTYPE) \
extern struct flatten_pointer* flatten_struct_type_##FLTYPE(const FLTYPE*); \
FUNCTION_DECLARE_FLATTEN_STRUCT2_TYPE_ARRAY(FLTYPE)
#define FUNCTION_DEFINE_FLATTEN_STRUCT2_ITER(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct2_##FLTYPE##_iter(const struct FLTYPE* _ptr, struct bqueue* __q) { \
\
typedef struct FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(struct FLTYPE)-1); \
(void)__q; \
if (__node) { \
uintptr_t p = (uintptr_t)_ptr; \
struct interval_tree_node *prev; \
while(__node) { \
if (__node->start>p) { \
assert(__node->storage!=0); \
struct interval_tree_node* nn = calloc(1,sizeof(struct interval_tree_node)); \
assert(nn!=0); \
nn->start = p; \
nn->last = __node->start-1; \
nn->storage = binary_stream_insert_front((void*)p,__node->start-p,__node->storage); \
interval_tree_insert(nn, &FLCTRL.imap_root); \
} \
p = __node->last+1; \
prev = __node; \
__node = interval_tree_iter_next(__node, (uintptr_t)_ptr, (uintptr_t)_ptr+sizeof(struct FLTYPE)-1); \
} \
if ((uintptr_t)_ptr+sizeof(struct FLTYPE)>p) { \
assert(prev->storage!=0); \
struct interval_tree_node* nn = calloc(1,sizeof(struct interval_tree_node)); \
assert(nn!=0); \
nn->start = p; \
nn->last = (uintptr_t)_ptr+sizeof(struct FLTYPE)-1; \
nn->storage = binary_stream_insert_back((void*)p,(uintptr_t)_ptr+sizeof(struct FLTYPE)-p,prev->storage); \
interval_tree_insert(nn, &FLCTRL.imap_root); \
} \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(struct FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(struct FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(struct FLTYPE*)-1); \
assert(__node!=0); \
size_t _node_offset = (uint64_t)_ptr-__node->start; \
__node->storage->alignment = _alignment; \
__node->storage->align_offset = _node_offset; \
return make_flatten_pointer(__node,_node_offset); \
} \
\
FUNCTION_DEFINE_FLATTEN_STRUCT2_ARRAY_ITER(FLTYPE)
#define FUNCTION_DECLARE_FLATTEN_STRUCT2_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct2_##FLTYPE##_iter(const struct FLTYPE*, struct bqueue*); \
FUNCTION_DECLARE_FLATTEN_STRUCT2_ARRAY_ITER(FLTYPE)
#define FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE2_ITER(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct_type2_##FLTYPE##_iter(const FLTYPE* _ptr, struct bqueue* __q) { \
\
typedef FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(FLTYPE)-1); \
(void)__q; \
if (__node) { \
uintptr_t p = (uintptr_t)_ptr; \
struct interval_tree_node *prev; \
while(__node) { \
if (__node->start>p) { \
assert(__node->storage!=0); \
struct interval_tree_node* nn = calloc(1,sizeof(struct interval_tree_node)); \
assert(nn!=0); \
nn->start = p; \
nn->last = __node->start-1; \
nn->storage = binary_stream_insert_front((void*)p,__node->start-p,__node->storage); \
interval_tree_insert(nn, &FLCTRL.imap_root); \
} \
p = __node->last+1; \
prev = __node; \
__node = interval_tree_iter_next(__node, (uintptr_t)_ptr, (uintptr_t)_ptr+sizeof(FLTYPE)-1); \
} \
if ((uintptr_t)_ptr+sizeof(FLTYPE)>p) { \
assert(prev->storage!=0); \
struct interval_tree_node* nn = calloc(1,sizeof(struct interval_tree_node)); \
assert(nn!=0); \
nn->start = p; \
nn->last = (uintptr_t)_ptr+sizeof(FLTYPE)-1; \
nn->storage = binary_stream_insert_back((void*)p,(uintptr_t)_ptr+sizeof(FLTYPE)-p,prev->storage); \
interval_tree_insert(nn, &FLCTRL.imap_root); \
} \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(FLTYPE*)-1); \
assert(__node!=0); \
size_t _node_offset = (uint64_t)_ptr-__node->start; \
__node->storage->alignment = _alignment; \
__node->storage->align_offset = _node_offset; \
return make_flatten_pointer(__node,_node_offset); \
} \
\
FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE2_ARRAY_ITER(FLTYPE)
#define FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE2_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct_type2_##FLTYPE##_iter(const FLTYPE*, struct bqueue*); \
FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE2_ARRAY_ITER(FLTYPE)
#define FUNCTION_DEFINE_FLATTEN_STRUCT_ITER(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct_##FLTYPE##_iter(const struct FLTYPE* _ptr, struct bqueue* __q) { \
\
typedef struct FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(struct FLTYPE)-1); \
(void)__q; \
if (__node) { \
assert(__node->start==(uint64_t)_ptr); \
assert(__node->last==(uint64_t)_ptr+sizeof(struct FLTYPE)-1); \
return make_flatten_pointer(__node,0); \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(struct FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(struct FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(struct FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node->storage->alignment = _alignment; \
return make_flatten_pointer(__node,0); \
} \
\
FUNCTION_DEFINE_FLATTEN_STRUCT_ARRAY_ITER(FLTYPE)
#define FUNCTION_DECLARE_FLATTEN_STRUCT_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct_##FLTYPE##_iter(const struct FLTYPE*, struct bqueue*); \
FUNCTION_DECLARE_FLATTEN_STRUCT_ARRAY_ITER(FLTYPE)
#define FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE_ITER(FLTYPE,...) \
/* */ \
\
struct flatten_pointer* flatten_struct_type_##FLTYPE##_iter(const FLTYPE* _ptr, struct bqueue* __q) { \
\
typedef FLTYPE _container_type; \
size_t _alignment = 0; \
\
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr, (uint64_t)_ptr+sizeof(FLTYPE)-1); \
(void)__q; \
if (__node) { \
assert(__node->start==(uint64_t)_ptr); \
assert(__node->last==(uint64_t)_ptr+sizeof(FLTYPE)-1); \
return make_flatten_pointer(__node,0); \
} \
else { \
__node = calloc(1,sizeof(struct interval_tree_node)); \
assert(__node!=0); \
__node->start = (uint64_t)_ptr; \
__node->last = (uint64_t)_ptr + sizeof(FLTYPE)-1; \
struct blstream* storage; \
struct rb_node* rb = interval_tree_insert(__node, &FLCTRL.imap_root); \
struct rb_node* prev = rb_prev(rb); \
if (prev) { \
storage = binary_stream_insert_back(_ptr,sizeof(FLTYPE),((struct interval_tree_node*)prev)->storage); \
} \
else { \
struct rb_node* next = rb_next(rb); \
if (next) { \
storage = binary_stream_insert_front(_ptr,sizeof(FLTYPE),((struct interval_tree_node*)next)->storage); \
} \
else { \
storage = binary_stream_append(_ptr,sizeof(FLTYPE)); \
} \
} \
__node->storage = storage; \
} \
\
__VA_ARGS__ \
__node->storage->alignment = _alignment; \
return make_flatten_pointer(__node,0); \
} \
\
FUNCTION_DEFINE_FLATTEN_STRUCT_TYPE_ARRAY_ITER(FLTYPE)
#define FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE_ITER(FLTYPE) \
extern struct flatten_pointer* flatten_struct_type_##FLTYPE##_iter(const struct FLTYPE*, struct bqueue*); \
FUNCTION_DECLARE_FLATTEN_STRUCT_TYPE_ARRAY_ITER(FLTYPE)
#define FLATTEN_STRUCT(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT,T,p); \
if (p) { \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct_##T((p))); \
} \
} while(0)
#define FLATTEN_STRUCT2(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT2,T,p); \
if (p) { \
struct fixup_set_node* __inode = fixup_set_search((uintptr_t)p); \
if (!__inode) { \
fixup_set_reserve_address((uintptr_t)p); \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct2_##T((p))); \
} \
else { \
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uintptr_t)p,\
(uintptr_t)p+sizeof(struct T)-1); \
fixup_set_insert(__fptr->node,__fptr->offset,make_flatten_pointer(__node,(uintptr_t)p-__node->start)); \
} \
} \
} while(0)
#define FLATTEN_STRUCT_TYPE(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT_TYPE,T,p); \
if (p) { \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct_type_##T((p))); \
} \
} while(0)
#define FLATTEN_STRUCT_ARRAY(T,p,n) do { \
LIBFLAT_DBGM3(FLATTEN_STRUCT_ARRAY,T,p,n); \
if (p) { \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct_##T##_array((p),(n))); \
} \
} while(0)
#define FLATTEN_STRUCT_TYPE_ARRAY(T,p,n) do { \
LIBFLAT_DBGM3(FLATTEN_STRUCT_TYPE_ARRAY,T,p,n); \
if (p) { \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct_type_##T##_array((p),(n))); \
} \
} while(0)
#define FLATTEN_STRUCT_ITER(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT_ITER,T,p); \
if (p) { \
struct flatten_job __job; \
__job.node = __fptr->node; \
__job.offset = __fptr->offset; \
__job.size = 1; \
__job.ptr = (struct flatten_base*)p; \
__job.fun = (flatten_struct_t)&flatten_struct_##T##_array_iter; \
__job.fp = 0; \
__job.convert = 0; \
bqueue_push_back(__q,&__job,sizeof(struct flatten_job)); \
} \
} while(0)
#define FLATTEN_STRUCT2_ITER(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT2_ITER,T,p); \
if (p) { \
struct fixup_set_node* __inode = fixup_set_search((uintptr_t)p); \
if (!__inode) { \
fixup_set_reserve_address((uintptr_t)p); \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct2_##T##_iter((p),__q)); \
} \
else { \
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uintptr_t)p,\
(uintptr_t)p+sizeof(struct T)-1); \
assert(__node!=0); \
fixup_set_insert(__fptr->node,__fptr->offset,make_flatten_pointer(__node,(uintptr_t)p-__node->start)); \
} \
} \
} while(0)
#define FLATTEN_STRUCT_TYPE2_ITER(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT_TYPE2_ITER,T,p); \
if (p) { \
struct fixup_set_node* __inode = fixup_set_search((uintptr_t)p); \
if (!__inode) { \
fixup_set_reserve_address((uintptr_t)p); \
fixup_set_insert(__fptr->node,__fptr->offset,flatten_struct_type2_##T##_iter((p),__q)); \
} \
else { \
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uintptr_t)p,\
(uintptr_t)p+sizeof(T)-1); \
assert(__node!=0); \
fixup_set_insert_force_update(__fptr->node,__fptr->offset,make_flatten_pointer(__node,(uintptr_t)p-__node->start)); \
} \
} \
} while(0)
#define FLATTEN_STRUCT_TYPE_ITER(T,p) do { \
LIBFLAT_DBGTP(FLATTEN_STRUCT_TYPE_ITER,T,p); \
if (p) { \
struct flatten_job __job; \
__job.node = __fptr->node; \
__job.offset = __fptr->offset; \
__job.size = 1; \
__job.ptr = (struct flatten_base*)p; \
__job.fun = (flatten_struct_t)&flatten_struct_type_iter_##T##_array; \
__job.fp = 0; \
__job.convert = 0; \
bqueue_push_back(__q,&__job,sizeof(struct flatten_job)); \
} \
} while(0)
#define FLATTEN_STRUCT_ARRAY_ITER(T,p,n) do { \
LIBFLAT_DBGM3(FLATTEN_STRUCT_ARRAY_ITER,T,p,n); \
if (p) { \
struct flatten_job __job; \
__job.node = __fptr->node; \
__job.offset = __fptr->offset; \
__job.size = n; \
__job.ptr = (struct flatten_base*)p; \
__job.fun = (flatten_struct_t)&flatten_struct_##T##_array_iter; \
__job.fp = 0; \
__job.convert = 0; \
bqueue_push_back(__q,&__job,sizeof(struct flatten_job)); \
} \
} while(0)
#define FLATTEN_STRUCT_TYPE_ARRAY_ITER(T,p,n) do { \
LIBFLAT_DBGM3(FLATTEN_STRUCT_TYPE_ARRAY_ITER,T,p,n); \
if (p) { \
struct flatten_job __job; \
__job.node = __fptr->node; \
__job.offset = __fptr->offset; \
__job.size = n; \
__job.ptr = (struct flatten_base*)p; \
__job.fun = (flatten_struct_t)&flatten_struct_type_##T##_array_iter; \
__job.fp = 0; \
__job.convert = 0; \
bqueue_push_back(__q,&__job,sizeof(struct flatten_job)); \
} \
} while(0)
#define AGGREGATE_FLATTEN_STRUCT(T,f) do { \
LIBFLAT_DBGTF(AGGREGATE_FLATTEN_STRUCT,T,f,"%p",(void*)ATTR(f)); \
if (ATTR(f)) { \
fixup_set_insert(__node,offsetof(_container_type,f),flatten_struct_##T((const struct T*)ATTR(f))); \
} \
} while(0)
/*
#define acquire_attribute_nodes(start,size) \
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, start,start+size-1); \
assert(__node!=0); \
struct fixup_set_node* __inode = fixup_set_search(start);
acquire_attribute_nodes((uint64_t)_ptr+offsetof(_container_type,f),sizeof(struct T*))
*/
#define AGGREGATE_FLATTEN_STRUCT2(T,f) do { \
LIBFLAT_DBGTF(AGGREGATE_FLATTEN_STRUCT2,T,f,"%p",(void*)ATTR(f)); \
if (ATTR(f)) { \
size_t _off = offsetof(_container_type,f); \
struct interval_tree_node *__node = interval_tree_iter_first(&FLCTRL.imap_root, (uint64_t)_ptr+_off,\
(uint64_t)_ptr+_off+sizeof(struct T*)-1); \
assert(__node!=0); \
struct fixup_set_node* __inode = fixup_set_search((uint64_t)ATTR(f)); \
if (!__inode) { \
fixup_set_reserve_address((uintptr_t)ATTR(f)); \
fixup_set_insert(__node,(uint64_t)_ptr-__node->start+_off,flatten_struct2_##T((const struct T*)ATTR(f))); \
} \
else { \
struct interval_tree_node *__struct_node = interval_tree_iter_first(&FLCTRL.imap_root, (uintptr_t)ATTR(f),\
(uintptr_t)ATTR(f)+sizeof(struct T)-1); \
assert(__struct_node!=0); \
fixup_set_insert(__node,(uint64_t)_ptr-__node->start+_off, \
make_flatten_pointer(__struct_node,(uintptr_t)ATTR(f)-__struct_node->start)); \
} \
} \
} while(0)