频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
[C++]内存管理器--谈论如何自定义内存分配机制
2016-05-16 09:34:06         来源:stary_yan的博客  
收藏   我要投稿

Memory pools, also called fixed-size blocks allocation, is the use of pools for memory management that allows dynamic memory allocation comparable to malloc or C++’s operator new. As those implementations suffer from fragmentation because of variable block sizes, it is not recommendable to use them in a real time system due to performance. A more efficient solution is preallocating a number of memory blocks with the same size called the memory pool. The application can allocate, access, and free blocks represented by handles at run time.
(from wikipedia.)

这段话介绍了内存池的作用。大概就是预先分配一段内存来进行操作,可以简化一个一个分配内存所消耗的时间。

问题产生

In C++ memory pool is abstract data structure for memory allocation in fixed-size block. You should know that, in C++, a new operation is “expensive” because the default std allocator will do the following two things:

Call malloc function to allocate memory blocks which needs to traversal the free blocks of the system heap and choose the best blocks for allocation. Call the constructor of the object.

It cost a lot of time for operation.

However, a memory pool, say a memory manager here, is a good opportunity for programmers to find out a solution to mange some “frequently” allocating memory blocks manually which is more efficient.

实际上就是说,使用new操作实际上是很低效的。因为编译要在内存空间寻找一块刚好适宜你使用的内存块。这个寻找是非常慢的。在本例中会导致超时。

背景知识

Placement Syntax

In the C++ programming language, placement syntax allows programmers to explicitly specify the memory management of inpidual objects — i.e. their “placement” in memory. Normally, when an object is created dynamically, an allocation function is invoked in such a way that it will both allocate memory for the object, and initialize the object within the newly allocated memory. The placement syntax allows the programmer to supply additional arguments to the allocation function. A common use is to supply a pointer to a suitable region of storage where the object can be initialized, thus separating memory allocation from object construction.

正常的new运算实际上是把分配内存和初始化统一起来的。而占位new可以把这两个操作分隔开来。

The “placement” versions of the new and delete operators and functions are known as placement new and placement delete. A new expression, placement or otherwise, calls a new function, also known as an allocator function, whose name is operator new. Similarly, a delete expression calls a delete function, also known as a deallocator function, whose name is operator delete.

就比如说:

char* ptr = new char[sizeof(T)]; // allocate memory
T* tptr = new(ptr) T;            // construct in allocated storage ("place")
tptr->~T();                      // destruct
delete[] ptr;                    // deallocate memory

(如果没有看明白,后面还有更详细的介绍。)

Allocator

In C++ computer programming, allocators are an important component of the C++ Standard Library. The standard library provides several data structures, such as list and set, commonly referred to as containers. A common trait among these containers is their ability to change size during the execution of the program. To achieve this, some form of dynamic memory allocation is usually required. Allocators handle all the requests for allocation and deallocation of memory for a given container. The C++ Standard Library provides general-purpose allocators that are used by default, however, custom allocators may also be supplied by the programmer.

Usually, you will find a second parameter in stl containers such as vectror, list and etc. And the stl classes have a default allocator for the template parameter, you do not need to specify it usually. But this time, you need to write a allocator yourself indicate the class memory manager.

比如说以下的例子:

#include 
#include 

namespace MyLib {
   template 
   class MyAlloc {
     public:
       // type definitions
       typedef T        value_type;
       typedef T*       pointer;
       typedef const T* const_pointer;
       typedef T&       reference;
       typedef const T& const_reference;
       typedef std::size_t    size_type;
       typedef std::ptrdiff_t difference_type;

       // rebind allocator to type U
       template 
       struct rebind {
           typedef MyAlloc other;
       };

       // return address of values
       pointer address (reference value) const {
           return &value;
       }
       const_pointer address (const_reference value) const {
           return &value;
       }

       /* constructors and destructor
        * - nothing to do because the allocator has no state
        */
       MyAlloc() throw() {
       }
       MyAlloc(const MyAlloc&) throw() {
       }
       template 
         MyAlloc (const MyAlloc&) throw() {
       }
       ~MyAlloc() throw() {
       }

       // return maximum number of elements that can be allocated
       size_type max_size () const throw() {
           return std::numeric_limits::max() / sizeof(T);
       }

