Partner sites: Aminet - Amiga downloadsIntuitionBase - Amiga guidesAmigaNN - Amiga newsAmiFund - Sponsor projects

[Y]UtilityBase
Your guide to Amiga development
Sign up at our provider 
and get $50 off!
Not logged in
  HomeProjectsForumArticlesResourcesLinksChatAbout 
Search
Login
Username:
Password:
Register now!
Forgot your password?
Aminet - Development
Blitz_AVI.lha (dev/basic)
bullet.lha (dev/lib)
MCC_TheBar-26.6.lha (dev/mui)
MCC_TextEditor-15.35.lha (dev/mui)
MCC_BetterString-11.19.lha (dev/mui)
fw_c2p_p2c.lha (dev/lib)
plib_examples.lha (dev/src)
plib.lha (dev/lib)
MCC_NList-0.107.lha (dev/mui)
MCC_NList-0.106.lha (dev/mui)
More...
Newest users
prowler
apache64 (Chris)
cpeel2300 (Chris)
Branquito
chris (Chris Young)

Pending:
ZebraZeem, Mad_Dog, hhjoker, voxel, JosDuchIt, MarcB, MarBo, Sollaris, sara, species
More...
Who's Online
Online members:

obarthel 17 min(s) ago

15 guests are online.

You are an Anonymous user. You can register for free by clicking here.
News sites
Amiga-News.de
Amiga.org
AmigaNN
Amigans.net
Amigaweb.net
AmigaWorld.net
AROS-Exec
MorphOS-News.de
MorphZone
polarBoing
Tutorials
Change Graphics cards in AmigaOS 4.
Installing the latest OS4 SDK in Cubic IDE
Writing Installer scripts for AmiUpdate
Cross Compiling for OS4 or OS3 using MS Visual Studio 2005
Installing an AmigaOS 4 cross compiler
More...
Sources
How to open and use the exec debug interface
How to install a hardware interrupt
Install SObjs with Installer
How to make clean picture datatypes
Most of the old ClassACT examples converted to OS4
More...
Documentations
How to write portable code for Amiga (english)
Comment écrire du code portable pour Amiga (français)
Development How to with OS3.9 SDK
The PartyPack Hack
The Amiga PDA Programming Guidelines
More...
DreamHost

Support
UtilityBase

[Valid RSS]

UtilityBase needs your help!

AmigaOS Classic - C/C++ development Forum / AmigaOS Classic - C/C++ development /

GCC (g++) Memory allocation issues

 
Author NovaCoder
Forums Member
#1 · Posted: 2010-07-23 03:35 · Edited by: NovaCoder
Ok this is really starting to annoy me now :(

I'm trying to port this project to 68k (020) using gcc V2.9.5.3 (CubicIDE) and I'm having memory issues. The code is a large C++ project with a mixture of new(), malloc() and calloc().

There seems to be some kind of arbitrary limit of memory that causes the program to crash. The only way I've managed to get it run is to convert all of the calls from malloc()/calloc() to the AllocMem() which I obviously shouldn't have to do.

Currently I've got the compiler setup to store the data as 'FAR', does it need to be set to 'EXTRA FAR' or something silly like that?

Help!
Author itix
Forums Member
#2 · Posted: 2010-07-23 08:12
You dont have to convert malloc() to AllocMem(). Just write your own malloc()/free() functions using AllocMem() and track allocations.

But I dont think problem is in malloc() or in new operator. Most likely your project has buffer overflow somewhere. I recommend using Wipeout tool from Aminet to find out.
Author Hypex
Forums Member
#3 · Posted: 2010-07-25 16:00 · Edited by: Hypex
NovaCoder:
Currently I've got the compiler setup to store the data as 'FAR', does it need to be set to 'EXTRA FAR' or something silly like that?

AFAIK the FAR operator only affects pointers like those stored in the global data segment and not how to what extent or how far across memory can be in relation to any size limit. But I did some research and found there is a farmalloc() function. You don't need a whole farm to store your memory, no? Hehe. ;-)

So there is small model and large model (far). Small can use a register to store the global base location and reference data as an offset, usually 16-bit, and with fast access. Large would just use 32-bit pointers direct in memory and was usually slower. With 32-bit code (020+) generated 32-bit offsets can be referenced off a base register so large model could be used without a speed loss.

If the memory alloc routines are crashing they'd be badly written as they should return NULL! It's possible they are taking memory of a local heap for short allocations. What I wonder is the stack? How large is your stack? :-)
Author NovaCoder
Forums Member
#4 · Posted: 2010-07-26 00:57
Ok I think I've found the problem at last.

