PDA

View Full Version : Is it possible to memory map a file in Qt



mcostalba
8th December 2006, 13:31
I have googled around but I've found only an old and not Qt APi QMemoryFile Class.

Thanks
Marco

wysota
8th December 2006, 16:31
Use "mmap()". Memory mapping is not know in Windows world, so there is no point putting it in Qt.

sunil.thaha
11th December 2006, 10:40
AFAIK, D-Bus is not available in Windows and Qt seems to have it ?:confused:

wysota
11th December 2006, 12:09
AFAIK, D-Bus is not available in Windows and Qt seems to have it ?:confused:

Ok, specially for you:


#include <sys/mman.h>

QwwMemoryMapper {
public:
enum Protection { None, Readable, Writable, Executable };
QwwMemoryMapper(QFile &f, int length, int prot, bool shared, int offset){
int protfl=PROT_NONE;
if(prot & Readable) protfl|=PROT_READ;
if(prot & Writable) protfl|=PROT_WRITE;
if(prot & Executable) protfl|=PROT_EXEC;
_pointer = mmap(0, length, protfl, shared ? MAP_SHARED : MAP_PRIVATE, f.handle(), offset);
_length = length;
}

~QwwMemoryMapper(){
munmap(_pointer, _length);
}

void *pointer() const { return _pointer; }

int mappedLength() const { return _length; }

private:
void *_pointer;
int _length;
};

As for DBUS:
1. It is being implemented on Windows
2. It is more than two functions (in contrast to mmap()) so it does make sense to map it to Qt API.

sunil.thaha
11th December 2006, 15:10
As for DBUS:
1. It is being implemented on Windows


Waw !!, Obvously did not know that. thank u :)



2. It is more than two functions (in contrast to mmap()) so it does make sense to map it to Qt API. Today 16:10

I agree, did not know what memory mapped file just was



void *_pointer;
int _length;

I read somewhere IIRC ( int C++ Coding standard ) , that it is not recommended to use an underscore as a start character ? And the author has not given reason for why it should not be used.

Just google and found this http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap5.html
http://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15211/fall.97/www/styleguide.html
http://www-hera-b.desy.de/subgroup/software/clue/docu/codingrules.html

wysota
11th December 2006, 23:23
I read somewhere IIRC ( int C++ Coding standard ) , that it is not recommended to use an underscore as a start character ? And the author has not given reason for why it should not be used.
Because this might cause name clashes. But it doesn't apply when used in classes because classes provide own namespaces for internal fields, so it won't cause trouble.

And sorry, but academic rules written by teachers for their students somehow doesn't seem to bother me that much :)

It's like with the Pirate Codebook - it's more a list of guidelines than actual rules.

jacek
11th December 2006, 23:36
I read somewhere IIRC ( int C++ Coding standard ) , that it is not recommended to use an underscore as a start character ?
It isn't recommended, because identifiers that start with underscore and capital letter or another underscore are reserved for the compiler. This is somewhat similar to a rule like "don't use pointers, because you might get a memory leak" --- it's just too defensive.

sunil.thaha
13th December 2006, 05:26
Well this is the reply I got from Marsha Cline.




Glad the C++ FAQ has helped.

According to the standard, the namespace reserved by the compiler includes identifiers that start with an underscore followed immediately by a capital letter as well as identifiers that contain a double-underscore anywhere within the identifier – beginning, ending, in the middle, anywhere. That means names like __foo or foo__bar or _Foo are bad, but _foo is okay *if* the compiler had no accidental names starting with an underscore followed immediately by a lower-case letter. However I personally avoid leading underscores since I was burned once by a compiler that had erroneously stolen one of those identifiers, so instead I use trailing underscores to indicate member variables.

Marshall

wysota
13th December 2006, 08:39
The class name is part of the identifier, so in reality those identifiers start with "QwwMemoryMapper". But if you really want to keep then conversation going, then simply run sed -e "s/_/m_/g" on the source code and let's forget about the problem.

Eldritch
13th December 2006, 20:49
Unless, of course, something reserves it via a #define.

I prefer length_, since if you've got a spiffy type-ahead UI, you won't have everything starting with the same character. :D

mcostalba
16th December 2006, 09:42
I prefer _length to length_ because all the following sucks

length_->something

length_.something

length_++

length_[n]

length_(x)

x = length_;

and probably also something more that I don't remember now.

jesse_mark
20th April 2015, 15:33
wysota
can you please give exmple how to use the QwwMemoryMapper class you wrote in here

yeye_olive
20th April 2015, 15:46
wysota
can you please give exmple how to use the QwwMemoryMapper class you wrote in here
Seriously? You resurrect a 9-year old thread to ask for a tutorial for a 20-line wrapper around two functions? Besides memory-mapped files do exist under Windows and have been in Qt for a while now (see QFileDevice::map()).

wysota
20th April 2015, 17:30
wysota
can you please give exmple how to use the QwwMemoryMapper class you wrote in here

Oh, come on...