overriding placement new + memory management

Discussion in 'C++' started by Lagarde Sébastien, May 4, 2007.

  1. 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.
    Lagarde Sébastien, May 4, 2007
    #1
    1. Advertising

  2. "Lagarde Sébastien" <> wrote in message
    news:463aee6a$0$32341$...
    > 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
    Sylvester Hesp, May 4, 2007
    #2
    1. Advertising

  3. 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" <> a écrit dans le message de news:
    463afbad$0$321$4all.nl...
    >
    > "Lagarde Sébastien" <> wrote in message
    > news:463aee6a$0$32341$...
    >> 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
    >
    Lagarde Sébastien, May 4, 2007
    #3
  4. 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" <> a écrit dans le message de news:
    463afbad$0$321$4all.nl...
    >
    > "Lagarde Sébastien" <> wrote in message
    > news:463aee6a$0$32341$...
    >> 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
    >
    Lagarde Sébastien, May 4, 2007
    #4
  5. Lagarde Sébastien

    James Kanze Guest

    Re: overriding placement new + memory management

    On May 4, 10:23 am, "Lagarde Sébastien" <> wrote:

    > 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:
    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
    James Kanze, May 4, 2007
    #5
  6. Re: overriding placement new + memory management

    thanks for the tips, I finally achieve my goal with the TLS tips suggested
    before.
    Thanks you all.

    "James Kanze" <> a écrit dans le message de news:
    ...
    On May 4, 10:23 am, "Lagarde Sébastien" <> wrote:

    > 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:
    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
    Lagarde Sébastien, May 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark P
    Replies:
    6
    Views:
    805
    James Dennett
    Apr 27, 2005
  2. mangesh
    Replies:
    15
    Views:
    630
    mlimber
    Jun 30, 2006
  3. Replies:
    13
    Views:
    898
    Alf P. Steinbach
    Jul 7, 2006
  4. SarahT
    Replies:
    1
    Views:
    337
    mlimber
    Jan 3, 2007
  5. Matt Oefinger
    Replies:
    0
    Views:
    213
    Matt Oefinger
    Jun 25, 2003
Loading...

Share This Page