MudOS中的内存管理
2021/4/7 11:32:24

有关内存管理的算法实在是太多了,多到什么程度呢?基本上能想得到的数据结构,都能出现在各式各样的内存管理算法之中,数组、链表、散列表、二叉树等等都在这里大放异彩。研究内存管理实在是一件有趣的事情,同时也能极大的提高自己的编程能力。
内存管理方案 
MudOS中定义了至少3套内存分配函数库:
1.        Build-in system malloc——系统内建函数库,即malloc,realloc,calloc,free;
2.        Satoria's malloc——这算是专门为MudOS打造的函数库,比大多数系统内建函数要快;
3.        BSD (Berkeley Software Distributions) malloc——随着FreeBSD发布出来的malloc源代码,速度应该算是最快的,但会浪费不少内存空间。
定义了至少2套包装(wrap)函数库:
1.          WRAPPEDMALLOC——简单的对内存分配函数进行了一下包装,提供了有限的内存分配统计功能:内存分配次数(alloc),内存释放次数(free),内存再分配次数(realloc);
2.          DEBUGMALLOC——正如其名所暗示的:调试期使用的内存分配函数,它的实现比较复杂,不仅提供了安全性检查,而且提供的统计功能也更完善,比如可以查出某次内存分配是为哪种数据类型提供内存空间。
 
在编译MudOS时,通过选择不同的宏定义来选择不同的内存管理方案:
/* macro.h */
/*
  Define for MALLOC, FREE, REALLOC, and CALLOC depend upon what malloc
  package and optional wrapper is used.  This technique is used because
  overlaying system malloc with another function also named malloc doesn't
  work on most mahines that have shared libraries.  It will also let
  us keep malloc stats even when system malloc is used.
 
  Please refer to options.h for selecting malloc package and wrapper.
*/
#if (defined(SYSMALLOC) + defined(SMALLOC) + defined(BSDMALLOC)) > 1
!Only one malloc package should be defined
#endif
 
#if (defined(WRAPPEDMALLOC) + defined(DEBUGMALLOC)) > 1
!Only one wrapper (at most) should be defined
#endif
 
#if defined (WRAPPEDMALLOC) && !defined(IN_MALLOC_WRAPPER)
 
#  define MALLOC(x)              wrappedmalloc(x)
#  define FREE(x)                wrappedfree(x)
#  define REALLOC(x, y)          wrappedrealloc(x, y)
#  define CALLOC(x, y)            wrappedcalloc(x, y)
#  define DXALLOC(x, t, d)        xalloc(x)
#  define DMALLOC(x, t, d)        MALLOC(x)
#  define DREALLOC(x, y, t, d)    REALLOC(x,y)
#  define DCALLOC(x, y, t, d)    CALLOC(x,y)
 
#else
 
#  if defined(DEBUGMALLOC) && !defined(IN_MALLOC_WRAPPER)
 
#    define MALLOC(x)              debugmalloc(x, 0, (char *)0)
#    define DMALLOC(x, t, d)        debugmalloc(x, t, d)
#    define DXALLOC(x, t, d)        debugmalloc(x, t, d)
#    define FREE(x)                debugfree(x)
#    define REALLOC(x,y)            debugrealloc(x,y,0,(char *)0)
#    define DREALLOC(x,y,tag,desc)  debugrealloc(x,y,tag,desc)
#    define CALLOC(x,y)            debugcalloc(x,y,0,(char *)0)
#    define DCALLOC(x,y,tag,desc)  debugcalloc(x,y,tag,desc)
 
#  else
 
#    include "malloc.h"
 
#  endif
#endif
 
/* malloc.h */
/*
* to use sysmalloc or malloc replacements
*/
#if defined(SYSMALLOC) ||

下一页
返回列表
返回首页
©2024 MUD游戏网_文字mud 电脑版
Powered by iwms