overload new and delete with memalign or malloc

Discussion in 'C++' started by JanW, Jan 23, 2007.

  1. JanW

    JanW Guest

    Hi,

    did a search for previous posts, but could still not figure out what
    I'm doing wrong in my code.

    Due to some DMA hardware requirements (IBM Cell processor) I'm
    attempting to overload new, new[], delete and delete[] with calls to
    memalign() to insure that all objects and data is aligned to 16 byte
    boundaries.

    For some reason I'm getting "multiple definition of 'operator new'":

    mpifxcorr-visibility.o: In function `operator new(unsigned int)':
    /home/jr/correlator/mpifxcorr/src/cell-memory.h:32: multiple definition
    of `operator new(unsigned int)'
    mpifxcorr-mode.o:/home/jr/correlator/mpifxcorr/src/cell-memory.h:32:
    first defined here

    cell-memory.h :

    #ifndef CELLMEMORY_H_
    #define CELLMEMORY_H_

    #include <new>
    #include <exception> // std::bad_alloc()
    #include <cstdlib> // (hmm, no memalign() here..?)
    #include <malloc.h> // for memalign()
    using namespace std;

    #define CELL_MEMALIGN 16

    void* operator new (size_t size)
    {
    void *p;
    p = memalign(CELL_MEMALIGN, size);
    if (p==0) { throw std::bad_alloc(); } // for ANSI/ISO compliant
    behavior
    return p;
    }

    void* operator new[] (size_t size)
    {
    void *p;
    p = memalign(CELL_MEMALIGN, size);
    if (p==0) { throw std::bad_alloc(); } // for ANSI/ISO compliant
    behavior
    return p;
    }

    void operator delete (void *p)
    {
    free(p);
    }

    void operator delete[] (void *p)
    {
    free(p);
    }
    #endif

    Any ideas? (Perhaps the problem is not in the above code?)

    thanks,
    - Jan
     
    JanW, Jan 23, 2007
    #1
    1. Advertisements

  2. JanW

    JanW Guest

    JanW wrote:
    > Hi,
    >
    > did a search for previous posts, but could still not figure out what
    > I'm doing wrong in my code.
    >
    > Due to some DMA hardware requirements (IBM Cell processor) I'm
    > attempting to overload new, new[], delete and delete[] with calls to
    > memalign() to insure that all objects and data is aligned to 16 byte
    > boundaries.
    >
    > For some reason I'm getting "multiple definition of 'operator new'":

    <snip>

    Argh, please disregard, my stupidity. Don't define functions in a
    header file, only declare.... Now it works.

    - Jan
     
    JanW, Jan 23, 2007
    #2
    1. Advertisements

  3. JanW

    Ian Collins Guest

    JanW wrote:
    > Hi,
    >
    > did a search for previous posts, but could still not figure out what
    > I'm doing wrong in my code.
    >
    > Due to some DMA hardware requirements (IBM Cell processor) I'm
    > attempting to overload new, new[], delete and delete[] with calls to
    > memalign() to insure that all objects and data is aligned to 16 byte
    > boundaries.
    >
    > For some reason I'm getting "multiple definition of 'operator new'":
    >

    If you put function definitions in headers they must be 'inline'.

    Surely malloc on your target will return correctly aligned memory?

    > using namespace std;


    NO, never ever do this in a header!

    --
    Ian Collins.
     
    Ian Collins, Jan 23, 2007
    #3
    1. Advertisements

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. Chris E. Yoon
    Replies:
    2
    Views:
    531
    Michiel Salters
    Jul 23, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    573
    Alf P. Steinbach
    Aug 6, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    564
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    640
    Nick Hounsome
    Jan 25, 2004
  5. Nimmi Srivastav
    Replies:
    1
    Views:
    924
    John Harrison
    Feb 1, 2004
  6. Guest
    Replies:
    7
    Views:
    16,293
    red floyd
    Jul 16, 2004
  7. Jef Driesen
    Replies:
    1
    Views:
    742
    Gernot Frisch
    Jan 19, 2005
  8. X X
    Replies:
    4
    Views:
    583
    red floyd
    Jul 19, 2010
Loading...