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/error.h>
12 #include <wasmtime/extern.h>
13 #include <wasmtime/store.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 *
26 wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max,
27                         bool is_64, bool shared);
28 
29 /**
30  * \brief Returns the minimum size, in pages, of the specified memory type.
31  *
32  * Note that this function is preferred over #wasm_memorytype_limits for
33  * compatibility with the memory64 proposal.
34  */
35 WASM_API_EXTERN uint64_t
36 wasmtime_memorytype_minimum(const wasm_memorytype_t *ty);
37 
38 /**
39  * \brief Returns the maximum size, in pages, of the specified memory type.
40  *
41  * If this memory type doesn't have a maximum size listed then `false` is
42  * returned. Otherwise `true` is returned and the `max` pointer is filled in
43  * with the specified maximum size, in pages.
44  *
45  * Note that this function is preferred over #wasm_memorytype_limits for
46  * compatibility with the memory64 proposal.
47  */
48 WASM_API_EXTERN bool wasmtime_memorytype_maximum(const wasm_memorytype_t *ty,
49                                                  uint64_t *max);
50 
51 /**
52  * \brief Returns whether this type of memory represents a 64-bit memory.
53  */
54 WASM_API_EXTERN bool wasmtime_memorytype_is64(const wasm_memorytype_t *ty);
55 
56 /**
57  * \brief Returns whether this type of memory represents a shared memory.
58  */
59 WASM_API_EXTERN bool wasmtime_memorytype_isshared(const wasm_memorytype_t *ty);
60 
61 /**
62  * \brief Creates a new WebAssembly linear memory
63  *
64  * \param store the store to create the memory within
65  * \param ty the type of the memory to create
66  * \param ret where to store the returned memory
67  *
68  * If an error happens when creating the memory it's returned and owned by the
69  * caller. If an error happens then `ret` is not filled in.
70  */
71 WASM_API_EXTERN wasmtime_error_t *
72 wasmtime_memory_new(wasmtime_context_t *store, const wasm_memorytype_t *ty,
73                     wasmtime_memory_t *ret);
74 
75 /**
76  * \brief Returns the type of the memory specified
77  */
78 WASM_API_EXTERN wasm_memorytype_t *
79 wasmtime_memory_type(const wasmtime_context_t *store,
80                      const wasmtime_memory_t *memory);
81 
82 /**
83  * \brief Returns the base pointer in memory where the linear memory starts.
84  */
85 WASM_API_EXTERN uint8_t *wasmtime_memory_data(const wasmtime_context_t *store,
86                                               const wasmtime_memory_t *memory);
87 
88 /**
89  * \brief Returns the byte length of this linear memory.
90  */
91 WASM_API_EXTERN size_t wasmtime_memory_data_size(
92     const wasmtime_context_t *store, const wasmtime_memory_t *memory);
93 
94 /**
95  * \brief Returns the length, in WebAssembly pages, of this linear memory
96  */
97 WASM_API_EXTERN uint64_t wasmtime_memory_size(const wasmtime_context_t *store,
98                                               const wasmtime_memory_t *memory);
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 *
113 wasmtime_memory_grow(wasmtime_context_t *store, const wasmtime_memory_t *memory,
114                      uint64_t delta, uint64_t *prev_size);
115 
116 #ifdef __cplusplus
117 } // extern "C"
118 #endif
119 
120 #endif // WASMTIME_MEMORY_H
121