Skip to content

Commit 5dd16c1

Browse files
authored
Merge pull request #162 from mutouyun/feature/memory
feat: Memory management refactoring and MSVC compilation fixes
2 parents ce0773b + 32244ac commit 5dd16c1

File tree

105 files changed

+6649
-1774
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+6649
-1774
lines changed

.github/workflows/c-cpp.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: C/C++ CI
22

33
on:
44
push:
5-
branches: [ master, develop, issue-* ]
5+
branches: [ master, develop, issue-*, feature/* ]
66
pull_request:
77
branches: [ master, develop ]
88

include/libipc/buffer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include <vector>
66
#include <type_traits>
77

8-
#include "libipc/export.h"
8+
#include "libipc/imp/export.h"
99
#include "libipc/def.h"
1010

1111
namespace ipc {
1212

13-
class IPC_EXPORT buffer {
13+
class LIBIPC_EXPORT buffer {
1414
public:
1515
using destructor_t = void (*)(void*, std::size_t);
1616

@@ -59,8 +59,8 @@ class IPC_EXPORT buffer {
5959
};
6060
}
6161

62-
friend IPC_EXPORT bool operator==(buffer const & b1, buffer const & b2);
63-
friend IPC_EXPORT bool operator!=(buffer const & b1, buffer const & b2);
62+
friend LIBIPC_EXPORT bool operator==(buffer const & b1, buffer const & b2);
63+
friend LIBIPC_EXPORT bool operator!=(buffer const & b1, buffer const & b2);
6464

6565
private:
6666
class buffer_;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* \file libconcur/intrusive_stack.h
3+
* \author mutouyun ([email protected])
4+
* \brief Define concurrent intrusive stack.
5+
*/
6+
#pragma once
7+
8+
#include <atomic>
9+
10+
namespace ipc {
11+
namespace concur {
12+
13+
/// \brief Intrusive stack node.
14+
/// \tparam T The type of the value.
15+
template <typename T>
16+
struct intrusive_node {
17+
T value;
18+
std::atomic<intrusive_node *> next;
19+
};
20+
21+
/// \brief Intrusive stack.
22+
/// \tparam T The type of the value.
23+
/// \tparam Node The type of the node.
24+
template <typename T, typename Node = intrusive_node<T>>
25+
class intrusive_stack {
26+
public:
27+
using node = Node;
28+
29+
private:
30+
std::atomic<node *> top_{nullptr};
31+
32+
public:
33+
intrusive_stack(intrusive_stack const &) = delete;
34+
intrusive_stack(intrusive_stack &&) = delete;
35+
intrusive_stack &operator=(intrusive_stack const &) = delete;
36+
intrusive_stack &operator=(intrusive_stack &&) = delete;
37+
38+
constexpr intrusive_stack() noexcept = default;
39+
40+
bool empty() const noexcept {
41+
return top_.load(std::memory_order_acquire) == nullptr;
42+
}
43+
44+
void push(node *n) noexcept {
45+
node *old_top = top_.load(std::memory_order_acquire);
46+
do {
47+
n->next.store(old_top, std::memory_order_relaxed);
48+
} while (!top_.compare_exchange_weak(old_top, n, std::memory_order_release
49+
, std::memory_order_acquire));
50+
}
51+
52+
node *pop() noexcept {
53+
node *old_top = top_.load(std::memory_order_acquire);
54+
do {
55+
if (old_top == nullptr) {
56+
return nullptr;
57+
}
58+
} while (!top_.compare_exchange_weak(old_top, old_top->next.load(std::memory_order_relaxed)
59+
, std::memory_order_release
60+
, std::memory_order_acquire));
61+
return old_top;
62+
}
63+
};
64+
65+
} // namespace concur
66+
} // namespace ipc

include/libipc/condition.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#include <cstdint> // std::uint64_t
44

5-
#include "libipc/export.h"
5+
#include "libipc/imp/export.h"
66
#include "libipc/def.h"
77
#include "libipc/mutex.h"
88

99
namespace ipc {
1010
namespace sync {
1111

12-
class IPC_EXPORT condition {
12+
class LIBIPC_EXPORT condition {
1313
condition(condition const &) = delete;
1414
condition &operator=(condition const &) = delete;
1515

include/libipc/def.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,26 @@ using uint_t = typename uint<N>::type;
2626
// constants
2727

2828
enum : std::uint32_t {
29-
invalid_value = (std::numeric_limits<std::uint32_t>::max)(),
30-
default_timeout = 100, // ms
29+
invalid_value = (std::numeric_limits<std::uint32_t>::max)(),
30+
default_timeout = 100, // ms
3131
};
3232

3333
enum : std::size_t {
34-
data_length = 64,
35-
large_msg_limit = data_length,
36-
large_msg_align = 1024,
37-
large_msg_cache = 32,
34+
central_cache_default_size = 1024 * 1024, ///< 1MB
35+
data_length = 64,
36+
large_msg_limit = data_length,
37+
large_msg_align = 1024,
38+
large_msg_cache = 32,
3839
};
3940

4041
enum class relat { // multiplicity of the relationship
41-
single,
42-
multi
42+
single,
43+
multi
4344
};
4445

4546
enum class trans { // transmission
46-
unicast,
47-
broadcast
47+
unicast,
48+
broadcast
4849
};
4950

5051
// producer-consumer policy flag
@@ -57,17 +58,17 @@ struct relat_trait;
5758

5859
template <relat Rp, relat Rc, trans Ts>
5960
struct relat_trait<wr<Rp, Rc, Ts>> {
60-
constexpr static bool is_multi_producer = (Rp == relat::multi);
61-
constexpr static bool is_multi_consumer = (Rc == relat::multi);
62-
constexpr static bool is_broadcast = (Ts == trans::broadcast);
61+
constexpr static bool is_multi_producer = (Rp == relat::multi);
62+
constexpr static bool is_multi_consumer = (Rc == relat::multi);
63+
constexpr static bool is_broadcast = (Ts == trans::broadcast);
6364
};
6465

6566
template <template <typename> class Policy, typename Flag>
6667
struct relat_trait<Policy<Flag>> : relat_trait<Flag> {};
6768

6869
// the prefix tag of a channel
6970
struct prefix {
70-
char const *str;
71+
char const *str;
7172
};
7273

7374
} // namespace ipc

include/libipc/export.h

Lines changed: 0 additions & 54 deletions
This file was deleted.

include/libipc/imp/aligned.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* \file libipc/aligned.h
3+
* \author mutouyun ([email protected])
4+
* \brief Defines the type suitable for use as uninitialized storage for types of given type.
5+
*/
6+
#pragma once
7+
8+
#include <array>
9+
#include <cstddef>
10+
11+
#include "libipc/imp/byte.h"
12+
13+
namespace ipc {
14+
15+
/**
16+
* \brief The type suitable for use as uninitialized storage for types of given type.
17+
* std::aligned_storage is deprecated in C++23, so we define our own.
18+
* \tparam T The type to be aligned.
19+
* \tparam AlignT The alignment of the type.
20+
* \see https://en.cppreference.com/w/cpp/types/aligned_storage
21+
* https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1413r3.pdf
22+
*/
23+
template <typename T, std::size_t AlignT = alignof(T)>
24+
class aligned {
25+
alignas(AlignT) std::array<ipc::byte, sizeof(T)> storage_;
26+
27+
public:
28+
/**
29+
* \brief Returns a pointer to the aligned storage.
30+
* \return A pointer to the aligned storage.
31+
*/
32+
T *ptr() noexcept {
33+
return reinterpret_cast<T *>(storage_.data());
34+
}
35+
36+
/**
37+
* \brief Returns a pointer to the aligned storage.
38+
* \return A pointer to the aligned storage.
39+
*/
40+
T const *ptr() const noexcept {
41+
return reinterpret_cast<const T *>(storage_.data());
42+
}
43+
44+
/**
45+
* \brief Returns a reference to the aligned storage.
46+
* \return A reference to the aligned storage.
47+
*/
48+
T &ref() noexcept {
49+
return *ptr();
50+
}
51+
52+
/**
53+
* \brief Returns a reference to the aligned storage.
54+
* \return A reference to the aligned storage.
55+
*/
56+
T const &ref() const noexcept {
57+
return *ptr();
58+
}
59+
};
60+
61+
/**
62+
* \brief Rounds up the given value to the given alignment.
63+
* \tparam T The type of the value.
64+
* \param value The value to be rounded up.
65+
* \param alignment The alignment to be rounded up to.
66+
* \return The rounded up value.
67+
* \see https://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number
68+
*/
69+
template <typename T>
70+
constexpr T round_up(T value, T alignment) noexcept {
71+
return (value + alignment - 1) & ~(alignment - 1);
72+
}
73+
74+
} // namespace ipc

0 commit comments

Comments
 (0)