overload new and delete with memalign or malloc

J

JanW

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
 
J

JanW

JanW said:
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
 
I

Ian Collins

JanW said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top