Giter Club home page Giter Club logo

memorypool's Introduction

MemoryPool

一个极简内存池实现(基于First Fit算法, 可扩展)

要一口气预分配大内存来管理(看了jemalloc和tcmalloc觉得自己真是naive :( )

Example

#include <stdio.h>
#include "memorypool.h"

struct TAT
{
    int T_T;
};

int main()
{
    MemoryPool *mp = MemoryPool_Init(1*GB + 500*MB + 500*KB, 1); // Open auto extend
    struct TAT *tat = (struct TAT *)MemoryPool_Alloc(mp, sizeof(struct TAT));
    tat->T_T = 2333;
    printf("%d\n", tat->T_T);
    MemoryPool_Free(mp, tat);
    MemoryPool_Clear(mp);
    MemoryPool_Destroy(mp);
    return 0;
}

API

  • 预定义宏

KB MB GB

  • 内存池

mem_size_t => unsigned long long

MemoryPool_Init 参数(mem_size_t mempoolsize, bool auto_extend)

mempoolsize: 内存池字节数

auto_extend: 是否自动扩展 (1->开启自动扩展 0->不开启)

MemoryPool_Alloc 行为与系统malloc一致(唔 参数多一个)

MemoryPool_Free 行为与系统free一致(唔 多了个返回值)

MemoryPool *MemoryPool_Init(mem_size_t mempoolsize, bool auto_extend);

void *MemoryPool_Alloc(MemoryPool *mp, mem_size_t wantsize);

bool MemoryPool_Free(MemoryPool *mp, void *p);

MemoryPool *MemoryPool_Clear(MemoryPool *mp);

bool MemoryPool_Destroy(MemoryPool *mp);
  • 获取内存池信息

get_mempool_usage 获取当前内存池已使用内存比例

get_mempool_prog_usage 获取内存池中真实分配内存比例(除去了内存池管理结构占用的内存)

double get_mempool_usage(MemoryPool *mp);

double get_mempool_prog_usage(MemoryPool *mp);

Update

  • 18-1-7 12.53 增加了自动扩展 (内存池耗尽时自动新扩展一个mempoolsize大小的内存)

Tips

  • 可通过注释main.c里的#include "memorypool.h"来切换对比系统malloc free和内存池

  • 暂不支持多线程

  • 多食用MemoryPool_Clear

  • 2GB 数据量下比系统malloc free平均快 30%-50% (食用MemoryPool_Clear效果更明显)

  • mem_size_t使用unsigned long long以支持4GB以上内存管理(头文件中MAX_MEM_SIZE宏定义了最大可管理内存)

  • 大量小块内存分配会有 20%-30% 内存空间损失(用于存储管理结构体 emmmmm)

memorypool's People

Contributors

userpro avatar

Watchers

James Cloos avatar

Forkers

yuanting1

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.