日期:2014-05-16  浏览次数:20903 次

C 语言实现的内存池 mpool.c : 适用于Windows和Linux

C 语言实现的内存池 mpool.c : 适用于Windows和Linux

cheungmine

当程序频繁分配内存,或者管理很多大块内存的时候,我们就需要一个简洁高效的内存池(memory pool)。很多语言提供了这个基础设施,这里我提供一个C语言的版本mpool.c:原始的mpool仅仅提供Unix/Linux的版本,我增加了Windows的版本,这样这个mpool就成为适用于Windows和Linux的完整的版本了。

全部的mpool有5个文件:

mpool.h

mpool_loc.h

mix4win.h (这个文件是我增加的)

mpool.c

Makefile

有2个测试文件:

mpool_t.c (自带的Linux上的测试文件)

main.c (我写的Windows上的测试文件)


如果你觉得本文对你有帮助,请点击下面的链接,以获得关于我的信息:

http://blog.csdn.net/cheungmine/article/details/8258551


本文全部内容可以在下面的链接下载到:

http://download.csdn.net/detail/cheungmine/4859693


为方便阅读,我把全部C文件都贴出来:

/**
 * mpool.h
 *
 * Memory pool defines.
 *
 * Copyright 1996 by Gray Watson.
 *
 * This file is part of the mpool package.
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose and without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies, and that the name of Gray Watson not be used in advertising
 * or publicity pertaining to distribution of the document or software
 * without specific, written prior permission.
 *
 * Gray Watson makes no representations about the suitability of the
 * software described herein for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * The author may be reached via http://256.com/gray/
 *
 * $Id: mpool.h,v 1.4 2006/05/31 20:26:11 gray Exp $
 *
 * cheungmine@gmail.com
 *   revised 12/7/2012
 */

#ifndef __MPOOL_H_INCLUDED
#define __MPOOL_H_INCLUDED

#include <sys/types.h>

/*
 * mpool flags to mpool_alloc or mpool_set_attr
 */

/*
 * Choose a best fit algorithm not first fit.  This takes more CPU
 * time but will result in a tighter heap.
 */
#define MPOOL_FLAG_BEST_FIT    (1<<0)

/*
 * By default the library adds 2 bytes onto all allocations to insert
 * a magic number that it can look for to determine how large a freed
 * memory chunk is.  This flag indicates that few if any frees are
 * going to be performed on the pool and to not waste memory on these
 * bytes.
 */
#define MPOOL_FLAG_NO_FREE    (1<<1)

/*
 * This enables very heavy packing at the possible expense of CPU.
 * This affects a number of parts of the library.
 *
 * By default the 1st page of memory is reserved for the main mpool
 * structure.  This flag will cause the rest of the 1st block to be
 * available for use as user memory.
 *
 * By default the library looks through the memory when freed looking
 * for a magic value.  There is an internal max size that it will look
 * and then it will give up.  This flag forces it to look until it
 * finds it.
 */
#define MPOOL_FLAG_HEAVY_PACKING  (1<<2)

/*
 * Use sbrk not mmap to allocate pages.
 * This is not recommended for normal use.
 */
#define MPOOL_FLAG_USE_SBRK    (1<<3)

/*
 * Mpool error codes
 */
#define MPOOL_SUCCESS  0         /* no error */
#define MPOOL_ERROR  1           /* general error */
#define MPOOL_E_ARG_NULL  2      /* function argument is null */
#define MPOOL_E_ARG_INVALID  3   /*