This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: malloc vs. new


new shouldn't be broken, since it just calls malloc. does malloc work
when making allocations of the exact same size?

two things to try as workarounds:

1) write your own operator new/new[]/delete/delete[]. tossing the
following into some file should work:

void* operator new(size_t size) {
	return malloc(size_t);
}
void operator delete(void* ptr) {
	if( ptr )
		free(ptr);
}

make versions for new[] and delete[] also, which are the same function
with different names.

2) use placement new, which is what you were getting at below. be wary
of alignment considerations for the buffer you use. also at clean up you
have to manually call the destructor IIRC. C++ is not my forte.

#include <new.h>

char buffer[1024] __attribute__((align(4));

CObject* pObj = new(buffer) CObject();

...
pObj->~CObject();

Good luck.

-----Original Message-----
From: Scott Dattalo [mailto:scott@dattalo.com]
Sent: Friday, June 21, 2002 7:18 PM
To: ecos-discuss@sources.redhat.com
Subject: [ECOS] malloc vs. new



In a project I'm porting to eCos there is some C++ code. Consequently, 
there are several new() calls for creating new objects. Unfortunately, 
calls to new() hang. Specifically, the kernel is unable to allocate
memory 
and throws an exception. This switches control to the exception thread
and 
control is never released...

Calls to malloc, OTOH, work just fine.

I've sifted through the documentation and the best I could come up with
is 
this snippet:

#define MEMORY_BUFFER  0x1000
char StaticMemoryBuffer[MEMORY_BUFFER];

...


  cyg_handle_t mempool;
  cyg_mempool_var mempool_obj;
  cyg_mempool_var_create(StaticMemoryBuffer, MEMORY_BUFFER, &mempool, 
&mempool_obj);

This still fails. How does one provide a memory allocation buffer for 
new()?

Scott




-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


--
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]