MemoryPool内存池(二)

本文将介绍如何实现一个简单的固定内存池。通过MemoryPool.h和MemoryPool.cpp两个文件,详细阐述内存池的创建与使用。
摘要由CSDN通过智能技术生成

前面对内存池已经有了一个大致的复习,现在实现一个简单的固定内存池
   
一、MemoryPool.h

#ifndef __SOFT_COLLECTION_H__
#define __SOFT_COLLECTION_H__

#include <iostream>
#include <string>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <memory.h>

#define MEMPOOL_ALIGNMENT 8

struct memory_block
{
   
	//参数直接赋值
	memory_block(unsigned short type = 1, unsigned short unit_size = 0);
	virtual ~memory_block(){
   };
		
	unsigned short size;
	unsigned short free;
	unsigned short first;
	//unsigned short dummy_align;
	
	memory_block* next;
	char data[1];
	
		
	static void* operator new(size_t, unsigned short type, unsigned short unit_size)
	{
   
		return ::operator new(sizeof(memory_block) + type * unit_size);
	}
	
	static void operator delete(void* p, size_t)
	{
   
		::operator delete(p);
	}
};
	
class memory_pool
{
   
public:
	//第一个参数动态分配(构建对象时分配),后两个参数直接赋值
	memory_pool(unsigned short _unit_size, unsigned short _init_size 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值