This source file includes following definitions.
- LgFloor
- AlignmentForSize
- NumMoveSize
- Init
- MetaDataAlloc
- metadata_system_bytes
#include "config.h"
#include "common.h"
#include "system-alloc.h"
namespace tcmalloc {
static inline int LgFloor(size_t n) {
int log = 0;
for (int i = 4; i >= 0; --i) {
int shift = (1 << i);
size_t x = n >> shift;
if (x != 0) {
n = x;
log += shift;
}
}
ASSERT(n == 1);
return log;
}
int AlignmentForSize(size_t size) {
int alignment = kAlignment;
if (size > kMaxSize) {
alignment = kPageSize;
} else if (size >= 128) {
alignment = (1 << LgFloor(size)) / 8;
} else if (size >= 16) {
alignment = 16;
}
if (alignment > kPageSize) {
alignment = kPageSize;
}
CHECK_CONDITION(size < 16 || alignment >= 16);
CHECK_CONDITION((alignment & (alignment - 1)) == 0);
return alignment;
}
int SizeMap::NumMoveSize(size_t size) {
if (size == 0) return 0;
int num = static_cast<int>(64.0 * 1024.0 / size);
if (num < 2) num = 2;
if (num > 32) num = 32;
return num;
}
void SizeMap::Init() {
if (ClassIndex(0) < 0) {
Log(kCrash, __FILE__, __LINE__,
"Invalid class index for size 0", ClassIndex(0));
}
if (ClassIndex(kMaxSize) >= sizeof(class_array_)) {
Log(kCrash, __FILE__, __LINE__,
"Invalid class index for kMaxSize", ClassIndex(kMaxSize));
}
int sc = 1;
int alignment = kAlignment;
CHECK_CONDITION(kAlignment <= 16);
for (size_t size = kAlignment; size <= kMaxSize; size += alignment) {
alignment = AlignmentForSize(size);
CHECK_CONDITION((size % alignment) == 0);
int blocks_to_move = NumMoveSize(size) / 4;
size_t psize = 0;
do {
psize += kPageSize;
while ((psize % size) > (psize >> 3)) {
psize += kPageSize;
}
} while ((psize / size) < (blocks_to_move));
const size_t my_pages = psize >> kPageShift;
if (sc > 1 && my_pages == class_to_pages_[sc-1]) {
const size_t my_objects = (my_pages << kPageShift) / size;
const size_t prev_objects = (class_to_pages_[sc-1] << kPageShift)
/ class_to_size_[sc-1];
if (my_objects == prev_objects) {
class_to_size_[sc-1] = size;
continue;
}
}
class_to_pages_[sc] = my_pages;
class_to_size_[sc] = size;
sc++;
}
if (sc != kNumClasses) {
Log(kCrash, __FILE__, __LINE__,
"wrong number of size classes: (found vs. expected )", sc, kNumClasses);
}
int next_size = 0;
for (int c = 1; c < kNumClasses; c++) {
const int max_size_in_class = class_to_size_[c];
for (int s = next_size; s <= max_size_in_class; s += kAlignment) {
class_array_[ClassIndex(s)] = c;
}
next_size = max_size_in_class + kAlignment;
}
for (size_t size = 0; size <= kMaxSize; size++) {
const int sc = SizeClass(size);
if (sc <= 0 || sc >= kNumClasses) {
Log(kCrash, __FILE__, __LINE__,
"Bad size class (class, size)", sc, size);
}
if (sc > 1 && size <= class_to_size_[sc-1]) {
Log(kCrash, __FILE__, __LINE__,
"Allocating unnecessarily large class (class, size)", sc, size);
}
const size_t s = class_to_size_[sc];
if (size > s || s == 0) {
Log(kCrash, __FILE__, __LINE__,
"Bad (class, size, requested)", sc, s, size);
}
}
for (size_t cl = 1; cl < kNumClasses; ++cl) {
num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl));
}
}
static uint64_t metadata_system_bytes_ = 0;
void* MetaDataAlloc(size_t bytes) {
void* result = TCMalloc_SystemAlloc(bytes, NULL);
if (result != NULL) {
metadata_system_bytes_ += bytes;
}
return result;
}
uint64_t metadata_system_bytes() { return metadata_system_bytes_; }
}