1
votes

The Objective-C runtime's hashtable2.mm file contains the following code:

static void bootstrap (void) {
    free(malloc(8));
    prototypes = ALLOCTABLE (DEFAULT_ZONE);
    prototypes->prototype = &protoPrototype; 
    prototypes->count = 1;
    prototypes->nbBuckets = 1; /* has to be 1 so that the right bucket is 0 */
    prototypes->buckets = ALLOCBUCKETS(DEFAULT_ZONE, 1);
    prototypes->info = NULL;
    ((HashBucket *) prototypes->buckets)[0].count = 1;
    ((HashBucket *) prototypes->buckets)[0].elements.one = &protoPrototype;
};

Why does it allocate and release the 8-bytes space immediately?

Another source of confusion is this method from objc-os.h:

static __inline void *malloc_zone_malloc(malloc_zone_t z, size_t size) { return malloc(size); }

While it uses only one parameter, does the signature ask for two?

1
Perhaps there is no allocation table before malloc() is called. - Weather Vane
I which project found you that code? - Mathieu
Regarding Q2: Probably the interface is designed to support zones but your current implementation does not provide them. Yet, the API is still the same and you must provide a function with the defined signature because everyone who uses that function will provide 2 parameters - Gerhardh

1 Answers

0
votes

For the second question, Objective-C runtime used to work with "zones" in order to destroy all allocated variables by just destroying the said zone, but it proved being error prone and later it was agreed to not use it anymore. The API, however still uses it for historical reasons (backward compatibility, i assume):

Zones are ignored on iOS and 64-bit runtime on OS X. You should not use zones in current development.