forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
1374 lines (1286 loc) · 45.4 KB
/
array.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
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
array constructors and primitives
*/
#include <stdlib.h>
#include <string.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#include "julia_assert.h"
#ifdef __cplusplus
extern "C" {
#endif
#define JL_ARRAY_IMPL_NUL 1
#define JL_ARRAY_ALIGN(jl_value, nbytes) LLT_ALIGN(jl_value, nbytes)
static inline void arrayassign_safe(int hasptr, jl_value_t *parent, char *dst, const jl_value_t *src, size_t nb) JL_NOTSAFEPOINT
{
// array can assume more alignment than a field would normally have
assert(nb >= jl_datatype_size(jl_typeof(src))); // nb might move some undefined bits, but we should be okay with that
if (hasptr) {
size_t nptr = nb / sizeof(void*);
memmove_refs((void**)dst, (void* const*)src, nptr);
jl_gc_multi_wb(parent, src);
}
else {
switch (nb) {
case 0: break;
case 1: *(uint8_t*)dst = *(uint8_t*)src; break;
case 2: *(uint16_t*)dst = *(uint16_t*)src; break;
case 4: *(uint32_t*)dst = *(uint32_t*)src; break;
case 8: *(uint64_t*)dst = *(uint64_t*)src; break;
case 16:
memcpy(jl_assume_aligned(dst, 16), jl_assume_aligned(src, 16), 16);
break;
default: memcpy(dst, src, nb);
}
}
}
static inline void memmove_safe(int hasptr, char *dst, const char *src, size_t nb) JL_NOTSAFEPOINT
{
if (hasptr)
memmove_refs((void**)dst, (void**)src, nb / sizeof(void*));
else
memmove(dst, src, nb);
}
// array constructors ---------------------------------------------------------
JL_DLLEXPORT char *jl_array_typetagdata(jl_array_t *a) JL_NOTSAFEPOINT
{
assert(jl_array_isbitsunion(a));
return ((char*)jl_array_data(a)) + ((jl_array_ndims(a) == 1 ? (a->maxsize - a->offset) : jl_array_len(a)) * a->elsize) + a->offset;
}
STATIC_INLINE jl_value_t *jl_array_owner(jl_array_t *a JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT
{
if (a->flags.how == 3) {
a = (jl_array_t*)jl_array_data_owner(a);
assert(jl_is_string(a) || a->flags.how != 3);
}
return (jl_value_t*)a;
}
#if defined(_P64) && defined(UINT128MAX)
typedef __uint128_t wideint_t;
#else
typedef uint64_t wideint_t;
#endif
#define MAXINTVAL (((size_t)-1)>>1)
static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
int8_t isunboxed, int8_t hasptr, int8_t isunion, int8_t zeroinit, int elsz)
{
jl_task_t *ct = jl_current_task;
size_t i, tot, nel=1;
void *data;
jl_array_t *a;
for(i=0; i < ndims; i++) {
size_t di = dims[i];
wideint_t prod = (wideint_t)nel * (wideint_t)di;
if (prod > (wideint_t) MAXINTVAL || di > MAXINTVAL)
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
nel = prod;
}
assert(atype == NULL || isunion == jl_is_uniontype(jl_tparam0(atype)));
if (isunboxed) {
wideint_t prod = (wideint_t)elsz * (wideint_t)nel;
if (prod > (wideint_t) MAXINTVAL)
jl_error("invalid Array size");
tot = prod;
if (elsz == 1 && !isunion) {
// extra byte for all julia allocated byte arrays
tot++;
}
if (isunion) {
// an extra byte for each isbits union array element, stored after a->maxsize
tot += nel;
}
}
else {
wideint_t prod = (wideint_t)sizeof(void*) * (wideint_t)nel;
if (prod > (wideint_t) MAXINTVAL)
jl_error("invalid Array size");
tot = prod;
}
int ndimwords = jl_array_ndimwords(ndims);
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t);
if (tot <= ARRAY_INLINE_NBYTES) {
// align data area
if (tot >= ARRAY_CACHE_ALIGN_THRESHOLD)
tsz = JL_ARRAY_ALIGN(tsz, JL_CACHE_BYTE_ALIGNMENT);
else if (isunboxed && elsz >= 4)
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT);
size_t doffs = tsz;
tsz += tot;
// jl_array_t is large enough that objects will always be aligned 16
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
assert(((size_t)a & 15) == 0);
// No allocation or safepoint allowed after this
a->flags.how = 0;
data = (char*)a + doffs;
}
else {
data = jl_gc_managed_malloc(tot);
// Allocate the Array **after** allocating the data
// to make sure the array is still young
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
// No allocation or safepoint allowed after this
a->flags.how = 2;
jl_gc_track_malloced_array(ct->ptls, a);
}
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
if (zeroinit)
memset(data, 0, tot);
a->data = data;
if (JL_ARRAY_IMPL_NUL && elsz == 1)
((char*)data)[tot - 1] = '\0';
#ifdef STORE_ARRAY_LEN
a->length = nel;
#endif
a->flags.ndims = ndims;
a->flags.ptrarray = !isunboxed;
a->flags.hasptr = hasptr;
a->elsize = elsz;
a->flags.isshared = 0;
a->flags.isaligned = 1;
a->offset = 0;
if (ndims == 1) {
a->nrows = nel;
a->maxsize = nel;
}
else if (a->flags.ndims != ndims) {
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
}
else {
size_t *adims = &a->nrows;
for (i = 0; i < ndims; i++)
adims[i] = dims[i];
}
return a;
}
static inline jl_array_t *_new_array(jl_value_t *atype, uint32_t ndims, size_t *dims)
{
jl_value_t *eltype = jl_tparam0(atype);
size_t elsz = 0, al = 0;
if (!jl_is_kind(jl_typeof(eltype)))
jl_type_error_rt("Array", "element type", (jl_value_t*)jl_type_type, eltype);
int isunboxed = jl_islayout_inline(eltype, &elsz, &al);
int isunion = jl_is_uniontype(eltype);
int hasptr = isunboxed && (jl_is_datatype(eltype) && ((jl_datatype_t*)eltype)->layout->npointers > 0);
if (!isunboxed) {
elsz = sizeof(void*);
al = elsz;
}
else {
elsz = LLT_ALIGN(elsz, al);
}
int zi = !isunboxed || hasptr || isunion || (jl_is_datatype(eltype) && ((jl_datatype_t*)eltype)->zeroinit);
return _new_array_(atype, ndims, dims, isunboxed, hasptr, isunion, zi, elsz);
}
jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims, size_t *dims,
int isunboxed, int hasptr, int isunion, int elsz)
{
return _new_array_(atype, ndims, dims, isunboxed, hasptr, isunion, 0, elsz);
}
#ifndef JL_NDEBUG
static inline int is_ntuple_long(jl_value_t *v)
{
if (!jl_is_tuple(v))
return 0;
jl_value_t *tt = jl_typeof(v);
size_t i, nfields = jl_nparams(tt);
for (i = 0; i < nfields; i++) {
if (jl_tparam(tt, i) != (jl_value_t*)jl_long_type) {
return 0;
}
}
return 1;
}
#endif
JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
jl_value_t *_dims)
{
jl_task_t *ct = jl_current_task;
jl_array_t *a;
size_t ndims = jl_nfields(_dims);
assert(is_ntuple_long(_dims));
size_t *dims = (size_t*)_dims;
assert(jl_types_equal(jl_tparam0(jl_typeof(data)), jl_tparam0(atype)));
int ndimwords = jl_array_ndimwords(ndims);
int tsz = sizeof(jl_array_t) + ndimwords * sizeof(size_t) + sizeof(void*);
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
// No allocation or safepoint allowed after this
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
a->flags.ndims = ndims;
a->offset = 0;
a->data = NULL;
a->flags.isaligned = data->flags.isaligned;
jl_array_t *owner = (jl_array_t*)jl_array_owner(data);
jl_value_t *eltype = jl_tparam0(atype);
size_t elsz = 0, align = 0;
int isboxed = !jl_islayout_inline(eltype, &elsz, &align);
assert(isboxed == data->flags.ptrarray);
if (!isboxed) {
a->elsize = LLT_ALIGN(elsz, align);
jl_value_t *ownerty = jl_typeof(owner);
size_t oldelsz = 0, oldalign = 0;
if (ownerty == (jl_value_t*)jl_string_type) {
oldalign = 1;
}
else {
jl_islayout_inline(jl_tparam0(ownerty), &oldelsz, &oldalign);
}
if (oldalign < align)
jl_exceptionf(jl_argumenterror_type,
"reinterpret from alignment %d bytes to alignment %d bytes not allowed",
(int) oldalign, (int) align);
a->flags.ptrarray = 0;
a->flags.hasptr = data->flags.hasptr;
}
else {
a->elsize = sizeof(void*);
a->flags.ptrarray = 1;
a->flags.hasptr = 0;
}
// if data is itself a shared wrapper,
// owner should point back to the original array
jl_array_data_owner(a) = (jl_value_t*)owner;
a->flags.how = 3;
a->data = data->data;
a->flags.isshared = 1;
data->flags.isshared = 1;
if (ndims == 1) {
size_t l = dims[0];
#ifdef STORE_ARRAY_LEN
a->length = l;
#endif
a->nrows = l;
a->maxsize = l;
}
else if (a->flags.ndims != ndims) {
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
}
else {
size_t *adims = &a->nrows;
size_t l = 1;
wideint_t prod;
for (size_t i = 0; i < ndims; i++) {
adims[i] = dims[i];
prod = (wideint_t)l * (wideint_t)adims[i];
if (prod > (wideint_t) MAXINTVAL)
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
l = prod;
}
#ifdef STORE_ARRAY_LEN
a->length = l;
#endif
}
return a;
}
JL_DLLEXPORT jl_array_t *jl_string_to_array(jl_value_t *str)
{
jl_task_t *ct = jl_current_task;
jl_array_t *a;
int ndimwords = jl_array_ndimwords(1);
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t) + sizeof(void*);
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, jl_array_uint8_type);
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
a->flags.ndims = 1;
a->offset = 0;
a->data = jl_string_data(str);
a->flags.isaligned = 0;
a->elsize = 1;
a->flags.ptrarray = 0;
a->flags.hasptr = 0;
jl_array_data_owner(a) = str;
a->flags.how = 3;
a->flags.isshared = 1;
size_t l = jl_string_len(str);
#ifdef STORE_ARRAY_LEN
a->length = l;
#endif
a->nrows = a->maxsize = l;
return a;
}
// own_buffer != 0 iff GC should call free() on this pointer eventually
JL_DLLEXPORT jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data,
size_t nel, int own_buffer)
{
jl_task_t *ct = jl_current_task;
jl_array_t *a;
jl_value_t *eltype = jl_tparam0(atype);
int isunboxed = jl_stored_inline(eltype);
if (isunboxed && jl_is_uniontype(eltype))
jl_exceptionf(jl_argumenterror_type,
"unsafe_wrap: unspecified layout for union element type");
size_t elsz;
unsigned align;
if (isunboxed) {
elsz = jl_datatype_size(eltype);
align = jl_datatype_align(eltype);
}
else {
align = elsz = sizeof(void*);
}
if (((uintptr_t)data) & ((align > JL_HEAP_ALIGNMENT ? JL_HEAP_ALIGNMENT : align) - 1))
jl_exceptionf(jl_argumenterror_type,
"unsafe_wrap: pointer %p is not properly aligned to %u bytes", data, align);
int ndimwords = jl_array_ndimwords(1);
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t);
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
// No allocation or safepoint allowed after this
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
a->data = data;
#ifdef STORE_ARRAY_LEN
a->length = nel;
#endif
a->elsize = LLT_ALIGN(elsz, align);
a->flags.ptrarray = !isunboxed;
a->flags.hasptr = isunboxed && (jl_is_datatype(eltype) && ((jl_datatype_t*)eltype)->layout->npointers > 0);
a->flags.ndims = 1;
a->flags.isshared = 1;
a->flags.isaligned = 0; // TODO: allow passing memalign'd buffers
if (own_buffer) {
a->flags.how = 2;
jl_gc_track_malloced_array(ct->ptls, a);
jl_gc_count_allocd(nel*elsz + (elsz == 1 ? 1 : 0));
}
else {
a->flags.how = 0;
}
a->nrows = nel;
a->maxsize = nel;
a->offset = 0;
return a;
}
JL_DLLEXPORT jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data,
jl_value_t *_dims, int own_buffer)
{
jl_task_t *ct = jl_current_task;
size_t nel = 1;
jl_array_t *a;
size_t ndims = jl_nfields(_dims);
wideint_t prod;
assert(is_ntuple_long(_dims));
size_t *dims = (size_t*)_dims;
for (size_t i = 0; i < ndims; i++) {
prod = (wideint_t)nel * (wideint_t)dims[i];
if (prod > (wideint_t) MAXINTVAL)
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
nel = prod;
}
if (__unlikely(ndims == 1))
return jl_ptr_to_array_1d(atype, data, nel, own_buffer);
jl_value_t *eltype = jl_tparam0(atype);
int isunboxed = jl_stored_inline(eltype);
if (isunboxed && jl_is_uniontype(eltype))
jl_exceptionf(jl_argumenterror_type,
"unsafe_wrap: unspecified layout for union element type");
size_t elsz;
unsigned align;
if (isunboxed) {
elsz = jl_datatype_size(eltype);
align = jl_datatype_align(eltype);
}
else {
align = elsz = sizeof(void*);
}
if (((uintptr_t)data) & ((align > JL_HEAP_ALIGNMENT ? JL_HEAP_ALIGNMENT : align) - 1))
jl_exceptionf(jl_argumenterror_type,
"unsafe_wrap: pointer %p is not properly aligned to %u bytes", data, align);
int ndimwords = jl_array_ndimwords(ndims);
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t);
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
// No allocation or safepoint allowed after this
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
a->data = data;
#ifdef STORE_ARRAY_LEN
a->length = nel;
#endif
a->elsize = LLT_ALIGN(elsz, align);
a->flags.ptrarray = !isunboxed;
a->flags.hasptr = isunboxed && (jl_is_datatype(eltype) && ((jl_datatype_t*)eltype)->layout->npointers > 0);
a->flags.ndims = ndims;
a->offset = 0;
a->flags.isshared = 1;
a->flags.isaligned = 0;
if (own_buffer) {
a->flags.how = 2;
jl_gc_track_malloced_array(ct->ptls, a);
jl_gc_count_allocd(nel*elsz + (elsz == 1 ? 1 : 0));
}
else {
a->flags.how = 0;
}
assert(ndims != 1); // handled above
if (a->flags.ndims != ndims)
jl_exceptionf(jl_argumenterror_type, "invalid Array dimensions");
memcpy(&a->nrows, dims, ndims * sizeof(size_t));
return a;
}
JL_DLLEXPORT jl_array_t *jl_new_array(jl_value_t *atype, jl_value_t *_dims)
{
size_t ndims = jl_nfields(_dims);
assert(is_ntuple_long(_dims));
return _new_array(atype, ndims, (size_t*)_dims);
}
JL_DLLEXPORT jl_array_t *jl_alloc_array_1d(jl_value_t *atype, size_t nr)
{
return _new_array(atype, 1, &nr);
}
JL_DLLEXPORT jl_array_t *jl_alloc_array_2d(jl_value_t *atype, size_t nr,
size_t nc)
{
size_t d[2] = {nr, nc};
return _new_array(atype, 2, &d[0]);
}
JL_DLLEXPORT jl_array_t *jl_alloc_array_3d(jl_value_t *atype, size_t nr,
size_t nc, size_t z)
{
size_t d[3] = {nr, nc, z};
return _new_array(atype, 3, &d[0]);
}
JL_DLLEXPORT jl_array_t *jl_pchar_to_array(const char *str, size_t len)
{
jl_array_t *a = jl_alloc_array_1d(jl_array_uint8_type, len);
memcpy(a->data, str, len);
return a;
}
JL_DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a)
{
size_t len = jl_array_len(a);
if (len == 0) {
// this may seem like purely an optimization (which it also is), but it
// also ensures that calling `String(a)` doesn't corrupt a previous
// string also created the same way, where `a = StringVector(_)`.
return jl_an_empty_string;
}
if (a->flags.how == 3 && a->offset == 0 && a->elsize == 1 &&
(jl_array_ndims(a) != 1 ||
((a->maxsize + sizeof(void*) + 1 <= GC_MAX_SZCLASS) == (len + sizeof(void*) + 1 <= GC_MAX_SZCLASS)))) {
jl_value_t *o = jl_array_data_owner(a);
if (jl_is_string(o)) {
a->flags.isshared = 1;
*(size_t*)o = len;
a->nrows = 0;
#ifdef STORE_ARRAY_LEN
a->length = 0;
#endif
a->maxsize = 0;
return o;
}
}
a->nrows = 0;
#ifdef STORE_ARRAY_LEN
a->length = 0;
#endif
a->maxsize = 0;
return jl_pchar_to_string((const char*)jl_array_data(a), len);
}
JL_DLLEXPORT jl_value_t *jl_alloc_string(size_t len)
{
if (len == 0)
return jl_an_empty_string;
size_t sz = sizeof(size_t) + len + 1; // add space for trailing \nul protector and size
if (sz < len) // overflow
jl_throw(jl_memory_exception);
jl_task_t *ct = jl_current_task;
jl_value_t *s;
jl_ptls_t ptls = ct->ptls;
const size_t allocsz = sz + sizeof(jl_taggedvalue_t);
if (sz <= GC_MAX_SZCLASS) {
int pool_id = jl_gc_szclass_align8(allocsz);
jl_gc_pool_t *p = &ptls->heap.norm_pools[pool_id];
int osize = jl_gc_sizeclasses[pool_id];
s = jl_gc_pool_alloc(ptls, (char*)p - (char*)ptls, osize);
}
else {
if (allocsz < sz) // overflow in adding offs, size was "negative"
jl_throw(jl_memory_exception);
s = jl_gc_big_alloc(ptls, allocsz);
}
jl_set_typeof(s, jl_string_type);
*(size_t*)s = len;
jl_string_data(s)[len] = 0;
return s;
}
JL_DLLEXPORT jl_value_t *jl_pchar_to_string(const char *str, size_t len)
{
jl_value_t *s = jl_alloc_string(len);
if (len > 0)
memcpy(jl_string_data(s), str, len);
return s;
}
JL_DLLEXPORT jl_value_t *jl_cstr_to_string(const char *str)
{
return jl_pchar_to_string(str, strlen(str));
}
JL_DLLEXPORT jl_array_t *jl_alloc_vec_any(size_t n)
{
return jl_alloc_array_1d(jl_array_any_type, n);
}
JL_DLLEXPORT jl_value_t *jl_apply_array_type(jl_value_t *type, size_t dim)
{
jl_value_t *boxed_dim = jl_box_long(dim);
JL_GC_PUSH1(&boxed_dim);
jl_value_t *ret = jl_apply_type2((jl_value_t*)jl_array_type, type, boxed_dim);
JL_GC_POP();
return ret;
}
// array primitives -----------------------------------------------------------
#ifndef STORE_ARRAY_LEN
JL_DLLEXPORT size_t jl_array_len_(jl_array_t *a)
{
size_t l = 1;
for(size_t i=0; i < jl_array_ndims(a); i++)
l *= jl_array_dim(a, i);
return l;
}
#endif
JL_DLLEXPORT jl_value_t *jl_ptrarrayref(jl_array_t *a JL_PROPAGATES_ROOT, size_t i) JL_NOTSAFEPOINT
{
assert(i < jl_array_len(a));
assert(a->flags.ptrarray);
jl_value_t *elt = jl_atomic_load_relaxed(((_Atomic(jl_value_t*)*)a->data) + i);
if (elt == NULL)
jl_throw(jl_undefref_exception);
return elt;
}
JL_DLLEXPORT jl_value_t *jl_arrayref(jl_array_t *a, size_t i)
{
if (a->flags.ptrarray)
return jl_ptrarrayref(a, i);
assert(i < jl_array_len(a));
jl_value_t *eltype = (jl_value_t*)jl_tparam0(jl_typeof(a));
if (jl_is_uniontype(eltype)) {
// isbits union selector bytes are always stored directly after the last array element
uint8_t sel = jl_array_typetagdata(a)[i];
eltype = jl_nth_union_component(eltype, sel);
if (jl_is_datatype_singleton((jl_datatype_t*)eltype))
return ((jl_datatype_t*)eltype)->instance;
}
jl_value_t *r = undefref_check((jl_datatype_t*)eltype, jl_new_bits(eltype, &((char*)a->data)[i * a->elsize]));
if (__unlikely(r == NULL))
jl_throw(jl_undefref_exception);
return r;
}
JL_DLLEXPORT int jl_array_isassigned(jl_array_t *a, size_t i)
{
if (a->flags.ptrarray) {
return jl_atomic_load_relaxed(((_Atomic(jl_value_t*)*)jl_array_data(a)) + i) != NULL;
}
else if (a->flags.hasptr) {
jl_datatype_t *eltype = (jl_datatype_t*)jl_tparam0(jl_typeof(a));
assert(eltype->layout->first_ptr >= 0);
jl_value_t **elem = (jl_value_t**)((char*)a->data + i * a->elsize);
return elem[eltype->layout->first_ptr] != NULL;
}
return 1;
}
JL_DLLEXPORT void jl_arrayset(jl_array_t *a JL_ROOTING_ARGUMENT, jl_value_t *rhs JL_ROOTED_ARGUMENT JL_MAYBE_UNROOTED, size_t i)
{
assert(i < jl_array_len(a));
jl_value_t *eltype = jl_tparam0(jl_typeof(a));
if (eltype != (jl_value_t*)jl_any_type) {
JL_GC_PUSH1(&rhs);
if (!jl_isa(rhs, eltype))
jl_type_error("arrayset", eltype, rhs);
JL_GC_POP();
}
if (!a->flags.ptrarray) {
int hasptr;
if (jl_is_uniontype(eltype)) {
uint8_t *psel = &((uint8_t*)jl_array_typetagdata(a))[i];
unsigned nth = 0;
if (!jl_find_union_component(eltype, jl_typeof(rhs), &nth))
assert(0 && "invalid arrayset to isbits union");
*psel = nth;
if (jl_is_datatype_singleton((jl_datatype_t*)jl_typeof(rhs)))
return;
hasptr = 0;
}
else {
hasptr = a->flags.hasptr;
}
arrayassign_safe(hasptr, jl_array_owner(a), &((char*)a->data)[i * a->elsize], rhs, a->elsize);
}
else {
jl_atomic_store_relaxed(((_Atomic(jl_value_t*)*)a->data) + i, rhs);
jl_gc_wb(jl_array_owner(a), rhs);
}
}
JL_DLLEXPORT void jl_arrayunset(jl_array_t *a, size_t i)
{
if (i >= jl_array_len(a))
jl_bounds_error_int((jl_value_t*)a, i + 1);
if (a->flags.ptrarray)
jl_atomic_store_relaxed(((_Atomic(jl_value_t*)*)a->data) + i, NULL);
else if (a->flags.hasptr) {
size_t elsize = a->elsize;
jl_assume(elsize >= sizeof(void*) && elsize % sizeof(void*) == 0);
memset((char*)a->data + elsize * i, 0, elsize);
}
}
// at this size and bigger, allocate resized array data with malloc directly
// instead of managing them separately as gc objects
#define MALLOC_THRESH 1048576
// Resize the buffer to a max size of `newlen`
// The buffer can either be newly allocated or realloc'd, the return
// value is 1 if a new buffer is allocated and 0 if it is realloc'd.
// the caller needs to take care of moving the data from the old buffer
// to the new one if necessary.
// When this function returns, the `->data` pointer always points to
// the **beginning** of the new buffer.
static int NOINLINE array_resize_buffer(jl_array_t *a, size_t newlen)
{
jl_task_t *ct = jl_current_task;
assert(!a->flags.isshared || a->flags.how == 3);
size_t elsz = a->elsize;
size_t nbytes = newlen * elsz;
size_t oldnbytes = a->maxsize * elsz;
size_t oldoffsnb = a->offset * elsz;
size_t oldlen = a->nrows;
int isbitsunion = jl_array_isbitsunion(a);
assert(nbytes >= oldnbytes);
if (elsz == 1 && !isbitsunion) {
nbytes++;
oldnbytes++;
}
if (isbitsunion) {
nbytes += newlen;
oldnbytes += a->maxsize;
}
int newbuf = 0;
if (a->flags.how == 2) {
// already malloc'd - use realloc
char *olddata = (char*)a->data - oldoffsnb;
a->data = jl_gc_managed_realloc(olddata, nbytes, oldnbytes,
a->flags.isaligned, (jl_value_t*)a);
}
else if (a->flags.how == 3 && jl_is_string(jl_array_data_owner(a)) && !isbitsunion) {
// if data is in a String, keep it that way
jl_value_t *s;
if (a->flags.isshared) {
s = jl_alloc_string(nbytes - (elsz == 1));
newbuf = 1;
}
else {
s = jl_gc_realloc_string(jl_array_data_owner(a), nbytes - (elsz == 1));
}
jl_array_data_owner(a) = s;
jl_gc_wb(a, s);
a->data = jl_string_data(s);
}
else {
newbuf = 1;
if (nbytes >= MALLOC_THRESH) {
a->data = jl_gc_managed_malloc(nbytes);
jl_gc_track_malloced_array(ct->ptls, a);
a->flags.how = 2;
a->flags.isaligned = 1;
}
else {
a->data = jl_gc_alloc_buf(ct->ptls, nbytes);
a->flags.how = 1;
jl_gc_wb_buf(a, a->data, nbytes);
}
}
if (JL_ARRAY_IMPL_NUL && elsz == 1 && !isbitsunion)
memset((char*)a->data + oldnbytes - 1, 0, nbytes - oldnbytes + 1);
(void)oldlen;
assert(oldlen == a->nrows &&
"Race condition detected: recursive resizing on the same array.");
a->flags.isshared = 0;
a->maxsize = newlen;
return newbuf;
}
static void NOINLINE array_try_unshare(jl_array_t *a)
{
if (a->flags.isshared) {
if (a->flags.how != 3)
jl_error("cannot resize array with shared data");
// allow resizing when data is shared with a String
if (jl_is_string(jl_array_data_owner(a)))
return;
assert(a->offset == 0);
size_t len = a->maxsize;
size_t nbytes = len * a->elsize;
if (jl_array_isbitsunion(a)) {
nbytes += len;
}
char *olddata = (char*)a->data;
int newbuf = array_resize_buffer(a, len);
assert(newbuf);
(void)newbuf;
memcpy(a->data, olddata, nbytes);
}
}
size_t overallocation(size_t maxsize)
{
if (maxsize < 8)
return 8;
// compute maxsize = maxsize + 4*maxsize^(7/8) + maxsize/8
// for small n, we grow faster than O(n)
// for large n, we grow at O(n/8)
// and as we reach O(memory) for memory>>1MB,
// this means we end by adding about 10% of memory each time
int exp2 = sizeof(maxsize) * 8 -
#ifdef _P64
__builtin_clzll(maxsize);
#else
__builtin_clz(maxsize);
#endif
maxsize += ((size_t)1 << (exp2 * 7 / 8)) * 4 + maxsize / 8;
return maxsize;
}
STATIC_INLINE void jl_array_grow_at_beg(jl_array_t *a, size_t idx, size_t inc,
size_t n)
{
// designed to handle the case of growing and shrinking at both ends
if (__unlikely(a->flags.isshared)) {
if (a->flags.how != 3)
jl_error("cannot resize array with shared data");
if (inc == 0) {
// If inc > 0, it will always trigger the slow path and unshare the
// buffer
array_try_unshare(a);
return;
}
}
size_t newnrows = n + inc;
size_t elsz = a->elsize;
size_t nbinc = inc * elsz;
char *data = (char*)a->data;
char *newdata;
char *typetagdata;
char *newtypetagdata = NULL;
int isbitsunion = jl_array_isbitsunion(a);
if (isbitsunion) typetagdata = jl_array_typetagdata(a);
if (a->offset >= inc) {
// already have enough space in a->offset
newdata = data - nbinc;
a->offset -= inc;
if (isbitsunion) newtypetagdata = typetagdata - inc;
if (idx > 0) {
// inserting new elements after 1st element
memmove_safe(a->flags.hasptr, newdata, data, idx * elsz);
if (isbitsunion) {
memmove(newtypetagdata, typetagdata, idx);
memset(newtypetagdata + idx, 0, inc);
}
}
}
else {
// not enough room for requested growth from existing a->offset
size_t oldoffset = a->offset;
size_t oldoffsnb = oldoffset * elsz;
size_t oldmaxsize = a->maxsize;
size_t nb1 = idx * elsz;
if (inc > (a->maxsize - n) / 2 - (a->maxsize - n) / 20) {
// not enough room for requested growth from end of array
size_t newlen = inc * 2;
while (n + 2 * inc > newlen - a->offset)
newlen *= 2;
size_t newmaxsize = overallocation(a->maxsize);
if (newlen < newmaxsize)
newlen = newmaxsize;
size_t newoffset = (newlen - newnrows) / 2;
if (!array_resize_buffer(a, newlen)) {
data = (char*)a->data + oldoffsnb;
}
newdata = (char*)a->data + newoffset * elsz;
if (isbitsunion) {
typetagdata = data + (oldmaxsize - oldoffset) * elsz + oldoffset;
newtypetagdata = newdata + (a->maxsize - newoffset) * elsz + newoffset;
memmove(newtypetagdata, typetagdata, idx);
memset(newtypetagdata + idx, 0, inc);
memmove(newtypetagdata + idx + inc, typetagdata + idx, n - idx);
}
// We could use memcpy if resizing allocates a new buffer,
// hopefully it's not a particularly important optimization.
if (idx > 0 && newdata < data) {
memmove_safe(a->flags.hasptr, newdata, data, nb1);
}
memmove_safe(a->flags.hasptr, newdata + nbinc + nb1, data + nb1, n * elsz - nb1);
if (idx > 0 && newdata > data) {
memmove_safe(a->flags.hasptr, newdata, data, nb1);
}
a->offset = newoffset;
}
else {
// use extra space between a->nrows & a->maxsize
a->offset = (a->maxsize - newnrows) / 2;
newdata = data - oldoffsnb + a->offset * elsz;
if (isbitsunion) newtypetagdata = newdata + (a->maxsize - a->offset) * elsz + a->offset;
if (idx > 0 && newdata < data) {
memmove_safe(a->flags.hasptr, newdata, data, nb1);
if (isbitsunion) {
memmove(newtypetagdata, typetagdata, idx);
memset(newtypetagdata + idx, 0, inc);
}
}
memmove_safe(a->flags.hasptr, newdata + nbinc + nb1, data + nb1, n * elsz - nb1);
if (isbitsunion) memmove(newtypetagdata + idx + inc, typetagdata + idx, n - idx);
if (idx > 0 && newdata > data) {
memmove_safe(a->flags.hasptr, newdata, data, nb1);
if (isbitsunion) {
memmove(newtypetagdata, typetagdata, idx);
memset(newtypetagdata + idx, 0, inc);
}
}
}
}
#ifdef STORE_ARRAY_LEN
a->length = newnrows;
#endif
a->nrows = newnrows;
a->data = newdata;
if (jl_is_array_zeroinit(a)) {
memset(newdata + idx * elsz, 0, nbinc);
}
if (newtypetagdata) {
memset(newtypetagdata + idx, 0, inc);
}
}
STATIC_INLINE void jl_array_grow_at_end(jl_array_t *a, size_t idx,
size_t inc, size_t n)
{
// optimized for the case of only growing and shrinking at the end
if (__unlikely(a->flags.isshared)) {
if (a->flags.how != 3)
jl_error("cannot resize array with shared data");
if (inc == 0) {
// If inc > 0, it will always trigger the slow path and unshare the
// buffer
array_try_unshare(a);
return;
}
}
size_t elsz = a->elsize;
char *data = (char*)a->data;
char *typetagdata;
char *newtypetagdata;
int isbitsunion = jl_array_isbitsunion(a);
if (isbitsunion) typetagdata = jl_array_typetagdata(a);
int has_gap = n > idx;
size_t reqmaxsize = a->offset + n + inc;
if (__unlikely(reqmaxsize > a->maxsize)) {
size_t nb1 = idx * elsz;
size_t nbinc = inc * elsz;
// grow either by our computed overallocation factor or exactly the requested size,
// whichever is larger
size_t newmaxsize = overallocation(a->maxsize);
if (newmaxsize < reqmaxsize)
newmaxsize = reqmaxsize;
size_t oldmaxsize = a->maxsize;
int newbuf = array_resize_buffer(a, newmaxsize);
char *newdata = (char*)a->data + a->offset * elsz;
if (isbitsunion) newtypetagdata = newdata + (a->maxsize - a->offset) * elsz + a->offset;
if (newbuf) {
memcpy(newdata, data, nb1);
if (isbitsunion) {
memcpy(newtypetagdata, typetagdata, idx);
if (has_gap) memcpy(newtypetagdata + idx + inc, typetagdata + idx, n - idx);
memset(newtypetagdata + idx, 0, inc);
}
if (has_gap) memcpy(newdata + nb1 + nbinc, data + nb1, n * elsz - nb1);
}
else {
if (isbitsunion) {
typetagdata = newdata + (oldmaxsize - a->offset) * elsz + a->offset;
if (has_gap) memmove(newtypetagdata + idx + inc, typetagdata + idx, n - idx);
memmove(newtypetagdata, typetagdata, idx);
memset(newtypetagdata + idx, 0, inc);
}
if (has_gap) memmove_safe(a->flags.hasptr, newdata + nb1 + nbinc, newdata + nb1, n * elsz - nb1);
}
a->data = data = newdata;
}
else if (has_gap) {
if (isbitsunion) {
memmove(typetagdata + idx + inc, typetagdata + idx, n - idx);
memset(typetagdata + idx, 0, inc);
}
size_t nb1 = idx * elsz;
memmove_safe(a->flags.hasptr, data + nb1 + inc * elsz, data + nb1, n * elsz - nb1);
}
else {
// there was enough room for requested growth already in a->maxsize
if (isbitsunion)
memset(typetagdata + idx, 0, inc);
}
size_t newnrows = n + inc;
#ifdef STORE_ARRAY_LEN
a->length = newnrows;
#endif
a->nrows = newnrows;
if (jl_is_array_zeroinit(a)) {
memset(data + idx * elsz, 0, inc * elsz);
}
}
JL_DLLEXPORT void jl_array_grow_at(jl_array_t *a, ssize_t idx, size_t inc)
{
// No need to explicitly unshare.
// Shared arrays are guaranteed to trigger the slow path for growing.
size_t n = jl_array_nrows(a);
if (idx < 0 || idx > n)
jl_bounds_error_int((jl_value_t*)a, idx + 1);
if (idx + 1 < n / 2) {
jl_array_grow_at_beg(a, idx, inc, n);
}
else {
jl_array_grow_at_end(a, idx, inc, n);
}
}
JL_DLLEXPORT void jl_array_grow_end(jl_array_t *a, size_t inc)
{
size_t n = jl_array_nrows(a);
jl_array_grow_at_end(a, n, inc, n);
}