2

does anyone know if the Marshal class is available on the xbox360, specifically the AllocHGlobal method.

Unfortunately I don't have access to an xbox right now, otherwise I would test it myself!

Basically I want to be able to allocate unmanaged memory myself, ie. this piece of code should work:

IntPtr ptr = Marshal.AllocHGlobal(10000);
void* v = (void*)ptr.ToPointer();
byte* b = (byte*)v;
b[0] = 1;
b[2] = 3;
Marshal.FreeHGlobal(ptr);

If anyone is in a kind mood and has access to an xbox and an XNA creators club subscription, you can stick that piece of code into the update method of your game and see if it works.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Martin
  • 12,469
  • 13
  • 64
  • 128
  • Why on earth do you want to do that? – SLaks Dec 18 '09 at 01:32
  • Because the xbox garbage collector sucks, so allocating lots and lots of things only to lose the references is a bad idea. I'm writing some code which needs very short lived large arrays of numbers, perfect for a little bit of manual memory management. – Martin Dec 18 '09 at 01:36
  • You could try re-using the arrays. – SLaks Dec 18 '09 at 01:37
  • 1
    I could, but performance of the GC scales with size of the heap. So pooling lots of large arrays would be a generally bad idea. Also, this code is multithreaded and a thread safe pool is a real pain in the arse to build – Martin Dec 18 '09 at 01:38
  • 1
    Couldn't you "pool" only one large array and do your own memory management on top of that? – Peter Lillevold Dec 18 '09 at 08:36
  • Yes I could, but that's basically what Marshal does, so that's why I want marshal. Unfortunately it doesn't exist, so I guess writing my own GC may be what I have to do :/ – Martin Jan 02 '10 at 02:01
  • 1
    @Martin I'm pretty sure that the GC performance has more to do with the number of live references it has to walk, than the total size of the heap. A single large array of value-types that contain no references should have negligible performance impact. – Andrew Russell Jun 07 '10 at 13:50
  • @Andrew I've since found that you're correct, and the large array is the approach I went for. – Martin Jun 07 '10 at 19:48

1 Answers1

7

No, you can't.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964