Skip to content

Commit 2b33982

Browse files
committed
Use $new instead of alloc
1 parent b20e11d commit 2b33982

File tree

13 files changed

+48
-924
lines changed

13 files changed

+48
-924
lines changed

include/libipc/mem/new.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace mem {
2626
class LIBIPC_EXPORT block_collector {
2727
public:
2828
virtual ~block_collector() noexcept = default;
29-
virtual void recycle(void *p) noexcept = 0;
29+
virtual void recycle(void */*p*/) noexcept {}
3030
};
3131

3232
#if defined(LIBIPC_CPP_17)
@@ -182,8 +182,7 @@ T *$new(A &&... args) noexcept {
182182
/// \brief Destroys object previously allocated by the `$new` and releases obtained memory area.
183183
/// \note This function is thread-safe. If the pointer type passed in is different from `$new`,
184184
/// additional performance penalties may be incurred.
185-
template <typename T>
186-
void $delete(T *p) noexcept {
185+
inline void $delete(void *p) noexcept {
187186
if (p == nullptr) return;
188187
auto *b = reinterpret_cast<byte *>(p) - regular_head_size;
189188
auto *r = reinterpret_cast<recycle_t *>(b);

include/libipc/pool_alloc.h

-103
This file was deleted.

src/libipc/ipc.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "libipc/ipc.h"
1515
#include "libipc/def.h"
1616
#include "libipc/shm.h"
17-
#include "libipc/pool_alloc.h"
1817
#include "libipc/queue.h"
1918
#include "libipc/policy.h"
2019
#include "libipc/rw_lock.h"
@@ -26,6 +25,7 @@
2625
#include "libipc/utility/utility.h"
2726

2827
#include "libipc/mem/resource.h"
28+
#include "libipc/mem/new.h"
2929
#include "libipc/platform/detail.h"
3030
#include "libipc/circ/elem_array.h"
3131

@@ -64,10 +64,15 @@ struct msg_t : msg_t<0, AlignSize> {
6464
};
6565

6666
template <typename T>
67-
ipc::buff_t make_cache(T& data, std::size_t size) {
68-
auto ptr = ipc::mem::alloc(size);
67+
ipc::buff_t make_cache(T &data, std::size_t size) {
68+
auto *ptr = ipc::mem::$new<void>(size);
6969
std::memcpy(ptr, &data, (ipc::detail::min)(sizeof(data), size));
70-
return { ptr, size, ipc::mem::free };
70+
return {
71+
ptr, size,
72+
[](void *p, std::size_t) noexcept {
73+
ipc::mem::$delete(p);
74+
}
75+
};
7176
}
7277

7378
acc_t *cc_acc(std::string const &pref) {
@@ -259,8 +264,8 @@ chunk_info_t *chunk_storage_info(conn_info_head *inf, std::size_t chunk_size) {
259264
guard.unlock();
260265
LIBIPC_UNUSED std::lock_guard<ipc::rw_lock> guard {lock};
261266
it = storages.emplace(chunk_size, chunk_handle_ptr_t{
262-
ipc::mem::alloc<chunk_handle_t>(), [](chunk_handle_t *p) {
263-
ipc::mem::destruct(p);
267+
ipc::mem::$new<chunk_handle_t>(), [](chunk_handle_t *p) {
268+
ipc::mem::$delete(p);
264269
}}).first;
265270
}
266271
}
@@ -447,7 +452,7 @@ constexpr static queue_t* queue_of(ipc::handle_t h) noexcept {
447452
static bool connect(ipc::handle_t * ph, ipc::prefix pref, char const * name, bool start_to_recv) {
448453
assert(ph != nullptr);
449454
if (*ph == nullptr) {
450-
*ph = ipc::mem::alloc<conn_info_t>(pref.str, name);
455+
*ph = ipc::mem::$new<conn_info_t>(pref.str, name);
451456
}
452457
return reconnect(ph, start_to_recv);
453458
}
@@ -490,7 +495,7 @@ static bool reconnect(ipc::handle_t * ph, bool start_to_recv) {
490495
}
491496

492497
static void destroy(ipc::handle_t h) noexcept {
493-
ipc::mem::free(info_of(h));
498+
ipc::mem::$delete(info_of(h));
494499
}
495500

496501
static std::size_t recv_count(ipc::handle_t h) noexcept {
@@ -654,20 +659,20 @@ static ipc::buff_t recv(ipc::handle_t h, std::uint64_t tm) {
654659
conn_info_t * inf;
655660
ipc::circ::cc_t curr_conns;
656661
ipc::circ::cc_t conn_id;
657-
} *r_info = ipc::mem::alloc<recycle_t>(recycle_t{
662+
} *r_info = ipc::mem::$new<recycle_t>(recycle_t{
658663
buf_id,
659664
inf,
660665
que->elems()->connections(std::memory_order_relaxed),
661666
que->connected_id()
662667
});
663668
if (r_info == nullptr) {
664-
ipc::log("fail: ipc::mem::alloc<recycle_t>.\n");
669+
ipc::log("fail: ipc::mem::$new<recycle_t>.\n");
665670
return ipc::buff_t{buf, msg_size}; // no recycle
666671
} else {
667672
return ipc::buff_t{buf, msg_size, [](void* p_info, std::size_t size) {
668673
auto r_info = static_cast<recycle_t *>(p_info);
669674
LIBIPC_UNUSED auto finally = ipc::guard([r_info] {
670-
ipc::mem::free(r_info);
675+
ipc::mem::$delete(r_info);
671676
});
672677
recycle_storage<flag_t>(r_info->storage_id,
673678
r_info->inf,

src/libipc/mem/resource.h

-12
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,10 @@
55
#include <string>
66

77
#include "libipc/def.h"
8-
#include "libipc/memory/alloc.h"
98
#include "libipc/imp/fmt.h"
109
#include "libipc/mem/polymorphic_allocator.h"
1110

1211
namespace ipc {
13-
namespace mem {
14-
15-
//using async_pool_alloc = static_wrapper<variable_wrapper<async_wrapper<
16-
// detail::fixed_alloc<
17-
// variable_alloc <sizeof(void*) * 1024 * 256>,
18-
// fixed_expand_policy<sizeof(void*) * 1024, sizeof(void*) * 1024 * 256>
19-
// >,
20-
// default_recycler >>>;
21-
using async_pool_alloc = ipc::mem::static_alloc;
22-
23-
} // namespace mem
2412

2513
template <typename T>
2614
struct hash : public std::hash<T> {};

0 commit comments

Comments
 (0)