5 #define __STDC_FORMAT_MACROS
25 const int32_t block_size)
33 if (p->
size >= block_size) {
37 if (p == head_avail_) {
62 char prior_tag = *(
reinterpret_cast<char *
>(block_ctl) - 1);
63 assert((prior_tag == kTagAvail) || (prior_tag == kTagReserved));
65 int32_t new_size = block_ctl->
size();
69 if (prior_tag == kTagAvail) {
74 new_size += prior_size;
76 reinterpret_cast<char *
>(block_ctl) - prior_size);
78 UnlinkAvailBlock(new_avail);
79 if (rover_ == new_avail)
83 int32_t succ_size = *
reinterpret_cast<int32_t *
>(
84 reinterpret_cast<char *
>(new_avail) + new_size);
88 reinterpret_cast<char *
>(new_avail) + new_size);
89 UnlinkAvailBlock(succ_avail);
90 new_size += succ_size;
91 if (rover_ == succ_avail)
96 new_avail->
size = new_size;
97 new (AvailBlockTag::GetTagLocation(new_avail))
AvailBlockTag(new_size);
99 EnqueueAvailBlock(new_avail);
111 block->
link_next = head_avail_->ConvertToLink(arena_);
143 if (total_size < kMinBlockSize)
144 total_size = kMinBlockSize;
151 return ReserveBlock(p, total_size);
162 : arena_(reinterpret_cast<char *>(sxmmap_align(arena_size)))
163 , head_avail_(reinterpret_cast<
AvailBlockCtl *>(arena_ + sizeof(uint64_t)))
164 , rover_(head_avail_)
166 , arena_size_(arena_size)
172 const unsigned char padding = 7;
175 (
sizeof(uint64_t) +
sizeof(
AvailBlockCtl) + padding + 1 +
sizeof(int32_t));
176 assert((usable_size % 8) == 0);
186 free_block->
size = usable_size;
196 *(
reinterpret_cast<char *
>(free_block) - 1) =
kTagReserved;
208 unsigned char pattern)
216 int usable_size = free_block->
size -
219 memset(free_block + 1, pattern, usable_size);
240 int32_t remaining_size = block->
size - block_size;
243 block_size += remaining_size;
248 if (remaining_size == 0) {
256 char *new_block =
reinterpret_cast<char *
>(block) + remaining_size;
MallocArena(unsigned arena_size)
AvailBlockCtl * FindAvailBlock(const int32_t block_size)
void ShrinkTo(int32_t smaller_size)
static void * GetTagLocation(AvailBlockCtl *block)
assert((mem||(size==0))&&"Out Of Memory")
void UnlinkAvailBlock(AvailBlockCtl *block)
int32_t ConvertToLink(char *base)
static const int kMinBlockSize
void * ReserveBlock(AvailBlockCtl *block, int32_t block_size)
AvailBlockCtl * GetNextPtr(char *base)
void EnqueueAvailBlock(AvailBlockCtl *block)
static const char kTagReserved
static MallocArena * CreateInitialized(unsigned arena_size, unsigned char pattern)
AvailBlockCtl * head_avail_
static uint64_t RoundUp8(const uint64_t size)
AvailBlockCtl * GetPrevPtr(char *base)
void * Malloc(const uint32_t size)
char * arena_
The memory block.
uint32_t GetSize(void *ptr) const