overload new and delete with memalign or malloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JanW

    overload new and delete with memalign or malloc

    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_M EMALIGN, 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_M EMALIGN, 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

    #2
    Re: overload new and delete with memalign or malloc

    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

    Comment

    • Ian Collins

      #3
      Re: overload new and delete with memalign or malloc

      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.

      Comment

      Working...