Memory management: default new & delete vs. allocator & deallocator using a pool

/***********************************************************************************************
All rights reserved by Allen Young. Welcome to communicate and discuss. If you want to quote this article, please declare its origin. Thank you!
***********************************************************************************************/

C++ provides two operators for dynamic memory management: new and delete. They are general operators, which means they can handle requests of different amount of memory. It seems to be perfect, however, the execution performance and the space efficiency are not good enough sometimes. Their flexibilities leave some room for improvement in performance and efficiency, and this is the main reason that you want to write your own version of new and delete.

    First of all, you should know that the execution process of new is divided into two steps: first allocate enough raw space, if succeeds, then construct the object and place it in the space; if fails, return immediately (actually is more complex than just return, but it is not related to the topic of this article). So it is with delete: first destruct the object, then deallocate the space. This article focuses on the allocation and deallocation of memory space (the construction and destruction is covered in another article).

    Operator new always give you more memory than you request. You may doubt about it, but it really works in that way. Let us see the form of using delete: it has only one argument which is the pointer of memory to give back to the heap. Ok, but how large is this memory? It doesn't know! So, when new is performed, it adds some extra space, which is used to store the size of the requested memory, to the head of the memory block given to user. And then delete uses that information besides the pointer passed in, to do the cleanup. This is transparent to the user, but it can lead to very low space efficiency when there are many small objects in your program because a large part of the allocated memory is used for bookkeeping.

    How to avoid this waste? The answer is using a memory pool. It can improve the space efficiency, as well as the execution performance, by using a linked list to manage the small memory blocks. At the first time that newing an object, allocate a large memory block and make it a linked list, in which the nodes are of the same size as that object. Then take one node out for use when newing, and put it back to the list when deleting. Many materials have talked about this. The following example and its implementation is from Item 10 in <Effective C++>, by Scott Meyers (Slight changes are made).

//Airplane.h
#ifndef _AIRPLANE_H
#define  _AIRPLANE_H

#include 
< cstddef >

//  this class represents the real information of an airplane
class  AirplaneReq  {
  
// many data and method members
}
;

//  this class holds the address of a real airplane
class  Airplane  {
 
public:
  
static void * operator new(size_t);
  
static void operator delete(void *, size_t);
 
public:
  union 
{
    AirplaneReq 
* req;
    Airplane 
*    next;
  }
;
  
static const int BLOCK_SIZE;
  
static Airplane * head_of_free_list;
}
;

#endif

    A little trick is used here: a union holds both the pointer to the real representations of a plane, and the pointer to next Airplane block. There is no conflict, because the pointer to next block is used only when that block is in the linked list. When allocated successfully, the pointer to the real representations is used only.

//Airplane.cpp
#include  " Airplane.h "

const   int  Airplane::BLOCK_SIZE  =   128 ;
Airplane 
*  Airplane::head_of_free_list  =   0 ;

void   *  Airplane:: operator   new (size_t size)  {
  
if(size != sizeof(Airplane))
    
return ::operator new(size);

  Airplane 
* p = head_of_free_list;

  
if(p) {
    head_of_free_list 
= p->next;
  }

  
else {
    
// create the link list
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值