Skip to content

Commit 1b74b1a

Browse files
authored
Merge pull request #2 from Zia-Rashid/dev
cleaned up dead/unnecessary code, somehow improved performance.
2 parents 55f5d62 + 6d2e297 commit 1b74b1a

5 files changed

Lines changed: 25 additions & 151 deletions

File tree

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ BIN_DIR := bin
4343
ZIALLOC_MAIN := allocators/zialloc/alloc.cpp
4444
ifeq ($(ALLOCATOR),$(ZIALLOC_MAIN))
4545
ALLOC_CPP_SRCS := allocators/zialloc/alloc.cpp \
46-
allocators/zialloc/free.cpp \
4746
allocators/zialloc/os.cpp \
4847
allocators/zialloc/segments.cpp
4948
ALLOC_OBJ := $(patsubst %.cpp,$(BUILD_DIR)/%.o,$(ALLOC_CPP_SRCS))

allocators/zialloc/free.cpp

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

allocators/zialloc/mem.h

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ constexpr size_t page_kind_size(int kind) {
8686
SEGMENT_SIZE;
8787
}
8888

89-
typedef enum segment_kind_e {
90-
SEGMENT_NORM, // most allocations
91-
SEGMENT_HUGE, // if page kind is "huge"
92-
SEGMENT_GUARD
93-
} segment_kind_t;
94-
9589
typedef enum page_status_e { FULL, ACTIVE, EMPTY } page_status_t;
9690

9791
// all power aligned (i think), we can fill unused space w/ guard chunks.
@@ -107,46 +101,6 @@ typedef enum chunk_max_e {
107101
CHUNK_XL // whatever it wants to be
108102
} chunk_max_t;
109103

110-
// Memory can reside in arena's, direct OS allocated, or statically allocated.
111-
// The memid keeps track of this.
112-
typedef enum memkind_e {
113-
MEM_NONE, // not allocated
114-
MEM_EXTERNAL, // not owned by mimalloc but provided externally (via
115-
// `mi_manage_os_memory` for example)
116-
MEM_STATIC, // allocated in a static area and should not be freed (for arena
117-
// meta data for example)
118-
MEM_OS, // allocated from the OS
119-
MEM_OS_HUGE, // allocated as huge OS pages (usually 1GiB, pinned to physical
120-
// memory)
121-
MEM_OS_REMAP, // allocated in a remapable area (i.e. using `mremap`)
122-
MEM_ARENA // allocated from an arena (the usual case)
123-
} memkind_t;
124-
125-
constexpr static inline bool memkind_is_os(memkind_t memkind) {
126-
return (memkind >= MEM_OS && memkind <= MEM_OS_REMAP);
127-
}
128-
129-
typedef struct memid_os_info {
130-
void *base; // actual base address of the block (used for offset aligned
131-
// allocations)
132-
size_t size; // full allocation size
133-
} memid_os_info_t;
134-
typedef struct memid_arena_info {
135-
size_t block_index; // index in the arena
136-
uint8_t id; // arena id (>= 1)
137-
bool is_exclusive; // this arena can only be used for specific arena
138-
// allocations
139-
} memid_arena_info_t;
140-
typedef struct memid_s {
141-
union {
142-
memid_os_info_t os; // MI_MEM_OS
143-
memid_arena_info_t arena; // MI_MEM_ARENA
144-
} mem;
145-
bool is_pinned; // `true` if we cannot decommit/reset/protect in this memory
146-
bool initially_committed;
147-
bool initially_zero;
148-
} memid_t;
149-
150104
static inline pid_t current_tid() { return syscall(SYS_gettid); }
151105

