src/java.base/share/classes/jdk/internal/jimage/ImageBufferCache.java
author erikj
Wed, 07 Mar 2018 13:26:15 -0800
changeset 49357 aaedb8343784
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198243: Add build time check for global operator new/delete in object files Reviewed-by: tbell, kbarrett, dholmes, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     4
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    10
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    15
 * accompanied this code).
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    16
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    20
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    23
 * questions.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    24
 */
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    25
package jdk.internal.jimage;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    26
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    27
import java.lang.ref.WeakReference;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    28
import java.nio.ByteBuffer;
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    29
import java.util.Arrays;
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    30
import java.util.Comparator;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    31
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    32
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    33
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    34
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    35
 * It is used internally in the JDK to implement jimage/jrtfs access,
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    36
 * but also compiled and delivered as part of the jrt-fs.jar to support access
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    37
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    38
 */
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    39
class ImageBufferCache {
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    40
    private static final int MAX_CACHED_BUFFERS = 3;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    41
    private static final int LARGE_BUFFER = 0x10000;
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    42
    private static final ThreadLocal<BufferReference[]> CACHE =
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    43
        new ThreadLocal<BufferReference[]>() {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    44
            @Override
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    45
            protected BufferReference[] initialValue() {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    46
                // 1 extra slot to simplify logic of releaseBuffer()
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    47
                return new BufferReference[MAX_CACHED_BUFFERS + 1];
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    48
            }
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    49
        };
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    50
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    51
    private static ByteBuffer allocateBuffer(long size) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    52
        return ByteBuffer.allocateDirect((int)((size + 0xFFF) & ~0xFFF));
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    53
    }
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    54
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    55
    static ByteBuffer getBuffer(long size) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    56
        if (size < 0 || Integer.MAX_VALUE < size) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    57
            throw new IndexOutOfBoundsException("size");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    58
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    59
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    60
        ByteBuffer result = null;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    61
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    62
        if (size > LARGE_BUFFER) {
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    63
            result = allocateBuffer(size);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    64
        } else {
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    65
            BufferReference[] cache = CACHE.get();
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    66
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    67
            // buffers are ordered by decreasing capacity
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    68
            // cache[MAX_CACHED_BUFFERS] is always null
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    69
            for (int i = MAX_CACHED_BUFFERS - 1; i >= 0; i--) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    70
                BufferReference reference = cache[i];
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    71
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    72
                if (reference != null) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    73
                    ByteBuffer buffer = reference.get();
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    74
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    75
                    if (buffer != null && size <= buffer.capacity()) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    76
                        cache[i] = null;
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    77
                        result = buffer;
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    78
                        result.rewind();
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    79
                        break;
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    80
                    }
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    81
                }
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    82
            }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    83
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    84
            if (result == null) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    85
                result = allocateBuffer(size);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    86
            }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    87
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    88
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    89
        result.limit((int)size);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    90
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    91
        return result;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    92
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    93
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    94
    static void releaseBuffer(ByteBuffer buffer) {
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    95
        if (buffer.capacity() > LARGE_BUFFER) {
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    96
            return;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    97
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    98
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
    99
        BufferReference[] cache = CACHE.get();
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   100
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   101
        // expunge cleared BufferRef(s)
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   102
        for (int i = 0; i < MAX_CACHED_BUFFERS; i++) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   103
            BufferReference reference = cache[i];
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   104
            if (reference != null && reference.get() == null) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   105
                cache[i] = null;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   106
            }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   107
        }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   108
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   109
        // insert buffer back with new BufferRef wrapping it
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   110
        cache[MAX_CACHED_BUFFERS] = new BufferReference(buffer);
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   111
        Arrays.sort(cache, DECREASING_CAPACITY_NULLS_LAST);
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   112
        // squeeze the smallest one out
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   113
        cache[MAX_CACHED_BUFFERS] = null;
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   114
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   115
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   116
    private static Comparator<BufferReference> DECREASING_CAPACITY_NULLS_LAST =
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   117
        new Comparator<BufferReference>() {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   118
            @Override
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   119
            public int compare(BufferReference br1, BufferReference br2) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   120
                return Integer.compare(br2 == null ? 0 : br2.capacity,
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   121
                                       br1 == null ? 0 : br1.capacity);
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   122
            }
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   123
        };
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   124
37342
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   125
    private static class BufferReference extends WeakReference<ByteBuffer> {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   126
        // saved capacity so that DECREASING_CAPACITY_NULLS_LAST comparator
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   127
        // is stable in the presence of GC clearing the WeakReference concurrently
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   128
        final int capacity;
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   129
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   130
        BufferReference(ByteBuffer buffer) {
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   131
            super(buffer);
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   132
            capacity = buffer.capacity();
3f54fbfc2706 8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents: 36511
diff changeset
   133
        }
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   134
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   135
}