STL source code analysis: Space Configurator

Source: Internet
Author: User

After reading the rewrite, I read it again without knowing it. I didn't consider multithreading in it. I mainly wanted to implement the following memory pool.

Mempool. h


#ifndef MEMPOOL_H_#define MEMPOOL_H_#include <cstddef>#include <new>#include <cstdlib>namespace flysnow {enum {STEP_ = 8};enum {MAX_BYTES_ = 128};enum {FREELIST_NUM_ = MAX_BYTES_/STEP_};class Mempool{private:    union pool_bucket {        union pool_bucket* next_node;        char bucket_addr[1];    };    static size_t up_to_nearest(size_t n) {        return STEP_*((n>>3) + (n%8 ? 1:0));    }    static size_t freelist_index(size_t n) {        return ((n>>3) + (n%8 ? 0:-1));    }    static void* refill(size_t n);    static char* alloc_memory(size_t size,int &pool_node_num);    static pool_bucket* free_list_[FREELIST_NUM_];    static char* start_pool;    static char* end_pool;    static size_t heap_size;public:    static void* allocate(size_t n);    static void deallocate(void *p,size_t n);    static size_t get_heap_size() {return heap_size;}};}#endif


Mempool. cpp

# Include "mempool. H "namespace flysnow {char * mempool: start_pool = 0; char * mempool: end_pool = 0; size_t mempool: heap_size = 0; mempool: pool_bucket * mempool :: free_list _ [freelist_num _] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; void * mempool: allocate (size_t N) {If (n> max_bytes _) {return malloc (n);} pool_bucket * P; pool_bucket ** free_list_index = free_list _ + freelist_index (n); P = * free_list_index; if (0 = P) {void * q = refill (up_to_nearest (n); Return Q;} * free_list_index = p-> next_node; return P;} void mempool :: deallocate (void * P, size_t N) {pool_bucket * q = (pool_bucket *) P; If (n> max_bytes _) {free (p); return ;} pool_bucket ** free_list_index = free_list _ + freelist_index (n); q-> next_node = * free_list_index; * free_list_index = Q;} void * mempool: refill (size_t N) {int pool_node_num = 20; int I; pool_bucket ** free_list_index = free_list _ + freelist_index (n); char * Pool = alloc_memory (n, pool_node_num); if (1 = pool_node_num) {return pool ;} pool_bucket * result = (pool_bucket *) pool; pool_bucket * Current, * Next; pool + = N; * free_list_index = next = (pool_bucket *) pool; next = (pool_bucket *) pool; for (I = 1; ++ I) {current = next; next = (pool_bucket *) (char *) next + n ); if (pool_node_num-1 = I) {current-> next_node = NULL; break;} else {current-> next_node = next;} return result;} Char * mempool: alloc_memory (size_t size, int & outcome) {size_t total_size = size * bytes; size_t remain_size = end_pool-start_pool; If (total_size <= remain_size) {char * result = start_pool; start_pool + = total_size; return result;} else if (remain_size> = size) {pool_node_num = remain_size /Size; size_t return_size = pool_node_num * size; char * result = start_pool; start_pool + = return_size; return result ;} else {size_t get_size = 2 * total_size + up_to_nearest (heap_size> 4); If (remain_size> 0) {// if the remaining 7 bytes exist, they will be placed in the free linked list of 8 bytes, however, in the end, when allocate 8 byte //, will it fail? // Previously, the memory allocated by get_size was always a multiple of 8, so the above situations would not occur .... Required ** free_list_index = free_list _ + freelist_index (remain_size); (optional *) start_pool)-> next_node = * free_list_index; * free_list_index = (pool_bucket *) start_pool ;} start_pool = (char *) malloc (get_size); If (0 = start_pool) {pool_bucket ** free_list_index; pool_bucket * P; For (INT I = size; I <= max_bytes _; I + = step _) {free_list_index = free_list _ + freelist_index (I); P = * free_list_index; I F (0! = P) {* free_list_index = p-> next_node; start_pool = (char *) P; end_pool = start_pool + I; Return (alloc_memory (size, pool_node_num ));}} end_pool = 0; start_pool = (char *) malloc (get_size);} heap_size + = get_size; end_pool = start_pool + get_size; // return (alloc_memory (size, pool_node_num )); char * result = start_pool; start_pool + = total_size; return result ;}}}


Main. cpp

#include <iostream>#include "mempool.h"using namespace std;int main(){    char *p;    for(size_t i=0;i<100000;++i){       p = (char*)flysnow::Mempool::allocate(i%1029+1);       cout<<flysnow::Mempool::get_heap_size()<<' ';    }    cout << endl;    system("PAUSE");    return 0;}



STL source code analysis: Space Configurator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.