17a1b7cdfSAlex Crichton /**
27a1b7cdfSAlex Crichton  * \file wasmtime/memory.h
37a1b7cdfSAlex Crichton  *
47a1b7cdfSAlex Crichton  * Wasmtime API for interacting with wasm memories.
57a1b7cdfSAlex Crichton  */
67a1b7cdfSAlex Crichton 
77a1b7cdfSAlex Crichton #ifndef WASMTIME_MEMORY_H
87a1b7cdfSAlex Crichton #define WASMTIME_MEMORY_H
97a1b7cdfSAlex Crichton 
107a1b7cdfSAlex Crichton #include <wasm.h>
11f8fee938STyler Rockwood #include <wasmtime/error.h>
127a1b7cdfSAlex Crichton #include <wasmtime/extern.h>
137a1b7cdfSAlex Crichton #include <wasmtime/store.h>
147a1b7cdfSAlex Crichton 
157a1b7cdfSAlex Crichton #ifdef __cplusplus
167a1b7cdfSAlex Crichton extern "C" {
177a1b7cdfSAlex Crichton #endif
187a1b7cdfSAlex Crichton 
197a1b7cdfSAlex Crichton /**
20e68aa995SAlex Crichton  * \brief Creates a new memory type from the specified parameters.
21e68aa995SAlex Crichton  *
22e68aa995SAlex Crichton  * Note that this function is preferred over #wasm_memorytype_new for
23e68aa995SAlex Crichton  * compatibility with the memory64 proposal.
24e68aa995SAlex Crichton  */
25*7948e0ffSNick Fitzgerald WASM_API_EXTERN wasmtime_error_t *
26288c1513SJuan Gomez wasmtime_memorytype_new(uint64_t min, bool max_present, uint64_t max,
27*7948e0ffSNick Fitzgerald                         bool is_64, bool shared, uint8_t page_size_log2,
28*7948e0ffSNick Fitzgerald                         wasm_memorytype_t **ret);
29e68aa995SAlex Crichton 
30e68aa995SAlex Crichton /**
31e68aa995SAlex Crichton  * \brief Returns the minimum size, in pages, of the specified memory type.
32e68aa995SAlex Crichton  *
33e68aa995SAlex Crichton  * Note that this function is preferred over #wasm_memorytype_limits for
34e68aa995SAlex Crichton  * compatibility with the memory64 proposal.
35e68aa995SAlex Crichton  */
36f8fee938STyler Rockwood WASM_API_EXTERN uint64_t
37f8fee938STyler Rockwood wasmtime_memorytype_minimum(const wasm_memorytype_t *ty);
38e68aa995SAlex Crichton 
39e68aa995SAlex Crichton /**
40e68aa995SAlex Crichton  * \brief Returns the maximum size, in pages, of the specified memory type.
41e68aa995SAlex Crichton  *
42e68aa995SAlex Crichton  * If this memory type doesn't have a maximum size listed then `false` is
43e68aa995SAlex Crichton  * returned. Otherwise `true` is returned and the `max` pointer is filled in
44e68aa995SAlex Crichton  * with the specified maximum size, in pages.
45e68aa995SAlex Crichton  *
46e68aa995SAlex Crichton  * Note that this function is preferred over #wasm_memorytype_limits for
47e68aa995SAlex Crichton  * compatibility with the memory64 proposal.
48e68aa995SAlex Crichton  */
49f8fee938STyler Rockwood WASM_API_EXTERN bool wasmtime_memorytype_maximum(const wasm_memorytype_t *ty,
50f8fee938STyler Rockwood                                                  uint64_t *max);
51e68aa995SAlex Crichton 
52e68aa995SAlex Crichton /**
53e68aa995SAlex Crichton  * \brief Returns whether this type of memory represents a 64-bit memory.
54e68aa995SAlex Crichton  */
55e68aa995SAlex Crichton WASM_API_EXTERN bool wasmtime_memorytype_is64(const wasm_memorytype_t *ty);
56e68aa995SAlex Crichton 
57e68aa995SAlex Crichton /**
5855bd797aSMilek7  * \brief Returns whether this type of memory represents a shared memory.
5955bd797aSMilek7  */
6055bd797aSMilek7 WASM_API_EXTERN bool wasmtime_memorytype_isshared(const wasm_memorytype_t *ty);
6155bd797aSMilek7 
6255bd797aSMilek7 /**
63*7948e0ffSNick Fitzgerald  * \brief Returns the page size, in bytes, of this memory type.
64*7948e0ffSNick Fitzgerald  */
65*7948e0ffSNick Fitzgerald WASM_API_EXTERN uint64_t
66*7948e0ffSNick Fitzgerald wasmtime_memorytype_page_size(const wasm_memorytype_t *ty);
67*7948e0ffSNick Fitzgerald 
68*7948e0ffSNick Fitzgerald /**
69*7948e0ffSNick Fitzgerald  * \brief Returns the log2 of this memory type's page size, in bytes.
70*7948e0ffSNick Fitzgerald  */
71*7948e0ffSNick Fitzgerald WASM_API_EXTERN uint8_t
72*7948e0ffSNick Fitzgerald wasmtime_memorytype_page_size_log2(const wasm_memorytype_t *ty);
73*7948e0ffSNick Fitzgerald 
74*7948e0ffSNick Fitzgerald /**
757a1b7cdfSAlex Crichton  * \brief Creates a new WebAssembly linear memory
767a1b7cdfSAlex Crichton  *
777a1b7cdfSAlex Crichton  * \param store the store to create the memory within
787a1b7cdfSAlex Crichton  * \param ty the type of the memory to create
797a1b7cdfSAlex Crichton  * \param ret where to store the returned memory
807a1b7cdfSAlex Crichton  *
817a1b7cdfSAlex Crichton  * If an error happens when creating the memory it's returned and owned by the
827a1b7cdfSAlex Crichton  * caller. If an error happens then `ret` is not filled in.
837a1b7cdfSAlex Crichton  */
84f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *
85f8fee938STyler Rockwood wasmtime_memory_new(wasmtime_context_t *store, const wasm_memorytype_t *ty,
86f8fee938STyler Rockwood                     wasmtime_memory_t *ret);
877a1b7cdfSAlex Crichton 
887a1b7cdfSAlex Crichton /**
89cb7d953aSLukas Forst  * \brief Returns the type of the memory specified
907a1b7cdfSAlex Crichton  */
91f8fee938STyler Rockwood WASM_API_EXTERN wasm_memorytype_t *
92f8fee938STyler Rockwood wasmtime_memory_type(const wasmtime_context_t *store,
93f8fee938STyler Rockwood                      const wasmtime_memory_t *memory);
947a1b7cdfSAlex Crichton 
957a1b7cdfSAlex Crichton /**
967a1b7cdfSAlex Crichton  * \brief Returns the base pointer in memory where the linear memory starts.
977a1b7cdfSAlex Crichton  */
98f8fee938STyler Rockwood WASM_API_EXTERN uint8_t *wasmtime_memory_data(const wasmtime_context_t *store,
99f8fee938STyler Rockwood                                               const wasmtime_memory_t *memory);
1007a1b7cdfSAlex Crichton 
1017a1b7cdfSAlex Crichton /**
1027a1b7cdfSAlex Crichton  * \brief Returns the byte length of this linear memory.
1037a1b7cdfSAlex Crichton  */
1047a1b7cdfSAlex Crichton WASM_API_EXTERN size_t wasmtime_memory_data_size(
105f8fee938STyler Rockwood     const wasmtime_context_t *store, const wasmtime_memory_t *memory);
1067a1b7cdfSAlex Crichton 
1077a1b7cdfSAlex Crichton /**
1087a1b7cdfSAlex Crichton  * \brief Returns the length, in WebAssembly pages, of this linear memory
1097a1b7cdfSAlex Crichton  */
110f8fee938STyler Rockwood WASM_API_EXTERN uint64_t wasmtime_memory_size(const wasmtime_context_t *store,
111f8fee938STyler Rockwood                                               const wasmtime_memory_t *memory);
1127a1b7cdfSAlex Crichton 
1137a1b7cdfSAlex Crichton /**
1147a1b7cdfSAlex Crichton  * \brief Attempts to grow the specified memory by `delta` pages.
1157a1b7cdfSAlex Crichton  *
1167a1b7cdfSAlex Crichton  * \param store the store that owns `memory`
1177a1b7cdfSAlex Crichton  * \param memory the memory to grow
1187a1b7cdfSAlex Crichton  * \param delta the number of pages to grow by
1197a1b7cdfSAlex Crichton  * \param prev_size where to store the previous size of memory
1207a1b7cdfSAlex Crichton  *
1217a1b7cdfSAlex Crichton  * If memory cannot be grown then `prev_size` is left unchanged and an error is
1227a1b7cdfSAlex Crichton  * returned. Otherwise `prev_size` is set to the previous size of the memory, in
1237a1b7cdfSAlex Crichton  * WebAssembly pages, and `NULL` is returned.
1247a1b7cdfSAlex Crichton  */
125f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *
126f8fee938STyler Rockwood wasmtime_memory_grow(wasmtime_context_t *store, const wasmtime_memory_t *memory,
127f8fee938STyler Rockwood                      uint64_t delta, uint64_t *prev_size);
1287a1b7cdfSAlex Crichton 
129*7948e0ffSNick Fitzgerald /**
130*7948e0ffSNick Fitzgerald  * \brief Returns the size of a page, in bytes, for this memory.
131*7948e0ffSNick Fitzgerald  *
132*7948e0ffSNick Fitzgerald  * \param store the store that owns `memory`
133*7948e0ffSNick Fitzgerald  * \param memory the memory to get the page size of
134*7948e0ffSNick Fitzgerald  *
135*7948e0ffSNick Fitzgerald  * WebAssembly memories are made up of a whole number of pages, so the byte size
136*7948e0ffSNick Fitzgerald  * (as returned by #wasmtime_memory_data_size) will always be a multiple of
137*7948e0ffSNick Fitzgerald  * their page size. Different Wasm memories may have different page sizes.
138*7948e0ffSNick Fitzgerald  *
139*7948e0ffSNick Fitzgerald  * By default this is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or `65536`)
140*7948e0ffSNick Fitzgerald  * but [the custom-page-sizes proposal] allows opting into a page size of
141*7948e0ffSNick Fitzgerald  * `1`. Future extensions might allow any power of two as a page size.
142*7948e0ffSNick Fitzgerald  *
143*7948e0ffSNick Fitzgerald  * [the custom-page-sizes proposal]:
144*7948e0ffSNick Fitzgerald  * https://github.com/WebAssembly/custom-page-sizes
145*7948e0ffSNick Fitzgerald  */
146*7948e0ffSNick Fitzgerald WASM_API_EXTERN uint64_t wasmtime_memory_page_size(
147*7948e0ffSNick Fitzgerald     wasmtime_context_t *store, const wasmtime_memory_t *memory);
148*7948e0ffSNick Fitzgerald 
149*7948e0ffSNick Fitzgerald /**
150*7948e0ffSNick Fitzgerald  * \brief Returns the log2 of this memory's page size, in bytes.
151*7948e0ffSNick Fitzgerald  *
152*7948e0ffSNick Fitzgerald  * \param store the store that owns `memory`
153*7948e0ffSNick Fitzgerald  * \param memory the memory to get the page size of
154*7948e0ffSNick Fitzgerald  *
155*7948e0ffSNick Fitzgerald  * WebAssembly memories are made up of a whole number of pages, so the byte size
156*7948e0ffSNick Fitzgerald  * (as returned by #wasmtime_memory_data_size) will always be a multiple of
157*7948e0ffSNick Fitzgerald  * their page size. Different Wasm memories may have different page sizes.
158*7948e0ffSNick Fitzgerald  *
159*7948e0ffSNick Fitzgerald  * By default the page size is 64KiB (aka `0x10000`, `2**16`, `1<<16`, or
160*7948e0ffSNick Fitzgerald  * `65536`) but [the custom-page-sizes proposal] allows opting into a page
161*7948e0ffSNick Fitzgerald  * size of `1`. Future extensions might allow any power of two as a page
162*7948e0ffSNick Fitzgerald  * size.
163*7948e0ffSNick Fitzgerald  *
164*7948e0ffSNick Fitzgerald  * [the custom-page-sizes proposal]:
165*7948e0ffSNick Fitzgerald  * https://github.com/WebAssembly/custom-page-sizes
166*7948e0ffSNick Fitzgerald  */
167*7948e0ffSNick Fitzgerald WASM_API_EXTERN uint8_t wasmtime_memory_page_size_log2(
168*7948e0ffSNick Fitzgerald     wasmtime_context_t *store, const wasmtime_memory_t *memory);
169*7948e0ffSNick Fitzgerald 
1707a1b7cdfSAlex Crichton #ifdef __cplusplus
1717a1b7cdfSAlex Crichton } // extern "C"
1727a1b7cdfSAlex Crichton #endif
1737a1b7cdfSAlex Crichton 
1747a1b7cdfSAlex Crichton #endif // WASMTIME_MEMORY_H
175