annotate graal/com.oracle.jvmci.meta/src/com/oracle/jvmci/meta/MethodIdHolder.java @ 21556:48c1ebd24120

renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 May 2015 00:36:16 +0200
parents graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/MethodIdHolder.java@0627ebc2a3ea
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
1 /*
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
4 *
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
8 *
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
13 * accompanied this code).
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
14 *
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
18 *
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
21 * questions.
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
22 */
21556
48c1ebd24120 renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21549
diff changeset
23 package com.oracle.jvmci.meta;
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
24
20902
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
25 import java.util.*;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
26 import java.util.function.*;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
27
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
28 /**
21549
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21375
diff changeset
29 * An object that can be assigned a globally unique identifier for use as a key in a
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21375
diff changeset
30 * {@link MethodIdMap}.
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
31 */
21549
0627ebc2a3ea moved MethodId* classes from c.o.g.graphbuilderconf to c.o.g.api.meta (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 21375
diff changeset
32 public interface MethodIdHolder {
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
33 /**
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
34 * Sets the unique, positive, non-zero identifier for this method.
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
35 */
20902
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
36 void setMethodId(int id);
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
37
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
38 /**
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
39 * Gets the identifier set by {@link #setMethodId(int)} or 0 if no identifier was assigned to
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
40 * this method.
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
41 */
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
42 int getMethodId();
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
43
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
44 /**
20902
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
45 * A singleton class for allocating globally unique method identifiers.
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
46 */
20902
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
47 static final class MethodIdAllocator {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
48
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
49 /**
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
50 * Ensures a given method has a unique identifier.
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
51 */
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
52 public int assignId(MethodIdHolder holder) {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
53 assert Thread.holdsLock(instance) : "must only be called from within MethodIdHolder.allocateIds";
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
54 int id = holder.getMethodId();
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
55 if (id == 0) {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
56 id = nextId++;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
57 holder.setMethodId(id);
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
58 if (idVerifierMap != null) {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
59 idVerifierMap.put(holder, id);
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
60 }
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
61 } else {
21375
fef56da8474e Relax assertion to allow MethodIdHolders that have pre-assigned ids
Christian Wimmer <christian.wimmer@oracle.com>
parents: 20903
diff changeset
62 assert !idVerifierMap.containsKey(holder) || idVerifierMap.get(holder) == id;
20902
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
63 }
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
64 return id;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
65 }
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
66
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
67 private int nextId = 1;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
68 private final Map<MethodIdHolder, Integer> idVerifierMap;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
69
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
70 @SuppressWarnings("all")
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
71 private MethodIdAllocator() {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
72 boolean assertionsEnabled = false;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
73 assert assertionsEnabled = true;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
74 idVerifierMap = assertionsEnabled ? new HashMap<>() : null;
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
75 }
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
76
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
77 /**
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
78 * Singleton instance.
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
79 */
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
80 private static final MethodIdAllocator instance = new MethodIdAllocator();
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
81 }
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
82
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
83 /**
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
84 * Executes some given code that ensures some set of {@link ResolvedJavaMethod}s have unique ids
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
85 * {@linkplain MethodIdHolder#setMethodId(int) assigned} to them. The
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
86 * {@link Consumer#accept(Object)} method of the given object is called under a global lock.
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
87 */
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
88 static void assignIds(Consumer<MethodIdAllocator> methodIdConsumer) {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
89 synchronized (MethodIdAllocator.instance) {
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
90 methodIdConsumer.accept(MethodIdAllocator.instance);
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
91 }
7cc48e7e6bfe generalized InvocationPluginIdHolder to MethodIdHolder
Doug Simon <doug.simon@oracle.com>
parents: 19885
diff changeset
92 }
19885
b950967f74c7 replaced HashMap with a side-table (i.e., an array) for looking up InvocationPlugins
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
93 }