422,023 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,023 IT Pros & Developers. It's quick & easy.

overload new and delete with memalign or malloc

P: n/a
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

Jan 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jan 23 '07 #2

P: n/a
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.
Jan 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.