473,408 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Use of placement new in memory mapped i/o

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?

Jul 7 '06 #1
13 2725
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

Sa*******@gmail.com 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?
Jul 7 '06 #2
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

Sa*******@gmail.com 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?
Jul 7 '06 #3
Sa*******@gmail.com 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.
Jul 7 '06 #4
* Sa*******@gmail.com:
[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.
* Sa*******@gmail.com:
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?
Jul 7 '06 #5
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

Jul 7 '06 #6
Sa*******@gmail.com 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.
Jul 7 '06 #7
Alf P. Steinbach wrote:
* Sa*******@gmail.com:
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

Jul 7 '06 #8
Sa*******@gmail.com 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

Jul 7 '06 #9
Sa*******@gmail.com 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

Jul 7 '06 #10
* mlimber:
Sa*******@gmail.com 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);
For the sake of discussion, let's assume this works.

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.
No, not necessarily. You have 'private' data so this is not a POD, and
the compiler is free to reorder things, including placing some padding
at the start. A POD, on the other hand, would be OK except if it had
padding at the end which intruded into a read-only or non-existent
address area -- doing hardware level things is fraught with dangers
like that, and using non-POD classes introduces additional gotchas.

--
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?
Jul 7 '06 #11
Alf P. Steinbach wrote:
* mlimber:
Sa*******@gmail.com 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);

For the sake of discussion, let's assume this works.

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.

No, not necessarily. You have 'private' data so this is not a POD, and
the compiler is free to reorder things, including placing some padding
at the start. A POD, on the other hand, would be OK except if it had
padding at the end which intruded into a read-only or non-existent
address area -- doing hardware level things is fraught with dangers
like that, and using non-POD classes introduces additional gotchas.
Fair enough. Make the data public or live with non-portability (of
course, working with hardware is usually inherently non-portable
anyway).

Cheers! --M

Jul 7 '06 #12

Alf P. Steinbach wrote:
* mlimber:
Sa*******@gmail.com 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);

For the sake of discussion, let's assume this works.

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.

No, not necessarily. You have 'private' data so this is not a POD, and
the compiler is free to reorder things, including placing some padding
at the start. A POD, on the other hand, would be OK except if it had
padding at the end which intruded into a read-only or non-existent
address area -- doing hardware level things is fraught with dangers
like that, and using non-POD classes introduces additional gotchas.

--
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?

Wow, I didnt know that compiler could reorder in non-POD classes??? Are
you sure?
Can you give me some references??

Ramneek
www.lazybugz.net

Jul 7 '06 #13
* rami:
[quoting signature, excessive quoting]
Please don't quote signatures. Please don't quote excessively. Please
read the FAQ on how to post.

Thanks in advance.

--
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?
Jul 7 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
1
by: Peter Nolan | last post by:
Hi All, I have some software that currently loads database tables into sorted arrays in memory to be binary searched by processes. (Long story as to why it does this..) Each process must either...
8
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some...
5
by: ma740988 | last post by:
I've got a struct which is comprised of POD types. I know in advance where in memory the struct resides. To make a long story short, I'm doing transfers of 16 bit ADC sampled data between...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.