The realization of Objective-c exploring Alloc method

Source: Internet
Author: User

Most of OS X,ios is exposed as open source software on Apple open source. Although I would like you to refer to the source code of the NSObject class, it is regrettable that the foundation framework containing the NSObject class is not exposed. However, the foundation framework uses the source code of the Core Foundation framework, and the source of the memory management part by calling the NSObject class is public. However, without the source code of the NSObject class, it is difficult to understand the internal implementation details of the NSObject class. To do this, we can explain it through the GNU step of Open source software.

The GNU step is the interchangeable framework of the cocoa framework. In other words, the GNU step source code does not say that Apple's cocoa is identical, but from the user's point of view, the behavior and implementation are the same, or very similar. Understanding the GNU Step Source code is also equivalent to understanding Apple's cocoa implementation.

Let's take a look at the Alloc class method of the NSObject class in the GNU Step source code. In order to clarify the focus, there are areas where the quoted source code is extracted or modified without changing the original intent.

ID obj = [nsobject alloc];

The Alloc class method of the above call is implemented in the NSOBJECT.M source code as follows:

+ (ID) alloc

{

return [self Allocwithzone:nsdefaultmalloczone ()];

}

+ (ID) allocwithzone: (struct _nszone *) zone

{

Return Nsallocateobject (self, 0, z);

}

The Nsallocateobject function is assigned an object through the Allocwithzone: Class method call. Let's take a look at the implementation of the Nsallocateobject function:

struct obj_layout{

Nsuinteger retained;

};

Inline ID

Nsallocateobject (Class aclass, Nsuinteger extrabytes, Nszone *zone)

{

int size = The amount of memory required to accommodate 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 an object by calling the Nszonemalloc function, then resets the memory space to 0, and finally returns the pointer used as an object.

The following is the simplified source code after removing Nszone:

struct obj_layout{

Nsuinteger retained;

};

+ (ID) alloc

{

int size = sizeof (struct obj_layout) + object size;

struct Obj_layout *p = (struct obj_layout *) calloc (1, size);

Return (ID) (p + 1);

}

The Alloc class method uses the retained integer in the struct obj_layout to hold the reference count (next to the article) and writes it to the object memory header, which is returned after the object's memory block is set to 0.

The realization of Objective-c exploring Alloc method

Related Article

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.