Skip to content

Commit

Permalink
remove co::fixed_alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed May 5, 2022
1 parent 9978195 commit 10bebc1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 99 deletions.
12 changes: 3 additions & 9 deletions include/co/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@ inline T* static_new(Args&&... args) {
}


// alloc memory for fixed-size objects, do not realloc it
__coapi void* fixed_alloc(size_t size);

// like fixed_alloc(), except that the memory is zero-cleared
__coapi void* fixed_zalloc(size_t size);

// alloc memory and construct an object on it
// new T(args) --> co::make<T>(args)
template <typename T, typename... Args>
inline T* make(Args&&... args) {
return new (co::fixed_alloc(sizeof(T))) T(std::forward<Args>(args)...);
return new (co::alloc(sizeof(T))) T(std::forward<Args>(args)...);
}

// delete the object created by co::make()
Expand Down Expand Up @@ -112,12 +106,12 @@ struct stl_allocator {

#if (__cplusplus >= 201703L) // C++17
T* allocate(size_type n) {
return static_cast<T*>(co::fixed_alloc(n * sizeof(T)));
return static_cast<T*>(co::alloc(n * sizeof(T)));
}
T* allocate(size_type n, const void*) { return allocate(n); }
#else
pointer allocate(size_type n, const void* = 0) {
return static_cast<pointer>(co::fixed_alloc(n * sizeof(value_type)));
return static_cast<pointer>(co::alloc(n * sizeof(value_type)));
}
#endif

Expand Down
18 changes: 9 additions & 9 deletions src/co/co.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void EventImpl::signal() {

// memory: |4(refn)|4|EventImpl|
Event::Event() {
_p = (uint32*) co::fixed_alloc(sizeof(EventImpl) + 8);
_p = (uint32*) co::alloc(sizeof(EventImpl) + 8);
_p[0] = 1;
new (_p + 2) EventImpl();
}
Expand All @@ -120,7 +120,7 @@ void Event::signal() const {

// memory: |4(refn)|4(counter)|EventImpl|
WaitGroup::WaitGroup() {
_p = (uint32*) co::fixed_alloc(sizeof(EventImpl) + 8);
_p = (uint32*) co::alloc(sizeof(EventImpl) + 8);
_p[0] = 1; // refn
_p[1] = 0; // counter
new (_p + 2) EventImpl();
Expand Down Expand Up @@ -199,7 +199,7 @@ inline void MutexImpl::unlock() {

// memory: |4(refn)|4|MutexImpl|
Mutex::Mutex() {
_p = (uint32*) co::fixed_alloc(sizeof(MutexImpl) + 8);
_p = (uint32*) co::alloc(sizeof(MutexImpl) + 8);
_p[0] = 1; // refn
new (_p + 2) MutexImpl();
}
Expand Down Expand Up @@ -314,7 +314,7 @@ inline size_t PoolImpl::size() const {

// memory: |4(refn)|4|PoolImpl|
Pool::Pool() {
_p = (uint32*) co::fixed_alloc(sizeof(PoolImpl) + 8);
_p = (uint32*) co::alloc(sizeof(PoolImpl) + 8);
_p[0] = 1;
new (_p + 2) PoolImpl();
}
Expand All @@ -327,7 +327,7 @@ Pool::~Pool() {
}

Pool::Pool(std::function<void*()>&& ccb, std::function<void(void*)>&& dcb, size_t cap) {
_p = (uint32*) co::fixed_alloc(sizeof(PoolImpl) + 8);
_p = (uint32*) co::alloc(sizeof(PoolImpl) + 8);
_p[0] = 1;
new (_p + 2) PoolImpl(std::move(ccb), std::move(dcb), cap);
}
Expand Down Expand Up @@ -355,7 +355,7 @@ class PipeImpl {
PipeImpl(uint32 buf_size, uint32 blk_size, uint32 ms)
: _buf_size(buf_size), _blk_size(blk_size),
_rx(0), _wx(0), _ms(ms), _full(false) {
_buf = (char*) co::fixed_alloc(_buf_size);
_buf = (char*) co::alloc(_buf_size);
}

~PipeImpl() {
Expand All @@ -379,11 +379,11 @@ class PipeImpl {
waitx* w;
const bool on_stack = gSched->on_stack(buf);
if (on_stack) {
w = (waitx*) co::fixed_alloc(sizeof(waitx) + _blk_size);
w = (waitx*) co::alloc(sizeof(waitx) + _blk_size);
w->buf = (char*)w + sizeof(waitx);
w->len = sizeof(waitx) + _blk_size;
} else {
w = (waitx*) co::fixed_alloc(sizeof(waitx));
w = (waitx*) co::alloc(sizeof(waitx));
w->buf = buf;
w->len = sizeof(waitx);
}
Expand Down Expand Up @@ -524,7 +524,7 @@ void PipeImpl::write(const void* p) {
}

Pipe::Pipe(uint32 buf_size, uint32 blk_size, uint32 ms) {
_p = (uint32*) co::fixed_alloc(sizeof(PipeImpl) + 8);
_p = (uint32*) co::alloc(sizeof(PipeImpl) + 8);
_p[0] = 1;
new (_p + 2) PipeImpl(buf_size, blk_size, ms);
}
Expand Down
8 changes: 4 additions & 4 deletions src/co/io_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ IoEvent::IoEvent(sock_t fd, io_event_t ev)
: _fd(fd), _to(0), _nb_tcp(ev == ev_read ? nb_tcp_recv : nb_tcp_send), _timeout(false) {
auto s = gSched;
s->add_io_event(fd, ev); // add socket to IOCP
_info = (PerIoInfo*) co::fixed_zalloc(sizeof(PerIoInfo));
_info = (PerIoInfo*) co::zalloc(sizeof(PerIoInfo));
assert(_info);
_info->mlen = sizeof(PerIoInfo);
_info->co = (void*) s->running();
Expand All @@ -21,7 +21,7 @@ IoEvent::IoEvent(sock_t fd, int n)
: _fd(fd), _to(0), _nb_tcp(0), _timeout(false) {
auto s = gSched;
s->add_io_event(fd, ev_read); // add socket to IOCP
_info = (PerIoInfo*) co::fixed_zalloc(sizeof(PerIoInfo) + n);
_info = (PerIoInfo*) co::zalloc(sizeof(PerIoInfo) + n);
assert(_info);
_info->mlen = sizeof(PerIoInfo) + n;
_info->co = (void*) s->running();
Expand All @@ -33,14 +33,14 @@ IoEvent::IoEvent(sock_t fd, io_event_t ev, const void* buf, int size, int n)
auto s = gSched;
s->add_io_event(fd, ev);
if (!s->on_stack(buf)) {
_info = (PerIoInfo*) co::fixed_zalloc(sizeof(PerIoInfo) + n);
_info = (PerIoInfo*) co::zalloc(sizeof(PerIoInfo) + n);
assert(_info);
_info->mlen = sizeof(PerIoInfo) + n;
_info->co = (void*) s->running();
_info->buf.buf = (char*)buf;
_info->buf.len = size;
} else {
_info = (PerIoInfo*) co::fixed_alloc(sizeof(PerIoInfo) + n + size);
_info = (PerIoInfo*) co::alloc(sizeof(PerIoInfo) + n + size);
assert(_info);
memset(_info, 0, sizeof(PerIoInfo) + n);
_info->mlen = sizeof(PerIoInfo) + n + size;
Expand Down
2 changes: 1 addition & 1 deletion src/co/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct waitx_t {
};

inline waitx_t* make_waitx(void* co) {
waitx_t* w = (waitx_t*) co::fixed_alloc(sizeof(waitx_t)); assert(w);
waitx_t* w = (waitx_t*) co::alloc(sizeof(waitx_t)); assert(w);
w->co = (Coroutine*)co;
w->state = st_wait;
return w;
Expand Down
76 changes: 8 additions & 68 deletions src/mem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,11 @@ inline void* SmallAlloc::realloc(void* p, uint32 o, uint32 n) {
}


void* ThreadAlloc::alloc(size_t n) {
inline void* ThreadAlloc::alloc(size_t n) {
void* p = 0;
SmallAlloc* sa;
if (n <= 2048) {
const uint32 u = n > 16 ? (_pow2_align((uint32)n) >> 4) : 1;
const uint32 u = n > 16 ? (god::align_up((uint32)n, 16) >> 4) : 1;
if (_sa && (p = _sa->alloc(u))) goto _end;
{
auto& l = *(DoubleLink**)&_sa;
Expand Down Expand Up @@ -693,7 +693,7 @@ void* ThreadAlloc::alloc(size_t n) {
}

} else if (n <= g_max_alloc_size) {
const uint32 u = _pow2_align((uint32)n) >> 12;
const uint32 u = god::align_up((uint32)n, 4096) >> 12;
if (_la && (p = _la->alloc(u))) goto _end;

{
Expand Down Expand Up @@ -755,29 +755,29 @@ inline void ThreadAlloc::free(void* p, size_t n) {
}
}

void* ThreadAlloc::realloc(void* p, size_t o, size_t n) {
inline void* ThreadAlloc::realloc(void* p, size_t o, size_t n) {
if (unlikely(!p)) return this->alloc(n);
if (unlikely(o > g_max_alloc_size)) return ::realloc(p, n);
CHECK_LT(o, n) << "realloc error, new size must be greater than old size..";

if (o <= 2048) {
const uint32 k = (o > 16 ? _pow2_align((uint32)o) : 16);
const uint32 k = (o > 16 ? god::align_up((uint32)o, 16) : 16);
if (n <= (size_t)k) return p;

auto sa = (SmallAlloc*) god::align_down(p, 1u << g_sb_bits);
if (sa == _sa && n <= 2048) {
const uint32 l = _pow2_align((uint32)n);
const uint32 l = god::align_up((uint32)n, 16);
auto x = sa->realloc(p, k >> 4, l >> 4);
if (x) return x;
}

} else {
const uint32 k = _pow2_align((uint32)o);
const uint32 k = god::align_up((uint32)o, 4096);
if (n <= (size_t)k) return p;

auto la = (LargeAlloc*) god::align_down(p, 1u << g_lb_bits);
if (la == _la && n <= g_max_alloc_size) {
const uint32 l = _pow2_align((uint32)n);
const uint32 l = god::align_up((uint32)n, 4096);
auto x = la->realloc(p, k >> 12, l >> 12);
if (x) return x;
}
Expand All @@ -788,66 +788,13 @@ void* ThreadAlloc::realloc(void* p, size_t o, size_t n) {
return x;
}

inline void* ThreadAlloc::fixed_alloc(size_t n) {
void* p = 0;
SmallAlloc* sa;
if (n <= 2048) {
const uint32 u = n > 16 ? (god::align_up((uint32)n, 16) >> 4) : 1;
if (_sa && (p = _sa->alloc(u))) goto _end;
{
auto& l = *(DoubleLink**)&_sa;
if (l && l->next) {
list_move_head_back(l);
if ((p = _sa->alloc(u))) goto _end;
}
}

if (_lb && (sa = _lb->make_small_alloc())) {
list_push_front(*(DoubleLink**)&_sa, (DoubleLink*)sa);
p = sa->alloc(u);
goto _end;
}

{
auto& l = *(DoubleLink**)&_lb;
if (l && l->next) {
list_move_head_back(l);
if ((sa = _lb->make_small_alloc())) {
list_push_front(*(DoubleLink**)&_sa, (DoubleLink*)sa);
p = sa->alloc(u);
goto _end;
}
}

auto lb = galloc()->make_large_block(_id);
if (lb) {
list_push_front(l, (DoubleLink*)lb);
sa = lb->make_small_alloc();
list_push_front(*(DoubleLink**)&_sa, (DoubleLink*)sa);
p = sa->alloc(u);
}
goto _end;
}

} else {
p = this->alloc(n);
}

_end:
return p;
}

} // xx

#ifndef CO_USE_SYS_MALLOC
void* static_alloc(size_t n) {
return xx::thread_alloc()->static_alloc(n);
}

void* fixed_alloc(size_t n) {
return xx::thread_alloc()->fixed_alloc(n);
}

void* alloc(size_t n) {
return xx::thread_alloc()->alloc(n);
}
Expand All @@ -862,7 +809,6 @@ void* realloc(void* p, size_t o, size_t n) {

#else
void* static_alloc(size_t n) { return ::malloc(n); }
void* fixed_alloc(size_t n) { return ::malloc(n); }
void* alloc(size_t n) { return ::malloc(n); }
void free(void* p, size_t) { ::free(p); }
void* realloc(void* p, size_t, size_t n) { return ::realloc(p, n); }
Expand All @@ -874,10 +820,4 @@ void* zalloc(size_t size) {
return p;
}

void* fixed_zalloc(size_t size) {
auto p = co::fixed_alloc(size);
if (p) memset(p, 0, size);
return p;
}

} // co
3 changes: 1 addition & 2 deletions test/mem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ void test_fun(int id) {
v.clear();
t.restart();
for (int i = 0; i < N; ++i) {
v[i] = co::fixed_alloc(32);
//v[i] = co::alloc(32);
v[i] = co::alloc(32);
}
us = t.us();
avg = us * 1000.0 / N - vavg;
Expand Down
12 changes: 6 additions & 6 deletions unitest/mem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ DEF_test(mem) {
EXPECT_EQ(*x, 7);
}

DEF_case(fixed) {
void* p = co::fixed_alloc(8);
DEF_case(small) {
void* p = co::alloc(8);
EXPECT_NE(p, (void*)0);

*(uint32*)p = 7;
Expand All @@ -26,16 +26,16 @@ DEF_test(mem) {
void* x = p;
co::free(p, 8);

p = co::fixed_alloc(72);
p = co::alloc(72);
EXPECT_NE(p, (void*)0);
EXPECT_EQ(p, x);
co::free(p, 72);

p = co::fixed_alloc(2048);
p = co::alloc(2048);
EXPECT_EQ(p, x);
co::free(p, 2048);

p = co::fixed_alloc(4096);
p = co::alloc(4096);
EXPECT_NE(p, (void*)0);
EXPECT_NE(p, x);
co::free(p, 4096);
Expand All @@ -45,7 +45,7 @@ DEF_test(mem) {
co::del(v);
}

DEF_case(deflt) {
DEF_case(realloc) {
void* p;
p = co::alloc(48);
EXPECT_NE(p, (void*)0);
Expand Down

0 comments on commit 10bebc1

Please sign in to comment.