In-depth analysis of ALLOC/RETAIN/RELEASE/DEALLOC implementations

Source: Internet
Author: User

First, the GNUstep source code to describe the implementation of each function (GNUstep is the Cocoa framework of the interchange framework, the behavior and implementation of the same way very similar )

GNUstep The Alloc method of the NSObject class in the source code :

id = obj = [NSObject alloc];

/**********************************/

+ (ID) alloc{

return [self Allocwithzone:nsdefaultmalloczone ()];


}

+ (ID) Allocwithzone: (Nszone *) z{

Return Nsallocateobject (SELF,0,Z);

}

//nszone is a structure introduced to prevent memory fragmentation, and multi-picture management of the memory-allocated area itself

The Nsallocateobject function is as follows :

struct obj_layout{

Nsuinteger retained;

}


Inline ID nsallocateobject (Class aclass,nsuinteger extrabytes,nszone *zone) {

int size = The memory required to hold the object ;

ID new = Nszonemalloc (Zone, size);

memset (new, 0, size);

New = (ID) & ((struct obj_layout *) new) [1];

}

The nsallocateobject function allocates the memory space required to hold the object by calling the nszonemalloc function , and then the memory space is set to 0. The last pointer used to return the object .


/**************************/

-(Nsuinteger) retaincount{

return Nsextrarefcount (self) + 1;

}


Inline Nsuinteger nsextrarefcount (id anobject) {

return (struct obj_layout *) anobject) [ -1].retained;

///object is addressed to find the object memory header , which accesses the retained variable

}

/***************************/

-(ID) retain{

Nsincrementextrarefcount (self);

return self;

}


inline void Nsincrementextrarefcount (id anobject) {

if ((struct obj_layout *) anobject) [ -1].retained = = Uint_max-1] {

[NSException raise:nsinternalinconsistencyexception format:@ "Nsincrementextrarefcount () ask to increment too far"];

(struct obj_layout *) anobject) [ -1].retained++;

}

}


/********************************/

-(void) release{

if (Nsdecrementextrarefcountwaszero (self))

[Self dealloc];

}

BOOL Nsdecrementextrarefcountwaszero (id anobject) {

if ((struct obj_layout *) anobject) [ -1].retained = = 0) {

return YES;

}else{

(struct obj_layout *) anobject) [ -1].retained-;

return NO;

}

}


/**********************************/

-(void) dealloc{

Nsdeallocateobject (self);

}

inline void Nsdeallocateobject (id anobject) {

struct Obj_layout *o = & (struct obj_layout *) anobject) [-1];

Free (o);

}

The above is the implementation in GNUstep

//////////////////////////////////////////////////////////////////////////

Apple's implementation

By setting a breakpoint on the Alloc class method of the NSObject class , view the function it executes as :

+alloc

+allocwithzone

Class_createinstance

Calloc


Each method calls the _cfdoexternrefoperation function

/************************************************/

int _cfdoexternrefoperation (uintptr_t op,id obj) {

Cfbasichashref table = Gets the hash list (obj) corresponding to the object ;

int count;

int count;
    switch (OP) {
    case Operation_retaincount:
     count = cfbasichashgetcountofkey (table, obj);
    return count;
     case Operation_retain:
    cfbasichashaddvalue (table, obj);
    return obj;
     case Operation_release:
    count = cfbasichashremovevalue (table, obj);
     return 0 = = count;
    }

}


What is a hash table

A hash table (also known as a hash table) is a lookup algorithm, unlike a linked list or tree, where the hash table algorithm does not need to perform a series of comparisons with keywords (the keyword is the value of a data item in the data element that identifies a data element).

the hash table algorithm wants to be able to do as far as possible without any comparison, through a single access to find the data element, it must be stored in the data element and its keywords ( available key representation) between the establishment of a definite correspondence between the Corresponds to a unique storage location in each keyword and hash list. So when looking, just find the location of the given keyword in the hash table based on the corresponding relationship. This correspondence is called a hash function ( available as H (key) .

A set of key keys is imaged to a finite contiguous range of addresses based on the Set hash function h (key) and the method of handling conflicts, and the image of the keyword in the address range is stored as the data element in the table. This is called a hash table, which is called a hash, and the resulting storage location is called a hash address.


The apples are stored in the records of the reference tables.

Benefits:

(1) The allocation of memory blocks of objects without regard to memory head

(2) A memory block address is stored in the reference count table record, which can be traced back to a block of memory.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

In-depth analysis of ALLOC/RETAIN/RELEASE/DEALLOC implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.