719 public long getTotalCapacity() { 720 return Bits.totalCapacity.get(); 721 } 722 @Override 723 public long getMemoryUsed() { 724 return Bits.reservedMemory.get(); 725 } 726 }; 727 } 728 @Override 729 public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) { 730 return new DirectByteBuffer(addr, cap, ob); 731 } 732 @Override 733 public void truncate(Buffer buf) { 734 buf.truncate(); 735 } 736 }); 737 } 738 739 // -- Bulk get/put acceleration -- 740 741 // These numbers represent the point at which we have empirically 742 // determined that the average cost of a JNI call exceeds the expense 743 // of an element by element copy. These numbers may change over time. 744 static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6; 745 static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6; 746 747 // This number limits the number of bytes to copy per call to Unsafe's 748 // copyMemory method. A limit is imposed to allow for safepoint polling 749 // during a large copy 750 static final long UNSAFE_COPY_THRESHOLD = 1024L * 1024L; 751 752 // These methods do no bounds checking. Verification that the copy will not 753 // result in memory corruption should be done prior to invocation. 754 // All positions and lengths are specified in bytes. 755 756 /** 757 * Copy from given source array to destination address. 758 * 759 * @param src 760 * source array 761 * @param srcBaseOffset 762 * offset of first element of storage in source array 763 * @param srcPos 764 * offset within source array of the first element to read 765 * @param dstAddr 766 * destination address 767 * @param length 768 * number of bytes to copy 769 */ 770 static void copyFromArray(Object src, long srcBaseOffset, long srcPos, 771 long dstAddr, long length) 772 { 773 long offset = srcBaseOffset + srcPos; 774 while (length > 0) { 775 long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; 776 unsafe.copyMemory(src, offset, null, dstAddr, size); 777 length -= size; 778 offset += size; 779 dstAddr += size; 780 } 781 } 782 783 /** 784 * Copy from source address into given destination array. 785 * 786 * @param srcAddr 787 * source address 788 * @param dst 789 * destination array 790 * @param dstBaseOffset 791 * offset of first element of storage in destination array 792 * @param dstPos 793 * offset within destination array of the first element to write 794 * @param length 795 * number of bytes to copy 796 */ 797 static void copyToArray(long srcAddr, Object dst, long dstBaseOffset, long dstPos, 798 long length) 799 { 800 long offset = dstBaseOffset + dstPos; 801 while (length > 0) { 802 long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; 803 unsafe.copyMemory(null, srcAddr, dst, offset, size); 804 length -= size; 805 srcAddr += size; 806 offset += size; 807 } 808 } 809 810 /** 811 * Copy and unconditionally byte swap 16 bit elements from a heap array to off-heap memory 812 * 813 * @param src 814 * the source array, must be a 16-bit primitive array type 815 * @param srcPos 816 * byte offset within source array of the first element to read 817 * @param dstAddr 818 * destination address 819 * @param length 820 * number of bytes to copy 821 */ 822 static void copyFromCharArray(Object src, long srcPos, long dstAddr, long length) { 823 unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 2); 824 } 825 826 /** 827 * Copy and unconditionally byte swap 16 bit elements from off-heap memory to a heap array 828 * 829 * @param srcAddr 830 * source address 831 * @param dst 832 * destination array, must be a 16-bit primitive array type 833 * @param dstPos 834 * byte offset within the destination array of the first element to write 835 * @param length 836 * number of bytes to copy 837 */ 838 static void copyToCharArray(long srcAddr, Object dst, long dstPos, long length) { 839 unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 2); 840 } 841 842 /** 843 * Copy and unconditionally byte swap 16 bit elements from a heap array to off-heap memory 844 * 845 * @param src 846 * the source array, must be a 16-bit primitive array type 847 * @param srcPos 848 * byte offset within source array of the first element to read 849 * @param dstAddr 850 * destination address 851 * @param length 852 * number of bytes to copy 853 */ 854 static void copyFromShortArray(Object src, long srcPos, long dstAddr, long length) { 855 unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 2); 856 } 857 858 /** 859 * Copy and unconditionally byte swap 16 bit elements from off-heap memory to a heap array 860 * 861 * @param srcAddr 862 * source address 863 * @param dst 864 * destination array, must be a 16-bit primitive array type 865 * @param dstPos 866 * byte offset within the destination array of the first element to write 867 * @param length 868 * number of bytes to copy 869 */ 870 static void copyToShortArray(long srcAddr, Object dst, long dstPos, long length) { 871 unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 2); 872 } 873 874 /** 875 * Copy and unconditionally byte swap 32 bit elements from a heap array to off-heap memory 876 * 877 * @param src 878 * the source array, must be a 32-bit primitive array type 879 * @param srcPos 880 * byte offset within source array of the first element to read 881 * @param dstAddr 882 * destination address 883 * @param length 884 * number of bytes to copy 885 */ 886 static void copyFromIntArray(Object src, long srcPos, long dstAddr, long length) { 887 unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 4); 888 } 889 890 /** 891 * Copy and unconditionally byte swap 32 bit elements from off-heap memory to a heap array 892 * 893 * @param srcAddr 894 * source address 895 * @param dst 896 * destination array, must be a 32-bit primitive array type 897 * @param dstPos 898 * byte offset within the destination array of the first element to write 899 * @param length 900 * number of bytes to copy 901 */ 902 static void copyToIntArray(long srcAddr, Object dst, long dstPos, long length) { 903 unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 4); 904 } 905 906 /** 907 * Copy and unconditionally byte swap 64 bit elements from a heap array to off-heap memory 908 * 909 * @param src 910 * the source array, must be a 64-bit primitive array type 911 * @param srcPos 912 * byte offset within source array of the first element to read 913 * @param dstAddr 914 * destination address 915 * @param length 916 * number of bytes to copy 917 */ 918 static void copyFromLongArray(Object src, long srcPos, long dstAddr, long length) { 919 unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 8); 920 } 921 922 /** 923 * Copy and unconditionally byte swap 64 bit elements from off-heap memory to a heap array 924 * 925 * @param srcAddr 926 * source address 927 * @param dst 928 * destination array, must be a 64-bit primitive array type 929 * @param dstPos 930 * byte offset within the destination array of the first element to write 931 * @param length 932 * number of bytes to copy 933 */ 934 static void copyToLongArray(long srcAddr, Object dst, long dstPos, long length) { 935 unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 8); 936 } 937 } | 719 public long getTotalCapacity() { 720 return Bits.totalCapacity.get(); 721 } 722 @Override 723 public long getMemoryUsed() { 724 return Bits.reservedMemory.get(); 725 } 726 }; 727 } 728 @Override 729 public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) { 730 return new DirectByteBuffer(addr, cap, ob); 731 } 732 @Override 733 public void truncate(Buffer buf) { 734 buf.truncate(); 735 } 736 }); 737 } 738 739 // These numbers represent the point at which we have empirically 740 // determined that the average cost of a JNI call exceeds the expense 741 // of an element by element copy. These numbers may change over time. 742 static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6; 743 static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6; 744 } |