       // allocate but don't initialize num elements of type T
       pointer allocate (size_type num, const void* = 0) {
           // print message and allocate memory with global new
           std::cerr << "allocate " << num << " element(s)"
                     << " of size " << sizeof(T) << std::endl;
           pointer ret = (pointer)(::operator new(num*sizeof(T)));
           std::cerr << " allocated at: " << (void*)ret << std::endl;
           return ret;
       }

       // initialize elements of allocated storage p with value value
       void construct (pointer p, const T& value) {
           // initialize memory with placement new
           new((void*)p)T(value);
       }

       // destroy elements of initialized storage p
       void destroy (pointer p) {
           // destroy objects by calling their destructor
           p->~T();
       }

       // deallocate storage p of deleted elements
       void deallocate (pointer p, size_type num) {
           // print message and deallocate memory with global delete
           std::cerr << "deallocate " << num << " element(s)"
                     << " of size " << sizeof(T)
                     << " at: " << (void*)p << std::endl;
           ::operator delete((void*)p);
       }
   };

   // return that all specializations of this allocator are interchangeable
   template 
   bool operator== (const MyAlloc&,
                    const MyAlloc&) throw() {
       return true;
   }
   template 
   bool operator!= (const MyAlloc&,
                    const MyAlloc&) throw() {
       return false;
   }
}

问题产生

在这个程序中我们要求分配十万级的内存,如果使用STL 默认的分配机制一定会超过1s(可以去掉注释看看效果。)所以要想不超时,方法就是自己定义分配机制。

#ifndef STACK_ALLOC_H
#define STACK_ALLOC_H

#include 
#include 

template 
struct StackNode_ {
  T data;
  StackNode_* prev;
};

/** T is the object to store in the stack, Alloc is the allocator to use */
template  >
class StackAlloc {
 public:
  typedef StackNode_ Node;
  typedef typename Alloc::template rebind::other allocator;

  /** Default constructor */
  StackAlloc() { head_ = 0; }
  /** Default destructor */
  ~StackAlloc() { clear(); }

  /** Returns true if the stack is empty */
  bool empty() { return (head_ == 0); }

  /** Deallocate all elements and empty the stack */
  void clear() {
    Node* curr = head_;
    while (curr != 0) {
      Node* tmp = curr->prev;
      allocator_.destroy(curr);
      allocator_.deallocate(curr, 1);
      curr = tmp;
    }
    head_ = 0;
  }

  /** Put an element on the top of the stack */
  void push(T element) {
    Node* newNode = allocator_.allocate(1);

    allocator_.construct(newNode, Node());
    newNode->data = element;

    newNode->prev = head_;

    head_ = newNode;
  }

  /** Remove and return the topmost element on the stack */
  T pop() {
    T result = head_->data;
    Node* tmp = head_->prev;
    allocator_.destroy(head_);
    allocator_.deallocate(head_, 1);
    head_ = tmp;
    return result;
  }

  /** Return the topmost element */
  T top() { return (head_->data); }

 private:
  allocator allocator_;
  Node* head_;
};

#endif  // !STACK_ALLOC_H

#include 
#include 
#include 
#include 

#include "MemoryManager.hpp"
#include "StackAlloc.hpp"

/* Adjust these values depending on how much you trust your computer */
 #define ELEMS 520000
#define REPS 50

int main() {
  clock_t start;
//  int ELEMS;
//  std::cin >> ELEMS;
//   Use the default allocator
//   StackAlloc > stackDefault;
//    
//   start = clock();
//   for (int j = 0; j < REPS; j++) {
//       
//     assert(stackDefault.empty());
//     for (int i = 0; i < ELEMS / 4; i++) {
//       // Unroll to time the actual code and not the loop
//       stackDefault.push(i);
//       stackDefault.push(i);
//       stackDefault.push(i);
//       stackDefault.push(i);
//     }
//     for (int i = 0; i < ELEMS / 4; i++) {
//       // Unroll to time the actual code and not the loop
//       stackDefault.pop();
//       stackDefault.pop();
//       stackDefault.pop();
//       stackDefault.pop();
//     }
//   }
//   std::cout << "Default Allocator Time: ";
//   std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";

  /* Use MemoryManager */
  StackAlloc > stackPool;
  start = clock();
  for (int j = 0; j < REPS; j++) {
    assert(stackPool.empty());

    for (int i = 0; i < ELEMS / 4; i++) {
      // Unroll to time the actual code and not the loop
      stackPool.push(i);

      stackPool.push(i);

      stackPool.push(i);

      stackPool.push(i);

    }

    for (int i = 0; i < ELEMS / 4; i++) {
      // Unroll to time the actual code and not the loop
      stackPool.pop();

      stackPool.pop();
      stackPool.pop();
      stackPool.pop();
    }
  }

  std::cout << "Done Processing allocation." << std::endl;
  std::cout << "You can run the program locally to check the difference "
               "between MemoryManager and std::Alloc"
            << std::endl;
   std::cout << "MemoryManager Allocator Time: ";
   std::cout << (((double)clock() - start) / CLOCKS_PER_SEC) << "\n\n";

  return 0;
}

