473,473 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

overriding placement new + memory management

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 current file, line and function:

setOwner(const char *file, const u32 line, const char *func)
{
sourceFile = file;
sourceLine = line;
sourceFunc = func;
}

Then I can use this information to show some debug with an overloaded global
new opertor
void *operator new(size_t reportedSize)
{
use information
do normal new code
}

(This is the fluid studio memory manager)

This macro allow to deal with placement new.
My problem is that I need to have this debug informaiton thread safe.
So I put a critical section in Set Owner and leave it in operator new.

setOwner(const char *file, const u32 line, const char *func)
{
EnterCriticalSection()
sourceFile = file;
sourceLine = line;
sourceFunc = func;
}

void *operator new(size_t reportedSize)
{
use information
do normal new code
LeaveCriticalSection()
}

The problem is with placement new, : I don't want to debug placement new,
this is not required, but
as my macro is there I enter in SetOwner, so in the critical section but
never
leave...
So to resolve the problem I wrote my own version of placement new:

inline void* __cdecl operator new[](size_t, void* anywhere)
{
leaveCriticalSection();
return anywhere;
}

but my version of placement news seems to never been call and I block in
critical section.
I read on a web page that replacing global placement new is illegal. Is it
true ?
I try to force the usage of my placement new with some compiler specific
macro
(like define
#define __PLACEMENT_NEW_INLINE
#define __PLACEMENT_VEC_NEW_INLINE for visual 2005
before to include my own) but this seems not work.

any idea about own to force the use of my placement new ? Is it possible ?
or anybody have a better idea to debug with multithreading.

I know that i can't do an undef macro before the placement new and redo the
macro after, but as
I want to integrate external source code, I want to be able to use external
source without to have
to modify each placement new.

Thanks for your anwser.

Cheers

Sebastion Lagarde.
May 4 '07 #1
5 5811

"Lagarde Sébastien" <sl******@e-neko.comwrote in message
news:46***********************@news.free.fr...
Hello,

[... snip ...]

any idea about own to force the use of my placement new ? Is it possible ?
or anybody have a better idea to debug with multithreading.
Can't you just make use of thread local variables? That way you don't have
to use a critical section as each thread is using it's own set of variables.

- Sylvester Hesp
May 4 '07 #2
It sound like a good idea,
I think a problem is that I have a lot of variable, (lot of statistic and
other),

I will check this. Thanks.

Any other esaier way ?

"Sylvester Hesp" <s.****@oisyn.nla écrit dans le message de news:
46*********************@news.xs4all.nl...
>
"Lagarde Sébastien" <sl******@e-neko.comwrote in message
news:46***********************@news.free.fr...
>Hello,

[... snip ...]

any idea about own to force the use of my placement new ? Is it possible
?
or anybody have a better idea to debug with multithreading.

Can't you just make use of thread local variables? That way you don't have
to use a critical section as each thread is using it's own set of
variables.

- Sylvester Hesp

May 4 '07 #3
I found the problem of the TLS approch.
The problem is that i can allocate memory in a thread and free it in another
thread.
if I use TLS, I can't track my pointer...

"Sylvester Hesp" <s.****@oisyn.nla écrit dans le message de news:
46*********************@news.xs4all.nl...
>
"Lagarde Sébastien" <sl******@e-neko.comwrote in message
news:46***********************@news.free.fr...
>Hello,

[... snip ...]

any idea about own to force the use of my placement new ? Is it possible
?
or anybody have a better idea to debug with multithreading.

Can't you just make use of thread local variables? That way you don't have
to use a critical section as each thread is using it's own set of
variables.

- Sylvester Hesp

May 4 '07 #4
On May 4, 10:23 am, "Lagarde Sébastien" <slaga...@e-neko.comwrote:
I write code to debug new call with following macro:
#define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-),
FALSE) ? NULL : new
[...]
The problem is with placement new, : I don't want to debug
placement new, this is not required, but as my macro is there
I enter in SetOwner, so in the critical section but never
leave...
So to resolve the problem I wrote my own version of placement new:
inline void* __cdecl operator new[](size_t, void* anywhere)
{
leaveCriticalSection();
return anywhere;
}
but my version of placement news seems to never been call and I block in
critical section.
If operator new is inline, then the original version has already
been compiled into the library code.

Formally, using #define to define a keyword is undefined
behavior if you include any standard header. Practically, I
wouldn't be a bit surprised if it didn't cause problems if you
do it before including the standard headers: all it takes is for
a template to be used in two different translation units.

The usual solution here is to write system specific code to do a
stack walkback in the debugging operator new. This only gives
you a hex dump of the stack, to begin with, but it's not as if
you need to use the information that often, and usually, there
are platform specific tools available which will interpret the
hex addresses for you. The stack walkback code is, of course,
very, very platform specific, but it's rather straightforward on
Intel architecture, and g++ has an extention in its runtime that
you can use with it, so those two platforms are easy.
I read on a web page that replacing global placement new is
illegal. Is it true ?
I think so. It's not listed among the functions you can
replace. (Note too that the replacement functions are NOT
allowed to be inline. This is to ensure that even library code
compiled elsewhere will get the replacement.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 4 '07 #5
thanks for the tips, I finally achieve my goal with the TLS tips suggested
before.
Thanks you all.

"James Kanze" <ja*********@gmail.coma écrit dans le message de news:
11*********************@p77g2000hsh.googlegroups.c om...
On May 4, 10:23 am, "Lagarde Sébastien" <slaga...@e-neko.comwrote:
I write code to debug new call with following macro:
#define new (MemoryManager::Get().setOwner (__FILE__, __LINE__,
_FUNCTION-),
FALSE) ? NULL : new
[...]
The problem is with placement new, : I don't want to debug
placement new, this is not required, but as my macro is there
I enter in SetOwner, so in the critical section but never
leave...
So to resolve the problem I wrote my own version of placement new:
inline void* __cdecl operator new[](size_t, void* anywhere)
{
leaveCriticalSection();
return anywhere;
}
but my version of placement news seems to never been call and I block in
critical section.
If operator new is inline, then the original version has already
been compiled into the library code.

Formally, using #define to define a keyword is undefined
behavior if you include any standard header. Practically, I
wouldn't be a bit surprised if it didn't cause problems if you
do it before including the standard headers: all it takes is for
a template to be used in two different translation units.

The usual solution here is to write system specific code to do a
stack walkback in the debugging operator new. This only gives
you a hex dump of the stack, to begin with, but it's not as if
you need to use the information that often, and usually, there
are platform specific tools available which will interpret the
hex addresses for you. The stack walkback code is, of course,
very, very platform specific, but it's rather straightforward on
Intel architecture, and g++ has an extention in its runtime that
you can use with it, so those two platforms are easy.
I read on a web page that replacing global placement new is
illegal. Is it true ?
I think so. It's not listed among the functions you can
replace. (Note too that the replacement functions are NOT
allowed to be inline. This is to ensure that even library code
compiled elsewhere will get the replacement.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 7 '07 #6

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...
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...
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 }
13
by: Samshayam | last post by:
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...
1
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy {...
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...
9
by: kamsmartx | last post by:
hi i have the problem in this program import java.io.*; import java.io.InputStream; import java.io.OutputStream; import java.io.FileInputStream; import java.io.FileOutputStream;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.