Re: Size of a class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



There seems to be some confusion here I guess, when you create a file
mapping you effectively allocate a memory segment backed by a file (regular
or paging file).
That means that the size of the memory segment is the size specified when
calling CreateFileMapping.
Now, when you map a view of the file, by calling MapViewOfFile, you
effectively reserve a number of bytes starting at a location relative to the
beginning of the segment (offset), with a size specified by the last
argument. That means that, nor the size of the 'file' reflects the presence
of any data, nor do you have an idea of the size of the mapped view(s).
So your only option is to include the size of the mapped view into the data
stored in the buffer, like this:

....
Marshal.WriteInt32(new IntPtr((int)pBuf), (int)ms.Length);
byte[] buff = ms.GetBuffer();
// marshal managed byte array to unmanaged memory buffer (backed by a
file).
for (int i = 0; i < ms.Length; i++ )
Marshal.WriteByte(new IntPtr((int)pBuf + sizeof(int) + i), buff[i]);
..
// signal "data available" to readers....

Here, pBuf is the pointer to a mapped view returned by MapViewOfFile, and ms
is a MemoryStream object reference.

The reading side can read the data back using something like this:

pBuf = MapViewOfFile(handle, access , 0, 0, 0))
int len = Marshal.ReadInt32(pBuf, 0);
byte[] buff = new byte[len];
for (int i = 0; i <len; i++ )
buff[i] = Marshal.ReadByte(new IntPtr((int)pBuf + sizeof(int)), i);
MemoryStream ms = new MemoryStream(buff);
BinaryFormatter bf = new BinaryFormatter();
Object o = bf.Deserialize(ms);
//use object o
....

Another option is to marshal a structure which include size and possible
other objects properties, but in general above is all you need.


Willy.


"Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0095AC0B-2F87-43B7-87C7-0DDEE6D468E6@xxxxxxxxxxxxxxxx
| Willy
| Further to my previous reply, I have solved problem 1: use OpenFileMapping
| not CreateFileMapping - fairly simple!
| However I have scoured the web for a solution to Problem 2 - how to find
the
| size of the file created and written in a different process, and there
simply
| does not seem to be a way of doing it (other than somehow embedding the
size
| in the written data). Several people have posted the question on several
| forums, but no one has managed to provide an answer. If you know of one
I'd
| certainly like to hear it.
| --
| Dave
|
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > "Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
| > news:1D5DB32A-6069-4C59-91C4-14A927D80BF3@xxxxxxxxxxxxxxxx
| > |I am serialising an object to a memory mapped file (using the
| > | CreateFileMapping and MapViewOfFile p/invoke calls). These need to
know
| > the
| > | maximum size of the "file". I can put in a "good guess" ie it won't be
| > more
| > | than, say, 1K, but it would be tidier to use the actaul size. Is it
| > actually
| > | possible to find out how big an object (or even better, a class) would
be
| > | when it is serialised (I suppose one way would be to separately
serialise
| > it
| > | to a memory stream and get the size of the memory stream, but would
that
| > be
| > | accurate? It's certainly not very efficient.)
| > | --
| > | Dave
| >
| > You'll have serialize the object to a MemoryStream anyway, why do you
| > consider this as being inefficient?
| >
| > Willy.
| >
| >
| >


.



Relevant Pages

  • Re: What wrong with abstract record Id?
    ... Why would you EVER care what id ms-access uses for the relation? ... Ask if your boos chars what memory segment word gets loaded into? ... An autonumber is some mechanistic to generate a number. ...
    (microsoft.public.access.forms)
  • Re: hanging programs - ultimate termination method other than power switch?
    ... Keep in mind that Kill really doesnt do much more than *send a signal* ... I'm not sure if OS X uses shared memory in the same way, ... memory segment that contains the process using the ipcrm command. ...
    (comp.sys.mac.system)
  • System parameter shmmax
    ... Unfortunately it's aimed to both Oracle and developers... ... The biggest memory segment a DB use is 300MB. ... A developer team needs to create 1 GB shared memory segment because they ...
    (SunManagers)
  • Re: IPC::Shareable exceptionally slow on large segs
    ... With a default memory segment size of 64K, ... shared memory is the most basic of IPC ... I use the large hash for DNSBL cache lookups in a threaded environment. ...
    (comp.lang.perl.misc)
  • Re: Solaris 5.5.1: Questions on shared memory and the ipcs program
    ... >> memory segment that is visible to this program and its descendants ... Solaris loads shared memory kernel module on demand (as well as ...
    (comp.unix.solaris)