Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: custom memory allocations




On Sep 30, 2005, at 1:09 AM, Niko Matsakis wrote:

What I'd like to do is something like:

@implementation NSObject (ArenaCategory)

+ (id)allocWithArena:(Arena*)arena
{
    void *mem = arena_alloc (arena, sizeof (???));
    initialize memory:mem;
    return (id)mem;
}

@end

I have a question on what you are trying to do in the above and why you are doing that way? Why are you applying a category to NSObject that is attempting to construct (I assume) an instance of NSObject that also has space somehow setup for the thing you call "Arena".


Are you trying to get this non-NSObject object or structure "Arena" to act as an NSObject?
I guess you can't otherwise make "Arena" a subclass of NSObject?


Just making sure you aren't making something harder on yourself then needed because of some misunderstanding.

As you can see I'm not sure how to:

1. Find out the size of the class object which received the message
2. Initialize the memory with whatever details ObjC needs to know what kind of object this is

You could use Foundation provided NSAllocateObject [1] or the Objective-C runtime provided class_createInstance[2] function. These functions allow you to allocate memory for a class (includes isa pointer and class defined instance variable) and any addition space you need for non-class defined variables. Using this will give you a block of memory of the size needed.


To define a structure that lets you work with this block of memory likely use @defs(NSObject) [3]...

struct arenaNSObject {
    @defs(NSObject)
    ...arena vars...
}

-Shawn

[1] <http://developer.apple.com/documentation/Cocoa/Reference/ Foundation/ObjC_classic/Functions/FoundationFunctions.html#// apple_ref/doc/uid/20000055-BCIHCIIB>
[2] <http://developer.apple.com/documentation/Cocoa/Reference/ ObjCRuntimeRef/ObjCRuntimeRef/chapter_1.2_section_11.html#//apple_ref/ doc/uid/TP40001418-DontLinkChapterID_2-BBCJDEAJ>
[3] <http://developer.apple.com/documentation/Cocoa/Conceptual/ ObjectiveC/LanguageOverview/chapter_3_section_8.html#//apple_ref/doc/ uid/20001424-TPXREF164>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden


This email sent to email@hidden
References: 
 >custom memory allocations (From: Niko Matsakis <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.