This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
bytes.c
2495 lines (1960 loc) · 69.4 KB
/
bytes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "autogen/hilti-hlt.h"
#include "bytes.h"
#include "exceptions.h"
#include "globals.h"
#include "hutil.h"
#include "int.h"
#include "memory_.h"
#include "string_.h"
#include "threading.h"
static const size_t __HLT_BYTES_MIN_RESERVE = 32;
// Bytes object cannot be changed anymore.
static const int _BYTES_FLAG_FROZEN = 1;
// Data of this node contains a separator object. It's fine to cast to
// __hlt_bytes_object in that case. The other data pointers inside the bytes
// object aren't valid in this case, and set to null.
static const int _BYTES_FLAG_OBJECT = 2;
// Layout here must match libhilti.ll!
struct __hlt_bytes {
__hlt_gchdr __gchdr; // Header for memory management.
__hlt_thread_mgr_blockable blockable; // For blocking until changed.
int8_t flags; // Flags as combiniation of _BYTES_FLAG_* values.
struct __hlt_bytes* next; // Next part. Ref counted.
hlt_bytes_size offset; // The offset of this chunks first byte relative to the beginning of the
// bytes object it's part of.
int8_t* start; // Pointer to first data byte.
int8_t* end; // Pointer to one after last data byte used so far.
int8_t* reserved; // Pointer to one after the last data byte available.
int8_t* to_free; // Need to free data pointed to when dtoring.
hlt_bytes_size* marks; // If non-null, array of offsets of marks within this chunk. Terminated
// by -1. Must be freed.
int8_t data[]; // Inline data starts here if free is zero.
};
// Specialized bytes object storing a separator object.
struct __hlt_bytes_object {
struct __hlt_bytes b; // Common header.
const hlt_type_info* type; // Type of the object;
char object[]; // Object's storage starts here, with size determined by type.
};
// Hoisted version when storing on the stack. Layout here must match
// libhilti.ll!
struct __hlt_bytes_hoisted {
struct __hlt_bytes b;
int8_t data[32];
};
typedef struct __hlt_bytes_object __hlt_bytes_object;
static hlt_iterator_bytes GenericEndPos = {0, 0};
static hlt_bytes* _hlt_bytes_new(const int8_t* data, hlt_bytes_size len, hlt_bytes_size reserve,
hlt_execution_context* ctx);
static void __add_chunk(hlt_bytes* tail, hlt_bytes* c, hlt_execution_context* ctx);
static inline hlt_bytes_size min(hlt_bytes_size a, hlt_bytes_size b)
{
return a < b ? a : b;
}
static inline __hlt_bytes_object* __get_object(const hlt_bytes* b)
{
return (b && (b->flags & _BYTES_FLAG_OBJECT)) ? (__hlt_bytes_object*)b : 0;
}
static inline hlt_bytes* __tail(hlt_bytes* b, int8_t consider_object)
{
if ( ! b )
return 0;
while ( b->next ) {
if ( ! consider_object && __get_object(b->next) )
break;
b = b->next;
}
return b;
}
static inline int8_t __at_object(const hlt_iterator_bytes i)
{
return i.bytes && __get_object(i.bytes) != 0;
}
static inline int8_t __at_mark(const hlt_iterator_bytes i)
{
hlt_bytes* b = i.bytes;
if ( ! (b && b->marks) )
return 0;
for ( hlt_bytes_size* m = b->marks; m && *m != -1; m++ ) {
if ( *m == (i.cur - b->start) )
return 1;
}
return 0;
}
static inline int8_t __is_end(const hlt_iterator_bytes p)
{
return p.bytes == 0 || (p.bytes == __tail(p.bytes, false) && p.cur >= p.bytes->end) ||
__at_object(p);
}
static inline int8_t __is_frozen(const hlt_bytes* b)
{
return b ? (b->flags & _BYTES_FLAG_FROZEN) : false;
}
#if 0
// For debugging.
static void __print_bytes(const char* prefix, const hlt_bytes* b)
{
if ( ! b ) {
fprintf(stderr, "%s: (null)\n", prefix);
return;
}
fprintf(stderr, "%s: %p b( ", prefix, b);
for ( ; b; b = b->next ) {
fprintf(stderr, "#%ld:%p-%p \"", b->end - b->start, b->start, b->end);
if ( __get_object(b) ) {
fprintf(stderr, " [[object]] ");
continue;
}
for ( int i = 0; i < min(b->end - b->start, 20); i++ ) {
if ( isprint(b->start[i]) )
fprintf(stderr, "%c", b->start[i]);
else
fprintf(stderr, "\\x%02x", b->start[i]);
}
if ( (b->end - b->start) > 20 )
fprintf(stderr, "...");
fprintf(stderr, "\" ");
if ( b->marks ) {
fprintf(stderr, "[ ");
for ( hlt_bytes_size* m = b->marks; m && *m >= 0; m++ )
fprintf(stderr, "m@%" PRId64 " ", *m);
fprintf(stderr, "] ");
}
}
fprintf(stderr, ")\n");
}
#endif
#if 0
static void __print_iterator(const char* prefix, hlt_iterator_bytes i)
{
fprintf(stderr, "%s: i(#%p@%lu) is_end=%d at_object=%d at_mark=%d\n", prefix, i.bytes,
i.bytes ? i.cur - i.bytes->start : 0, __is_end(i), __at_object(i), __at_mark(i));
__print_bytes(" -> ", i.bytes);
}
#endif
static inline int8_t __is_empty(const hlt_bytes* b, int8_t consider_objects)
{
for ( const hlt_bytes* c = b; c; c = c->next ) {
if ( __get_object(c) )
return ! consider_objects;
if ( c->start != c->end )
return 0;
}
return 1;
}
// TODO: Frequent enough to want a double-linked list?
static inline hlt_bytes* __pred(hlt_bytes* b, hlt_bytes* p)
{
for ( ; b && b->next != p; b = b->next )
;
return b;
}
// Does not ref the iterator.
static inline hlt_iterator_bytes __create_iterator(hlt_bytes* bytes, int8_t* cur)
{
hlt_iterator_bytes i = {bytes, cur};
return i;
}
void __hlt_iterator_bytes_incr_by(hlt_iterator_bytes* p, int64_t n, hlt_exception** excpt,
hlt_execution_context* ctx, int8_t adj_ref,
int8_t move_beyond_end);
hlt_iterator_bytes hlt_bytes_offset(hlt_bytes* b, hlt_bytes_size p, hlt_exception** excpt,
hlt_execution_context* ctx);
// This version does not adjust the reference count and must be called only
// when the potentiall changed iterator will not be visible to the HILTI
// layer.
static inline void __normalize_iter(hlt_iterator_bytes* pos)
{
if ( ! pos->bytes || __at_object(*pos) )
return;
// If the pos was previously an end position but now new data has been
// added, adjust it so that it's pointing to the next byte (or even
// beyond that, if the previous iterator recorded a future position).
// Note we do not adjust the reference count in this version.
int64_t incr = 0;
while ( pos->cur >= pos->bytes->end && pos->bytes->next && ! __at_object(*pos) ) {
incr += (pos->cur - pos->bytes->end);
pos->bytes = pos->bytes->next;
pos->cur = pos->bytes->start;
}
if ( incr )
__hlt_iterator_bytes_incr_by(pos, incr, 0, 0, 0, 1);
}
// This version adjust the reference count and should be called only when the
// potentiall changed iterator will be visible to the HILTI layer.
static inline void __normalize_iter_hilti(hlt_iterator_bytes* pos, hlt_execution_context* ctx)
{
if ( ! pos->bytes || __at_object(*pos) )
return;
// If the pos was previously an end position but now new data has been
// added, adjust it so that it's pointing to the next byte.
int64_t incr = 0;
while ( pos->cur >= pos->bytes->end && pos->bytes->next ) {
incr += (pos->cur - pos->bytes->end);
pos->bytes = pos->bytes->next;
pos->cur = pos->bytes->start;
}
if ( incr )
__hlt_iterator_bytes_incr_by(pos, incr, 0, 0, 1, 1);
}
hlt_bytes_size __hlt_bytes_len(hlt_bytes* b)
{
hlt_bytes_size len = 0;
for ( ; b && ! __get_object(b); b = b->next )
len += (b->end - b->start);
return len;
}
// This reservers enough space to fit all old plus addl_reserve new marks
// plus the final -1, and then returns a pointer to where the first new mark
// should be stored.
hlt_bytes_size* __hlt_bytes_reserve_space_for_more_marks(hlt_bytes_size** dst, int addl_reserve)
{
assert(dst);
if ( ! *dst ) {
*dst = hlt_malloc((addl_reserve + 1) * sizeof(hlt_bytes_size));
return *dst;
}
else {
int len_dst = 0;
for ( hlt_bytes_size *i = *dst; *i >= 0; i++, len_dst++ )
;
*dst = hlt_realloc(*dst, (len_dst + addl_reserve + 1) * sizeof(hlt_bytes_size),
(len_dst + 1) * sizeof(hlt_bytes_size));
return *dst + len_dst;
}
}
void __hlt_bytes_append_mark(hlt_bytes* b, hlt_bytes_size mark, hlt_execution_context* ctx)
{
hlt_bytes* tail = __tail(b, true);
if ( __get_object(tail) ) {
// Need to add an empty block to record the mark.
hlt_bytes* empty = _hlt_bytes_new(0, 0, 0, ctx);
__add_chunk(tail, empty, ctx);
tail = empty;
}
hlt_bytes_size* dst = __hlt_bytes_reserve_space_for_more_marks(&tail->marks, 1);
*dst++ = (mark >= 0 ? mark : (tail->end - tail->start));
*dst++ = -1;
}
void __hlt_bytes_copy_marks(hlt_bytes_size** marks, hlt_bytes* b, int8_t* first, int8_t* last,
int8_t adjoffset)
{
if ( ! (marks && b->marks) )
return;
int n = 0;
for ( hlt_bytes_size *i = b->marks; *i != -1; i++, n++ )
;
hlt_bytes_size* dst = __hlt_bytes_reserve_space_for_more_marks(marks, n);
int64_t offset_first = first ? (first - b->start) : -1;
int64_t offset_last = last ? (last - b->start) : -1;
for ( hlt_bytes_size* p = b->marks; *p != -1; p++ ) {
if ( *p < 0 )
continue;
hlt_bytes_size offset = *p + adjoffset;
if ( offset_first >= 0 && offset < offset_first )
continue;
if ( offset_last >= 0 && offset >= offset_last )
continue;
*dst++ = offset;
}
*dst++ = -1;
}
// c not yet ref'ed.
static void __add_chunk(hlt_bytes* tail, hlt_bytes* c, hlt_execution_context* ctx)
{
assert(c);
assert(tail);
assert(! tail->next);
assert(! __is_frozen(tail));
GC_CCTOR(c, hlt_bytes, ctx);
tail->next = c;
c->offset = tail->offset + (__get_object(tail) ? 0 : tail->end - tail->start);
if ( tail->marks ) {
for ( hlt_bytes_size* p = tail->marks; *p != -1; p++ ) {
if ( *p >= (tail->end - tail->start) ) {
__hlt_bytes_append_mark(tail, *p - (tail->end - tail->start), ctx);
*p = -2; // Delete.
}
}
}
}
static inline void _hlt_bytes_init(hlt_bytes* b, const int8_t* data, hlt_bytes_size len,
hlt_bytes_size reserve, hlt_execution_context* ctx)
{
assert(reserve >= len);
b->flags = 0;
b->next = 0;
b->offset = 0;
b->start = b->data;
b->end = b->start + len;
b->reserved = b->start + reserve;
b->to_free = 0;
b->marks = 0;
hlt_thread_mgr_blockable_init(&b->blockable);
if ( data )
memcpy(b->data, data, len);
}
static inline void _hlt_bytes_init_reuse(hlt_bytes* b, int8_t* data, hlt_bytes_size len,
hlt_execution_context* ctx)
{
b->flags = 0;
b->next = 0;
b->offset = 0;
b->start = data;
b->end = data + len;
b->reserved = data + len;
b->to_free = data;
b->marks = 0;
hlt_thread_mgr_blockable_init(&b->blockable);
}
static void _hlt_bytes_init_object(__hlt_bytes_object* b, const hlt_type_info* type, void* obj,
hlt_execution_context* ctx)
{
b->b.next = 0;
b->b.flags = _BYTES_FLAG_OBJECT;
b->b.offset = 0;
b->b.marks = 0;
b->type = type;
hlt_thread_mgr_blockable_init(&b->b.blockable);
if ( obj ) {
memcpy(&b->object, obj, type->size);
GC_CCTOR_GENERIC(&b->object, type, ctx);
}
}
static inline void _hlt_bytes_init_hoisted(__hlt_bytes_hoisted* dst, const int8_t* data,
hlt_bytes_size len, hlt_execution_context* ctx)
{
hlt_bytes* b = &dst->b;
if ( b->to_free )
// Previous use had allocated memory.
//
// TODO: Is it save to assume that a newly allocated instance will
// have this cleared?
hlt_free(b->to_free);
if ( b->marks )
// Previous use had allocated memory.
hlt_free(b->marks);
if ( len <= sizeof(dst->data) ) {
b->start = dst->data;
b->reserved = b->start + sizeof(dst->data);
b->to_free = 0;
}
else {
b->start = hlt_malloc(len);
b->reserved = b->start + len;
b->to_free = b->start;
}
b->flags = 0;
b->offset = 0;
b->next = 0;
b->end = b->start + len;
b->marks = 0;
hlt_thread_mgr_blockable_init(&b->blockable);
if ( data )
memcpy(b->data, data, len);
}
static hlt_bytes* _hlt_bytes_new(const int8_t* data, hlt_bytes_size len, hlt_bytes_size reserve,
hlt_execution_context* ctx)
{
if ( ! reserve )
reserve = (len ? len : __HLT_BYTES_MIN_RESERVE);
hlt_bytes* b = GC_NEW_CUSTOM_SIZE_NO_INIT(hlt_bytes, sizeof(hlt_bytes) + reserve, ctx);
_hlt_bytes_init(b, data, len, reserve, ctx);
return b;
}
static hlt_bytes* _hlt_bytes_new_ref(const int8_t* data, hlt_bytes_size len, hlt_bytes_size reserve,
hlt_execution_context* ctx)
{
if ( ! reserve )
reserve = (len ? len : __HLT_BYTES_MIN_RESERVE);
hlt_bytes* b = GC_NEW_CUSTOM_SIZE_NO_INIT_REF(hlt_bytes, sizeof(hlt_bytes) + reserve, ctx);
_hlt_bytes_init(b, data, len, reserve, ctx);
return b;
}
static hlt_bytes* _hlt_bytes_new_reuse(int8_t* data, hlt_bytes_size len, hlt_execution_context* ctx)
{
hlt_bytes* b = GC_NEW_NO_INIT(hlt_bytes, ctx);
_hlt_bytes_init_reuse(b, data, len, ctx);
return b;
}
#if 0
static hlt_bytes* _hlt_bytes_new_reuse_ref(int8_t* data, hlt_bytes_size len,
hlt_execution_context* ctx)
{
hlt_bytes* b = GC_NEW_NO_INIT_REF(hlt_bytes, ctx);
_hlt_bytes_init_reuse(b, data, len, ctx);
return b;
}
#endif
static hlt_bytes* _hlt_bytes_new_object(const hlt_type_info* type, void* obj,
hlt_execution_context* ctx)
{
__hlt_bytes_object* b =
GC_NEW_CUSTOM_SIZE_NO_INIT(hlt_bytes, sizeof(__hlt_bytes_object) + type->size, ctx);
_hlt_bytes_init_object(b, type, obj, ctx);
return &b->b;
}
static hlt_bytes* _hlt_bytes_new_object_ref(const hlt_type_info* type, void* obj,
hlt_execution_context* ctx)
{
__hlt_bytes_object* b =
GC_NEW_CUSTOM_SIZE_NO_INIT_REF(hlt_bytes, sizeof(__hlt_bytes_object) + type->size, ctx);
_hlt_bytes_init_object(b, type, obj, ctx);
return &b->b;
}
void hlt_bytes_dtor(hlt_type_info* ti, hlt_bytes* b, hlt_execution_context* ctx)
{
b->start = b->end = 0;
GC_CLEAR(b->next, hlt_bytes, ctx);
__hlt_bytes_object* obj = __get_object(b);
if ( obj ) {
GC_DTOR_GENERIC(&obj->object, obj->type, ctx);
}
else {
if ( b->to_free )
hlt_free(b->to_free);
if ( b->marks )
hlt_free(b->marks);
}
}
void hlt_iterator_bytes_dtor(hlt_type_info* ti, hlt_iterator_bytes* p, hlt_execution_context* ctx)
{
GC_DTOR(p->bytes, hlt_bytes, ctx);
}
void hlt_iterator_bytes_cctor(hlt_type_info* ti, hlt_iterator_bytes* p, hlt_execution_context* ctx)
{
GC_CCTOR(p->bytes, hlt_bytes, ctx);
}
void hlt_bytes_new_hoisted(__hlt_bytes_hoisted* dst, hlt_exception** excpt,
hlt_execution_context* ctx)
{
_hlt_bytes_init_hoisted(dst, 0, 0, ctx);
}
hlt_bytes* hlt_bytes_new(hlt_exception** excpt, hlt_execution_context* ctx)
{
return _hlt_bytes_new(0, 0, 0, ctx);
}
hlt_bytes* hlt_bytes_new_from_data(int8_t* data, hlt_bytes_size len, hlt_exception** excpt,
hlt_execution_context* ctx)
{
return _hlt_bytes_new_reuse(data, len, ctx);
}
void hlt_bytes_new_from_data_copy_hoisted(__hlt_bytes_hoisted* dst, const int8_t* data,
hlt_bytes_size len, hlt_exception** excpt,
hlt_execution_context* ctx)
{
_hlt_bytes_init_hoisted(dst, data, len, ctx);
}
hlt_bytes* hlt_bytes_new_from_data_copy(const int8_t* data, hlt_bytes_size len,
hlt_exception** excpt, hlt_execution_context* ctx)
{
return _hlt_bytes_new(data, len, 0, ctx);
}
void* hlt_bytes_clone_alloc(const hlt_type_info* ti, void* srcp, __hlt_clone_state* cstate,
hlt_exception** excpt, hlt_execution_context* ctx)
{
hlt_bytes* src = *(hlt_bytes**)srcp;
__hlt_bytes_object* so = __get_object(src);
if ( so )
return _hlt_bytes_new_object_ref(so->type, 0, ctx);
else
return _hlt_bytes_new_ref(0, src->end - src->start, 0, ctx);
}
void hlt_bytes_clone_init(void* dstp, const hlt_type_info* ti, void* srcp,
__hlt_clone_state* cstate, hlt_exception** excpt,
hlt_execution_context* ctx)
{
hlt_bytes* src = *(hlt_bytes**)srcp;
hlt_bytes* dst = *(hlt_bytes**)dstp;
assert(src && dst);
dst->flags = src->flags;
dst->offset = src->offset;
dst->marks = 0;
int first = 1;
for ( ; src; src = src->next ) {
hlt_bytes* b = 0;
__hlt_bytes_object* so = __get_object(src);
if ( so ) {
if ( first ) {
// Already allocated.
__hlt_bytes_object* no = (__hlt_bytes_object*)dst;
memcpy(&no->object, &so->object, no->type->size);
GC_CCTOR_GENERIC(&no->object, no->type, ctx);
b = &no->b;
}
else {
void* p = &so->object;
__hlt_bytes_object* no =
(__hlt_bytes_object*)_hlt_bytes_new_object(so->type, p, ctx);
b = &no->b;
}
}
else {
if ( first ) {
// Already allocated.
hlt_bytes_size n = (src->end - src->start);
memcpy(dst->start, src->start, n);
b = dst;
}
else {
hlt_bytes_size n = (src->end - src->start);
b = _hlt_bytes_new(src->start, n, 0, ctx);
}
__hlt_bytes_copy_marks(&b->marks, src, 0, 0, 0);
}
if ( ! first ) {
__add_chunk(dst, b, ctx);
dst = b;
}
else
first = 0;
}
}
// Returns the number of bytes stored.
hlt_bytes_size hlt_bytes_len(hlt_bytes* b, hlt_exception** excpt, hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return 0;
}
return __hlt_bytes_len(b);
}
// Returns true if empty.
int8_t hlt_bytes_empty(hlt_bytes* b, hlt_exception** excpt, hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return 0;
}
return __is_empty(b, false);
}
void __hlt_bytes_append_raw(hlt_bytes* b, int8_t* raw, hlt_bytes_size len, hlt_exception** excpt,
hlt_execution_context* ctx, int8_t reuse)
{
if ( ! len ) {
hlt_free(raw);
return;
}
if ( __is_frozen(b) ) {
hlt_free(raw);
hlt_set_exception(excpt, &hlt_exception_value_error, 0, ctx);
return;
}
// TODO: Copy into available space if large enough.
hlt_bytes* c;
if ( reuse )
c = _hlt_bytes_new_reuse(raw, len, ctx);
else
c = _hlt_bytes_new(raw, len, 0, ctx);
__add_chunk(__tail(b, true), c, ctx);
hlt_thread_mgr_unblock(&b->blockable, ctx);
}
// Appends one Bytes object to another.
void __hlt_bytes_append(hlt_bytes* b, hlt_bytes* other, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return;
}
if ( __is_frozen(b) ) {
hlt_set_exception(excpt, &hlt_exception_value_error, 0, ctx);
return;
}
hlt_bytes_size len = hlt_bytes_len(other, excpt, ctx);
if ( ! len )
return;
// TODO: Copy into available space if large enough.
hlt_bytes* dst = _hlt_bytes_new(0, len, 0, ctx);
int8_t* p = dst->start;
for ( ; other && ! __get_object(other); other = other->next ) {
__hlt_bytes_copy_marks(&dst->marks, other, 0, 0, p - dst->start);
hlt_bytes_size n = other->end - other->start;
memcpy(p, other->start, n);
p += n;
}
__add_chunk(__tail(b, true), dst, ctx);
hlt_thread_mgr_unblock(&b->blockable, ctx);
}
void hlt_bytes_append(hlt_bytes* b, hlt_bytes* other, hlt_exception** excpt,
hlt_execution_context* ctx)
{
__hlt_bytes_append(b, other, excpt, ctx);
}
void hlt_bytes_append_raw(hlt_bytes* b, int8_t* raw, hlt_bytes_size len, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return;
}
__hlt_bytes_append_raw(b, raw, len, excpt, ctx, 1);
}
void hlt_bytes_append_raw_copy(hlt_bytes* b, int8_t* raw, hlt_bytes_size len, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return;
}
__hlt_bytes_append_raw(b, raw, len, excpt, ctx, 0);
}
static void _hlt_bytes_concat_into(hlt_bytes* dst, hlt_bytes* b1, hlt_bytes* b2,
hlt_exception** excpt, hlt_execution_context* ctx)
{
// Assumes that dst has enough space available.
#ifdef DEBUG
hlt_bytes_size len1 = hlt_bytes_len(b1, excpt, ctx);
hlt_bytes_size len2 = hlt_bytes_len(b2, excpt, ctx);
assert(len1 + len2 <= (dst->reserved - dst->start));
_UNUSED(len1);
_UNUSED(len2);
#endif
int8_t* p = dst->start;
for ( ; b1 && ! __get_object(b1); b1 = b1->next ) {
__hlt_bytes_copy_marks(&dst->marks, b1, 0, 0, p - dst->start);
hlt_bytes_size n = b1->end - b1->start;
memcpy(p, b1->start, n);
p += n;
}
for ( ; b2 && ! __get_object(b2); b2 = b2->next ) {
__hlt_bytes_copy_marks(&dst->marks, b2, 0, 0, p - dst->start);
hlt_bytes_size n = b2->end - b2->start;
memcpy(p, b2->start, n);
p += n;
}
}
void hlt_bytes_concat_hoisted(__hlt_bytes_hoisted* dst, hlt_bytes* b1, hlt_bytes* b2,
hlt_exception** excpt, hlt_execution_context* ctx)
{
assert(dst);
if ( ! (dst && b1 && b2) )
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
hlt_bytes_size len1 = hlt_bytes_len(b1, excpt, ctx);
hlt_bytes_size len2 = hlt_bytes_len(b2, excpt, ctx);
_hlt_bytes_init_hoisted(dst, 0, len1 + len2, ctx);
_hlt_bytes_concat_into(&dst->b, b1, b2, excpt, ctx);
}
hlt_bytes* hlt_bytes_concat(hlt_bytes* b1, hlt_bytes* b2, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( ! (b1 && b2) ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return 0;
}
hlt_bytes_size len1 = hlt_bytes_len(b1, excpt, ctx);
hlt_bytes_size len2 = hlt_bytes_len(b2, excpt, ctx);
hlt_bytes* dst = _hlt_bytes_new(0, len1 + len2, 0, ctx);
_hlt_bytes_concat_into(dst, b1, b2, excpt, ctx);
return dst;
}
void __hlt_bytes_find_byte(hlt_iterator_bytes* p, hlt_bytes* b, int8_t chr, hlt_exception** excpt,
hlt_execution_context* ctx)
{
for ( ; b && ! __get_object(b); b = b->next ) {
int8_t* c = memchr(b->start, chr, b->end - b->start);
if ( c ) {
*p = __create_iterator(b, c);
return;
}
}
*p = GenericEndPos;
}
hlt_iterator_bytes hlt_bytes_find_byte(hlt_bytes* b, int8_t chr, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
return GenericEndPos;
}
hlt_iterator_bytes p;
__hlt_bytes_find_byte(&p, b, chr, excpt, ctx);
return p;
}
void __hlt_bytes_end(hlt_iterator_bytes* p, hlt_bytes* b, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( ! b ) {
hlt_set_exception(excpt, &hlt_exception_null_reference, 0, ctx);
*p = GenericEndPos;
return;
}
p->bytes = __tail(b, false);
p->cur = p->bytes->end;
}
void __hlt_bytes_begin(hlt_iterator_bytes* p, hlt_bytes* b, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( __is_empty(b, false) ) {
__hlt_bytes_end(p, b, excpt, ctx);
return;
}
p->bytes = b;
p->cur = b->start;
__normalize_iter(p);
}
void __hlt_iterator_bytes_incr(hlt_iterator_bytes* p, hlt_exception** excpt,
hlt_execution_context* ctx, int8_t adj_ref)
{
if ( __is_end(*p) )
// Fail silently.
return;
// Can we stay inside the same chunk?
if ( p->cur + 1 < p->bytes->end ) {
++p->cur;
return;
}
if ( ! p->bytes->next ) {
// End reached.
p->cur = p->bytes->end;
return;
}
// Switch chunk.
if ( adj_ref ) {
p->bytes = p->bytes->next;
}
else
p->bytes = p->bytes->next;
p->cur = p->bytes->start;
}
void __hlt_iterator_bytes_incr_by(hlt_iterator_bytes* p, int64_t n, hlt_exception** excpt,
hlt_execution_context* ctx, int8_t adj_ref,
int8_t move_beyond_end)
{
if ( __is_end(*p) )
// Fail silently.
return;
if ( ! n )
return;
while ( 1 ) {
// Can we stay inside the same chunk?
if ( p->cur + n < p->bytes->end ) {
p->cur += n;
return;
}
if ( ! p->bytes->next || __get_object(p->bytes) ) {
// End reached.
if ( ! move_beyond_end )
p->cur = p->bytes->end;
else
p->cur = p->bytes->end + n;
return;
}
// Switch chunk.
n -= (p->bytes->end - p->cur);
if ( adj_ref ) {
p->bytes = p->bytes->next;
}
else
p->bytes = p->bytes->next;
p->cur = p->bytes->start;
}
// Cannot reach.
assert(false);
}
int8_t __hlt_iterator_bytes_deref(hlt_iterator_bytes p, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( __is_end(p) ) {
// Position is out range.
hlt_set_exception(excpt, &hlt_exception_value_error, 0, ctx);
return 0;
}
return *p.cur;
}
int8_t __hlt_iterator_bytes_eq(hlt_iterator_bytes p1, hlt_iterator_bytes p2, hlt_exception** excpt,
hlt_execution_context* ctx)
{
if ( __is_end(p1) && __is_end(p2) )
return 1;
return p1.bytes == p2.bytes && p1.cur == p2.cur;
}
// Returns the number of bytes from p1 to p2 (not counting p2).
hlt_bytes_size __hlt_iterator_bytes_diff(hlt_iterator_bytes p1, hlt_iterator_bytes p2,
hlt_exception** excpt, hlt_execution_context* ctx)
{
if ( __is_end(p1) ) {
if ( ! __is_end(p2) && ! (__at_object(p1) || __at_object(p2)) ) {
// Invalid starting position.
// hlt_set_exception(excpt, &hlt_exception_value_error, 0, ctx); // FIXME
}
return 0;
}
if ( __is_end(p2) )
return hlt_bytes_len(p1.bytes, excpt, ctx) -
(((p1.cur <= p1.bytes->end) ? p1.cur : p1.bytes->end) - p1.bytes->start);
hlt_bytes_size n =
(hlt_iterator_bytes_index(p2, excpt, ctx) - hlt_iterator_bytes_index(p1, excpt, ctx));
return (n > 0 ? n : 0);
}
void __hlt_bytes_find_byte_from(hlt_iterator_bytes* p, hlt_iterator_bytes i, int8_t chr,
hlt_exception** excpt, hlt_execution_context* ctx)
{
// First chunk.
hlt_bytes* b = i.bytes;
int8_t* c = memchr(i.cur, chr, b->end - i.cur);