补充知识

placement new

这里给出更详细的知识介绍。

placement new是重载operator new的一个标准、全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本)。

它的原型如下:

void *operator new( size_t, void *p ) throw()  { return p; }

区别new、operator new、placement new

首先我们区分下几个容易混淆的关键词:new、operator new、placement new

new和delete操作符我们应该都用过,它们是对堆中的内存进行申请和释放,而这两个都是不能被重载的。要实现不同的内存分配行为,需要重载operator new,而不是new和delete。

看如下代码:

class MyClass {…};

MyClass * p=new MyClass;

这里的new实际上是执行如下3个过程:

1调用operator new分配内存;

2调用构造函数生成类对象;

3返回相应指针。

operator new就像operator+一样,是可以重载的,但是不能在全局对原型为void operator new(size_t size)这个原型进行重载,一般只能在类中进行重载。如果类中没有重载operator new,那么调用的就是全局的::operator new来完成堆的分配。同理,operator new[]、operator delete、operator delete[]也是可以重载的,一般你重载了其中一个,那么最好把其余三个都重载一遍。

placement new是operator new的一个重载版本,只是我们很少用到它。如果你想在已经分配的内存中创建一个对象,使用new是不行的。也就是说placement new**允许你在一个已经分配好的内存中(栈或堆中)构造一个新的对象。原型中void*p实际上就是指向一个已经分配好的内存缓冲区的的首地址**。

我们知道使用new操作符分配内存需要在堆中查找足够大的剩余空间,这个操作速度是很慢的,而且有可能出现无法分配内存的异常(空间不够)。placement new就可以解决这个问题。我们构造对象都是在一个预先准备好了的内存缓冲区中进行,不需要查找内存,内存分配的时间是常数;而且不会出现在程序运行中途出现内存不足的异常。所以,placement new非常适合那些对时间要求比较高,长时间运行不希望被打断的应用程序。

使用方法如下:

缓冲区提前分配

可以使用堆的空间,也可以使用栈的空间,所以分配方式有如下两种:

class MyClass {…}; 
char *buf=new char[N*sizeof(MyClass)+ sizeof(int) ] ; 或者char buf[N*sizeof(MyClass)+ sizeof(int) ];
对象的构造
MyClass * pClass=new(buf) MyClass;
对象的销毁
一旦这个对象使用完毕,你必须显式的调用类的析构函数进行销毁对象。但此时内存空间不会被释放,以便其他的对象的构造。

pClass->~MyClass();

内存的释放
如果缓冲区在堆中,那么调用delete[] buf;进行内存的释放;如果在栈中,那么在其作用域内有效,跳出作用域,内存自动释放。

注意:

1)在C++标准中,对于placement operator new []有如下的说明: placement operator new[] needs implementation-defined amount of additional storage to save a size of array. 所以我们必须申请比原始对象大小多出sizeof(int)个字节来存放对象的个数,或者说数组的大小。

2)使用方法第二步中的new才是placement new,其实是没有申请内存的,只是调用了构造函数,返回一个指向已经分配好的内存的一个指针,所以对象销毁的时候不需要调用delete释放空间,但必须调用析构函数销毁对象。


实际上是可以显式调用构造函数和析构函数的。

这有什么用?

有时候,在对象的生命周期结束前,想先结束这个对象的时候就会派上用场了。

new的时候,其实做了两件事,一是:调用malloc分配所需内存,二是:调用构造函数。

delete的时候,也是做了两件事,一是:调用析造函数,二是:调用free释放内存。

int _tmain(int argc, _TCHAR* argv[])
{
    MyClass* pMyClass = (MyClass*)malloc(sizeof(MyClass));
     pMyClass->MyClass();
    // …
}

编译pMyClass->MyClass()出错:

error C2273: ‘function-style cast’ : illegal as right side of ‘->’operator

