Search in sources :

Example 1 with UnsafeWasmMemory

use of org.graalvm.wasm.memory.UnsafeWasmMemory in project graal by oracle.

the class WasmPolyglotTestSuite method unsafeMemoryFreed.

@Test
public void unsafeMemoryFreed() throws IOException {
    Context.Builder contextBuilder = Context.newBuilder(WasmLanguage.ID);
    Source.Builder sourceBuilder = Source.newBuilder(WasmLanguage.ID, ByteSequence.create(binaryReturnConst), "main");
    Source source = sourceBuilder.build();
    contextBuilder.allowExperimentalOptions(true);
    contextBuilder.option("wasm.UseUnsafeMemory", "true");
    Context context = contextBuilder.build();
    context.enter();
    context.eval(source);
    final Value mainModule = context.getBindings(WasmLanguage.ID).getMember("main");
    mainModule.getMember("main").execute();
    final TruffleLanguage.Env env = WasmContext.get(null).environment();
    final UnsafeWasmMemory memory = (UnsafeWasmMemory) env.asGuestValue(mainModule.getMember("memory"));
    Assert.assertTrue("Memory should have been allocated.", !memory.freed());
    context.close();
    Assert.assertTrue("Memory should have been freed.", memory.freed());
}
Also used : WasmContext(org.graalvm.wasm.WasmContext) Context(org.graalvm.polyglot.Context) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) Value(org.graalvm.polyglot.Value) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with UnsafeWasmMemory

use of org.graalvm.wasm.memory.UnsafeWasmMemory in project graal by oracle.

the class SymbolTable method allocateMemory.

public void allocateMemory(int declaredMinSize, int declaredMaxSize) {
    checkNotParsed();
    validateSingleMemory();
    memory = new MemoryInfo(declaredMinSize, declaredMaxSize);
    module().addLinkAction((context, instance) -> {
        final int maxAllowedSize = minUnsigned(declaredMaxSize, module().limits().memoryInstanceSizeLimit());
        module().limits().checkMemoryInstanceSize(declaredMinSize);
        final WasmMemory wasmMemory;
        if (context.environment().getOptions().get(WasmOptions.UseUnsafeMemory)) {
            wasmMemory = new UnsafeWasmMemory(declaredMinSize, declaredMaxSize, maxAllowedSize);
        } else {
            wasmMemory = new ByteArrayWasmMemory(declaredMinSize, declaredMaxSize, maxAllowedSize);
        }
        final int memoryIndex = context.memories().register(wasmMemory);
        final WasmMemory allocatedMemory = context.memories().memory(memoryIndex);
        instance.setMemory(allocatedMemory);
    });
}
Also used : UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Aggregations

UnsafeWasmMemory (org.graalvm.wasm.memory.UnsafeWasmMemory)2 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)1 Context (org.graalvm.polyglot.Context)1 Source (org.graalvm.polyglot.Source)1 Value (org.graalvm.polyglot.Value)1 WasmContext (org.graalvm.wasm.WasmContext)1 ByteArrayWasmMemory (org.graalvm.wasm.memory.ByteArrayWasmMemory)1 WasmMemory (org.graalvm.wasm.memory.WasmMemory)1 Test (org.junit.Test)1