My memory Allocator

Source: Internet
Author: User

ObjectPool.h

#include <stddef.h>classobjectpool{Private: Objectpool (unsignedintStep, unsignedintsize); ~Objectpool (); Public:    Staticobjectpool&Getsingleton (); void*alloc (size_t size); voidDeallocvoid*ptr, size_t size);Private:    classObjectpoolimpl; Objectpoolimpl*Pool; unsignedintsize; unsignedintstep;};#defineUseobjectpool static void* operator new (size_t size) {return Objectpool::getsingleton (). Alloc (size);} static void operator delete (void *ptr, size_t size) {Objectpool::getsingleton (). Dealloc (ptr, size);}

ObjectPool.cpp

#include"ObjectPool.h"#include"KRYMalloc.h"#include<stdio.h>structchunkdata{unsignedintfreenodesize; Chunkdata*next;};structfreenode{unsignedintID; intFlag; Freenode*next;};StaticUnsignedintFreenodeoffect =Offsetof (Freenode, next);StaticUnsignedintNumnodeperchunk = the;classobjectpool::objectpoolimpl{ Public: Objectpoolimpl () {}voidInit (unsignedintsize) {Chunkhead=0; Freehead=0; Freenodesize= Freenodeoffect +size; if(sizeof(freenode*) >size) {Freenodesize= Freenodeoffect +sizeof(freenode*); } chunkdatasize=sizeof(chunkdata) + freenodesize *Numnodeperchunk; }    ~Objectpoolimpl () {Chunkdata*cur =0;  while(chunkhead) {cur=Chunkhead; Chunkhead= chunkhead->Next;        Kry_free (cur); }    }    void*Alloc () {freenode*node; Chunkdata*Chunk; Char*temp; if(0==freehead) {Chunk= (chunkdata*) Kry_malloc (chunkdatasize); Chunk->freenodesize =Numnodeperchunk; Chunk->next =Chunkhead; Chunkhead=Chunk; Temp= (Char*) Chunk +sizeof(Chunkdata);  for(unsignedinti =0; i < Numnodeperchunk; ++i) {node= (freenode*) temp; Node->id =i; Node->flag =0; Node->next =Freehead; Freehead=node; Temp+=freenodesize; }} node=Freehead; Freehead= freehead->Next; Temp= (Char*) Node-sizeof(chunkdata)-Node->id *freenodesize; Chunk= (chunkdata*) temp; Chunk->freenodesize-=1; if(0! = node->flag) {            intError =0; printf ("Object Alloc error\n"); } node->flag + =1; return&node->Next; }    voidDeallocvoid*ptr) {        Char*temp = (Char*) PTR-Freenodeoffect; Freenode*node = (freenode*) temp; Temp-=sizeof(chunkdata) + Node->id *freenodesize; Chunkdata*chunk = (chunkdata*) temp; Chunk->freenodesize + =1; if(1! = node->flag) {            intError =0; printf ("Object Dealloc error\n"); } node->flag-=1; Node->next =Freehead; Freehead=node; }Private: Chunkdata*Chunkhead; Freenode*Freehead; unsignedintfreenodesize; unsignedintChunkdatasize;};o Bjectpool::objectpool (unsignedintStep, unsignedintsize) {     This->step =step;  This->size =size; Pool=NewObjectpoolimpl[size];  for(unsignedinti =0; i < size; ++i) {pool[i].init (step* i +step); }}objectpool::~Objectpool () {Delete[] pool;} Objectpool&Objectpool::getsingleton () {StaticObjectpool Singleton (sizeof(int*), -); returnSingleton;}void*Objectpool::alloc (size_t size) {//return Kry_malloc (size);    void*data =0; if(Size > This->step * This-size) {Data=kry_malloc (size); }    Else{Data= pool[(size + step –1)/Step-1].alloc (); }    returndata;}voidObjectpool::d Ealloc (void*ptr, size_t size) {    //Kry_free (PTR); return;    if(Size > This->step * This-size)    {Kry_free (PTR); }    Else{pool[(size+ Step-1)/Step-1].dealloc (PTR); }}

My memory Allocator

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.