Giter VIP home page Giter VIP logo

memory-pool's Introduction

MemoryPool

内存池的作用

在C和C++语言中,经常需要动态分配内存,我们会用到new,delete,malloc,free。但是当我们分配很多小块的内存时,会造成很多的内存碎片,大大降低了内存的使用效率。为了减少内存碎片的出现,采用了内存池技术。

内存池的实现原理

内存池的先调用malloc函数申请一大块内存,然后维护一个空闲链表,该链表是一个个小的空闲内存片,没当需要内存时就从空闲链表上拿过来一个小片内存使用。如果空闲链表为空了,就从之前分配的大块内存去取几个插入到空闲链表上。如果分配的大块内存也用光了,就继续用malloc申请一大块。

实现

在学习STL源码时,了解了allocator类,于是打算自己实现一个简单的内存池

对于用户主要使用两个接口:

newElement();
newElement(const U& args);

该函数用于创建一个对象,并调用构造函数,最后返回对象指针。有带参数版本和无参数版本。

void deleteElement(pointer p);

该函数先调用析构函数,然后回收内存到内存池;

memory-pool's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.