mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
fine-tune
This commit is contained in:
parent
d36c3949ce
commit
fbf3c622e8
@ -5,6 +5,8 @@
|
||||
#include "buffer.h"
|
||||
#include "shm.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ipc {
|
||||
|
||||
using handle_t = void*;
|
||||
|
||||
@ -44,6 +44,7 @@ class scope_alloc_base {
|
||||
protected:
|
||||
struct block_t {
|
||||
block_t * next_;
|
||||
std::size_t size_;
|
||||
} * list_ = nullptr;
|
||||
|
||||
enum : std::size_t {
|
||||
@ -74,7 +75,7 @@ private:
|
||||
while (list_ != nullptr) {
|
||||
auto curr = list_;
|
||||
list_ = list_->next_;
|
||||
alloc_.free(curr);
|
||||
alloc_.free(curr, curr->size_);
|
||||
}
|
||||
// now list_ is nullptr
|
||||
}
|
||||
@ -103,8 +104,9 @@ public:
|
||||
}
|
||||
|
||||
void* alloc(std::size_t size) {
|
||||
auto curr = static_cast<block_t*>(alloc_.alloc(aligned_block_size + size));
|
||||
auto curr = static_cast<block_t*>(alloc_.alloc(size += aligned_block_size));
|
||||
curr->next_ = list_;
|
||||
curr->size_ = size;
|
||||
return (reinterpret_cast<byte_t*>(list_ = curr) + aligned_block_size);
|
||||
}
|
||||
};
|
||||
@ -151,9 +153,23 @@ public:
|
||||
};
|
||||
|
||||
struct fixed_expand_policy {
|
||||
|
||||
enum : std::size_t {
|
||||
base_size = sizeof(void*) * 1024 / 2
|
||||
};
|
||||
|
||||
static std::size_t prev(std::size_t& e) {
|
||||
if ((e /= 2) == 0) e = 1;
|
||||
return e;
|
||||
}
|
||||
|
||||
static std::size_t next(std::size_t& e) {
|
||||
return e *= 2;
|
||||
}
|
||||
|
||||
template <std::size_t BlockSize>
|
||||
IPC_CONSTEXPR_ static std::size_t next(std::size_t & e) {
|
||||
return ipc::detail::max<std::size_t>(BlockSize, (sizeof(void*) * 1024) / 2) * (e *= 2);
|
||||
static std::size_t next(std::size_t & e) {
|
||||
return ipc::detail::max<std::size_t>(BlockSize, base_size) * next(e);
|
||||
}
|
||||
};
|
||||
|
||||
@ -205,8 +221,9 @@ public:
|
||||
}
|
||||
|
||||
void clear() {
|
||||
ExpandP::prev(this->init_expand_);
|
||||
this->cursor_ = nullptr;
|
||||
alloc_.~alloc_policy();
|
||||
this->init(this->init_expand_);
|
||||
}
|
||||
|
||||
void* alloc() {
|
||||
|
||||
@ -35,14 +35,11 @@ struct chunk_mapping_policy {
|
||||
};
|
||||
|
||||
template <typename AllocP>
|
||||
struct chunk_alloc_recoverer {
|
||||
public:
|
||||
using alloc_policy = AllocP;
|
||||
|
||||
constexpr static void swap(chunk_alloc_recoverer &) {}
|
||||
constexpr static void clear() {}
|
||||
constexpr static void try_recover(alloc_policy &) {}
|
||||
constexpr static void collect(alloc_policy &&) {}
|
||||
struct chunk_alloc_recoverer : default_alloc_recoverer<AllocP> {
|
||||
void collect(alloc_policy && alc) {
|
||||
alc.clear(); // recycle memory to the central heap (static_chunk_alloc)
|
||||
default_alloc_recoverer<AllocP>::collect(std::move(alc));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@ -238,7 +238,7 @@ private slots:
|
||||
void test_prod_cons_1v3();
|
||||
void test_prod_cons_performance();
|
||||
void test_queue();
|
||||
} unit__;
|
||||
} /*unit__*/;
|
||||
|
||||
#include "test_circ.moc"
|
||||
|
||||
|
||||
@ -172,9 +172,9 @@ struct test_performance<AllocT, ModeT, 1> {
|
||||
};
|
||||
|
||||
void Unit::test_static() {
|
||||
test_performance<ipc::mem::static_alloc, alloc_FIFO , 8>::start();
|
||||
test_performance<ipc::mem::static_alloc, alloc_LIFO , 8>::start();
|
||||
test_performance<ipc::mem::static_alloc, alloc_random, 8>::start();
|
||||
//test_performance<ipc::mem::static_alloc, alloc_FIFO , 8>::start();
|
||||
//test_performance<ipc::mem::static_alloc, alloc_LIFO , 8>::start();
|
||||
//test_performance<ipc::mem::static_alloc, alloc_random, 8>::start();
|
||||
}
|
||||
|
||||
void Unit::test_pool() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user