Coder Social home page Coder Social logo

Comments (1)

wenyongh avatar wenyongh commented on May 30, 2024

I tried to use pass float* and size_t* to wasm module. After using wasm_runtime_module_malloc(). I found that the absolute address is different. Here is my cpp code(I only copyed the part which has problem)

float* test_input;
    test_input=new float[5]{1.0,2.0,3.0,4.0,5.0};
    std::cout<<test_input<<std::endl;
    uint32_t buffer_for_wasm_test_input=wasm_runtime_module_malloc(wasm_module_inst,sizeof(float)*5, (void**)&test_input);
    std::cout<<test_input<<std::endl;
    void* input=wasm_runtime_addr_app_to_native(wasm_module_inst, buffer_for_wasm_test_input);
    std::cout<<input<<std::endl;
    size_t* shape_input=new size_t[3]{1,1,5};
    std::cout<<shape_input<<std::endl;
    uint32_t buffer_for_wasm_shape_input=wasm_runtime_module_malloc(wasm_module_inst,sizeof(size_t)*3,(void**)&shape_input);
    std::cout<<shape_input<<std::endl;
    void* test=wasm_runtime_addr_app_to_native(wasm_module_inst, buffer_for_wasm_shape_input);
    std::cout<<test<<std::endl;

And the result is that

0x5605cbb7e420
0x7f4e9fa10610
0x7f4e9fa10610
0x5605cbb7e440
0x7f4e9fa10630
0x7f4e9fa10630

I am wondering why after using the wasm_runtime_module_malloc(), the pointer address is different. And if I want them to be same ,how to do that? Thank you!

Hi, the wasm_runtime_module_malloc returns both the offset in the linear memory (buffer_for_wasm_test_input) and the absolute address of that offset in native (test_input = linear_base_addr + buffer_for_wasm_test_input), and you can convert them to each other by calling wasm_runtime_addr_app_to_native/wasm_runtime_addr_native_to_app.

I suppose you want to module_malloc the memory, and put the data into it, maybe you can:

    float test_input_buf[5] = {1.0,2.0,3.0,4.0,5.0};
    float* test_input = NULL;

    uint32_t buffer_for_wasm_test_input=wasm_runtime_module_malloc(wasm_module_inst,sizeof(float)*5, (void**)&test_input);
    if (buffer_for_wasm_test_input == 0)
        return error;
    memcpy(test_input, test_input_buf, sizeof(float) * 5);
    ...

from wasm-micro-runtime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.