Add documentation to `fiber_pool_allocate_memory`.

This commit is contained in:
Samuel Williams 2019-07-19 20:18:42 +12:00
Родитель 517f7f9b57
Коммит 0a7093a8e9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A0765423A44728FB
1 изменённых файлов: 7 добавлений и 0 удалений

7
cont.c
Просмотреть файл

@ -396,6 +396,12 @@ fiber_pool_vacancy_initialize(struct fiber_pool * fiber_pool, struct fiber_pool_
inline static void * inline static void *
fiber_pool_allocate_memory(size_t * count, size_t stride) fiber_pool_allocate_memory(size_t * count, size_t stride)
{ {
// We use a divide-by-2 strategy to try and allocate memory. We are trying
// to allocate `count` stacks. In normal situation, this won't fail. But
// if we ran out of address space, or we are allocating more memory than
// the system would allow (e.g. overcommit * physical memory + swap), we
// divide count by two and try again. This condition should only be
// encountered in edge cases, but we handle it here gracefully.
while (*count > 1) { while (*count > 1) {
#if defined(_WIN32) #if defined(_WIN32)
void * base = VirtualAlloc(0, (*count)*stride, MEM_COMMIT, PAGE_READWRITE); void * base = VirtualAlloc(0, (*count)*stride, MEM_COMMIT, PAGE_READWRITE);
@ -411,6 +417,7 @@ fiber_pool_allocate_memory(size_t * count, size_t stride)
void * base = mmap(NULL, (*count)*stride, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0); void * base = mmap(NULL, (*count)*stride, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
if (base == MAP_FAILED) { if (base == MAP_FAILED) {
// If the allocation fails, count = count / 2, and try again.
*count = (*count) >> 1; *count = (*count) >> 1;
} }
else { else {