Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp>
  • Loading branch information
Koichi98 committed Jan 19, 2025
1 parent fe8821b commit 5e8fe55
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/agnocast_heaphook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
use libc;

extern "C" {
fn get_publisher_num_borrowed_from_agnocastlib() -> u32;
fn get_publisher_num_borrowed() -> u32;
}

static POINTER_SIZE: LazyLock<usize> = LazyLock::new(std::mem::size_of::<&usize>);
Expand Down Expand Up @@ -241,9 +241,9 @@ thread_local! {
static HOOKED : Cell<bool> = const { Cell::new(false) }
}

fn get_publisher_num_borrowed() -> u32 {
fn get_publisher_num_borrowed_wrapper() -> u32 {
let ret;
unsafe {ret = get_publisher_num_borrowed_from_agnocastlib()}
unsafe {ret = get_publisher_num_borrowed()}
ret
}

Expand All @@ -266,7 +266,7 @@ pub unsafe extern "C" fn __libc_start_main(

#[no_mangle]
pub extern "C" fn malloc(size: usize) -> *mut c_void {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed() == 0 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed_wrapper() == 0 {
return unsafe { ORIGINAL_MALLOC(size) };
}

Expand Down Expand Up @@ -315,7 +315,7 @@ pub extern "C" fn free(ptr: *mut c_void) {

#[no_mangle]
pub extern "C" fn calloc(num: usize, size: usize) -> *mut c_void {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed() == 0 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed_wrapper() == 0 {
return unsafe { ORIGINAL_CALLOC(num, size) };
}

Expand Down Expand Up @@ -346,7 +346,7 @@ pub extern "C" fn realloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {
(None, false)
};

if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed() == 0 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed_wrapper() == 0 {
// In the child processes, ignore the free operation to the shared memory
let realloc_ret: *mut c_void = if !allocated_by_original {
unsafe { ORIGINAL_MALLOC(new_size) }
Expand Down Expand Up @@ -382,7 +382,7 @@ pub extern "C" fn realloc(ptr: *mut c_void, new_size: usize) -> *mut c_void {

#[no_mangle]
pub extern "C" fn posix_memalign(memptr: &mut *mut c_void, alignment: usize, size: usize) -> i32 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed() == 0 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed_wrapper() == 0 {
return unsafe { ORIGINAL_POSIX_MEMALIGN(memptr, alignment, size) };
}

Expand All @@ -400,7 +400,7 @@ pub extern "C" fn posix_memalign(memptr: &mut *mut c_void, alignment: usize, siz

#[no_mangle]
pub extern "C" fn aligned_alloc(alignment: usize, size: usize) -> *mut c_void {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed() == 0 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed_wrapper() == 0 {
return unsafe { ORIGINAL_ALIGNED_ALLOC(alignment, size) };
}

Expand All @@ -418,7 +418,7 @@ pub extern "C" fn aligned_alloc(alignment: usize, size: usize) -> *mut c_void {

#[no_mangle]
pub extern "C" fn memalign(alignment: usize, size: usize) -> *mut c_void {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed() == 0 {
if IS_FORKED_CHILD.load(Ordering::Relaxed) || get_publisher_num_borrowed_wrapper() == 0 {
return unsafe { ORIGINAL_MEMALIGN(alignment, size) };
}

Expand Down
2 changes: 1 addition & 1 deletion src/agnocastlib/include/agnocast_publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace agnocast
{

extern int agnocast_fd;
extern "C" uint32_t get_publisher_num_borrowed_from_agnocastlib();
extern "C" uint32_t get_publisher_num_borrowed();

template <typename MessageT>
class Publisher
Expand Down
4 changes: 1 addition & 3 deletions src/agnocastlib/src/agnocast_publisher.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "agnocast_publisher.hpp"
#include <complex.h>

using namespace agnocast;

thread_local uint32_t publisher_num_borrowed = 0;

extern "C" uint32_t agnocast::get_publisher_num_borrowed_from_agnocastlib(){
extern "C" uint32_t agnocast::get_publisher_num_borrowed(){
return publisher_num_borrowed;
}

Expand Down Expand Up @@ -132,7 +131,6 @@ std::vector<uint64_t> borrow_loaned_message_core(
std::copy_n(
static_cast<const uint64_t *>(ioctl_args.ret_released_addrs), ioctl_args.ret_len,
addresses.begin());

return addresses;
}

Expand Down

0 comments on commit 5e8fe55

Please sign in to comment.