Skip to content

Commit 3660086

Browse files
committed
runtime: memory: use field assignment with default syntax
Use a more idiomatic syntax for create default followed by field assignment. Also cleanup code in the same file.
1 parent d812b4f commit 3660086

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

src/runtime/memory/mod.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,67 +37,65 @@ pub trait DemiMemoryAllocator {
3737
}
3838
}
3939

40-
/// Converts a list of DemiBuffers into a scatter-gather array.
41-
pub fn into_sgarray(bufs: ArrayVec<DemiBuffer, DEMI_SGARRAY_MAXLEN>) -> Result<demi_sgarray_t, Fail> {
42-
// Check the sizes before allocating anything.
43-
if bufs.is_empty() {
44-
let cause = "cannot allocate a zero element scatter-gather array";
45-
error!("into_sgarray(): {}", cause);
46-
return Err(Fail::new(libc::EINVAL, cause));
40+
pub fn into_sgarray(buffers: ArrayVec<DemiBuffer, DEMI_SGARRAY_MAXLEN>) -> Result<demi_sgarray_t, Fail> {
41+
if buffers.is_empty() {
42+
error!("into_sgarray(): buffers is empty");
43+
return Err(Fail::new(libc::EINVAL, "buffers is empty"));
4744
}
4845

49-
if bufs.len() > DEMI_SGARRAY_MAXLEN {
50-
let cause = format!("cannot allocate a {} element scatter-gather array", bufs.len());
51-
error!("into_sgarray(): {}", cause);
52-
return Err(Fail::new(libc::EINVAL, &cause));
46+
if buffers.len() > DEMI_SGARRAY_MAXLEN {
47+
error!(
48+
"into_sgarray(): too many buffers: {}, max: {}",
49+
buffers.len(),
50+
DEMI_SGARRAY_MAXLEN
51+
);
52+
return Err(Fail::new(libc::EINVAL, "too many buffers"));
5353
}
5454

55-
// Create a scatter-gather segment to expose the DemiBuffers to the user.
56-
let mut sga: demi_sgarray_t = demi_sgarray_t::default();
57-
sga.num_segments = bufs.len() as u32;
55+
let mut sga: demi_sgarray_t = demi_sgarray_t {
56+
num_segments: buffers.len() as u32,
57+
..Default::default()
58+
};
5859

59-
for (i, buf) in bufs.into_iter().enumerate() {
60-
sga.segments[i].data_buf_ptr = buf.as_ptr() as *mut c_void;
61-
sga.segments[i].data_len_bytes = buf.len() as u32;
62-
sga.segments[i].reserved_metadata_ptr = buf.into_raw().as_ptr() as *mut c_void;
60+
for (i, buffer) in buffers.into_iter().enumerate() {
61+
sga.segments[i].data_buf_ptr = buffer.as_ptr() as *mut c_void;
62+
sga.segments[i].data_len_bytes = buffer.len() as u32;
63+
sga.segments[i].reserved_metadata_ptr = buffer.into_raw().as_ptr() as *mut c_void;
6364
}
6465

65-
// Create and return a new scatter-gather array (which inherits the DemiBuffer's reference).
6666
Ok(sga)
6767
}
6868

69-
/// Allocates a scatter-gather array.
7069
pub fn sgaalloc<M: DemiMemoryAllocator>(size: usize, mem_alloc: &M) -> Result<demi_sgarray_t, Fail> {
71-
// Check the sizes before allocating anything.
72-
// We can't allocate a zero-sized buffer.
7370
if size == 0 {
74-
let cause = "cannot allocate a zero-sized buffer";
75-
error!("sgaalloc(): {}", cause);
76-
return Err(Fail::new(libc::EINVAL, cause));
71+
error!("sgaalloc(): cannot allocate zero-sized buffer");
72+
return Err(Fail::new(libc::EINVAL, "cannot allocate zero-sized buffer"));
7773
}
7874

7975
// First allocate the underlying DemiBuffer.
8076
if size > mem_alloc.max_buffer_size_bytes() * DEMI_SGARRAY_MAXLEN {
8177
return Err(Fail::new(libc::EINVAL, "size too large for a single demi_sgaseg_t"));
8278
}
79+
8380
// Calculate the number of DemiBuffers to allocate.
8481
let max_buffer_size_bytes: usize = mem_alloc.max_buffer_size_bytes();
8582
let remainder: usize = size % max_buffer_size_bytes;
8683
let len: usize = (size - remainder) / max_buffer_size_bytes;
8784
let mut bufs: ArrayVec<DemiBuffer, DEMI_SGARRAY_MAXLEN> = ArrayVec::new();
85+
8886
for _ in 0..len {
8987
bufs.push(mem_alloc.allocate_demi_buffer(max_buffer_size_bytes)?);
9088
}
89+
9190
// If there is any remaining length, allocate a partial buffer.
9291
if remainder > 0 {
9392
bufs.push(mem_alloc.allocate_demi_buffer(remainder)?);
9493
}
94+
9595
into_sgarray(bufs)
9696
}
9797

98-
/// Releases a scatter-gather array.
9998
pub fn sgafree(sga: demi_sgarray_t) -> Result<(), Fail> {
100-
// Check arguments.
10199
if sga.num_segments > DEMI_SGARRAY_MAXLEN as u32 {
102100
return Err(Fail::new(libc::EINVAL, "demi_sgarray_t has invalid segment count"));
103101
}
@@ -106,13 +104,13 @@ pub fn sgafree(sga: demi_sgarray_t) -> Result<(), Fail> {
106104
let buf: DemiBuffer = convert_sgaseg_to_demi_buffer(&sga.segments[i])?;
107105
drop(buf);
108106
}
107+
109108
Ok(())
110109
}
111110

112-
/// Clones a scatter-gather array. The [sga_buf] field must point to the first DemiBuffer in the chain and the elements
113-
/// of [segments] must be the rest of the chain.
111+
/// The [sga_buf] field must point to the first DemiBuffer in the chain and the elements of [segments] must be the rest
112+
/// of the chain.
114113
pub fn clone_sgarray(sga: &demi_sgarray_t) -> Result<ArrayVec<DemiBuffer, DEMI_SGARRAY_MAXLEN>, Fail> {
115-
// Check arguments.
116114
if sga.num_segments > DEMI_SGARRAY_MAXLEN as u32 || sga.num_segments == 0 {
117115
return Err(Fail::new(libc::EINVAL, "demi_sgarray_t has invalid segment count"));
118116
}

0 commit comments

Comments
 (0)