author | dxu |
Thu, 04 Apr 2013 15:39:17 -0700 | |
changeset 16734 | da1901d79073 |
parent 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
16734
da1901d79073
8000406: change files using @GenerateNativeHeader to use @Native
dxu
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package com.apple.jobjc; |
|
26 |
||
27 |
import java.nio.ByteOrder; |
|
28 |
||
29 |
import com.apple.jobjc.Coder.PrimitivePointerCoder; |
|
30 |
||
13166
8470c1078142
7181501: Add some GenerateNativeHeader annotations and misc Mac adjustments to makefiles
erikj
parents:
12047
diff
changeset
|
31 |
|
12047 | 32 |
public final class NativeArgumentBuffer{ |
33 |
private static final ThreadLocal<NativeArgumentBuffer> threadLocal = new ThreadLocal<NativeArgumentBuffer>(); |
|
34 |
||
35 |
static NativeArgumentBuffer getThreadLocalBuffer(final JObjCRuntime runtime) { |
|
36 |
runtime.assertOK(); |
|
37 |
final NativeArgumentBuffer alreadyCreated = threadLocal.get(); |
|
38 |
if (alreadyCreated != null) return alreadyCreated; |
|
39 |
||
40 |
final NativeArgumentBuffer newThreadLocalState = new NativeArgumentBuffer(runtime); |
|
41 |
threadLocal.set(newThreadLocalState); |
|
42 |
return newThreadLocalState; |
|
43 |
} |
|
44 |
||
45 |
final JObjCRuntime runtime; |
|
46 |
||
47 |
final NativeBuffer buffer; |
|
48 |
||
49 |
long argPtrsPtr; |
|
50 |
long argValuesPtr; |
|
51 |
final long retValPtr; |
|
52 |
||
53 |
private static final int MAX_ARGS = 512; |
|
54 |
private static final int BUF_SIZE = MAX_ARGS * 8 * 2; |
|
55 |
private static final int ARG_VALS_OFFSET = BUF_SIZE/2; |
|
56 |
||
57 |
private NativeArgumentBuffer(final JObjCRuntime runtime) { |
|
58 |
runtime.assertOK(); |
|
59 |
this.runtime = runtime; |
|
60 |
this.buffer = new NativeBuffer(BUF_SIZE); |
|
61 |
this.buffer.buffer.order(ByteOrder.nativeOrder()); |
|
62 |
||
63 |
reset(); |
|
64 |
this.retValPtr = buffer.bufferPtr; |
|
65 |
} |
|
66 |
||
67 |
||
68 |
// Call before each new call |
|
69 |
public void reset() { |
|
70 |
argPtrsPtr = buffer.bufferPtr; |
|
71 |
argValuesPtr = buffer.bufferPtr + ARG_VALS_OFFSET; |
|
72 |
assert buffer.ptrInBounds(argValuesPtr); |
|
73 |
} |
|
74 |
||
75 |
// Push a pointer to a block of memory |
|
76 |
public void doPutArgPtr(long ptr) { |
|
77 |
assert buffer.ptrInBounds(argPtrsPtr); |
|
78 |
PrimitivePointerCoder.INST.push(runtime, argPtrsPtr, ptr); |
|
79 |
argPtrsPtr += JObjCRuntime.PTR_LEN; |
|
80 |
} |
|
81 |
||
82 |
// Call this after having written a value of size `sizeof` to `argValuesPtr`. |
|
83 |
public void didPutArgValue(int sizeof) { |
|
84 |
assert buffer.ptrInBounds(argValuesPtr); |
|
85 |
doPutArgPtr(argValuesPtr); |
|
86 |
argValuesPtr += sizeof; |
|
87 |
} |
|
88 |
||
89 |
||
90 |
@Override public String toString() { |
|
91 |
final StringBuilder builder = new StringBuilder(); |
|
92 |
final long bptr = buffer.bufferPtr; |
|
93 |
||
94 |
for(long i = bptr; i < bptr + ARG_VALS_OFFSET; i += JObjCRuntime.PTR_LEN){ |
|
95 |
if(argPtrsPtr == i) |
|
96 |
builder.append("*"); |
|
97 |
builder.append(PrimitivePointerCoder.INST.popPtr(JObjCRuntime.inst(), i)); |
|
98 |
builder.append(" "); |
|
99 |
} |
|
100 |
||
101 |
builder.append("\n"); |
|
102 |
||
103 |
for(long i = bptr + ARG_VALS_OFFSET; i < bptr + BUF_SIZE; i += JObjCRuntime.PTR_LEN){ |
|
104 |
if(argValuesPtr == i) |
|
105 |
builder.append("*"); |
|
106 |
builder.append(PrimitivePointerCoder.INST.popPtr(JObjCRuntime.inst(), i)); |
|
107 |
builder.append(" "); |
|
108 |
} |
|
109 |
||
110 |
return builder.toString(); |
|
111 |
} |
|
112 |
} |