Verified Commit 2f4d3084 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Remove `utils` module

parent 414fc9da
Loading
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

add_subdirectory(utils)
add_subdirectory(logger)
add_subdirectory(net)
add_subdirectory(posix_file)

src/utils/CMakeLists.txt

deleted100644 → 0
+0 −31
Original line number Diff line number Diff line
################################################################################
# Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain            #
#                                                                              #
# This software was partially supported by the EuroHPC-funded project ADMIRE   #
#   (Project ID: 956748, https://www.admire-eurohpc.eu).                       #
#                                                                              #
# This file is part of Cargo.                                                  #
#                                                                              #
# Cargo is free software: you can redistribute it and/or modify                #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# Cargo is distributed in the hope that it will be useful,                     #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with Cargo.  If not, see <https://www.gnu.org/licenses/>.              #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

add_subdirectory(utils)

target_include_directories(
  utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)

set_property(TARGET utils PROPERTY POSITION_INDEPENDENT_CODE ON)

src/utils/utils/CMakeLists.txt

deleted100644 → 0
+0 −31
Original line number Diff line number Diff line
################################################################################
# Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain            #
#                                                                              #
# This software was partially supported by the EuroHPC-funded project ADMIRE   #
#   (Project ID: 956748, https://www.admire-eurohpc.eu).                       #
#                                                                              #
# This file is part of Cargo.                                                  #
#                                                                              #
# Cargo is free software: you can redistribute it and/or modify                #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# Cargo is distributed in the hope that it will be useful,                     #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with Cargo.  If not, see <https://www.gnu.org/licenses/>.              #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

add_library(utils STATIC)

set_property(TARGET utils PROPERTY POSITION_INDEPENDENT_CODE ON)

target_sources(
  utils PRIVATE utils.hpp utils.cpp signal_listener.hpp ctype_ptr.hpp
)

src/utils/utils/ctype_ptr.hpp

deleted100644 → 0
+0 −123
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of Cargo.
 *
 * Cargo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cargo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cargo.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#ifndef UTILS_C_PTR_HPP
#define UTILS_C_PTR_HPP

#include <memory>
#include <vector>

namespace utils {

// A manager for a C raw pointer. It allows to automatically delete dynamically
// allocated C structs in a RAII manner (provided that there is a deleter
// function for the struct available).
template <typename T, auto fn>
struct deleter {
    void
    operator()(T* ptr) {
        fn(ptr);
    }
};

template <typename T, auto fn>
using ctype_ptr =
        std::unique_ptr<typename std::remove_pointer<T>::type,
                        deleter<typename std::remove_pointer<T>::type, fn>>;

// A manager for a vector of C raw pointers. It allows to automatically
// delete the dynamically allocated C structs pointed by each vector elements
// in a RAII manner (provided that there is a deleter function for the struct
// available). Can also be used to directly pass an array of C pointers to C
// APIs by means of the data() function.
template <typename T, auto fn>
struct ctype_ptr_vector {

    ctype_ptr_vector() = default;

    ctype_ptr_vector(T* const data, size_t size) {
        reserve(size);

        for(size_t i = 0; i < size; ++i) {
            emplace_back(data[i]);
        }
    }

    ctype_ptr_vector(ctype_ptr_vector&& rhs) noexcept
        : m_data(std::move(rhs.m_data)), m_addrs(std::move(rhs.m_addrs)) {}

    ctype_ptr_vector&
    operator=(ctype_ptr_vector&&) noexcept = default;

    ~ctype_ptr_vector() = default;

    constexpr void
    reserve(size_t n) {
        m_data.reserve(n);
        m_addrs.reserve(n);
    }

    template <typename... Args>
    constexpr void
    emplace_back(Args&&... args) {
        const auto& tmp = m_data.emplace_back(args...);
        m_addrs.push_back(tmp.get());
    }

    constexpr const T*
    data() const noexcept {
        return m_addrs.data();
    }

    constexpr T*
    data() noexcept {
        return m_addrs.data();
    }

    constexpr std::size_t
    size() const noexcept {
        return m_data.size();
    }

    constexpr T*
    release() noexcept {

        auto* data = (T*) calloc(m_data.size(), sizeof(T));

        for(size_t i = 0; i < m_data.size(); ++i) {
            data[i] = m_data[i].release();
            m_addrs[i] = nullptr;
        }

        return data;
    }


    std::vector<utils::ctype_ptr<T, fn>> m_data{};
    std::vector<T> m_addrs{};
};

} // namespace utils

#endif // UTILS_C_PTR_HPP
+0 −110
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of Cargo.
 *
 * Cargo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cargo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cargo.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#ifndef UTILS_SIGNAL_LISTENER_HPP
#define UTILS_SIGNAL_LISTENER_HPP

#include <boost/asio.hpp>
#include <boost/asio/signal_set.hpp>

namespace {
template <class F, class... Args>
void
do_for(F f, Args... args) {
    [[maybe_unused]] int x[] = {(f(args), 0)...};
}
} // namespace

namespace utils {

namespace ba = boost::asio;

struct signal_listener {

    using SignalHandlerType = std::function<void(int)>;

    signal_listener() : m_ios(), m_signals(m_ios) {}

    template <typename SignalHandlerType, typename... Args>
    void
    set_handler(SignalHandlerType&& handler, Args... signums) {

        m_user_handler = std::forward<SignalHandlerType>(handler);
        m_signals.clear();

        ::do_for([&](int signum) { m_signals.add(signum); }, signums...);
    }

    void
    clear_handler() {
        m_user_handler = nullptr;
        m_signals.clear();
    }

    void
    do_accept() {

        if(m_user_handler) {
            m_signals.async_wait(std::bind( // NOLINT
                    &signal_listener::signal_handler, this,
                    std::placeholders::_1, std::placeholders::_2));
        }
    }

    void
    run() {
        std::thread([&]() {
            do_accept();
            m_ios.run();
        }).detach();
    }

    void
    stop() {
        m_ios.stop();
    }

private:
    void
    signal_handler(boost::system::error_code ec, int signal_number) {
        // a signal occurred, invoke installed handler
        if(!ec) {
            m_user_handler(signal_number);
        }

        // reinstall handler
        m_signals.async_wait(
                std::bind(&signal_listener::signal_handler, this, // NOLINT
                          std::placeholders::_1, std::placeholders::_2));
    }


    ba::io_service m_ios;
    ba::signal_set m_signals;
    SignalHandlerType m_user_handler;
};

} // namespace utils

#endif // UTILS_SIGNAL_LISTENER_HPP
Loading