比特幣源碼研讀(2)-main(1)

比特幣源碼研讀(2)-main函數(shù)(1)

main函數(shù)介紹

Main函數(shù)位置:bitcoin/src/bitcoind.cpp

Main函數(shù)流程圖:


Main函數(shù)的框架很簡單,設(shè)置環(huán)境進(jìn)行noui_connect()(無界面通信)----初始化參數(shù)-----返回是否成功值。

SetupEnvironment()函數(shù)

在util.cpp中。

該函數(shù)分為三部分:內(nèi)存分配區(qū)設(shè)置,本地化設(shè)置,本地化文件路徑設(shè)置


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

說明:

內(nèi)存分配區(qū)設(shè)置,32位體統(tǒng)中,arenas設(shè)置位1,從glibc2.10開始,C庫默認(rèn)每個(gè)core創(chuàng)建2個(gè)heap arenas。眾所周知,這會(huì)引起過多的虛擬地址空間使用。因此設(shè)置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系統(tǒng)

try {

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

? ? ? ? ? ? ? ? ? ? ? ?//如果本地系統(tǒng)無效,則報(bào)runtime error錯(cuò)誤

} catch (const std::runtime_error&) {

setenv("LC_ALL", "C",1); ? ? ? ? ? ?//檢測到runtime error,查詢本地的C相關(guān)的變量

}

#endif

說明

本地化設(shè)置。在大部分的POSIX系統(tǒng)中,(比如linux,但不上BSD系統(tǒng)),本地環(huán)境都是無效的,在此情況下,C會(huì)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);//使用本地區(qū)域語言

}

說明:

使用本地語言。地化文件路徑設(shè)置,我理解的這段代碼意思,讀取本地的語言類型,比如中文,

本地路徑會(huì)被延遲初始化,為了避免在多個(gè)線程環(huán)境中出現(xiàn)未定義錯(cuò)誤,在main主線程中會(huì)被單獨(dú)設(shè)置,通過fs::path命令,dummy locale提取內(nèi)部默認(rèn)路徑,用于明確地imbue the path

知識(shí)普及:

QT:linux種的C++的圖形庫,用于應(yīng)用程序開發(fā)

try:c++中處理異常數(shù)據(jù)用。如果try語句塊中的程序段發(fā)生異常,且拋棄了該異常,則這個(gè)異常就 ? ? ? ? ? 可以被try語句塊語句塊后面的某個(gè)catch語句所捕獲并處理,捕獲和處理的條件是被拋棄的異常 ? ? ? ? ?類型與chatch語句的異常類型相匹配。

POSIX :可移植操作系統(tǒng)portable operating system interface

Locate:讓使用者可以很快速的搜尋檔案系統(tǒng)內(nèi)是否有指定的檔案。其方法是先建立一個(gè)包括系統(tǒng)內(nèi) ? ? ? ? ? ? 所有檔案名稱及路徑的數(shù)據(jù)庫,之后當(dāng)尋找時(shí)就只需要查詢這個(gè)數(shù)據(jù)庫,而不必實(shí)際深入檔 ? ? ? ? ? ? ?案系統(tǒng)之中

setenv命令:查詢和顯示環(huán)境變量

imbue函數(shù):輸出使用的區(qū)域語言對(duì)象

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容