[orbitcpp-list] A 'new' problem ;-)
Status: Beta
Brought to you by:
philipd
|
From: Phil D. <ph...@us...> - 2000-04-19 09:48:32
|
Hi Andreas, All,
I've been trying to use operator new() to allocate the space for
sequence types, however it seems to do a little more than I thought.
if you do blah[3]
it calls blah::operator new[](3*sizeof(blah) + <space for mem block>)
and then swizzles the pointer to hide the memory block - rather like
the memory alloc functions in ORBit. I've noticed that is only does
this if the type has a destructor, or nested destructors.
I've managed to get this working with the ORBit memory allocation
stuff on my linux box, but I used knowledge of what was in the C++
operator new[] memory block to do it, so I doubt it's portable, or 64
bit clean :-(
So now I'm looking for a portable way to allocate the memory (using
ORBit alloc functions) and invoke the constructors of the elements.
Idea1:
Allocate a seperate array with new[] and then allocate an ORBit block
of memory and copy the elements across
+ portable
+ simple
- expensive, especially for big or nested structs.
Idea2:
Write some idl compiler logic to write out code which manually
allocates empty strings and objrefs after the block of memory has been
allocated.
+ portable
- could be complex, especially for nested structures
Idea3:
Determine the size of the operator new[] control block and write the
operation new[] as follows:
pointer = sequence_alloc_function(num elements);
return pointer - control block size
The C++ runtime will then 'hide' the control block by incrementing the
returned pointer, and invoke the relevant constructors.
Once the mem block is returned, deduct the size of the ORBit block to
get the 'real' first byte of allocated memory, then fill in an ORBit
block in the same way that the C sequence alloc function does.
~ dubiously portable (it looks portable to me, but who knows?
+ fairly simple
+ efficient
(n.b. the above pseudocode assumes that the ORBit block is bigger
than the operator new[] control block)
I'm going to implement idea3 for the time being. Please, does somebody
have a better idea?
Cheers,
Phil.
P.S. I've attached some code which deduces the size of the operator new
control block - it's 4 on my intel linux box for classes with a
destructor. |