Пушыстый (_winnie) wrote,
Пушыстый
_winnie

С++ fixed size allocator

Оказывается, я могу написать простейший fixed-size аллокатор быстрее, чем компилируется проект под boost. Старая закалка разработчика игр!


#include <stdlib.h> const int SIZE = 256; void *first_free = NULL; void new_block() { const int LARGE_BLOCK_SIZE = 1024*1000; char *block = (char*)malloc(LARGE_BLOCK_SIZE); const int N_BLOCKS = LARGE_BLOCK_SIZE / SIZE; for (int i = 0; i < N_BLOCKS; ++i) { *(void**)(block + i * SIZE) = (i != N_BLOCKS - 1) ? (block + (i+1) * SIZE) : NULL; } first_free = block; } void *fixed_alloc() { if (!first_free) new_block(); void *result = first_free; first_free = *(void **)first_free; return result; } void fixed_free(void *ptr) { *(void**)ptr = first_free; first_free = ptr; }
_Winnie C++ Colorizer

Tags: c++
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 47 comments