Skip to content

Commit

Permalink
refactor: smart pointer usage
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Aponte <[email protected]>
  • Loading branch information
federico-sysdig authored and poiana committed May 6, 2024
1 parent 44cb8fe commit 87a7481
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 116 deletions.
38 changes: 19 additions & 19 deletions test/drivers/test_suites/syscall_exit_suite/fork_x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ TEST(SyscallExit, forkX_father)

TEST(SyscallExit, forkX_child)
{
auto evt_test = new event_test(__NR_fork, EXIT_EVENT);
event_test evt_test(__NR_fork, EXIT_EVENT);

evt_test->enable_capture();
evt_test.enable_capture();

/*=============================== TRIGGER SYSCALL ===========================*/

Expand Down Expand Up @@ -166,69 +166,69 @@ TEST(SyscallExit, forkX_child)

/*=============================== TRIGGER SYSCALL ===========================*/

evt_test->disable_capture();
evt_test.disable_capture();

/* In some architectures we are not able to catch the `clone exit child
* event` from the `sys_exit` tracepoint. This is because there is no
* default behavior among different architectures... you can find more
* info in `driver/feature_gates.h`.
*/
#ifdef CAPTURE_SCHED_PROC_FORK
evt_test->assert_event_absence(ret_pid);
evt_test.assert_event_absence(ret_pid);
#else
evt_test->assert_event_presence(ret_pid);
evt_test.assert_event_presence(ret_pid);

if(HasFatalFailure())
{
return;
}

evt_test->parse_event();
evt_test.parse_event();

evt_test->assert_header();
evt_test.assert_header();

/*=============================== ASSERT PARAMETERS ===========================*/

/* Parameter 1: res (type: PT_PID)*/
evt_test->assert_numeric_param(1, (int64_t)0);
evt_test.assert_numeric_param(1, (int64_t)0);

/* Parameter 2: exe (type: PT_CHARBUF) */
#ifndef __powerpc64__ // Page fault
evt_test->assert_charbuf_param(2, info.args[0]);
evt_test.assert_charbuf_param(2, info.args[0]);

/* Parameter 3: args (type: PT_CHARBUFARRAY) */
/* Starting from `1` because the first is `exe`. */
evt_test->assert_charbuf_array_param(3, &info.args[1]);
evt_test.assert_charbuf_array_param(3, &info.args[1]);
#endif
/* Parameter 4: tid (type: PT_PID) */
evt_test->assert_numeric_param(4, (int64_t)ret_pid);
evt_test.assert_numeric_param(4, (int64_t)ret_pid);

/* Parameter 5: pid (type: PT_PID) */
/* We are the main thread of the process so it's equal to `tid`. */
evt_test->assert_numeric_param(5, (int64_t)ret_pid);
evt_test.assert_numeric_param(5, (int64_t)ret_pid);

/* Parameter 6: ptid (type: PT_PID) */
evt_test->assert_numeric_param(6, (int64_t)::getpid());
evt_test.assert_numeric_param(6, (int64_t)::getpid());

/* Parameter 7: cwd (type: PT_CHARBUF) */
/* leave the current working directory empty like in the old probe. */
evt_test->assert_empty_param(7);
evt_test.assert_empty_param(7);

/* Parameter 14: comm (type: PT_CHARBUF) */
evt_test->assert_charbuf_param(14, TEST_EXECUTABLE_NAME);
evt_test.assert_charbuf_param(14, TEST_EXECUTABLE_NAME);

/* Parameter 15: cgroups (type: PT_CHARBUFARRAY) */
evt_test->assert_cgroup_param(15);
evt_test.assert_cgroup_param(15);

/* Parameter 16: flags (type: PT_FLAGS32) */
evt_test->assert_numeric_param(16, (uint32_t)0);
evt_test.assert_numeric_param(16, (uint32_t)0);

/* Parameter 21: pid_namespace init task start_time monotonic time in ns (type: PT_UINT64) */
evt_test->assert_numeric_param(21, (uint64_t)0, GREATER_EQUAL);
evt_test.assert_numeric_param(21, (uint64_t)0, GREATER_EQUAL);

/*=============================== ASSERT PARAMETERS ===========================*/

evt_test->assert_num_params_pushed(21);
evt_test.assert_num_params_pushed(21);

#endif
}
Expand Down
6 changes: 3 additions & 3 deletions userspace/libsinsp/container_engine/docker/podman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ bool podman::resolve(sinsp_threadinfo *tinfo, bool query_os_for_missing_info)
{
std::string container_id, api_sock;

if(m_api_sock_can_exist == nullptr)
if(!m_api_sock_can_exist.has_value())
{
m_api_sock_can_exist.reset(new bool(can_api_sock_exist()));
m_api_sock_can_exist = can_api_sock_exist();
}

if(! (*(m_api_sock_can_exist.get())))
if(!m_api_sock_can_exist.value())
{
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion userspace/libsinsp/container_engine/docker/podman.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <libsinsp/container_engine/docker/base.h>

#include <optional>

namespace libsinsp {
namespace container_engine {

Expand All @@ -16,7 +18,7 @@ class podman : public docker_base

// true if any file matching any possible api socket pattern
// exists. Is set at the first call to resolve()
std::unique_ptr<bool> m_api_sock_can_exist;
std::optional<bool> m_api_sock_can_exist;

// Return true if any possible api socket pattern exists.
bool can_api_sock_exist();
Expand Down
8 changes: 3 additions & 5 deletions userspace/libsinsp/fdinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ class SINSP_PUBLIC sinsp_fdinfo

virtual std::unique_ptr<sinsp_fdinfo> clone() const
{
auto ret = std::make_unique<sinsp_fdinfo>();
*(ret.get()) = *this;
return ret;
return std::make_unique<sinsp_fdinfo>(*this);
}

/*!
Expand Down Expand Up @@ -462,7 +460,7 @@ class sinsp_fdtable
{
for(auto it = m_table.begin(); it != m_table.end(); ++it)
{
if (!callback(it->first, *(it->second.get())))
if (!callback(it->first, *it->second))
{
return false;
}
Expand All @@ -474,7 +472,7 @@ class sinsp_fdtable
{
for(auto it = m_table.begin(); it != m_table.end(); ++it)
{
if (!callback(it->first, *(it->second.get())))
if (!callback(it->first, *it->second))
{
return false;
}
Expand Down
17 changes: 6 additions & 11 deletions userspace/libsinsp/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,30 +192,25 @@ void sinsp_filter::add_check(std::unique_ptr<sinsp_filter_check> chk)
sinsp_filter_compiler::sinsp_filter_compiler(
sinsp* inspector,
const std::string& fltstr)
: m_flt_str(fltstr),
m_factory(std::make_shared<sinsp_filter_factory>(inspector, m_default_filterlist))
{
m_factory.reset(new sinsp_filter_factory(inspector, m_default_filterlist));
m_filter = NULL;
m_flt_str = fltstr;
m_flt_ast = NULL;
}

sinsp_filter_compiler::sinsp_filter_compiler(
std::shared_ptr<sinsp_filter_factory> factory,
const std::string& fltstr)
: m_flt_str(fltstr),
m_factory(factory)
{
m_factory = factory;
m_filter = NULL;
m_flt_str = fltstr;
m_flt_ast = NULL;
}

sinsp_filter_compiler::sinsp_filter_compiler(
std::shared_ptr<sinsp_filter_factory> factory,
const libsinsp::filter::ast::expr* fltast)
: m_flt_ast(fltast),
m_factory(factory)
{
m_factory = factory;
m_filter = NULL;
m_flt_ast = fltast;
}

std::unique_ptr<sinsp_filter> sinsp_filter_compiler::compile()
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class SINSP_PUBLIC sinsp_filter_compiler:
std::unique_ptr<sinsp_filter> m_filter;
std::vector<std::string> m_field_values;
std::shared_ptr<libsinsp::filter::ast::expr> m_internal_flt_ast;
const libsinsp::filter::ast::expr* m_flt_ast;
const libsinsp::filter::ast::expr* m_flt_ast = nullptr;
std::shared_ptr<sinsp_filter_factory> m_factory;
sinsp_filter_check_list m_default_filterlist;
};
Expand Down
6 changes: 3 additions & 3 deletions userspace/libsinsp/mpsc_priority_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class mpsc_priority_queue
// we just checked.
elm_ptr top = m_queue_top.load();
auto should_pop = pred(*top);

// we must not pop the element
if (!should_pop)
{
Expand Down Expand Up @@ -166,8 +166,8 @@ class mpsc_priority_queue
// gives the same result when inverting the operands, then we can
// assume them being equal.
Cmp c{};
auto res = c(*elm.get(), *r.elm.get());
if (res == c(*r.elm.get(), *elm.get()))
auto res = c(*elm, *r.elm);
if (res == c(*r.elm, *elm))
{
// if elements have the same priority, order them by
// temporal order of arrival in the queue by using an atomic
Expand Down
14 changes: 8 additions & 6 deletions userspace/libsinsp/sinsp_filtercheck_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ int32_t sinsp_filter_check_thread::parse_field_name(std::string_view val, bool a
if(alloc_state)
{
auto acc = m_inspector->m_thread_manager->dynamic_fields()->add_field<uint64_t>("_tmp_sinsp_filter_thread_totexectime");
m_thread_dyn_field_accessor.reset(new libsinsp::state::dynamic_struct::field_accessor<uint64_t>(acc.new_accessor<uint64_t>()));
m_thread_dyn_field_accessor =
std::make_unique<libsinsp::state::dynamic_struct::field_accessor<uint64_t>>(acc.new_accessor<uint64_t>());
}

return sinsp_filter_check::parse_field_name(val, alloc_state, needed_for_filtering);
Expand All @@ -414,7 +415,8 @@ int32_t sinsp_filter_check_thread::parse_field_name(std::string_view val, bool a
if(alloc_state)
{
auto acc = m_inspector->m_thread_manager->dynamic_fields()->add_field<uint64_t>("_tmp_sinsp_filter_thread_cpu");
m_thread_dyn_field_accessor.reset(new libsinsp::state::dynamic_struct::field_accessor<uint64_t>(acc.new_accessor<uint64_t>()));
m_thread_dyn_field_accessor =
std::make_unique<libsinsp::state::dynamic_struct::field_accessor<uint64_t>>(acc.new_accessor<uint64_t>());
}

return sinsp_filter_check::parse_field_name(val, alloc_state, needed_for_filtering);
Expand Down Expand Up @@ -486,7 +488,7 @@ uint8_t* sinsp_filter_check_thread::extract_thread_cpu(sinsp_evt *evt, OUT uint3
tcpu = user + system;

uint64_t last_t_tot_cpu = 0;
tinfo->get_dynamic_field(*m_thread_dyn_field_accessor.get(), last_t_tot_cpu);
tinfo->get_dynamic_field(*m_thread_dyn_field_accessor, last_t_tot_cpu);
if(last_t_tot_cpu != 0)
{
uint64_t deltaval = tcpu - last_t_tot_cpu;
Expand All @@ -501,7 +503,7 @@ uint8_t* sinsp_filter_check_thread::extract_thread_cpu(sinsp_evt *evt, OUT uint3
m_dval = 0;
}

tinfo->set_dynamic_field(*m_thread_dyn_field_accessor.get(), tcpu);
tinfo->set_dynamic_field(*m_thread_dyn_field_accessor, tcpu);

RETURN_EXTRACT_VAR(m_dval);
}
Expand Down Expand Up @@ -978,9 +980,9 @@ uint8_t* sinsp_filter_check_thread::extract_single(sinsp_evt *evt, OUT uint32_t*
if(tinfo != NULL)
{
uint64_t ptot = 0;
tinfo->get_dynamic_field(*m_thread_dyn_field_accessor.get(), ptot);
tinfo->get_dynamic_field(*m_thread_dyn_field_accessor, ptot);
m_u64val += ptot;
tinfo->set_dynamic_field(*m_thread_dyn_field_accessor.get(), m_u64val);
tinfo->set_dynamic_field(*m_thread_dyn_field_accessor, m_u64val);
RETURN_EXTRACT_VAR(m_u64val);
}
else
Expand Down
3 changes: 1 addition & 2 deletions userspace/libsinsp/test/filter_compiler.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ class mock_compiler_filter_factory: public sinsp_filter_factory
void test_filter_run(bool result, string filter_str)
{
sinsp inspector;
std::shared_ptr<sinsp_filter_factory> factory;
factory.reset(new mock_compiler_filter_factory(&inspector));
auto factory = std::make_shared<mock_compiler_filter_factory>(&inspector);
sinsp_filter_compiler compiler(factory, filter_str);
try
{
Expand Down
9 changes: 4 additions & 5 deletions userspace/libsinsp/test/filterchecks/mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,16 @@ class sinsp_filter_check_mock : public sinsp_filter_check
};

// Note the we create a filter check without values on purpose.
static std::unique_ptr<sinsp_filter_check> create_filtercheck_from_field(sinsp* inspector, std::string field,
static std::unique_ptr<sinsp_filter_check> create_filtercheck_from_field(sinsp* inspector, std::string_view field,
enum cmpop op = CO_EQ)
{
sinsp_filter_check_list filter_list;
filter_list.add_filter_check(std::make_unique<sinsp_filter_check_mock>());
std::unique_ptr<sinsp_filter_factory> factory;
factory.reset(new sinsp_filter_factory(inspector, filter_list));
auto check = factory->new_filtercheck(field.c_str());
sinsp_filter_factory factory(inspector, filter_list);
auto check = factory.new_filtercheck(field);
check->m_cmpop = op;
check->m_boolop = BO_NONE;
check->parse_field_name(field.c_str(), true, true);
check->parse_field_name(field, true, true);
return check;
}

Expand Down
10 changes: 5 additions & 5 deletions userspace/libsinsp/test/helpers/scoped_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ limitations under the License.

using namespace test_helpers;

scoped_pipe::scoped_pipe() : m_read_end(), m_write_end()
scoped_pipe::scoped_pipe()
{
int fds[2] = {};

Expand All @@ -43,18 +43,18 @@ scoped_pipe::scoped_pipe() : m_read_end(), m_write_end()
throw std::runtime_error(out.str());
}

m_read_end.reset(new scoped_file_descriptor(fds[0]));
m_write_end.reset(new scoped_file_descriptor(fds[1]));
m_read_end = std::make_unique<scoped_file_descriptor>(fds[0]);
m_write_end = std::make_unique<scoped_file_descriptor>(fds[1]);
}

scoped_file_descriptor& scoped_pipe::read_end()
{
return *m_read_end.get();
return *m_read_end;
}

scoped_file_descriptor& scoped_pipe::write_end()
{
return *m_write_end.get();
return *m_write_end;
}

void scoped_pipe::close()
Expand Down
8 changes: 4 additions & 4 deletions userspace/libsinsp/test/mpsc_priority_queue.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST(mpsc_priority_queue, order_consistency)
return std::greater_equal<int>{}(l.v, r.v);
}
};

using val_t = std::unique_ptr<val>;

mpsc_priority_queue<val_t, val_less> q;
Expand Down Expand Up @@ -98,14 +98,14 @@ TEST(mpsc_priority_queue, single_concurrent_producer)
{
continue;
}

if (!q.try_pop(v))
{
failed++;
continue;
}

failed += (*v.get() != i) ? 1 : 0;
failed += (*v != i) ? 1 : 0;
i++;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ TEST(mpsc_priority_queue, multi_concurrent_producers)
failed++;
}

last_val = *v.get();
last_val = *v;
i++;
}

Expand Down
Loading

0 comments on commit 87a7481

Please sign in to comment.