Calling _msize() via PInvoke from C# -
i'm writing c# library calling app pass in large amount of contiguous, unmanaged memory. calling app can either .net or visual c++ (it go through intermediate c++/cli library before calling library if c++). useful validate there sufficient memory, decided call _msize() function. unfortunately, _msize seems give me wrong size back. went , modified allocation routine in sample app , call _msize. here code snipet:
public unsafe class mymemory { /// <returns></returns> [dllimport("msvcrt.dll", setlasterror = true)] public static extern int _msize(intptr handle); public static intptr myalloc(int size) { intptr retval = marshal.allochglobal(size); ... int memsize = mymemory._msize(retval); if (memsize < size) { ... } return retval; }
when pass in size 199229440, memsize of 199178885. i've seen similar results different numbers. less 0.01% off, totally understand if over, fact is under, meaning _msize thinks allocated memory less asked for. have clue why is? , recommendations on should instead appreciated well.
p/invoke localsize
function instead.
_msize
determining size of block allocated malloc
(and friends). allochglobal
wrapper around globalalloc
or localalloc
(depending on reference believe; think 2 equivalent), , want localsize
function determine size of block returned. far can tell, marshal
doesn't contain wrapper localsize
, can call using p/invoke.
so seems it's sheer luck _msize
returning useful @ all. perhaps malloc
uses globalalloc
(or localalloc
), either or when asked large blocks, , requests bit of space bookkeeping; in case _msize
trying compensate that.
Comments
Post a Comment