MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / realloc

Method realloc

crates/wasmtime/src/runtime/component/func/options.rs:140–189  ·  view source on GitHub ↗

Invokes the memory allocation function (which is style after `realloc`) with the specified parameters. # Panics This will panic if realloc hasn't been configured for this lowering via its canonical options.

(
        &mut self,
        old: usize,
        old_size: usize,
        old_align: u32,
        new_size: usize,
    )

Source from the content-addressed store, hash-verified

138 /// This will panic if realloc hasn't been configured for this lowering via
139 /// its canonical options.
140 pub fn realloc(
141 &mut self,
142 old: usize,
143 old_size: usize,
144 old_align: u32,
145 new_size: usize,
146 ) -> Result<usize> {
147 assert!(self.allow_realloc);
148
149 let (component, store) = self.instance.component_and_store_mut(self.store.0);
150 let instance = self.instance.id().get(store);
151 let options = &component.env_component().options[self.options];
152 let realloc_ty = component.realloc_func_ty();
153 let realloc = match options.data_model {
154 CanonicalOptionsDataModel::Gc {} => unreachable!(),
155 CanonicalOptionsDataModel::LinearMemory(m) => m.realloc.unwrap(),
156 };
157 let realloc = instance.runtime_realloc(realloc);
158
159 let params = (
160 u32::try_from(old)?,
161 u32::try_from(old_size)?,
162 old_align,
163 u32::try_from(new_size)?,
164 );
165
166 type ReallocFunc = crate::TypedFunc<(u32, u32, u32, u32), u32>;
167
168 // Invoke the wasm malloc function using its raw and statically known
169 // signature.
170 let result = unsafe {
171 ReallocFunc::call_raw(&mut StoreContextMut(store), &realloc_ty, realloc, params)?
172 };
173
174 if result % old_align != 0 {
175 bail!("realloc return: result not aligned");
176 }
177 let result = usize::try_from(result)?;
178
179 if self
180 .as_slice_mut()
181 .get_mut(result..)
182 .and_then(|s| s.get_mut(..new_size))
183 .is_none()
184 {
185 bail!("realloc return: beyond end of memory")
186 }
187
188 Ok(result)
189 }
190
191 /// Returns a fixed mutable slice of memory `N` bytes large starting at
192 /// offset `N`, panicking on out-of-bounds.

Callers 7

store_argsMethod · 0.45
lower_listFunction · 0.45
lower_mapFunction · 0.45
lower_heap_argsMethod · 0.45
lower_stringFunction · 0.45
lower_listFunction · 0.45
lower_map_iterFunction · 0.45

Calls 12

StoreContextMutClass · 0.85
OkFunction · 0.85
env_componentMethod · 0.80
realloc_func_tyMethod · 0.80
getMethod · 0.45
idMethod · 0.45
unwrapMethod · 0.45
runtime_reallocMethod · 0.45
is_noneMethod · 0.45
get_mutMethod · 0.45
as_slice_mutMethod · 0.45

Tested by

no test coverage detected