比特币源码研读(2)-main(1)steemCreated with Sketch.

in btc •  7 years ago 

比特币源码研读(2)-main函数(1)

main函数介绍

Main函数位置:bitcoin/src/bitcoind.cpp

Main函数流程图:

Main函数的框架很简单,设置环境进行noui_connect()(无界面通信)----初始化参数-----返回是否成功值。

SetupEnvironment()函数

在util.cpp中。

该函数分为三部分:内存分配区设置,本地化设置,本地化文件路径设置

voidSetupEnvironment()

{

#ifdef HAVE_MALLOPT_ARENA_MAX

// glibc-specific: On 32-bit systems setthe number of arenas to 1.

// By default, since glibc 2.10, the Clibrary will create up to two heap

// arenas per core. This is known to causeexcessive virtual address space

// usage in our usage. Work around it bysetting the maximum number of

// arenas to 1.

if (sizeof(void*) == 4) {

mallopt(M_ARENA_MAX, 1);

}

#endif
说明:

内存分配区设置,32位体统中,arenas设置位1,从glibc2.10开始,C库默认每个core创建2个heap arenas。众所周知,这会引起过多的虚拟地址空间使用。因此设置arenas最大值为1

// On most POSIX systems (e.g. Linux, butnot BSD) the environment's locale

// may be invalid, in which case the"C" locale is used as fallback.

#if !defined(WIN32) &&!defined(MAC_OSX) && !defined(FreeBSD) &&!defined(OpenBSD) //如果不是POSIX系统

try {

std::locale(""); // Raises aruntime error if current locale is invalid

                   //如果本地系统无效,则报runtime error错误

} catch (const std::runtime_error&) {

setenv("LC_ALL", "C",1); //检测到runtime error,查询本地的C相关的变量

}

#endif
说明

本地化设置。在大部分的POSIX系统中,(比如linux,但不上BSD系统),本地环境都是无效的,在此情况下,C会fallback

// The path locale is lazy initializedand to avoid deinitialization errors

// in multithreading environments, it isset explicitly by the main thread.

// A dummy locale is used to extract theinternal default locale, used by

// fs::path, which is then used toexplicitly imbue the path.

std::locale loc =fs::path::imbue(std::locale::classic());

fs::path::imbue(loc);//使用本地区域语言

}
说明:

使用本地语言。地化文件路径设置,我理解的这段代码意思,读取本地的语言类型,比如中文,

本地路径会被延迟初始化,为了避免在多个线程环境中出现未定义错误,在main主线程中会被单独设置,通过fs::path命令,dummy locale提取内部默认路径,用于明确地imbue the path

知识普及:

QT:linux种的C++的图形库,用于应用程序开发

try:c++中处理异常数据用。如果try语句块中的程序段发生异常,且抛弃了该异常,则这个异常就 可以被try语句块语句块后面的某个catch语句所捕获并处理,捕获和处理的条件是被抛弃的异常 类型与chatch语句的异常类型相匹配。

POSIX :可移植操作系统portable operating system interface

Locate:让使用者可以很快速的搜寻档案系统内是否有指定的档案。其方法是先建立一个包括系统内 所有档案名称及路径的数据库,之后当寻找时就只需要查询这个数据库,而不必实际深入档 案系统之中

setenv命令:查询和显示环境变量

imbue函数:输出使用的区域语言对象

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

1 2

I sometimes worry about bitcoin at night. There is some comfort in thinking that every night, we are getting closer to the moon.

感谢你的分享,受益良多。