Object allocation question

  • Say I want to load a large number of objects from persistent store.  Would
    it be possible and legal to map a file into memory that already had a large
    number of objects laid out properly, and just patch the pointers that are
    references between an in-memory value and a persistent value?  I know some
    object databases do this (and may have patents that affect this).  But,
    would it work in Obj-C?  Or must each object individually go through
    alloc/init to function properly?  One thing I like about the Smalltalk
    derivatives is that you can start up a system with thousands or even
    millions of objects in under 2 seconds because you can just map the image
    into memory and go.  All object references are image relative addressing and
    no magic values need to be setup that reference code or things outside the
    image.



    Michael
  • On Oct 29, 2006, at 8:50 PM, Michael Latta wrote:

    > But, would it work in Obj-C?  Or must each object individually go
    > through
    > alloc/init to function properly?

    For now, it's not required.

    You can malloc a large block and just populate the isa pointers in
    the appropriate places instead of doing -alloc for each one. I'm not
    sure about how much success you'd have with trying to skip -init,
    though.

    I remember getting some errors when trying to access the isa pointer,
    and I got around it by doing this:

        id theObject = class_createInstance(NSClassFromString
    (className), sizeof(unsigned));
        id objectAlias = theObject;

    For some reason, I was able to change the value of objectAlias->isa,
    when I couldn't change it for theObject. All of this could change at
    any time, which is probably why it generates errors.

        - Scott
  • Responding to myself...

    On Oct 29, 2006, at 10:17 PM, Scott Stevenson wrote:

    > I remember getting some errors when trying to access the isa
    > pointer, and I got around it by doing this:
    >
    > id theObject = class_createInstance(NSClassFromString
    > (className), sizeof(unsigned));
    > id objectAlias = theObject;

    Forgot to mention the second argument to class_createInstance() would
    usually be 0. I had my own reasons for having that extra chuck of
    memory there.

        - Scott
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today