Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Use of placement new in memory mapped i/o

Reply
Thread Tools

Use of placement new in memory mapped i/o

 
 
Samshayam@gmail.com
Guest
Posts: n/a
 
      07-07-2006
I have come across the application of placement new in memory mapped
i/o in a number of books.I am not able to understand it completely, may
be becaues of my lack of knowledge with memory mapped i/o.In my
understanding it is a system where I/O devices are treated as memory
locations and are addressed using same number of address lines.How an
object placed at a specific location makes a difference to this?Some
body pls clarify?

 
Reply With Quote
 
 
 
 
rami
Guest
Posts: n/a
 
      07-07-2006
Samshayam,

Memory Mapped I/O need not be just on device and can be for files as
well. In that case you can create your structure in memory location
which will automatically be saved in the file (if this address is at
the location where file is being mapped). Obviously the implementation
is not as easy and requires to go through a lot of issues like
framentation, memory management etc..

Simple yet powerful technique and is used a lot in databases etc..

Regards,
Ramneek Handa
www.lazybugz.net

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have come across the application of placement new in memory mapped
> i/o in a number of books.I am not able to understand it completely, may
> be becaues of my lack of knowledge with memory mapped i/o.In my
> understanding it is a system where I/O devices are treated as memory
> locations and are addressed using same number of address lines.How an
> object placed at a specific location makes a difference to this?Some
> body pls clarify?


 
Reply With Quote
 
 
 
 
Samshayam@gmail.com
Guest
Posts: n/a
 
      07-07-2006
Rami,
Thanks alot.
C++ FAQ , says the following as application of placement new.
"For example, when your hardware has a memory-mapped I/O timer device,
and you want to place a Clock object at that memory location."
This is making a me again confused

regards
Sam


rami wrote:
> Samshayam,
>
> Memory Mapped I/O need not be just on device and can be for files as
> well. In that case you can create your structure in memory location
> which will automatically be saved in the file (if this address is at
> the location where file is being mapped). Obviously the implementation
> is not as easy and requires to go through a lot of issues like
> framentation, memory management etc..
>
> Simple yet powerful technique and is used a lot in databases etc..
>
> Regards,
> Ramneek Handa
> www.lazybugz.net
>
> (E-Mail Removed) wrote:
> > I have come across the application of placement new in memory mapped
> > i/o in a number of books.I am not able to understand it completely, may
> > be becaues of my lack of knowledge with memory mapped i/o.In my
> > understanding it is a system where I/O devices are treated as memory
> > locations and are addressed using same number of address lines.How an
> > object placed at a specific location makes a difference to this?Some
> > body pls clarify?


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-07-2006
(E-Mail Removed) wrote:
> Rami,
> Thanks alot.
> C++ FAQ , says the following as application of placement new.
> "For example, when your hardware has a memory-mapped I/O timer device,
> and you want to place a Clock object at that memory location."
> This is making a me again confused
>

Please don't top post, your reply should be after, or interleaved with
the message you are replying to.

This is a common situation with embedded applications, where you have
hardware device registers in your memory map and want to map a C++
struct over the registers. You use placement new to create the object
at the memory location occupied by the hardware device.

--
Ian Collins.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-07-2006
* (E-Mail Removed):
> [top-posting, excessive quoting]


Please don't top-post in this group. Please don't quote excessively.
Please read the FAQ on how the post.

Thanks in advance.


* (E-Mail Removed):
> C++ FAQ , says the following as application of placement new.
> "For example, when your hardware has a memory-mapped I/O timer device,
> and you want to place a Clock object at that memory location."
> This is making a me again confused


As well it should: hardware is very seldom C++-oriented. Placing a POD
(C-like struct) at some memory address to access memory mapped hardware
is useful, but you don't need placement new for that. A non-POD object
can have "hidden" fields, like a vtable pointer, and also its fields can
be in an unpredictable order; using such an object for memory mapped i/o
is a recipe for disaster.

In short, the example, at <url:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10>, seems to be
a leftover from some earlier editing, and in addition the 'new' in the
code there should be '::new'.

CC: Marshall Cline (the FAQ maintainer).
Thanks: Marshall Cline, for maintaining the FAQ.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Samshayam@gmail.com
Guest
Posts: n/a
 
      07-07-2006
> This is a common situation with embedded applications, where you have
> hardware device registers in your memory map and want to map a C++
> struct over the registers. You use placement new to create the object
> at the memory location occupied by the hardware device.
>
> --
> Ian Collins.


