Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why methodLists is a pointer to pointer in objc_class

both methodLists and protocols are chain-table, protocols is a pointer, but methodLists is a pointer to pointer, Why?

ocjc_class objc_method_list objc_protocol_list

like image 343
simpleapples Avatar asked Apr 13 '26 08:04

simpleapples


1 Answers

This is an implementation detail that is fallout from how both the history of Objective-C and the ability to dynamically add methods to classes.

Specifically, if you look at the contents of the method_list entries, you'll find that the methods are broken up into sets where each set contains all the methods from a particular category on the object. I.e. if your app were to define a category on UIView with five methods (don't do that -- bad design), then you'd find those five methods tacked on to the end of the method_list in a single objc_method_list (which is why the method_list entry is pointer aligned and variable sized).

This also extends to dynamic addition of methods. There is no need to bcopy a bunch of data into a newly allocated copy of the existing method data structure. Instead, the runtime can just allocate an objc_method_list and shove it on the end of the linked list (or at the head -- implementation detail).

like image 92
bbum Avatar answered Apr 15 '26 23:04

bbum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!