erts_alloc分析<一>

 在追踪ERLANG的BIF实现的时候,遇到了负责内存分配的模块erts_alloc,比如在list_to_atom的C语言实现中有限面的代码

1 char *buf = (char *) erts_alloc(ERTS_ALC_T_TMP, MAX_ATOM_CHARACTERS); 

首先看官方文档说明:

LIBRARY SUMMARY

An Erlang Run-Time System internal memory allocator library.

DESCRIPTION

erts_alloc is an Erlang Run-Time System internal memory allocator library. erts_alloc provides the Erlang Run-Time System with a number of memory allocators.

  erts_alloc模块是ERLANG运行时系统的内存分配库,这个库提供了一系列的内存分配器。

temp_alloc
Allocator used for temporary allocations.
eheap_alloc
Allocator used for Erlang heap data, such as Erlang process heaps.
binary_alloc
Allocator used for Erlang binary data.
ets_alloc
Allocator used for ETS data.
driver_alloc
Allocator used for driver data.
sl_alloc
Allocator used for memory blocks that are expected to be short-lived.
ll_alloc
Allocator used for memory blocks that are expected to be long-lived, for example Erlang code.
fix_alloc
A fast allocator used for some frequently used fixed size data types.
std_alloc
Allocator used for most memory blocks not allocated via any of the other allocators described above.
sys_alloc
This is normally the default malloc implementation used on the specific OS.
mseg_alloc
A memory segment allocator. mseg_alloc is used by other allocators for allocating memory segments and is currently only available on systems that have the mmap system call. Memory segments that are deallocated are kept for a while in a segment cache before they are destroyed. When segments are allocated, cached segments are used if possible instead of creating new segments. This in order to reduce the number of system calls made.
上述就是就是erts_alloc所能够支持的分配器类型,在OTP源代码的erl_alloc.types中追踪到参数对应的分配器类型,
#           <TYPE>      <ALLOCATOR>    <CLASS>    <DESCRIPTION>
type          TMP        TEMPORARY       SYSTEM    tmp      

#           <CLASS>      <DESCRIPTION>
class     SYSTEM         system_data  

#allocator  <ALLOCATOR>  <MULTI_THREAD> <DESCRIPTION>
allocator    TEMPORARY    true        temp_alloc     

从上面的定义可以知道ERTS_ALC_T_TMP参数实际上调用的分配器类型是temp_alloc,所分配的数据类型是system_data,

class    PROCESSES   process_data
class    ATOM        atom_data
class    CODE        code_data
class    ETS         ets_data
class    BINARIES    binary_data
class    SYSTEM      system_data    

上述是整个ERLANG系统中存在的数据类型,进程数据,原子数据,代码数据,ETS数据,二进制数据,系统数据

在atom.h中存在下述定义

#define MAX_ATOM_CHARACTERS 255

所以开始的那个句子就是调用了ERLANG的临时数据分配器分配出来一个大小为255个字节的内存区域。

posted @ 2014-04-17 01:47  polarisalo  阅读(607)  评论(0编辑  收藏  举报