In parts of the code it calls calloc() but passes in zero for the number variable (the first parameter). In other implementations this would just result in no memory being allocated and calloc() would simply return NULL but in gcc 68k it blows up!

Oh well, glad I've finally figured this one out after all this time.
Author Hypex
Forums Member
#5 · Posted: 2010-07-26 15:26 · Edited by: Hypex
Yeah, asking a routine to allocate nothing would confuse it somewhat. ;-)

Now, why is it trying to allocate nothing?

And, with AllocMem() not crashing, were you able to track where it failed?
Author NovaCoder
Forums Member
#6 · Posted: 2010-07-27 01:34
Hiya,

Yep AllocMem() is obviously a little more 'system friendly' than the 68k gcc implementation.

I can't really change their source code so I'll just set up a define and route it through my own function (ie check for zero allocation and return NULL if detected).
Author NovaCoder
Forums Member
#7 · Posted: 2010-07-30 02:50
this is funny.

So to fix this issue I created a define, like this:

#define calloc(num, size) callocEx(num, size)

With the idea being that I could redirect all of the original code to my new 'gcc friendly' versions which I want to INLINE in a header file like this:

inline void* callocEX(size_t num, size_t size) {
if(num >0) {
return calloc(num, size);
} else {
return NULL;
}
}

But I don't want the #define to remap the nested calloc call as I'd get a circular reference :)

So what's the best way around this? Wrap the method in a #undef and then redefine it again?
Author matthey
Forums Member
#8 · Posted: 2010-08-01 21:12 · Edited by: matthey
Something like this won't work?...

#define malloc(size) AllocVec(size,MEMF_ANY)
#define calloc(num,size) AllocVec(num*size,MEMF_CLEAR)
#define free(ptr) FreeVec(ptr)

I believe AllocVec() will handle the NULLs. If you think the multiplication is slower, I bet that's what calloc() does anyway. Multiplication isn't that slow on newer processors anyway. The way I suggest should be faster if it works. You would have to include the appropriate AmigaOS includes of course.
Author obarthel
Forums Member
#9 · Posted: 2010-08-02 08:56 · Edited by: obarthel
NovaCoder:
In parts of the code it calls calloc() but passes in zero for the number variable (the first parameter). In other implementations this would just result in no memory being allocated and calloc() would simply return NULL but in gcc 68k it blows up!

Blows up how? Are you sure it's just calloc()? It could be that at some points malloc() is called with a zero length parameter, too.

If the code came from a Unix system, it's possible that it expects calloc() to return a non-NULL result if the first parameter is zero. This is actually permitted by the ISO 'C' language standard and its counterpart POSIX because some systems will return a non-NULL result if malloc(), which calloc() is built upon, receives a zero length parameter.

Optimistically called "implementation defined behaviour", you are in for a surprise if the system you deploy your code on is not sharing the relevant implementation details with the one it was developed for.

Here's one way to try this. Add you own calloc() function which works something like this:

void * calloc(size_t count,size_t size)
{
size_t total_size;
void * result;

total_size = count * size;
if(total_size == 0)
total_size = sizeof(void *); /* Or any size you prefer */

result = malloc(total_size);
if(result != NULL)
memset(result,0,total_size);

return(result);
}

This function returns a non-NULL result unless it runs out of memory. It will always allocate at least as much memory as is required to store a single pointer, which may or may not be enough.

Writing a malloc() replacement that returns a non-NULL result for a zero length parameter is tricker, though...
Author Hypex
Forums Member
#10 · Posted: 2010-08-02 17:13
You could also make up your own functions and put an assert in so you know where in the code zero is being passed.
 
AmigaOS Classic - C/C++ development Forum / AmigaOS Classic - C/C++ development / GCC (g++) Memory allocation issues Top
Your Reply Click this icon to move up to the quoted message

 

UtilityBase is a site focused on development for Amiga systems,
spanning over all different Amiga clones, that be AmigaOS 3.x, 4.x, MorphOS, AROS or AmigaDE/Anywhere.
News syndication: RSS
Contact address: mail@utilitybase com