152106
static inline uint64_t generate_canary() {

allocators/zialloc/segments.cpp

Lines changed: 25 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,6 @@ class Page {
415415
page_status_t get_status() const { return status; }
416416
size_t get_chunk_usable() const { return chunk_usable; }
417417
pid_t get_owner_tid() const { return owner_tid; }
418-
uintptr_t get_base_addr() const { return reinterpret_cast<uintptr_t>(base); }
419-
size_t get_span_size() const { return page_span; }
420418
size_t get_segment_index() const { return owner_segment_idx; }
421419
Segment *get_owner_segment() const { return owner_segment; }
422420
};
@@ -512,45 +510,32 @@ class Segment {
512510
void *out = nullptr;
513511
page_status_t before = EMPTY;
514512
page_status_t after = EMPTY;
515-
if (mt) {
516-
std::lock_guard<std::mutex> lk(page_lock_for(&page));
513+
auto alloc_from_page = [&]() -> void * {
517514
const size_t target_req =
518515
(fixed_chunk_set.load(std::memory_order_relaxed))
519516
? fixed_chunk_usable.load(std::memory_order_relaxed)
520517
: req;
521518
if (!page.is_initialized()) {
522519
void *page_base = static_cast<void *>(static_cast<char *>(base) + idx * page_size);
523520
if (!page.init(page_base, size_class, target_req))
524-
continue;
521+
return nullptr;
525522
if (!fixed_chunk_set.load(std::memory_order_relaxed)) {
526523
fixed_chunk_usable.store(page.get_chunk_usable(), std::memory_order_relaxed);
527524
fixed_chunk_set.store(true, std::memory_order_relaxed);
528525
}
529526
}
530527
if (!page.can_hold(req)) {
531-
continue;
528+
return nullptr;
532529
}
533530

534-
out = page.allocate(req, &before, &after); // dispatch to page lvl alloc
535-
} else {
536-
const size_t target_req =
537-
(fixed_chunk_set.load(std::memory_order_relaxed))
538-
? fixed_chunk_usable.load(std::memory_order_relaxed)
539-
: req;
540-
if (!page.is_initialized()) {
541-
void *page_base = static_cast<void *>(static_cast<char *>(base) + idx * page_size);
542-
if (!page.init(page_base, size_class, target_req))
543-
continue;
544-
if (!fixed_chunk_set.load(std::memory_order_relaxed)) {
545-
fixed_chunk_usable.store(page.get_chunk_usable(), std::memory_order_relaxed);
546-
fixed_chunk_set.store(true, std::memory_order_relaxed);
547-
}
548-
}
549-
if (!page.can_hold(req)) {
550-
continue;
551-
}
531+
return page.allocate(req, &before, &after);
532+
};
552533

553-
out = page.allocate(req, &before, &after);
534+
if (mt) {
535+
std::lock_guard<std::mutex> lk(page_lock_for(&page));
536+
out = alloc_from_page();
537+
} else {
538+
out = alloc_from_page();
554539
}
555540

556541
if (!out)
@@ -592,30 +577,19 @@ class Segment {
592577
}
593578
};
594579

595-
typedef struct tc_page_s {
596-
Page *loc;
597-
page_kind_t kind;
598-
size_t size;
599-
void *freelist;
600-
} tc_page_t;
601-
602580
class ThreadCache {
603581
private:
604582
static std::atomic<uint32_t> live_threads;
605583
pid_t tid;
606584
bool is_active;
607-
std::vector<tc_page_t *> pages;
608585
Page *cached_pages[3];
609-
uintptr_t cached_page_bases[3];
610-
uintptr_t cached_page_ends[3];
611586
size_t preferred_seg_idx[3];
612587
bool preferred_seg_valid[3];
613588

614589
public:
615590
ThreadCache()
616-
: tid(current_tid()), is_active(true), pages(),
617-
cached_pages{nullptr, nullptr, nullptr}, cached_page_bases{0, 0, 0},
618-
cached_page_ends{0, 0, 0}, preferred_seg_idx{0, 0, 0},
591+
: tid(current_tid()), is_active(true), cached_pages{nullptr, nullptr, nullptr},
592+
preferred_seg_idx{0, 0, 0},
619593
preferred_seg_valid{false, false, false} {
620594
live_threads.fetch_add(1, std::memory_order_relaxed);
621595
g_live_threads.fetch_add(1, std::memory_order_relaxed);
@@ -644,13 +618,11 @@ class ThreadCache {
644618
return cached_pages[class_index_for_kind(kind)];
645619
}
646620

647-
void cache_page(page_kind_t kind, Page *page, uintptr_t page_base, size_t page_size) {
621+
void cache_page(page_kind_t kind, Page *page) {
648622
if (kind > PAGE_LG)
649623
return;
650624
const size_t idx = class_index_for_kind(kind);
651625
cached_pages[idx] = page;
652-
cached_page_bases[idx] = page_base;
653-
cached_page_ends[idx] = page_base + page_size;
654626
}
655627

656628
void clear_cached_page(page_kind_t kind, Page *page) {
@@ -659,8 +631,6 @@ class ThreadCache {
659631
const size_t idx = class_index_for_kind(kind);
660632
if (cached_pages[idx] == page) {
661633
cached_pages[idx] = nullptr;
662-
cached_page_bases[idx] = 0;
663-
cached_page_ends[idx] = 0;
664634
}
665635
}
666636

@@ -695,15 +665,11 @@ struct ClassShard {
695665

696666
class HeapState {
697667
private:
698-
memid_t memid;
699668
void *base;
700669
size_t reserved_size;
701670
uint32_t num_segments;
702671
std::vector<std::unique_ptr<Segment>> layout;
703-
std::vector<segment_kind_t> seg_kind;
704672
std::vector<void *> seg_bases;
705-
std::vector<page_kind_t> seg_page_kind;
706-
memkind_t mem_kind;
707673
uint64_t canary;
708674
size_t reserved_cursor;
709675
std::mutex heap_mu;
@@ -727,7 +693,7 @@ class HeapState {
727693
shard.non_full_segments.push_back(seg_idx);
728694
}
729695

730-
bool add_segment_nolock(void *segment_base, segment_kind_t kind, page_kind_t page_kind) {
696+
bool add_segment_nolock(void *segment_base, page_kind_t page_kind) {
731697
if (!segment_base)
732698
return false;
733699

@@ -737,9 +703,7 @@ class HeapState {
737703
return false;
738704

739705
layout.push_back(std::move(seg));
740-
seg_kind.push_back(kind);
741706
seg_bases.push_back(segment_base);
742-
seg_page_kind.push_back(page_kind);
743707
num_segments = static_cast<uint32_t>(layout.size());
744708

745709
ClassShard &shard = shard_for(page_kind);
@@ -755,7 +719,7 @@ class HeapState {
755719
return true;
756720
}
757721

758-
bool add_segment_from_reserved_nolock(segment_kind_t kind, page_kind_t page_kind) {
722+
bool add_segment_from_reserved_nolock(page_kind_t page_kind) {
759723
if (!base || reserved_size == 0)
760724
return false;
761725
if (reserved_cursor + SEGMENT_SIZE > reserved_size)
@@ -766,7 +730,7 @@ class HeapState {
766730
return false;
767731

768732
reserved_cursor += SEGMENT_SIZE;
769-
return add_segment_nolock(seg_base, kind, page_kind);
733+
return add_segment_nolock(seg_base, page_kind);
770734
}
771735

772736
void *alloc_xl(size_t size) {
@@ -819,8 +783,8 @@ class HeapState {
819783

820784
public:
821785
HeapState()
822-
: memid(), base(nullptr), reserved_size(0), num_segments(0), layout(),
823-
seg_kind(), seg_bases(), seg_page_kind(), mem_kind(MEM_NONE), canary(0),
786+
: base(nullptr), reserved_size(0), num_segments(0), layout(),
787+
seg_bases(), canary(0),
824788
reserved_cursor(0), heap_mu(), class_shards() {}
825789

826790
static HeapState &instance() {
@@ -837,13 +801,10 @@ class HeapState {
837801
reserved_size = size;
838802
reserved_cursor = 0;
839803
canary = generate_canary();
840-
mem_kind = MEM_OS;
841804

842805
const size_t cap = size / SEGMENT_SIZE;
843806
layout.reserve(cap);
844-
seg_kind.reserve(cap);
845807
seg_bases.reserve(cap);
846-
seg_page_kind.reserve(cap);
847808

848809
for (ClassShard &shard : class_shards) {
849810
std::lock_guard<std::mutex> shard_lk(shard.mu);
@@ -854,14 +815,9 @@ class HeapState {
854815
return true;
855816
}
856817

857-
bool add_segment(void *segment_base, segment_kind_t kind, page_kind_t page_kind) {
818+
bool add_segment_from_reserved(page_kind_t page_kind) {
858819
std::lock_guard<std::mutex> lk(heap_mu);
859-
return add_segment_nolock(segment_base, kind, page_kind);
860-
}
861-
862-
bool add_segment_from_reserved(segment_kind_t kind, page_kind_t page_kind) {
863-
std::lock_guard<std::mutex> lk(heap_mu);
864-
return add_segment_from_reserved_nolock(kind, page_kind);
820+
return add_segment_from_reserved_nolock(page_kind);
865821
}
866822

867823
void *allocate(size_t size) {
@@ -930,7 +886,7 @@ class HeapState {
930886
if (tc->get_active()) {
931887
tc->set_preferred_segment(kind, seg_idx);
932888
if (page)
933-
tc->cache_page(kind, page, page->get_base_addr(), page->get_span_size());
889+
tc->cache_page(kind, page);
934890
}
935891
if (page)
936892
g_last_alloc_usable = page->get_chunk_usable();
@@ -992,7 +948,7 @@ class HeapState {
992948
// grow from reserved heap (ideal) instead of mmaping more mem to expand.
993949
{
994950
std::lock_guard<std::mutex> lk(heap_mu);
995-
if (add_segment_from_reserved_nolock(SEGMENT_NORM, kind)) {
951+
if (add_segment_from_reserved_nolock(kind)) {
996952
return try_segment(layout.size() - 1);
997953
}
998954
}
@@ -1003,7 +959,7 @@ class HeapState {
1003959

1004960
{
1005961
std::lock_guard<std::mutex> lk(heap_mu);
1006-
if (!add_segment_nolock(seg_mem, SEGMENT_NORM, kind)) {
962+
if (!add_segment_nolock(seg_mem, kind)) {
1007963
free_segment(seg_mem, SEGMENT_SIZE);
1008964
return nullptr;
1009965
}
@@ -1050,7 +1006,7 @@ class HeapState {
10501006
}
10511007

10521008
if (tc->get_active()) {
1053-
tc->cache_page(kind, page, page->get_base_addr(), page->get_span_size());
1009+
tc->cache_page(kind, page);
10541010
if (page->get_status() == EMPTY)
10551011
tc->clear_cached_page(kind, page);
10561012
}
@@ -1076,7 +1032,6 @@ class HeapState {
10761032
return usable_xl(ptr);
10771033
}
10781034

1079-
std::vector<segment_kind_t> get_segment_kinds() { return seg_kind; }
10801035
uint32_t get_num_segments() { return num_segments; }
10811036

10821037
bool is_corrupted() {
@@ -1111,9 +1066,7 @@ class HeapState {
11111066
}
11121067

11131068
layout.clear();
1114-
seg_kind.clear();
11151069
seg_bases.clear();
1116-
seg_page_kind.clear();
11171070

11181071
for (ClassShard &shard : class_shards) {
11191072
std::lock_guard<std::mutex> shard_lk(shard.mu);
@@ -1125,29 +1078,20 @@ class HeapState {
11251078
reserved_size = 0;
11261079
num_segments = 0;
11271080
canary = 0;
1128-
mem_kind = MEM_NONE;
11291081
reserved_cursor = 0;
11301082
}
11311083
};
11321084

11331085
} // namespace
11341086

1135-
bool heap_register_segment(void *segment_base) {
1136-
return HeapState::instance().add_segment(segment_base, SEGMENT_NORM, PAGE_SM);
1137-
}
1138-
11391087
void heap_clear_metadata() { HeapState::instance().clear_metadata(); }
11401088

11411089
bool heap_init_reserved(void *reserved_base, size_t size) {
11421090
return HeapState::instance().init_reserved(reserved_base, size);
11431091
}
11441092

1145-
bool heap_add_segment_from_reserved(segment_kind_t kind) {
1146-
return HeapState::instance().add_segment_from_reserved(kind, PAGE_SM);
1147-
}
1148-
11491093
bool heap_add_segment_for_class(page_kind_t kind) {
1150-
return HeapState::instance().add_segment_from_reserved(SEGMENT_NORM, kind);
1094+
return HeapState::instance().add_segment_from_reserved(kind);
11511095
}
11521096

11531097
void *heap_alloc(size_t size) { return HeapState::instance().allocate(size); }

0 commit comments

Comments
 (0)