繼上一篇文章介紹了autoreleasepool的AutoreleasepoolPage,這篇文章咱們就來主要講講AutoreleasepoolPage::Push
跟AutoreleasepoolPage::Pop
還有一個autoreleasebash
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;
}
複製代碼
這裏分開兩條路,咱們先看第一個less
先說一下大概流程ui
1.獲取一個活躍的頁 2.判斷這頁是否是滿了,滿的就新建一個page 3.若是沒獲取到page,也是新建一個頁 4.把要push的obj,push到對應的頁裏this
一、
id *autoreleaseNewPage(id obj)
{
//獲取一個活躍的頁
AutoreleasePoolPage *page = hotPage();
//若是有就返回,而且判斷是不是滿的,滿的就會新建一個page
if (page) return autoreleaseFullPage(obj, page);
//若是沒獲取到page
else return autoreleaseNoPage(obj);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
二、
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);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
三、
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);
}
複製代碼
到此,第一條路就走完了。spa
一、
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);
}
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
二、
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);
}
---------------------------------------- 華麗的分割線 ---------------------------------------------------------------------
三、
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);
}
複製代碼
其實這兩條路是差很少的,在底層中都走了這兩個的方法
autoreleaseFullPage
autoreleaseNoPage
感受徹底能夠合起來啊- -,不知道蘋果這麼處理是爲了什麼。
#####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就先到這裏,接下來講說autoreleasedebug
在瞭解過push以後,咱們能夠很容易理解這個autorelease。看下面的代碼跳轉, autorelease最終走到了一個上文出現過的方法autoreleaseFast(),那麼其實autorelease作的操做也跟push同樣code
- (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;
}
複製代碼
先簡單說一下都作了什麼orm
- 判斷當前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); } 複製代碼