Thanks Collins.
Is there any specific advantage by doing it this way than accessing the
hardware directly?

regards
Sam

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-07-2006
(E-Mail Removed) wrote:
>>This is a common situation with embedded applications, where you have
>>hardware device registers in your memory map and want to map a C++
>>struct over the registers. You use placement new to create the object
>>at the memory location occupied by the hardware device.
>>
>>--
>>Ian Collins.

>
>
> Thanks Collins.


It's Ian.

> Is there any specific advantage by doing it this way than accessing the
> hardware directly?
>

You get a nice tidy wrapper round the hardware. You could achieve the
same result with a wrapper object that takes the address of the hardware
as its constructor parameter, all down to style realy.

By the way, it's customary not to quote signatures (the bit below the --).

--
Ian Collins.
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      07-07-2006
Alf P. Steinbach wrote:
> * (E-Mail Removed):
> > C++ FAQ , says the following as application of placement new.
> > "For example, when your hardware has a memory-mapped I/O timer device,
> > and you want to place a Clock object at that memory location."
> > This is making a me again confused

>
> As well it should: hardware is very seldom C++-oriented. Placing a POD
> (C-like struct) at some memory address to access memory mapped hardware
> is useful, but you don't need placement new for that. A non-POD object
> can have "hidden" fields, like a vtable pointer, and also its fields can
> be in an unpredictable order; using such an object for memory mapped i/o
> is a recipe for disaster.
>
> In short, the example, at <url:
> http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10>, seems to be
> a leftover from some earlier editing, and in addition the 'new' in the
> code there should be '::new'.


I still contend that ::new is unnecessary (cf.
http://groups.google.com/group/comp....6e66e030cd196).
If one defines a custom placement new, it is intended to be used (even
if it just forwards to the global new).

Cheers! --M

 
Reply With Quote
 
rami
Guest
Posts: n/a
 
      07-07-2006
(E-Mail Removed) wrote:
> Rami,
> Thanks alot.
> C++ FAQ , says the following as application of placement new.
> "For example, when your hardware has a memory-mapped I/O timer device,
> and you want to place a Clock object at that memory location."
> This is making a me again confused


Sam,
The reason why they mentioned was just so you know that an object
can be required to be in a fixed position and thus the reason of the
placement new operator. It doesnt matter what the example says. To have
a look at real world usage check:
http://lightwave2.com/persist/

Hope it helps,
Ramneek
http://www.lazybugz.net

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      07-07-2006
(E-Mail Removed) wrote:
> I have come across the application of placement new in memory mapped
> i/o in a number of books.I am not able to understand it completely, may
> be becaues of my lack of knowledge with memory mapped i/o.In my
> understanding it is a system where I/O devices are treated as memory
> locations and are addressed using same number of address lines.How an
> object placed at a specific location makes a difference to this?Some
> body pls clarify?


Let's say you have a read-only, 32-bit hardware register mapped at
address 0x8000 that has two fields, bit 0 and bits 1-5 respectively.
Assuming int is 32 bits, you might map it like this:

class Reg
{
private:
typedef unsigned int ui32;
ui32 reg_;
public:
ui32 GetField1() const volatile { return reg_ & 1; }
ui32 GetField2() const volatile { return (reg_ >> 1) & 0x1f; }
};

int main()
{
void* const regAddr = reinterpret_cast<void*>(0x8000);
const volatile Reg* const reg = new( regAddr ) Reg;

cout << "the first field is " << reg->GetField1() << '\n';
cout << "the second field is " << reg->GetField2() << '\n';
}

Since the Reg object is located at 0x8000 thanks to placement new, that
means that the sole POD datum is located at that address. As others
have noted, there are alternate ways of accomplishing this same thing
with accompanying advantages and disadvantages, but this one will work
and illustrates what the FAQ is talking about.

Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it a good practice to call the destructor explicitly and use placement new(this) in assignment operators? Michael Tsang C++ 8 12-14-2009 07:43 PM
Question on use of "placement" new letz@grame.fr C++ 11 05-19-2008 08:17 AM
overriding placement new + memory management Lagarde Sébastien C++ 5 05-07-2007 09:12 AM
Passing address of stack memory to placement new operator mangesh C++ 15 06-30-2006 06:01 PM
overriding operator new and accessing placement new Mark P C++ 6 04-27-2005 04:17 AM



Advertisments