摘要:
本篇将介绍一个通用内存分配器的实现.上一篇的开头已经介绍过,这个实现是组合first fit pool和fix_obj_pool以处理不同大小的分配请求.首先看由fix_obj_pool返回的内存的头结构:struct head_fix{unsigned short idx;/*0-255,表示在fix_objs的下标*/unsigned short chunk_idx;};这个头不会返回给上层应用,是给释放内存使用的.因为内存的请求是对齐到4字节的,小于1024字节的请求,被分成256个分配大小,分别是4,8,12,....1024.每个大小的请求由一个fix_obj_pool处理.idx的 阅读全文
摘要:
本篇介绍通用内存分配工具的另一个组件,first fit momery pool,也就是首次适应内存分配器。相对于best fit 算法,通常来说first fit具有更好的平均性能,具体分析可参考kunth计算机程序设计第一卷2.5的讨论.我的通用内存分配器准备组合使用fix obj pool和first fit pool.fix obj pool用于处理1-1024字节的小内存请求.这里分配的内存是对齐到4字节的.而对于1-1024字节以外的大内存块请求,将交给first fit pool处理.为了加快free时的处理效率,将对fix obj pool做一个小处理,具体将在下篇通用内存分配 阅读全文
摘要:
vector.h#ifndef _VECTOR_H#define _VECTOR_Hstruct vector;struct vector *vector_create(unsigned int val_size,unsigned int reserve_size);struct vector *vector_copy_create(struct vector*);void vector_copy(struct vector*,struct vector*);void vector_reserve(struct vector*,unsigned int);void vector_destroy 阅读全文
摘要:
对大小固定的对象提供的对象池工具,当对内存分配的请求大小不固定时,使用其它的通用内存池.fix_obj_pool.h#ifndef _FIX_OBJ_POOL_H#define _FIX_OBJ_POOL_Hstruct fix_obj_pool;/** obj_size:对象大小* default_size:默认对象池大小* align4:返回的对象地址是否需要对齐到4字节*/extern struct fix_obj_pool *create_pool(unsigned int obj_size,int default_size,int align4);extern void destro 阅读全文
摘要:
使用setjmp,longjmp实现的C与言异常处理框架。提供 抛出异常,捕获异常等功能。exception.h#ifndef _EXCEPTION_H#define _EXCEPTION_H#define except_alloc_failed 1 //内存分配失败#define except_list_empty 2 //list_pop操作,当list为空触发//............#endifexcept.h#ifndef _EXCEPT_H#define _EXCEPT_H#include <setjmp.h>#include <stdio.h>#incl 阅读全文
摘要:
近来考虑将项目基础框架的开发语言从C++换成C,免不了要编写一大堆的基础工具。本篇为第一篇,list,提供的接口和操作方式与std::list相似.后续将会陆续贴出map,vector,memory pool,hash_table等工具。list.h#ifndef _LIST_H#define _LIST_Hstruct list;struct node;struct fix_obj_pool;struct list_iter{ struct node **next; struct node *n;};struct fix_obj_pool *list_create_obj_poo... 阅读全文