首页 > 安全资讯 >

内存池代码(chunk模块)

16-12-30

内存池代码(chunk模块)。

#ifndef JY_CHUNK_H_  
#define JY_CHUNK_H_  
  
enum status  
{  
    busy = 1,  
    idle = 2  
};  
  
class JYChunk  
{  
    public:  
        JYChunk(int block_size = 1024);  
        ~JYChunk();  
        void*       Data();  
        int         GetStatus();  
        int         GetBlockSize();  
        int         SetBlockSize(int block_size);  
        int         SetStatus(int status);  
    private:  
        void*       m_data;  
        int         m_status;  
        int         m_block_size;  
};  
#endif
#include "JYChunk.h"  
#include <stdio.h>  
#include <stdlib.h>  
  
  
JYChunk::JYChunk(int block_size)  
{  
    m_block_size = block_size;  
    m_data = (void *)malloc(m_block_size);  
    m_status = idle;  
}  
  
  
JYChunk::~JYChunk()  
{  
    if(m_data)  
    {  
                    free(m_data);  
                    m_block_size = 0;  
    }  
}  
  
  
void* JYChunk::Data()  
{  
    if(m_data)  
    {  
                    return m_data;  
    }  
    return NULL;  
}  
  
  
int JYChunk::GetStatus()  
{  
    return m_status;  
}  
  
  
int JYChunk::GetBlockSize()  
{  
    return m_block_size;  
}  
  
  
int JYChunk::SetBlockSize(int block_size)  
{  
    m_block_size = block_size;  
}  
  
  
int JYChunk::SetStatus(int status)  
{  
    m_status = status;  
}
相关文章
最新文章
热点推荐