-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathArrayletObjectModel.hpp
1322 lines (1219 loc) · 45.3 KB
/
ArrayletObjectModel.hpp
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
/*******************************************************************************
* Copyright IBM Corp. and others 1991
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
/**
* @file
* @ingroup GC_Base
*/
#if !defined(ARRAYLETOBJECTMODEL_)
#define ARRAYLETOBJECTMODEL_
#include "j9.h"
#include "j9cfg.h"
#include "j9consts.h"
#include "modron.h"
#include "modronopt.h"
#include "ArrayletObjectModelBase.hpp"
#include "Bits.hpp"
#include "ForwardedHeader.hpp"
#include "Math.hpp"
#include "SlotObject.hpp"
class GC_ArrayletObjectModel : public GC_ArrayletObjectModelBase
{
/*
* Data members
*/
private:
protected:
public:
/*
* Function members
*/
private:
void AssertBadElementSize();
protected:
/* forward declare methods from parent class to avoid namespace issues */
MMINLINE uintptr_t
getSpineSizeWithoutHeader(ArrayLayout layout, uintptr_t numberArraylets, uintptr_t dataSize, bool alignData)
{
return GC_ArrayletObjectModelBase::getSpineSizeWithoutHeader(layout, numberArraylets, dataSize, alignData);
}
public:
/* forward declare methods from parent class to avoid namespace issues */
MMINLINE uintptr_t
arrayletSize(J9IndexableObject *objPtr, uintptr_t index, uintptr_t dataSizeInBytes, uintptr_t numberOfArraylets)
{
return GC_ArrayletObjectModelBase::arrayletSize(objPtr, index, dataSizeInBytes, numberOfArraylets);
}
MMINLINE uintptr_t
numArraylets(uintptr_t unadjustedDataSizeInBytes)
{
return GC_ArrayletObjectModelBase::numArraylets(unadjustedDataSizeInBytes);
}
/**
* Get the header size for contiguous arrays
* @return header size
*/
MMINLINE uintptr_t
contiguousIndexableHeaderSize()
{
return _contiguousIndexableHeaderSize;
}
/**
* Get the header size for discontiguous arrays
* @return header size
*/
MMINLINE uintptr_t
discontiguousIndexableHeaderSize()
{
return _discontiguousIndexableHeaderSize;
}
/**
* Get the spine size for the given indexable object
* @param objPtr Pointer to an array object
* @return The total size in bytes of objPtr's array spine;
* includes header, arraylet ptrs, and (if present) padding & inline data
*/
MMINLINE uintptr_t
getSpineSize(J9IndexableObject* objPtr)
{
ArrayLayout layout = getArrayLayout(objPtr);
return getSpineSize(objPtr, layout);
}
/**
* Get the spine size for an arraylet with these properties
* @param J9Class The class of the array.
* @param layout The layout of the indexable object
* @param numberArraylets Number of arraylets for this indexable object
* @param dataSize How many elements are in the indexable object
* @param alignData Should the data section be aligned
* @return spineSize The actual size in byte of the spine
*/
MMINLINE uintptr_t
getSpineSize(J9Class *clazzPtr, ArrayLayout layout, uintptr_t numberArraylets, uintptr_t dataSize, bool alignData)
{
uintptr_t result = getHeaderSize(clazzPtr, layout) + getSpineSizeWithoutHeader(layout, numberArraylets, dataSize, alignData);
return result;
}
/**
* Return the total number of arraylets for the given indexable object
* @param objPtr Pointer to an array object
* @return the number of arraylets used for an array of dataSizeInBytes bytes
*/
MMINLINE uintptr_t
numArraylets(J9IndexableObject *objPtr)
{
return numArraylets(getDataSizeInBytes(objPtr));
}
/**
* Check the given indexable object is inline contiguous
* @param objPtr Pointer to an array object
* @return true of array is inline contiguous
*/
MMINLINE bool
isInlineContiguousArraylet(J9IndexableObject *objPtr)
{
return (getArrayLayout(objPtr) == InlineContiguous);
}
/**
* Get the number of discontiguous arraylets for the given indexable object
* @param objPtr Pointer to an array object
* @return the number of arraylets stored external to the spine.
*/
MMINLINE uintptr_t
numExternalArraylets(J9IndexableObject *objPtr)
{
ArrayLayout layout = getArrayLayout(objPtr);
if (layout == InlineContiguous) {
return 0;
}
uintptr_t numberArraylets = numArraylets(objPtr);
if (layout == Hybrid) {
/* last arrayoid pointer points into spine (remainder data is contiguous with header) */
numberArraylets -= 1;
} else if (layout == Discontiguous) {
/* Data fits exactly within a whole number of arraylets */
AssertArrayletIsDiscontiguous(objPtr);
}
return numberArraylets;
}
/**
* Get the total number of bytes consumed by arraylets external to the
* given indexable object.
* @param objPtr Pointer to an array object
* @return the number of bytes consumed external to the spine
*/
MMINLINE uintptr_t
externalArrayletsSize(J9IndexableObject *objPtr)
{
uintptr_t numberArraylets = numExternalArraylets(objPtr);
return numberArraylets * _omrVM->_arrayletLeafSize;
}
/**
* Determine if the specified array object includes any arraylet leaf pointers.
* @param objPtr[in] the object to test
* @return true if the array has any internal or external leaf pointers, false otherwise
*/
MMINLINE bool
hasArrayletLeafPointers(J9IndexableObject *objPtr)
{
/* Contiguous arraylet has no implicit leaf pointer */
return !isInlineContiguousArraylet(objPtr);
}
/**
* Get the size in bytes for the arraylet at index in indexable object objPtr
* @param objPtr Pointer to an array object
* @param index the index in question. 0<=index<numArraylets(objPtr)
* @return the size of the indexth arraylet
*/
MMINLINE uintptr_t
arrayletSize(J9IndexableObject *objPtr, uintptr_t index)
{
uintptr_t dataSizeInBytes = getDataSizeInBytes(objPtr);
uintptr_t numberOfArraylets = numArraylets(dataSizeInBytes);
return arrayletSize(objPtr, index, dataSizeInBytes, numberOfArraylets);
}
/**
* Get the spine size for the given indexable object
* @param objPtr Pointer to an array object
* @param layout layout for array object
* @return The total size in bytes of objPtr's array spine;
* includes header, arraylet ptrs, and (if present) padding & inline data
*/
MMINLINE uintptr_t
getSpineSize(J9IndexableObject* objPtr, ArrayLayout layout)
{
return getSpineSize(J9GC_J9OBJECT_CLAZZ(objPtr, this), layout, getSizeInElements(objPtr));
}
/**
* Get the spine size for the given indexable object
* @param clazzPtr Pointer to the preserved J9Class, used in scavenger
* @param layout layout for array object
* @param size Size of indexable object
* @return The total size in bytes of objPtr's array spine;
* includes header, arraylet ptrs, and (if present) padding & inline data
*/
MMINLINE uintptr_t
getSpineSize(J9Class *clazzPtr, ArrayLayout layout, uintptr_t size)
{
return getHeaderSize(clazzPtr, layout) + getSpineSizeWithoutHeader(clazzPtr, layout, size);
}
/**
* Get the spine size without header for the given indexable object
* @param clazzPtr Pointer to the preserved J9Class, used in scavenger
* @param layout layout for array object
* @param size Size of indexable object
* @return The size in bytes of objPtr's array spine without header;
* includes arraylet ptrs, padding and inline data (if any is present)
*/
MMINLINE uintptr_t
getSpineSizeWithoutHeader(J9Class *clazzPtr, ArrayLayout layout, uintptr_t size)
{
uintptr_t dataSize = getDataSizeInBytes(clazzPtr, size);
uintptr_t numberArraylets = numArraylets(dataSize);
bool alignData = shouldAlignSpineDataSection(clazzPtr);
return getSpineSizeWithoutHeader(layout, numberArraylets, dataSize, alignData);
}
#if defined(J9VM_GC_ENABLE_DOUBLE_MAP)
/**
* Checks if arraylet falls into corner case of discontiguous data
* Arraylet possible cases:
* 0: Empty arraylets, in this case the array is represented as
* an arraylet however it does not contain any data, but it
* does contain an arrayoid (leaf pointer) that points to NULL.
* Even though this case is represented as a discontiguous arraylet
* internally due to its implementation, it is actually a contiguous
* array with length zero.
* 1: The total data size in arraylet is between 0 and region
* size. Small enough to make the arraylet layout contiguous,
* in which case this function is unreachable.
* 2: The total data size in arraylet is exacly the same size
* of a region. In this case we do not need to double
* map since we already have a contiguous representation of the
* data at first leaf.
* 3: Similar to first case, the data portion is slightly smaller than
* a region size, however not small enough to include header and data
* at the same region to make it contiguous. In which case we would
* have one leaf, where we also do not need to double map.
* 4: The total data size in arraylet is stricly greater than one region;
* however, not multiple of region size. Since with enabled double map
* layout is always discontiguous, we would have 2 or more arraylet leaves
* therefore we always double map.
* 5: The total data size in arraylet is stricly greater than one region and
* multiple of region size. Here we would have 2 or more arraylet leaves
* containing data. We always double map in this case.
*
* @param spine Pointer to an array indexable object spine
* @return false in case corner cases 0, 2 or 3 are valid. On the other hand,
* if cases 4 or 5 are true, the function returns true.
*/
MMINLINE bool
isArrayletDataDiscontiguous(J9IndexableObject *spine)
{
return numArraylets(spine) > 1;
}
/**
* Checks if arraylet falls into corner case of contiguous data
*
* @param spine Pointer to an array indexable object spine
* @return true in case corner cases 2 or 3 are valid. On the other hand,
* if cases 0, 4 or 5 are true, the function returns false.
*/
MMINLINE bool
isArrayletDataContiguous(J9IndexableObject *spine)
{
return (1 == numArraylets(spine)) && (getSizeInElements(spine) > 0);
}
#endif /* J9VM_GC_ENABLE_DOUBLE_MAP */
/**
* We can't use memcpy because it may be not atomic for pointers, use this function instead
* Copy data in uintptr_t words
* If length is not times of uintptr_t one more word is copied
* @param destAddr address copy to
* @param sourceAddr address copy from
* @param lengthInBytes requested size in bytes
*/
MMINLINE void
copyInWords (uintptr_t *destAddr, uintptr_t *sourceAddr, uintptr_t lengthInBytes)
{
uintptr_t lengthInWords = lengthInBytes / sizeof(uintptr_t);
while (lengthInWords--) {
*destAddr++ = *sourceAddr++;
}
}
/**
* Returns the header size of an arraylet for a given layout.
* This method is actually private, but since ArrayLayout is public,
* it cannot be declared in private section.
* @param layout Layout of the arraylet
* @return Size of header in bytes
*/
MMINLINE uintptr_t
getHeaderSize(J9Class *clazzPtr, ArrayLayout layout)
{
uintptr_t headerSize = contiguousIndexableHeaderSize();
if (layout != InlineContiguous) {
headerSize = discontiguousIndexableHeaderSize();
}
return headerSize;
}
/**
* Returns the size of data in an indexable object, in bytes, including leaves and alignment
* padding, excluding the header, or UDATA_MAX if an overflow occurs.
* @param arrayPtr Pointer to the indexable object whose size is required
* @return Aligned size in bytes excluding the header, or UDATA_MAX if an overflow occurs
*/
MMINLINE uintptr_t
getDataSizeInBytes(J9IndexableObject *arrayPtr)
{
return getDataSizeInBytes(J9GC_J9OBJECT_CLAZZ(arrayPtr, this), getSizeInElements(arrayPtr));
}
/**
* Returns the size of data in an indexable object, in bytes, including leaves and alignment
* padding, excluding the header, or UDATA_MAX if an overflow occurs.
* @param clazzPtr Pointer to the class of the object
* @param numberOfElements size from indexable object
* @return Aligned size in bytes excluding the header, or UDATA_MAX if an overflow occurs
*/
MMINLINE uintptr_t
getDataSizeInBytes(J9Class *clazzPtr, uintptr_t numberOfElements)
{
uintptr_t stride = J9ARRAYCLASS_GET_STRIDE(clazzPtr);
uintptr_t size = numberOfElements * stride;
uintptr_t alignedSize = UDATA_MAX;
if ((size / stride) == numberOfElements) {
alignedSize = MM_Math::roundToSizeofUDATA(size);
if (alignedSize < size) {
alignedSize = UDATA_MAX;
}
}
return alignedSize;
}
/**
* Get the size from the header for the given indexable object
* @param objPtr Pointer to an array object
* @return the size
*/
MMINLINE uintptr_t
getArraySize(J9IndexableObject *objPtr)
{
return compressObjectReferences()
? ((J9IndexableObjectContiguousCompressed*)objPtr)->size
: ((J9IndexableObjectContiguousFull*)objPtr)->size;
}
/**
* Get the layout for the given indexable object
* @param objPtr Pointer to an array object
* @return the ArrayLayout for objectPtr
*/
MMINLINE ArrayLayout
getArrayLayout(J9IndexableObject *objPtr)
{
GC_ArrayletObjectModel::ArrayLayout layout = GC_ArrayletObjectModel::InlineContiguous;
/* Trivial check for InlineContiguous. */
if (0 != getArraySize(objPtr)) {
return GC_ArrayletObjectModel::InlineContiguous;
}
/* Check if the objPtr is in the allowed arraylet range. */
if (((uintptr_t)objPtr >= (uintptr_t)_arrayletRangeBase) && ((uintptr_t)objPtr < (uintptr_t)_arrayletRangeTop)) {
uintptr_t dataSizeInBytes = getDataSizeInBytes(objPtr);
J9Class* clazz = J9GC_J9OBJECT_CLAZZ(objPtr, this);
layout = getArrayletLayout(clazz, dataSizeInBytes);
}
return layout;
}
MMINLINE ArrayLayout
getArrayletLayout(J9Class* clazz, uintptr_t dataSizeInBytes)
{
return getArrayletLayout(clazz, dataSizeInBytes, _largestDesirableArraySpineSize);
}
/**
* Get the layout of an indexable object given it's class, data size in bytes and the subspace's largestDesirableSpine.
* @param clazz The class of the object stored in the array.
* @param dataSizeInBytes the size in bytes of the data of the array.
* @param largestDesirableSpine The largest desirable spine of the arraylet.
*/
ArrayLayout getArrayletLayout(J9Class* clazz, uintptr_t dataSizeInBytes, uintptr_t largestDesirableSpine);
/**
* Perform a safe memcpy of one array to another.
* Assumes that destObject and srcObject have the same shape and size.
*/
MMINLINE void
memcpyArray(J9IndexableObject *destObject, J9IndexableObject *srcObject)
{
if (InlineContiguous == getArrayLayout(srcObject)) {
/* assume that destObject must have the same shape! */
uintptr_t sizeInBytes = getSizeInBytesWithoutHeader(srcObject);
uintptr_t* srcData = (uintptr_t*)getDataPointerForContiguous(srcObject);
uintptr_t* destData = (uintptr_t*)getDataPointerForContiguous(destObject);
copyInWords(destData, srcData, sizeInBytes);
} else {
bool const compressed = compressObjectReferences();
uintptr_t arrayletCount = numArraylets(srcObject);
fj9object_t *srcArraylets = getArrayoidPointer(srcObject);
fj9object_t *destArraylets = getArrayoidPointer(destObject);
for (uintptr_t i = 0; i < arrayletCount; i++) {
GC_SlotObject srcSlotObject(_omrVM, GC_SlotObject::addToSlotAddress(srcArraylets, i, compressed));
GC_SlotObject destSlotObject(_omrVM, GC_SlotObject::addToSlotAddress(destArraylets, i, compressed));
void* srcLeafAddress = srcSlotObject.readReferenceFromSlot();
void* destLeafAddress = destSlotObject.readReferenceFromSlot();
uintptr_t copySize = _omrVM->_arrayletLeafSize;
if (i == arrayletCount - 1) {
copySize = arrayletSize(srcObject, i);
}
copyInWords((uintptr_t *)destLeafAddress, (uintptr_t *)srcLeafAddress, copySize);
}
}
}
/**
* Perform a memcpy from a primitive array to native memory.
* Assumes that the destination is large enough.
*
* @param destData the native memory buffer to copy into
* @param srcObject the array to copy from
* @param elementIndex the start index (element-relative) to copy from
* @param elementCount the number of elements of data to copy
*/
/*MMINLINE*/ void
memcpyFromArray(void *destData, J9IndexableObject *srcObject, int32_t elementIndex, int32_t elementCount)
{
uintptr_t elementSize = J9ARRAYCLASS_GET_STRIDE(J9GC_J9OBJECT_CLAZZ(srcObject, this));
if (isInlineContiguousArraylet(srcObject)) {
// If the data is stored contiguously, then a simple copy is sufficient.
void* srcData = getDataPointerForContiguous(srcObject);
switch(elementSize)
{
case 1:
// byte/boolean
{
uint8_t* srcCursor = ((uint8_t*)srcData) + elementIndex;
uint8_t* destCursor = (uint8_t*)destData;
int32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
case 2:
// short/char
{
uint16_t* srcCursor = ((uint16_t*)srcData) + elementIndex;
uint16_t* destCursor = (uint16_t*)destData;
int32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
case 4:
// int/float
{
uint32_t* srcCursor = ((uint32_t*)srcData) + elementIndex;
uint32_t* destCursor = (uint32_t*)destData;
uint32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
case 8:
// long/double
{
U_64* srcCursor = ((U_64*)srcData) + elementIndex;
U_64* destCursor = (U_64*)destData;
uint32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
default :
// unreachable since we currently do not expect anything other than primitive arrays to be using them.
{
AssertBadElementSize();
}
break;
}
} else {
bool const compressed = compressObjectReferences();
fj9object_t *srcArraylets = getArrayoidPointer(srcObject);
void *outerDestCursor = destData;
uint32_t outerCount = elementCount;
uintptr_t arrayletLeafElements = _omrVM->_arrayletLeafSize / elementSize;
uintptr_t arrayletIndex = elementIndex / arrayletLeafElements;
uintptr_t arrayletElementOffset = elementIndex % arrayletLeafElements;
while(outerCount > 0) {
GC_SlotObject srcSlotObject(_omrVM, GC_SlotObject::addToSlotAddress(srcArraylets, arrayletIndex++, compressed));
void* srcLeafAddress = srcSlotObject.readReferenceFromSlot();
uint32_t innerCount = outerCount;
// Can we fulfill the remainder of the copy from this page?
if (innerCount + arrayletElementOffset > arrayletLeafElements) {
// Copy as much as we can
innerCount = (uint32_t)(arrayletLeafElements - arrayletElementOffset);
}
switch(elementSize)
{
case 1:
// byte/boolean
{
uint8_t* srcCursor = ((uint8_t*)srcLeafAddress) + arrayletElementOffset;
uint8_t* destCursor = (uint8_t*)outerDestCursor;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerDestCursor = (void*)destCursor;
arrayletElementOffset = 0; // Consumed
}
break;
case 2:
// short/char
{
uint16_t* srcCursor = ((uint16_t*)srcLeafAddress) + arrayletElementOffset;
uint16_t* destCursor = (uint16_t*)outerDestCursor;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerDestCursor = (void*)destCursor;
arrayletElementOffset = 0;
}
break;
case 4:
// int/float
{
uint32_t* srcCursor = ((uint32_t*)srcLeafAddress) + arrayletElementOffset;
uint32_t* destCursor = (uint32_t*)outerDestCursor;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerDestCursor = (void*)destCursor;
arrayletElementOffset = 0;
}
break;
case 8:
// long/double
{
U_64* srcCursor = ((U_64*)srcLeafAddress) + arrayletElementOffset;
U_64* destCursor = (U_64*)outerDestCursor;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerDestCursor = (void*)destCursor;
arrayletElementOffset = 0;
}
break;
default :
// unreachable since we currently do not expect anything other than primitive arrays to be using them.
{
AssertBadElementSize();
}
break;
}
}
}
}
/**
* Perform a memcpy from native memory to a primitive array.
* Assumes that the destination is large enough.
*
* @param destObject the array to copy to
* @param elementIndex the start index (element-relative) to copy from
* @param elementCount the number of elements of data to copy
* @param srcData the native memory buffer to copy from
*/
MMINLINE void
memcpyToArray(J9IndexableObject *destObject, int32_t elementIndex, int32_t elementCount, void *srcData)
{
uintptr_t elementSize = J9ARRAYCLASS_GET_STRIDE(J9GC_J9OBJECT_CLAZZ(destObject, this));
if (isInlineContiguousArraylet(destObject)) {
// If the data is stored contiguously, then a simple copy is sufficient.
void* destData = getDataPointerForContiguous(destObject);
switch(elementSize)
{
case 1:
// byte/boolean
{
uint8_t* srcCursor = (uint8_t*)srcData;
uint8_t* destCursor = ((uint8_t*)destData) + elementIndex;
int32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
case 2:
// short/char
{
uint16_t* srcCursor = (uint16_t*)srcData;
uint16_t* destCursor = ((uint16_t*)destData) + elementIndex;
int32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
case 4:
// int/float
{
uint32_t* srcCursor = (uint32_t*)srcData;
uint32_t* destCursor = ((uint32_t*)destData) + elementIndex;
uint32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
case 8:
// long/double
{
U_64* srcCursor = (U_64*)srcData;
U_64* destCursor = ((U_64*)destData) + elementIndex;
uint32_t count = elementCount;
while(count--) {
*destCursor++ = *srcCursor++;
}
}
break;
default :
// unreachable since we currently do not expect anything other than primitive arrays to be using them.
{
AssertBadElementSize();
}
break;
}
} else {
bool const compressed = compressObjectReferences();
fj9object_t *destArraylets = getArrayoidPointer(destObject);
void *outerSrcCursor = srcData;
uint32_t outerCount = elementCount;
uintptr_t arrayletLeafElements = _omrVM->_arrayletLeafSize / elementSize;
uintptr_t arrayletIndex = elementIndex / arrayletLeafElements;
uintptr_t arrayletElementOffset = elementIndex % arrayletLeafElements;
while(outerCount > 0) {
GC_SlotObject destSlotObject(_omrVM, GC_SlotObject::addToSlotAddress(destArraylets, arrayletIndex++, compressed));
void* destLeafAddress = destSlotObject.readReferenceFromSlot();
uint32_t innerCount = outerCount;
// Can we fulfill the remainder of the copy from this page?
if (innerCount + arrayletElementOffset > arrayletLeafElements) {
// Copy as much as we can
innerCount = (uint32_t)(arrayletLeafElements - arrayletElementOffset);
}
switch(elementSize)
{
case 1:
// byte/boolean
{
uint8_t* srcCursor = (uint8_t*)outerSrcCursor;
uint8_t* destCursor = ((uint8_t*)destLeafAddress) + arrayletElementOffset;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerSrcCursor = (void*)srcCursor;
arrayletElementOffset = 0; // Consumed
}
break;
case 2:
// short/char
{
uint16_t* srcCursor = (uint16_t*)outerSrcCursor;
uint16_t* destCursor = ((uint16_t*)destLeafAddress) + arrayletElementOffset;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerSrcCursor = (void*)srcCursor;
arrayletElementOffset = 0;
}
break;
case 4:
// int/float
{
uint32_t* srcCursor = (uint32_t*)outerSrcCursor;
uint32_t* destCursor = ((uint32_t*)destLeafAddress) + arrayletElementOffset;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerSrcCursor = (void*)srcCursor;
arrayletElementOffset = 0;
}
break;
case 8:
// long/double
{
U_64* srcCursor = (U_64*)outerSrcCursor;
U_64* destCursor = ((U_64*)destLeafAddress) + arrayletElementOffset;
outerCount -= innerCount; // Decrement the outer count before looping
while(innerCount--) {
*destCursor++ = *srcCursor++;
}
outerSrcCursor = (void*)srcCursor;
arrayletElementOffset = 0;
}
break;
default :
// unreachable since we currently do not expect anything other than primitive arrays to be using them.
{
AssertBadElementSize();
}
break;
}
}
}
}
/**
* Returns the size of an indexable object, in bytes, excluding the header.
* @param arrayPtr Pointer to the indexable object whose size is required
* @return Size of object in bytes excluding the header
*/
MMINLINE uintptr_t
getSizeInBytesWithoutHeader(J9IndexableObject *arrayPtr)
{
ArrayLayout layout = getArrayLayout(arrayPtr);
return getSpineSizeWithoutHeader(J9GC_J9OBJECT_CLAZZ(arrayPtr, this), layout, getSizeInElements(arrayPtr));
}
/**
* Returns the size of an indexable object, in bytes, including the header.
* WARNING: This implementation assumes the arraylet layout is InlineContiguous.
* @param clazz Pointer to the preserved indexable object class which may not be intact
* @param numberOfElements size from indexable object
* @return Size of object in bytes including the header
*/
MMINLINE uintptr_t
getSizeInBytesWithHeader(J9Class *clazz, uintptr_t numberOfElements)
{
ArrayLayout layout = InlineContiguous;
if(0 == numberOfElements) {
layout = Discontiguous;
}
return getSizeInBytesWithHeader(clazz, layout, numberOfElements);
}
/**
* Returns the size of an indexable object, in bytes, including the header.
* @param clazz Pointer to the preserved indexable object class which may not be intact
* @param flags Indexable object flags
* @param numberOfElements size from indexable object
* @return Size of object in bytes including the header
*/
MMINLINE uintptr_t
getSizeInBytesWithHeader(J9Class *clazz, ArrayLayout layout, uintptr_t numberOfElements)
{
return getSpineSize(clazz, layout, numberOfElements);
}
/**
* Returns the size of an indexable object, in bytes, including the header.
* @param arrayPtr Pointer to the indexable object whose size is required
* @return Size of object in bytes including the header
*/
MMINLINE uintptr_t
getSizeInBytesWithHeader(J9IndexableObject *arrayPtr)
{
ArrayLayout layout = getArrayLayout(arrayPtr);
return getSpineSize(J9GC_J9OBJECT_CLAZZ(arrayPtr, this), layout, getSizeInElements(arrayPtr));
}
#if defined(J9VM_ENV_DATA64)
/**
* Gets data pointer of a contiguous indexable object.
* Helper to get dataAddr field of contiguous indexable objects.
*
* @return Pointer which points to indexable object data
*/
MMINLINE void **
dataAddrSlotForContiguous(J9IndexableObject *arrayPtr)
{
AssertContiguousArrayletLayout(arrayPtr);
bool const compressed = compressObjectReferences();
void **dataAddrPtr = NULL;
if (compressed) {
dataAddrPtr = &((J9IndexableObjectWithDataAddressContiguousCompressed *)arrayPtr)->dataAddr;
} else {
dataAddrPtr = &((J9IndexableObjectWithDataAddressContiguousFull *)arrayPtr)->dataAddr;
}
return dataAddrPtr;
}
/**
* Gets data pointer of a discontiguous indexable object.
* Helper to get dataAddr field of discontiguous indexable objects.
*
* @return Pointer which points to discontiguous indexable object data
*/
MMINLINE void **
dataAddrSlotForDiscontiguous(J9IndexableObject *arrayPtr)
{
/* If double mapping is enabled only, arraylet will have a discontiguous layout.
* If sparse-heap is enabled, arraylet will have a contiguous layout. For now
* we can't simply Assert only the discontiguous case because there could also
* exist hybrid arraylets (which will be dicontinued in the future) */
bool const compressed = compressObjectReferences();
void **dataAddrPtr = NULL;
if (compressed) {
dataAddrPtr = &((J9IndexableObjectWithDataAddressDiscontiguousCompressed *)arrayPtr)->dataAddr;
} else {
dataAddrPtr = &((J9IndexableObjectWithDataAddressDiscontiguousFull *)arrayPtr)->dataAddr;
}
return dataAddrPtr;
}
/**
* Sets data pointer of a contiguous indexable object.
* Sets the data pointer of a contiguous indexable object; in this case
* dataAddr will point directly into the data right after dataAddr field
* (data resides in heap).
*
* @param arrayPtr Pointer to the indexable object whose size is required
*/
MMINLINE void
setDataAddrForContiguous(J9IndexableObject *arrayPtr)
{
void **dataAddrPtr = dataAddrSlotForContiguous(arrayPtr);
void *dataAddr = (void *)((uintptr_t)arrayPtr + contiguousIndexableHeaderSize());
*dataAddrPtr = dataAddr;
}
/**
* Sets data pointer of a discontiguous indexable object.
* Sets the data pointer of a discontiguous indexable object; in this case
* dataAddr will point to the contiguous representation of the data
* which resides outside the heap (assuming double map/sparse-heap is enabled).
* In case double map is disabled, the dataAddr will point to the first arrayoid
* of the discontiguous indexable object, which also resides right after dataAddr
* field.
*
* @param arrayPtr Pointer to the indexable object whose size is required
* @param address Pointer which points to indexable object data
*/
MMINLINE void
setDataAddrForDiscontiguous(J9IndexableObject *arrayPtr, void *address)
{
/* If double mapping is enabled only, arraylet will have a discontiguous layout.
* If sparse-heap is enabled, arraylet will have a contiguous layout. For now
* we can't simply Assert only the discontiguous case because there could also
* exist hybrid arraylets (which will be dicontinued in the future) */
void *calculatedDataAddr = address;
void **dataAddrPtr = dataAddrSlotForDiscontiguous(arrayPtr);
*dataAddrPtr = calculatedDataAddr;
}
/**
* Asserts that an indexable object pointer is indeed an indexable object
*
* @param arrayPtr Pointer to the indexable object
*/
void AssertArrayPtrIsIndexable(J9IndexableObject *arrayPtr);
/**
* Returns data pointer associated with a contiguous Indexable object.
* Data pointer will always be pointing at the arraylet data. In this
* case the data pointer will be pointing to address immediately after
* the header.
*
* @param arrayPtr Pointer to the indexable object whose size is required
* @return data address associated with the Indexable object
*/
MMINLINE void *
getDataAddrForContiguous(J9IndexableObject *arrayPtr)
{
void *dataAddr = *dataAddrSlotForContiguous(arrayPtr);
return dataAddr;
}
/**
* Returns data pointer associated with a discontiguous Indexable object.
* Data pointer will always be pointing at the arraylet data. In this
* case the data pointer will be pointing to address immediately after
* the header (the arrayoid), except when double mapping or sparse-heap
* is enabled. In these cases, the data pointer will point to the
* contiguous representation of the data; hence returning that pointer.
*
* @param arrayPtr Pointer to the indexable object whose size is required
* @return data address associated with the Indexable object
*/
MMINLINE void *
getDataAddrForDiscontiguous(J9IndexableObject *arrayPtr)
{
/* If double mapping is enabled only, arraylet will have a discontiguous layout.
* If sparse-heap is enabled, arraylet will have a contiguous layout. For now we
* Assert only the discontiguous case until sparse-heap is introduced. */
AssertDiscontiguousArrayletLayout(arrayPtr);
void *dataAddr = *dataAddrSlotForDiscontiguous(arrayPtr);
return dataAddr;
}
/**
* Returns data pointer associated with the Indexable object.
* Data pointer will always be pointing at the arraylet data. In all
* cases the data pointer will be pointing to address immediately after
* the header, except when double mapping is enabled. In this case,
* if double mapping is enabled and arraylet was double mapped
* successfully the data pointer will point to the contiguous
* representation of the data; hence returning that pointer.
*
* @param arrayPtr Pointer to the indexable object whose size is required
* @return data address associated with the Indexable object
*/
MMINLINE void *
getDataAddrForIndexableObject(J9IndexableObject *arrayPtr)
{
return (InlineContiguous == getArrayLayout(arrayPtr))
? getDataAddrForContiguous(arrayPtr)
: getDataAddrForDiscontiguous(arrayPtr);
}
/**
* Checks that the dataAddr field of the indexable object is correct.
*
* @param arrayPtr Pointer to the indexable object
* @return if the dataAddr field of the indexable object is correct
*/
MMINLINE bool
isValidDataAddr(J9IndexableObject *arrayPtr)
{
bool isValidDataAddress = true;
if (_isIndexableDataAddrPresent) {
void *dataAddr = getDataAddrForIndexableObject(arrayPtr);
isValidDataAddress = isValidDataAddr(arrayPtr, dataAddr);
}