Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-aptos committed Nov 12, 2024

Verified

This commit was signed with the committer’s verified signature.
KyleFromNVIDIA Kyle Edwards
1 parent a99eccd commit c95646e
Showing 6 changed files with 20 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aptos-move/framework/move-stdlib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ publish = false
[dependencies]
aptos-gas-schedule = { workspace = true }
aptos-native-interface = { workspace = true }
aptos-types = { workspace = true }
move-core-types = { path = "../../../third_party/move/move-core/types" }
move-vm-runtime = { path = "../../../third_party/move/move-vm/runtime" }
move-vm-types = { path = "../../../third_party/move/move-vm/types" }
5 changes: 2 additions & 3 deletions aptos-move/framework/move-stdlib/doc/mem.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
# Module `0x1::mem`

Module with methods for safe memory manipulation.
I.e. swapping/replacing non-copyable/non-droppable types.


- [Function `swap`](#0x1_mem_swap)
@@ -45,8 +44,8 @@ Swap contents of two passed mutable references.

## Function `replace`

Replace value reference points to with the given new value,
and return value it had before.
Replace the value reference points to with the given new value,
and return the value it had before.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="mem.md#0x1_mem_replace">replace</a>&lt;T&gt;(ref: &<b>mut</b> T, new: T): T
5 changes: 2 additions & 3 deletions aptos-move/framework/move-stdlib/sources/mem.move
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Module with methods for safe memory manipulation.
/// I.e. swapping/replacing non-copyable/non-droppable types.
module std::mem {
// TODO - functions here are `public(friend)` here for one release,
// and to be changed to `public` one release later.
@@ -9,8 +8,8 @@ module std::mem {
/// Swap contents of two passed mutable references.
public(friend) native fun swap<T>(left: &mut T, right: &mut T);

/// Replace value reference points to with the given new value,
/// and return value it had before.
/// Replace the value reference points to with the given new value,
/// and return the value it had before.
public(friend) fun replace<T>(ref: &mut T, new: T): T {
swap(ref, &mut new);
new
32 changes: 8 additions & 24 deletions aptos-move/framework/move-stdlib/src/natives/mem.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

//! Implementation of native functions for memory manipulation.
use aptos_gas_schedule::gas_params::natives::move_stdlib::MEM_SWAP_BASE;
use aptos_native_interface::{
safely_pop_arg, RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeError,
SafeNativeResult,
};
use move_core_types::vm_status::StatusCode;
use aptos_types::error;
use move_vm_runtime::native_functions::NativeFunction;
use move_vm_types::{
loaded_data::runtime_types::Type,
natives::function::PartialVMError,
values::{Reference, Value},
};
use smallvec::{smallvec, SmallVec};
use std::collections::VecDeque;

/// The feature is not enabled.
/// (0xD is unavailable)
pub const EFEATURE_NOT_ENABLED: u64 = 0x0D_0001;

pub fn get_feature_not_available_error() -> SafeNativeError {
SafeNativeError::Abort {
abort_code: EFEATURE_NOT_ENABLED,
}
}
pub const EFEATURE_NOT_ENABLED: u64 = 1;

/***************************************************************************************************
* native fun native_swap
@@ -47,23 +35,19 @@ fn native_swap(
.get_feature_flags()
.is_native_memory_operations_enabled()
{
return Err(get_feature_not_available_error());
return Err(SafeNativeError::Abort {
abort_code: error::unavailable(EFEATURE_NOT_ENABLED),
});
}

debug_assert!(args.len() == 2);

if args.len() != 2 {
return Err(SafeNativeError::InvariantViolation(PartialVMError::new(
StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR,
)));
}

context.charge(MEM_SWAP_BASE)?;

let ref1 = safely_pop_arg!(args, Reference);
let ref0 = safely_pop_arg!(args, Reference);
let left = safely_pop_arg!(args, Reference);
let right = safely_pop_arg!(args, Reference);

ref0.swap_values(ref1)?;
left.swap_values(right)?;

Ok(smallvec![])
}
8 changes: 6 additions & 2 deletions third_party/move/move-vm/types/src/values/values_impl.rs
Original file line number Diff line number Diff line change
@@ -939,11 +939,15 @@ impl_vm_value_from_primitive!(AccountAddress, Address);
*
*************************************************************************************/
impl Container {
/// Swaps contents of two mutable references.
///
/// Precondition for this funciton is that `self` and `other` are required to be
/// distinct references.
/// Move will guarantee that invariant, because it prevents from having two
/// mutable references to the same value.
fn swap_contents(&self, other: &Self) -> PartialVMResult<()> {
use Container::*;

// TODO: check if unique ownership?

match (self, other) {
(Vec(l), Vec(r)) => mem::swap(&mut *l.borrow_mut(), &mut *r.borrow_mut()),
(Struct(l), Struct(r)) => mem::swap(&mut *l.borrow_mut(), &mut *r.borrow_mut()),

0 comments on commit c95646e

Please sign in to comment.