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. Advertising

  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. Advertising

  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. 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. Chris E. Yoon
    Replies:
    2
    Views:
    391
    Michiel Salters
    Jul 23, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    394
    Alf P. Steinbach
    Aug 6, 2003
  3. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  4. Nimmi Srivastav
    Replies:
    1
    Views:
    704
    John Harrison
    Feb 1, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page