Any questions from a PROFI to a SUPER PROFI - 1. - page 27

 
sergeev:

This question concerns work with memory mapping.

Is it possible to dynamically change the size of allocated memory (CreateFileMapping) and its projection (MapViewOfFile) without resorting to copying and recreating?

That is, the issue is this:

A CreateFileMapping object is created in memory to exchange data between processes (writer-reader) of 100 bytes. and MapViewOfFile of the same size of 100 bytes.

The first process writing process can write all 100 bytes of data into memory which the second reading process can't manage to clean up.

So the challenge is, is there any way to extend the allocated memory size without re-creating CreateFileMapping / MapViewOfFile again ?
So that the first process would not wait for release, but would continue to write into the added size, while the second process would also continue to read further.

You can. But it's better to allocate more at once. It will work faster. I have it implemented in the same library.
 
Zhunko:
Yes, you can. But it's better to allocate a bigger size at once.

Erm, that's a bit of a misnomer.

The mapping has no size at all, only the maximum size is specified at creation, usually (and by default) equal to the size of the file we are working with.

The view size is set at creation and it can be changed without UnmapViewOfFile function only in case of shamanism with access flags, and maybe even impossible.

And why change the size at all?

 
Zhunko:
Yes, we can.
how ? don't send me to MSDN and google, I've been there half a day.

TheXpert:

Erm, that's a bit of a misnomer.

Mapping has no size at all, only a maximum size is specified at creation, usually (and by default) equal to the size of the file you're working with.

That's the point. I wrote that the size = 100 bytes is specified at creation.
secondly, I'm not working with a physical file, just with memory. one process passes the information to another process.

And why change the size at all?

During operation the allocated memory is filled to the full 100 bytes. But new data appear and consequently we have to add them. So we have to expand this size to add them.

That's why I'm asking - how to expand the 100 bytes I've allocated without intermediate copying and recreating.

I got it with the view. I just need to re-create it. But what about CreateFileMapping object itself? Can it be expanded without closing it?

 
Apparently the mapping size is set to a fixed size in Create.
.
You can probably only recreate.
.
BUT: if you create a sparse file of huge size (e.g. 2 gigabytes),
it will be OK. But the end of the file has to be understood outside the system file size.
.
But then one would already have to ask how to synchronize the process of reading from the file-
i.e. how to know what portion of the data is written and where.
 

In the main, there is no need for such a volume.

Processes simply write/read asynchronously. And we need a way for the writer not to be unhappy while the reader is waiting for all the data to be written.

That is, you need something like ArrayResize to expand the array without changing its contents. once the reader has grabbed all the data, the entire memory is available for writing again from the beginning.

 
jartmailru:

But then you have to ask how to synchronise the process of reading from the file -
i.e. how to know what portion of the data has been written and where.

I solved this problem at once.

but not yet with dynamic resizing :(

If Vadim (Zhunko) is so generous, I hope he will tell me which functions to use...
 
I repeat: there is such a thing as Sparse files...
This means that a 2 gig file takes up 0 bytes on disk.

Hmm. Can't you set the file to the maximum size
of data to be transferred?
 
jartmailru:
I repeat: there is such a thing as Sparse files...
It means that a 2 gig file takes 0 bytes on disk.

I don't use files, everything is done through memory. I've written above three times already.

// if it's easier

CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, FILE_SIZE, "Local\page");

I'm having trouble with this FILE_SIZE.

Hmm. And you can't set the file to the maximum size of the data to be transferred?
the amount of data is not known in advance.
I'm reading about Sparse, maybe they'll be useful if they can expand dynamically.
 
sergeev:

That's the point. I told you that the size was set to 100 bytes.

Secondly, I'm not working with a physical file, just memory. one process passes information to another process.

What's the difference? The essence is the same; it's just information is written to memory and not to disk. All the same, we're working with a handle opened via CreateFile.

There (when creating a file in memory) it is necessary to specify the maximum possible size, above which exactly there will be no information.

While you work, the allocated memory fills up to the full 100 bytes.

And what's the problem? Made another view with 100byte offset, but the mapping (i.e. file) size should take this into account beforehand.

I got it with the view. But what about the CreateFileMapping object itself? Can it be expanded without closing it?

Expanding it is wrong, its size should already take this into account.
 
TheXpert:
Expanding it is wrong, its size should already take this into account.
it seems to be the only option.
Reason: