1 /*
2 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
567 * Note: pointers off-heap are considered to be primitive arrays
568 *
569 * @throws RuntimeException if the pointer is invalid
570 * (<em>Note:</em> after optimization, invalid inputs may
571 * go undetected, which will lead to unpredictable
572 * behavior)
573 */
574 private void checkPrimitivePointer(Object o, long offset) {
575 checkPointer(o, offset);
576
577 if (o != null) {
578 // If on heap, it must be a primitive array
579 checkPrimitiveArray(o.getClass());
580 }
581 }
582
583
584 /// wrappers for malloc, realloc, free:
585
586 /**
587 * Allocates a new block of native memory, of the given size in bytes. The
588 * contents of the memory are uninitialized; they will generally be
589 * garbage. The resulting native pointer will never be zero, and will be
590 * aligned for all value types. Dispose of this memory by calling {@link
591 * #freeMemory}, or resize it with {@link #reallocateMemory}.
592 *
593 * <em>Note:</em> It is the resposibility of the caller to make
594 * sure arguments are checked before the methods are called. While
595 * some rudimentary checks are performed on the input, the checks
596 * are best effort and when performance is an overriding priority,
597 * as when methods of this class are optimized by the runtime
598 * compiler, some or all checks (if any) may be elided. Hence, the
599 * caller must not rely on the checks and corresponding
600 * exceptions!
601 *
602 * @throws RuntimeException if the size is negative or too large
603 * for the native size_t type
604 *
605 * @throws OutOfMemoryError if the allocation is refused by the system
606 *
607 * @see #getByte(long)
608 * @see #putByte(long, byte)
609 */
610 public long allocateMemory(long bytes) {
611 allocateMemoryChecks(bytes);
612
613 if (bytes == 0) {
614 return 0;
615 }
616
617 long p = allocateMemory0(bytes);
618 if (p == 0) {
619 throw new OutOfMemoryError();
620 }
621
622 return p;
623 }
624
625 /**
626 * Validate the arguments to allocateMemory
627 *
628 * @throws RuntimeException if the arguments are invalid
629 * (<em>Note:</em> after optimization, invalid inputs may
630 * go undetected, which will lead to unpredictable
644 * #reallocateMemory}. The address passed to this method may be null, in
645 * which case an allocation will be performed.
646 *
647 * <em>Note:</em> It is the resposibility of the caller to make
648 * sure arguments are checked before the methods are called. While
649 * some rudimentary checks are performed on the input, the checks
650 * are best effort and when performance is an overriding priority,
651 * as when methods of this class are optimized by the runtime
652 * compiler, some or all checks (if any) may be elided. Hence, the
653 * caller must not rely on the checks and corresponding
654 * exceptions!
655 *
656 * @throws RuntimeException if the size is negative or too large
657 * for the native size_t type
658 *
659 * @throws OutOfMemoryError if the allocation is refused by the system
660 *
661 * @see #allocateMemory
662 */
663 public long reallocateMemory(long address, long bytes) {
664 reallocateMemoryChecks(address, bytes);
665
666 if (bytes == 0) {
667 freeMemory(address);
668 return 0;
669 }
670
671 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
672 if (p == 0) {
673 throw new OutOfMemoryError();
674 }
675
676 return p;
677 }
678
679 /**
680 * Validate the arguments to reallocateMemory
681 *
682 * @throws RuntimeException if the arguments are invalid
683 * (<em>Note:</em> after optimization, invalid inputs may
|
1 /*
2 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
567 * Note: pointers off-heap are considered to be primitive arrays
568 *
569 * @throws RuntimeException if the pointer is invalid
570 * (<em>Note:</em> after optimization, invalid inputs may
571 * go undetected, which will lead to unpredictable
572 * behavior)
573 */
574 private void checkPrimitivePointer(Object o, long offset) {
575 checkPointer(o, offset);
576
577 if (o != null) {
578 // If on heap, it must be a primitive array
579 checkPrimitiveArray(o.getClass());
580 }
581 }
582
583
584 /// wrappers for malloc, realloc, free:
585
586 /**
587 * Round up allocation size to a multiple of HeapWordSize.
588 */
589 private long alignToHeapWordSize(long bytes) {
590 if (bytes >= 0) {
591 return (bytes + ADDRESS_SIZE - 1) & ~(ADDRESS_SIZE - 1);
592 } else {
593 throw invalidInput();
594 }
595 }
596
597 /**
598 * Allocates a new block of native memory, of the given size in bytes. The
599 * contents of the memory are uninitialized; they will generally be
600 * garbage. The resulting native pointer will never be zero, and will be
601 * aligned for all value types. Dispose of this memory by calling {@link
602 * #freeMemory}, or resize it with {@link #reallocateMemory}.
603 *
604 * <em>Note:</em> It is the resposibility of the caller to make
605 * sure arguments are checked before the methods are called. While
606 * some rudimentary checks are performed on the input, the checks
607 * are best effort and when performance is an overriding priority,
608 * as when methods of this class are optimized by the runtime
609 * compiler, some or all checks (if any) may be elided. Hence, the
610 * caller must not rely on the checks and corresponding
611 * exceptions!
612 *
613 * @throws RuntimeException if the size is negative or too large
614 * for the native size_t type
615 *
616 * @throws OutOfMemoryError if the allocation is refused by the system
617 *
618 * @see #getByte(long)
619 * @see #putByte(long, byte)
620 */
621 public long allocateMemory(long bytes) {
622 bytes = alignToHeapWordSize(bytes);
623
624 allocateMemoryChecks(bytes);
625
626 if (bytes == 0) {
627 return 0;
628 }
629
630 long p = allocateMemory0(bytes);
631 if (p == 0) {
632 throw new OutOfMemoryError();
633 }
634
635 return p;
636 }
637
638 /**
639 * Validate the arguments to allocateMemory
640 *
641 * @throws RuntimeException if the arguments are invalid
642 * (<em>Note:</em> after optimization, invalid inputs may
643 * go undetected, which will lead to unpredictable
657 * #reallocateMemory}. The address passed to this method may be null, in
658 * which case an allocation will be performed.
659 *
660 * <em>Note:</em> It is the resposibility of the caller to make
661 * sure arguments are checked before the methods are called. While
662 * some rudimentary checks are performed on the input, the checks
663 * are best effort and when performance is an overriding priority,
664 * as when methods of this class are optimized by the runtime
665 * compiler, some or all checks (if any) may be elided. Hence, the
666 * caller must not rely on the checks and corresponding
667 * exceptions!
668 *
669 * @throws RuntimeException if the size is negative or too large
670 * for the native size_t type
671 *
672 * @throws OutOfMemoryError if the allocation is refused by the system
673 *
674 * @see #allocateMemory
675 */
676 public long reallocateMemory(long address, long bytes) {
677 bytes = alignToHeapWordSize(bytes);
678
679 reallocateMemoryChecks(address, bytes);
680
681 if (bytes == 0) {
682 freeMemory(address);
683 return 0;
684 }
685
686 long p = (address == 0) ? allocateMemory0(bytes) : reallocateMemory0(address, bytes);
687 if (p == 0) {
688 throw new OutOfMemoryError();
689 }
690
691 return p;
692 }
693
694 /**
695 * Validate the arguments to reallocateMemory
696 *
697 * @throws RuntimeException if the arguments are invalid
698 * (<em>Note:</em> after optimization, invalid inputs may
|