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



I've not tried this, but you should be able to get the size by doing this:

#include <objc/objc-class.h>

@implementation NSObject (ArenaCategory)

+ (id)allocWithArena:(void*)arena
{
Class c = self; // note that self is a class, not an instance, in class methods

int size = c->instance_size;


// now do your allocation and figure out how to do your own class_createInstance initialization
}


@end

Note that there is still instance initialization, which includes zeroing the memory, and setting the isa pointer correctly. I'm not sure if the retain count is handled specially on alloc, or if you simply call retain on your new instance.

You should read the docs at:

file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/ Reference/ObjCRuntimeRef/index.html

and also consult the headers in /usr/include/objc/

- Ladd

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

This is more a question (I hope!) of using Obj. C's APIs then developing new ones. If this is the wrong forum, let me know.

I have some interest in allocating certain small objects on a custom arena; this is basically a function which allocates large blocks of memory and then hands them out in small chunks, without tracking who got what (hence no ability to reuse memory within the arena once it is handed out). At a certain point the entire arena is freed. This is not only efficient, it makes the job of tracking memory irrelevant.

Now, it seems to me that since ObjC so wisely separates the allocation of the object from the initialization of the object, I should be able to do this relatively easily.

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

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


Any help greatly appreciated.

Niko
_______________________________________________
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/ladd% 40criticalpath.com


This email sent to email@hidden


_______________________________________________ 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.