1 /**
2  * \file wasmtime/memory.h
3  *
4  * Wasmtime API for interacting with wasm memories.
5  */
6 
7 #ifndef WASMTIME_MEMORY_H
8 #define WASMTIME_MEMORY_H
9 
10 #include <wasm.h>
11 #include <wasmtime/extern.h>
12 #include <wasmtime/store.h>
13 #include <wasmtime/error.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /**
20  * \brief Creates a new memory type from the specified parameters.
21  *
22  * Note that this function is preferred over #wasm_memorytype_new for
23  * compatibility with the memory64 proposal.
24  */
25 WASM_API_EXTERN wasm_memorytype_t *wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max, bool is_64);
26 
27 /**
28  * \brief Returns the minimum size, in pages, of the specified memory type.
29  *
30  * Note that this function is preferred over #wasm_memorytype_limits for
31  * compatibility with the memory64 proposal.
32  */
33 WASM_API_EXTERN uint64_t wasmtime_memorytype_minimum(const wasm_memorytype_t *ty);
34 
35 /**
36  * \brief Returns the maximum size, in pages, of the specified memory type.
37  *
38  * If this memory type doesn't have a maximum size listed then `false` is
39  * returned. Otherwise `true` is returned and the `max` pointer is filled in
40  * with the specified maximum size, in pages.
41  *
42  * Note that this function is preferred over #wasm_memorytype_limits for
43  * compatibility with the memory64 proposal.
44  */
45 WASM_API_EXTERN bool wasmtime_memorytype_maximum(const wasm_memorytype_t *ty, uint64_t *max);
46 
47 /**
48  * \brief Returns whether this type of memory represents a 64-bit memory.
49  */
50 WASM_API_EXTERN bool wasmtime_memorytype_is64(const wasm_memorytype_t *ty);
51 
52 /**
53  * \brief Creates a new WebAssembly linear memory
54  *
55  * \param store the store to create the memory within
56  * \param ty the type of the memory to create
57  * \param ret where to store the returned memory
58  *
59  * If an error happens when creating the memory it's returned and owned by the
60  * caller. If an error happens then `ret` is not filled in.
61  */
62 WASM_API_EXTERN wasmtime_error_t *wasmtime_memory_new(
63     wasmtime_context_t *store,
64     const wasm_memorytype_t* ty,
65     wasmtime_memory_t *ret
66 );
67 
68 /**
69  * \brief Returns the tyep of the memory specified
70  */
71 WASM_API_EXTERN wasm_memorytype_t* wasmtime_memory_type(
72     const wasmtime_context_t *store,
73     const wasmtime_memory_t *memory
74 );
75 
76 /**
77  * \brief Returns the base pointer in memory where the linear memory starts.
78  */
79 WASM_API_EXTERN uint8_t *wasmtime_memory_data(
80     const wasmtime_context_t *store,
81     const wasmtime_memory_t *memory
82 );
83 
84 /**
85  * \brief Returns the byte length of this linear memory.
86  */
87 WASM_API_EXTERN size_t wasmtime_memory_data_size(
88     const wasmtime_context_t *store,
89     const wasmtime_memory_t *memory
90 );
91 
92 /**
93  * \brief Returns the length, in WebAssembly pages, of this linear memory
94  */
95 WASM_API_EXTERN uint64_t wasmtime_memory_size(
96     const wasmtime_context_t *store,
97     const wasmtime_memory_t *memory
98 );
99 
100 /**
101  * \brief Attempts to grow the specified memory by `delta` pages.
102  *
103  * \param store the store that owns `memory`
104  * \param memory the memory to grow
105  * \param delta the number of pages to grow by
106  * \param prev_size where to store the previous size of memory
107  *
108  * If memory cannot be grown then `prev_size` is left unchanged and an error is
109  * returned. Otherwise `prev_size` is set to the previous size of the memory, in
110  * WebAssembly pages, and `NULL` is returned.
111  */
112 WASM_API_EXTERN wasmtime_error_t *wasmtime_memory_grow(
113     wasmtime_context_t *store,
114     const wasmtime_memory_t *memory,
115     uint64_t delta,
116     uint64_t *prev_size
117 );
118 
119 #ifdef __cplusplus
120 }  // extern "C"
121 #endif
122 
123 #endif // WASMTIME_MEMORY_H
124