[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Dear GNUStep
From: |
Richard Frith-Macdonald |
Subject: |
Re: Dear GNUStep |
Date: |
Mon, 20 Feb 2017 09:35:51 +0000 |
> On 20 Feb 2017, at 06:31, 김정수 <comeonyoh89@gmail.com> wrote:
>
> Hi. I am Korean iOS developer who have studied GNUStep.
>
> This is not a bug just question. please give me a light...
>
>
>
> when we initialize object in Objective-C, we usually write like this.
>
>
> [NSObject alloc]. so I have a big question and tracked how to work 'alloc' in
> GNUStep
>
> and found it in your source code.
> (https://github.com/gnustep/base/blob/master/Source/NSObject.m)
>
>
>
> it finally call 'NSAllocateObject' method and I unstated how they work roughly
> but I cannot understand how obj_layout which is store retain count (called
> Header in my book) is added in Class.
>
> your code just write like this
> new = (id)&((obj)new)[1];
>
> but i cannot why [0] is obj_layout.
>
> I cannot find where they are connecting.
If you want to write portable code, you should just use the objects allocated
by the library you are using (because different libraries will do different
things) and not worry about the internals of memory allocation.
But ... in this particular case obj_layout is a structure big enough to;
a. hold a reference counter
b. ensure that if the start of the structure is aligned on the correct address
boundary to hold an object, then so is the end of the srtructure
So the actual memory allocated is the size of the obj_layout structure plus the
size of the object you want to store in that memory, and the pointer to the
object is offset into the allocated memory by the size of obj_layout.
That means that the reference counter can be stored in the obj_layout structure
at the start of the allocated memory, immediately before the object itself.
I hope that helps, though I suspect looking at the code may be easier to
understand than any explanation I can give in words.
- Dear GNUStep, 김정수, 2017/02/20
- Re: Dear GNUStep,
Richard Frith-Macdonald <=