天啊,它以为MyClass是这个类型。

解决办法有两个:

第一:pMyClass->MyClass::MyClass();

第二:new(pMyClass)MyClass();

第二种用法涉及C++ placement new 的用法 。

placement new的作用就是:创建对象(调用该类的构造函数)但是不分配内存,而是在已有的内存块上面创建对象。用于需要反复创建并删除的对象上,可以降低分配释放内存的性能消耗。请查阅placement new相关资料。

显示调用构造函数有什么用?

有时候,你可能由于效率考虑要用到malloc去给类对象分配内存,因为malloc是不调用构造函数的,所以这个时候会派上用场了。

另外下面也是可以的,虽然内置类型没有构造函数。

int* i = (int*)malloc(sizeof(int));
new (i) int();

感觉这些奇奇怪怪的用法最好在写代码库时,为了达到某个目时去使用,不推荐应用开发时使用。

#include 
using namespace std;

class MyClass
{
public:
    MyClass()
    {
        cout << "Constructors" << endl;
    }
    ~MyClass()
    {
        cout << "Destructors" << endl;
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
   MyClass* pMyClass =  new MyClass;
   pMyClass->~MyClass();
   delete pMyClass;

}

 

问题解决

#define __STL_NOTHROW throw()
#include 

template 
class MemoryManager {
public:
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef _Tp* pointer;
    typedef const _Tp* const_pointer;
    typedef _Tp& reference;
    typedef const _Tp& const_reference;
    typedef _Tp value_type;

    template 
    struct rebind {
        typedef MemoryManager<_Tp1> other;
    };

    MemoryManager() __STL_NOTHROW : _M_pool(NULL) {}
    MemoryManager(const MemoryManager&) __STL_NOTHROW : _M_pool(NULL) {}
    template 
    MemoryManager(const MemoryManager<_Tp1>&) __STL_NOTHROW {}
    ~MemoryManager() __STL_NOTHROW {
        chunk_pointer_ curr = _M_pool;
        while (curr != NULL) {
            chunk_pointer_ prev = curr->next;
            operator delete(reinterpret_cast(curr));
            curr = prev;
        }
    }

    pointer address(reference __x) const { return &__x; }
    const_pointer address(const_reference __x) const { return &__x; }

    _Tp* allocate(size_type __n, const void* = 0) {
        if (_M_pool == NULL) {
            allocateBlock();
        }
        pointer result = reinterpret_cast(_M_pool);
        _M_pool = _M_pool->next;
        return result;
    }

    // __p is not permitted to be a null pointer.
    void deallocate(pointer __p, size_type __n) {
        if (__p != NULL) {
            reinterpret_cast(__p)->next = _M_pool;
            _M_pool = reinterpret_cast(__p);
        }
    }

    size_type max_size() const __STL_NOTHROW { return size_t(-1) / sizeof(_Tp); }

    void construct(pointer __p, const _Tp& __val) { new (__p) _Tp(__val); }

    void destroy(pointer __p) { __p->~_Tp(); }

private:
    union chunk {
        value_type element;
        chunk* next;
    };

    typedef chunk* chunk_pointer_;
    typedef chunk chunk_type_;

    chunk_pointer_ _M_pool;

    void allocateBlock() {
        chunk_pointer_ temp =
        reinterpret_cast(operator new(sizeof(_Tp)));
        temp->next = _M_pool;
        _M_pool = temp;
    }
};

bool operator==(MemoryManager __M_1_,
                MemoryManager __M_2_) {
    return true;
}

bool operator!=(MemoryManager __M_1_,
                MemoryManager __M_2_) {
    return false;
}

这里写图片描述

总结

对于此题的MemoryManager,实际上只在增加元素时分配内存,而不会在删除元素时释放内存,直到最后才一起释放内存。(每删除一个元素,MemoryManager的根节点往前移。)在最后,Stack还会调用析构函数,把没有删除的匀速删除,经由MemoryManager的析构函数一起释放内存。从而避免了内存泄露。直接使用new会因为要找到合适的内存位置而耗费时间,但如果只是选取合适大小的内存位置却不需要太多的时间,从而节省了开销,此题就可以顺利完成。

点击复制链接 与好友分享!回本站首页
相关TAG标签 内存 C++ 机制
上一篇:静态链表 C++实现
下一篇:C++primer知识点(五)(终结C++primer)
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站