Skip to content

Commit 59a260e

Browse files
author
Jonathan Pallant (42 Technology)
committed
Fix memory allocations.
Now we can start the modem and bring up multiple sockets.
1 parent f19e085 commit 59a260e

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/ffi.rs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,25 @@ pub extern "C" fn nrf_modem_irrecoverable_error_handler(err: u32) -> ! {
146146
/// can be located anywhere in the application core's RAM instead of the shared
147147
/// memory regions. This function allocates dynamic memory for the library.
148148
#[no_mangle]
149-
pub extern "C" fn nrf_modem_os_alloc(num_bytes: usize) -> *mut u8 {
150-
debug!("nrf_modem_os_alloc({})", num_bytes);
149+
pub extern "C" fn nrf_modem_os_alloc(num_bytes_requested: usize) -> *mut u8 {
151150
let usize_size = core::mem::size_of::<usize>();
152151
let mut result = core::ptr::null_mut();
153152
unsafe {
154153
cortex_m::interrupt::free(|cs| {
154+
let num_bytes_allocated = num_bytes_requested + usize_size;
155155
let layout =
156-
core::alloc::Layout::from_size_align_unchecked(num_bytes + usize_size, usize_size);
156+
core::alloc::Layout::from_size_align_unchecked(num_bytes_allocated, usize_size);
157157
if let Some(ref mut allocator) = *crate::GENERIC_ALLOCATOR.borrow(cs).borrow_mut() {
158158
match allocator.allocate_first_fit(layout) {
159-
Ok(block) => {
159+
Ok(real_block) => {
160+
let real_ptr = real_block.as_ptr();
160161
// We need the block size to run the de-allocation. Store it in the first four bytes.
161-
core::ptr::write_volatile::<usize>(block.as_ptr() as *mut usize, num_bytes);
162+
core::ptr::write_volatile::<usize>(
163+
real_ptr as *mut usize,
164+
num_bytes_allocated,
165+
);
162166
// Give them the rest of the block
163-
result = block.as_ptr().offset(usize_size as isize);
167+
result = real_ptr.offset(usize_size as isize);
164168
}
165169
Err(_e) => {
166170
// Ignore
@@ -180,17 +184,18 @@ pub extern "C" fn nrf_modem_os_alloc(num_bytes: usize) -> *mut u8 {
180184
/// memory regions. This function allocates dynamic memory for the library.
181185
#[no_mangle]
182186
pub extern "C" fn nrf_modem_os_free(ptr: *mut u8) {
183-
debug!("nrf_modem_os_free({:?})", ptr);
184187
let usize_size = core::mem::size_of::<usize>() as isize;
185188
unsafe {
186189
cortex_m::interrupt::free(|cs| {
187190
// Fetch the size from the previous four bytes
188191
let real_ptr = ptr.offset(-usize_size);
189-
let num_bytes = core::ptr::read_volatile::<usize>(real_ptr as *const usize);
190-
let layout =
191-
core::alloc::Layout::from_size_align_unchecked(num_bytes, usize_size as usize);
192+
let num_bytes_allocated = core::ptr::read_volatile::<usize>(real_ptr as *const usize);
193+
let layout = core::alloc::Layout::from_size_align_unchecked(
194+
num_bytes_allocated,
195+
usize_size as usize,
196+
);
192197
if let Some(ref mut allocator) = *crate::GENERIC_ALLOCATOR.borrow(cs).borrow_mut() {
193-
allocator.deallocate(core::ptr::NonNull::new_unchecked(ptr), layout);
198+
allocator.deallocate(core::ptr::NonNull::new_unchecked(real_ptr), layout);
194199
}
195200
});
196201
}
@@ -201,21 +206,25 @@ pub extern "C" fn nrf_modem_os_free(ptr: *mut u8) {
201206
/// @param bytes Buffer size.
202207
/// @return pointer to allocated memory
203208
#[no_mangle]
204-
pub extern "C" fn nrf_modem_os_shm_tx_alloc(num_bytes: usize) -> *mut u8 {
205-
debug!("nrf_modem_os_shm_tx_alloc({})", num_bytes);
209+
pub extern "C" fn nrf_modem_os_shm_tx_alloc(num_bytes_requested: usize) -> *mut u8 {
206210
let usize_size = core::mem::size_of::<usize>();
207211
let mut result = core::ptr::null_mut();
208212
unsafe {
209213
cortex_m::interrupt::free(|cs| {
214+
let num_bytes_allocated = num_bytes_requested + usize_size;
210215
let layout =
211-
core::alloc::Layout::from_size_align_unchecked(num_bytes + usize_size, usize_size);
216+
core::alloc::Layout::from_size_align_unchecked(num_bytes_allocated, usize_size);
212217
if let Some(ref mut allocator) = *crate::TX_ALLOCATOR.borrow(cs).borrow_mut() {
213218
match allocator.allocate_first_fit(layout) {
214-
Ok(block) => {
219+
Ok(real_block) => {
220+
let real_ptr = real_block.as_ptr();
215221
// We need the block size to run the de-allocation. Store it in the first four bytes.
216-
core::ptr::write_volatile::<usize>(block.as_ptr() as *mut usize, num_bytes);
222+
core::ptr::write_volatile::<usize>(
223+
real_ptr as *mut usize,
224+
num_bytes_allocated,
225+
);
217226
// Give them the rest of the block
218-
result = block.as_ptr().offset(usize_size as isize);
227+
result = real_ptr.offset(usize_size as isize);
219228
}
220229
Err(_e) => {
221230
// Ignore
@@ -232,17 +241,18 @@ pub extern "C" fn nrf_modem_os_shm_tx_alloc(num_bytes: usize) -> *mut u8 {
232241
/// @param ptr Th buffer to free.
233242
#[no_mangle]
234243
pub extern "C" fn nrf_modem_os_shm_tx_free(ptr: *mut u8) {
235-
debug!("nrf_modem_os_shm_tx_free({:?})", ptr);
236244
let usize_size = core::mem::size_of::<usize>() as isize;
237245
unsafe {
238246
cortex_m::interrupt::free(|cs| {
239247
// Fetch the size from the previous four bytes
240248
let real_ptr = ptr.offset(-usize_size);
241-
let num_bytes = core::ptr::read_volatile::<usize>(real_ptr as *const usize);
242-
let layout =
243-
core::alloc::Layout::from_size_align_unchecked(num_bytes, usize_size as usize);
249+
let num_bytes_allocated = core::ptr::read_volatile::<usize>(real_ptr as *const usize);
250+
let layout = core::alloc::Layout::from_size_align_unchecked(
251+
num_bytes_allocated,
252+
usize_size as usize,
253+
);
244254
if let Some(ref mut allocator) = *crate::TX_ALLOCATOR.borrow(cs).borrow_mut() {
245-
allocator.deallocate(core::ptr::NonNull::new_unchecked(ptr), layout);
255+
allocator.deallocate(core::ptr::NonNull::new_unchecked(real_ptr), layout);
246256
}
247257
});
248258
}

0 commit comments

Comments
 (0)