《c语言接口与实现–创建可重用软件技术》人邮版第5章–内存管理,本章节涉及到c语言的内存分配与回收,内存管理在c语言中尤为重要,如果处理不当,会造成内存泄漏甚至系统崩溃的严重问题。本章介绍了一种内存管理方法的实现。比较难懂,需要结合图形来理解。
书中给出了两个源码mem.c和memchk.c, mem.c为简单的实现,是一般的使用方式;memchk.c是本章的精髓,还是先上完整的代码,然后逐步阐述我对代码的理解,如有错误请指正。
mem.h,我的except.h放在公共的include里面,而且做成了libexcept.so,后续的章节基本都会用到这个异常库。
#ifndef MEM_INCLUDED
#define MEM_INCLUDED
#include "../../include/except.h"
extern const Except_T Mem_Failed;
extern void *Mem_alloc(long nbytes, const char *file, \
int line);
extern void *Mem_calloc(long count, long nbytes, \
const char * file, int line);
extern void Mem_free(void *ptr, const char *file, int line);
extern void *Mem_resize(void *ptr, long nbytes, const char *file, int line);
#define ALLOC(nbytes) \
Mem_alloc((nbytes), __FILE__, __LINE__)
#define CALLOC(count, nbytes) \
Mem_calloc((count), (nbytes), __FILE__, __LINE__)
#define NEW(p) ((p) = ALLOC((long)sizeof*(p)))
#define NEW0(p) ((p) = CALLOC(1, (long)sizeof*(p)))
#define FREE(ptr) ((void) (Mem_free((ptr), \
__FILE__, __LINE__), (ptr) = 0))
#define RESIZE(ptr, nbytes) ((ptr) = Mem_resize((ptr), \
(nbytes), __FILE__, __LINE__))
#endif
memchk.c
#include <stdlib.h>
#include <string.h>
#include "assert.h"
#include "except.h"
#include "men.h"
// size 12 in 32bit system
union align
{
int i;
long l;
long *lp;
void *p;
void (*fp)(void);
float f;
double d;
long double ld;
};
// get hash value [0,sizeof(t)/sizeof((t)[0]) - 1]
// if t = htab value [0,2046]
#define hash(p, t) (((unsigned long) (p) >> 3) & \
(sizeof (t)/sizeof((t)[0])-1))
// except info
const struct Except_T Mem_Failed = {
"Allocation Failed"};
// mem list
static struct descriptor
{
struct descriptor *free;
struct descriptor *link;
const void *ptr;
long size;
const char *file;
int line;
} *htab[2048];
// descriptor unit
static struct descriptor freelist = {&freelist};
static struct descriptor *find(const void *ptr)
{
struct descriptor *bp = htab[hash(ptr, htab)];
while( bp && bp->ptr != ptr )
{
bp = bp->link;
}
return bp;
}
void Mem_free(void *ptr, const char *file, int line)
{
if (ptr)
{
struct descriptor *bp;
if (((unsigned long )ptr)%(sizeof(union align))!=0 \
|| (bp = find(ptr)) == NULL || bp->free)
{
Except_raise(&Assert_Failed, file,