iOS 自動釋放池autoreleasepool(二)
阿新 • 來源:網路 • 發佈:2019-12-25
繼上一篇文章介紹了autoreleasepool的AutoreleasepoolPage,這篇文章我們就來主要講講AutoreleasepoolPage::Push跟AutoreleasepoolPage::Pop 還有一個autorelease
AutoreleasepoolPage::Push
static inline void *push()
{
id *dest;
if (DebugPoolAllocation) {
// Each autorelease pool starts on a new pool page.
// 每個autoreleasepool從一個新的page開始
dest = autoreleaseNewPage(POOL_BOUNDARY);
} else {
// 已經有執行緒的頁
dest = autoreleaseFast(POOL_BOUNDARY);
}
assert(dest == EMPTY_POOL_PLACEHOLDER || *dest == POOL_BOUNDARY);
return dest;
}
複製程式碼
這裡分開兩條路,我們先看第一個
autoreleaseNewPage
先說一下大概流程
1.獲取一個活躍的頁 2.判斷這頁是不是滿了,滿的就新建一個page 3.如果沒獲取到page,也是新建一個頁 4.把要push的obj,push到對應的頁裡
1、
id *autoreleaseNewPage(id obj)
{
//獲取一個活躍的頁
AutoreleasePoolPage *page = hotPage();
//如果有就返回,並且判斷是否是滿的,滿的就會新建一個page
if (page) return autoreleaseFullPage(obj,page);
//如果沒獲取到page
else return autoreleaseNoPage(obj);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
2、
id *autoreleaseFullPage(id obj,AutoreleasePoolPage *page)
{
// The hot page is full.
// Step to the next non-full page,adding a new page if necessary.
// Then add the object to that page.
assert(page == hotPage());
assert(page->full() || DebugPoolAllocation);
// 如果滿了就迴圈,這一頁有child的話 就指向child,再繼續判斷是否滿
// 直到沒有child 就建立一個新的頁
do {
if (page->child) page = page->child;
else page = new AutoreleasePoolPage(page);
} while (page->full());
// 設定成活躍的頁
setHotPage(page);
// 將Obj 新增到這頁裡
return page->add(obj);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
3、
id *autoreleaseNoPage(id obj)
{
// "No page" could mean no pool has been pushed
// or an empty placeholder pool has been pushed and has no contents yet
assert(!hotPage());
// 翻譯:pushExtraBoundary 推入一個額外邊界
bool pushExtraBoundary = false;
if (haveEmptyPoolPlaceholder()) {
// We are pushing a second pool over the empty placeholder pool
// or pushing the first object into the empty placeholder pool.
// Before doing that,push a pool boundary on behalf of the pool
// that is currently represented by the empty placeholder.
//當push一個新頁或者第一頁的時候,在push之前需要先push一個poolboundary(邊界)
pushExtraBoundary = true;
}
else if (obj != POOL_BOUNDARY && DebugMissingPools) {
// We are pushing an object with no pool in place,// and no-pool debugging was requested by environment.
_objc_inform("MISSING POOLS: (%p) Object %p of class %s "
"autoreleased with no pool in place - "
"just leaking - break on "
"objc_autoreleaseNoPool() to debug",pthread_self(),(void*)obj,object_getClassName(obj));
objc_autoreleaseNoPool(obj);
return nil;
}
else if (obj == POOL_BOUNDARY && !DebugPoolAllocation) {
// We are pushing a pool with no pool in place,// and alloc-per-pool debugging was not requested.
// Install and return the empty pool placeholder.
return setEmptyPoolPlaceholder();
}
// We are pushing an object or a non-placeholder'd pool.
// Install the first page.
AutoreleasePoolPage *page = new AutoreleasePoolPage(nil);
setHotPage(page);
// Push a boundary on behalf of the previously-placeholder'd pool.
if (pushExtraBoundary) {
page->add(POOL_BOUNDARY);
}
// Push the requested object or pool.
return page->add(obj);
}
複製程式碼
到此,第一條路就走完了。
autoreleaseFast
1、
static inline id *autoreleaseFast(id obj)
{
//同樣獲取一個活頁
AutoreleasePoolPage *page = hotPage();
if (page && !page->full()) {//如果page沒滿的話
return page->add(obj);
} else if (page) {//如果page滿了
return autoreleaseFullPage(obj,page);
} else {//沒有獲取到活頁
return autoreleaseNoPage(obj);
}
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
2、
id *autoreleaseFullPage(id obj,adding a new page if necessary.
// Then add the object to that page.
assert(page == hotPage());
assert(page->full() || DebugPoolAllocation);
// 如果滿了就迴圈,這一頁有child的話 就指向child,再繼續判斷是否滿
// 直到沒有child 就建立一個新的頁
do {
if (page->child) page = page->child;
else page = new AutoreleasePoolPage(page);
} while (page->full());
// 設定成活躍的頁
setHotPage(page);
// 將Obj 新增到這頁裡
return page->add(obj);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
3、
id *autoreleaseNoPage(id obj)
{
// "No page" could mean no pool has been pushed
// or an empty placeholder pool has been pushed and has no contents yet
assert(!hotPage());
// 翻譯:pushExtraBoundary 推入一個額外邊界
bool pushExtraBoundary = false;
if (haveEmptyPoolPlaceholder()) {
// We are pushing a second pool over the empty placeholder pool
// or pushing the first object into the empty placeholder pool.
// Before doing that,// and alloc-per-pool debugging was not requested.
// Install and return the empty pool placeholder.
return setEmptyPoolPlaceholder();
}
// We are pushing an object or a non-placeholder'd pool.
// Install the first page.
AutoreleasePoolPage *page = new AutoreleasePoolPage(nil);
setHotPage(page);
// Push a boundary on behalf of the previously-placeholder'd pool.
if (pushExtraBoundary) {
page->add(POOL_BOUNDARY);
}
// Push the requested object or pool.
return page->add(obj);
}
複製程式碼
其實這兩條路是差不多的,在底層中都走了這兩個的方法autoreleaseFullPageautoreleaseNoPage
感覺完全可以合起來啊- -,不知道蘋果這麼處理是為了什麼。
#####page->add()
push的最終操作也就是這個add,看看這裡都幹了什麼
id *add(id obj)
{
assert(!full());
unprotect(); //取消保護
id *ret = next; // faster than `return next-1` because of aliasing
*next++ = obj;
protect(); // 保護
return ret;
}
複製程式碼
這裡做了什麼呢
- 這裡面有unprotect() protect() 這兩個可以理解為加鎖類似NSLock
- 建立一個臨時變數ret 來儲存next
- next ++ 記憶體地址移動。類似 0x0001 -> 0x0002
- 將obj儲存到新移動出來的記憶體地址上
那麼push就先到這裡,接下來說說autorelease
autorelease
在瞭解過push之後,我們可以很容易理解這個autorelease。看下面的程式碼跳轉, autorelease最終走到了一個上文出現過的方法autoreleaseFast(),那麼其實autorelease做的操作也跟push一樣
- (id)autorelease {
return ((id)self)->rootAutorelease();
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
objc_object::rootAutorelease()
{
if (isTaggedPointer()) return (id)this;
if (prepareOptimizedReturn(ReturnAtPlus1)) return (id)this;
return rootAutorelease2();
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
objc_object::rootAutorelease2()
{
assert(!isTaggedPointer());
return AutoreleasePoolPage::autorelease((id)this);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
static inline id autorelease(id obj)
{
assert(obj);
assert(!obj->isTaggedPointer());
id *dest __unused = autoreleaseFast(obj);
assert(!dest || dest == EMPTY_POOL_PLACEHOLDER || *dest == obj);
return obj;
}
複製程式碼
AutoreleasepoolPage::Pop
先簡單說一下都做了什麼
- 判斷當前pop物件是否是空池佔位符,是的話就推出去,按道理一般很少進去
- 通過棧頂的地址找到對應的page
- 記錄最高水位標記
- 開始pop,objc_release(obj);
- kill空白頁
static inline void pop(void *token)
{
AutoreleasePoolPage *page;
id *stop;
//只有在push的時候 push了一個邊界符, 才有可能出現EMPTY_POOL_PLACEHOLDER
if (token == (void*)EMPTY_POOL_PLACEHOLDER) {
// Popping the top-level placeholder pool.
// 推出最頂上的佔位池
if (hotPage()) {
// Pool was used. Pop its contents normally.
// Pool pages remain allocated for re-use as usual.
// 遞迴 coldPage呼叫begin
pop(coldPage()->begin());
} else {
// Pool was never used. Clear the placeholder.
setHotPage(nil);
}
return;
}
// 通過棧頂的地址找到對應的page
page = pageForPointer(token);
//token賦值給stop
stop = (id *)token;
if (*stop != POOL_BOUNDARY) {
if (stop == page->begin() && !page->parent) {
// Start of coldest page may correctly not be POOL_BOUNDARY:
// 1. top-level pool is popped,leaving the cold page in place
// 2. an object is autoreleased with no pool
} else {
// Error. For bincompat purposes this is not
// fatal in executables built with old SDKs.
return badPop(token);
}
}
// 記錄最高水位標記
if (PrintPoolHiwat) printHiwat();
// 從棧頂開始操作出棧,並向棧中的物件傳送release訊息,直到遇到第一個邊界符
page->releaseUntil(stop);
// memory: delete empty children
// 刪除空的child頁
if (DebugPoolAllocation && page->empty()) {
// special case: delete everything during page-per-pool debugging
AutoreleasePoolPage *parent = page->parent;
page->kill();
setHotPage(parent);
} else if (DebugMissingPools && page->empty() && !page->parent) {
// special case: delete everything for pop(top)
// when debugging missing autorelease pools
page->kill();
setHotPage(nil);
}
else if (page->child) {
// hysteresis: keep one empty child if page is more than half full
if (page->lessThanHalfFull()) {
page->child->kill();
}
else if (page->child->child) {
page->child->child->kill();
}
}
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
void releaseUntil(id *stop)
{
// Not recursive: we don't want to blow out the stack
// if a thread accumulates a stupendous amount of garbage
while (this->next != stop) {
// Restart from hotPage() every time,in case -release
// autoreleased more objects
AutoreleasePoolPage *page = hotPage();
// fixme I think this `while` can be `if`,but I can't prove it
while (page->empty()) {
page = page->parent;
setHotPage(page);
}
//類似push時候的操作
page->unprotect();
id obj = *--page->next;
memset((void*)page->next,SCRIBBLE,sizeof(*page->next));
page->protect();
if (obj != POOL_BOUNDARY) {
//如果當前物件不是邊界符,就釋放物件
objc_release(obj);
}
}
setHotPage(this);
#if DEBUG
// we expect any children to be completely empty
for (AutoreleasePoolPage *page = child; page; page = page->child) {
assert(page->empty());
}
#endif
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
void kill()
{
// Not recursive: we don't want to blow out the stack
// if a thread accumulates a stupendous amount of garbage
AutoreleasePoolPage *page = this;
//如果page有child節點 就不斷迴圈直到不再有child
while (page->child) page = page->child;
//建立一個deathptr臨時變數來儲存page
AutoreleasePoolPage *deathptr;
//如果page不等於this(可以理解成self)
//就不斷的迴圈把child節點一層一層的置為nil
do {
deathptr = page;
page = page->parent;
if (page) {
page->unprotect();
page->child = nil;
page->protect();
}
delete deathptr;
} while (deathptr != this);
}
複製程式碼