Jump to content

Recommended Posts

Hi all,

I am using BoostC++ v6.89 Pro with a PIC16F877

My application is not that big but the Class I have uses about 90 bytes of Heap space.

When I increase one of the string buffers by about 20 bytes the application crashes.

I allocate my class using:

---------------------------

ds1820 *q = new ds1820();

---------------------------

 

 

Memory Usage Report

===================

RAM available:368 bytes, used:88 bytes (24.0%), free:280 bytes (76.0%),

Heap size:280 bytes, Heap max single alloc:95 bytes

ROM available:8192 words, used:2665 words (32.6%), free:5527 words (67.4%)

-------------------------------------------------

 

 

My question is: Is the NEW keyword issuing a alloc for the Heap needed and is therefore restricted to 95 bytes?

 

 

Thanks

TimC

Link to post
Share on other sites
...My question is: Is the NEW keyword issuing a alloc for the Heap needed and is therefore restricted to 95 bytes?...

 

Yes the new operation is based on alloc (as well as delete is based on free). The total size of objects created using new is limited to the amount of free space in heap at the time such objects are created.

 

Regards,

Pavel

Link to post
Share on other sites

Thank you for the fast answer Pavel!

 

Moving from a 16F877 to a 18F452 my Heap max single alloc: moves from 95 bytes to 127 bytes.

Now I have a little more space to work with.

 

Is there any other ways to increase my max single alloc and therefore increase the size of my class?

 

Regards

TimC

Link to post
Share on other sites
Thank you for the fast answer Pavel!

 

Moving from a 16F877 to a 18F452 my Heap max single alloc: moves from 95 bytes to 127 bytes.

Now I have a little more space to work with.

 

Is there any other ways to increase my max single alloc and therefore increase the size of my class?

 

Regards

TimC

 

Heap size depends on how much memory is left after allocating space for variables in the code. Use fewer variables in the code and heap will get bigger. Or delete objects that are not used any more from heap (if there are any).

 

Regards,

Pavel

Link to post
Share on other sites
  • 3 weeks later...

Pavel,

Where is the best place to look to know if my class is going to fit in the max heap single allocation without problems?

 

Currently I am using a large dummy array to estimate how much memory I have left. When the class is too big and memory is corrupted, I reduce the size of the dummy array. I also tried adding up RAM use in all the class functions printed in the .stat file but that is a slow process as well.

 

Just wish there was a faster more professional way.

 

Regards

Tim C.

Link to post
Share on other sites
Where is the best place to look to know if my class is going to fit in the max heap single allocation without problems?

 

Currently I am using a large dummy array to estimate how much memory I have left. When the class is too big and memory is corrupted, I reduce the size of the dummy array. I also tried adding up RAM use in all the class functions printed in the .stat file but that is a slow process as well.

 

Just wish there was a faster more professional way.

 

Linker outputs heap size and max single alloc size. You need to know your class size and check if it fits into a single alloc:

 

Linking...

 

"C:\Program Files\SourceBoost\boostlink.pic.exe" /ld "C:\Program Files\SourceBoost\lib" libc.pic16.lib interrupt.obj /t PIC16F627 /d "C:\Program Files\SourceBoost\Samples\C\BoostC" /p interrupt

 

BoostLink Optimizing Linker Version 6.90

http://www.sourceboost.com

Copyright© 2004-2008 Pavel Baranov

Copyright© 2004-2008 David Hobday

 

 

Building CASM file

 

Memory Usage Report

===================

RAM available:224 bytes, used:4 bytes (1.8%), free:220 bytes (98.2%),

Heap size:220 bytes, Heap max single alloc:94 bytes

ROM available:1024 words, used:40 words (4.0%), free:984 words (96.0%)

 

 

success

Done

 

Regards,

Pavel

Link to post
Share on other sites
I know how to determine the alloc size, it's the size of MY class I want to know.

 

You can use the sizeof operation on the class data type. For compile time allocated class objects one can also look into the generated .asm file where sizes of all variables used in the code are listed.

 

Regards,

Pavel

Link to post
Share on other sites

TimC

My question is: Is the NEW keyword issuing a alloc for the Heap needed and is therefore restricted to 95 bytes?
The management heap works by having a single byte in the head of each block keeping track of the block size and whether or not it is in use. So thats one bit for usage and 7 bits for the block length. That means the maximum block you can allocate on the heap is 127bytes. A heap block must reside in a single RAM bank.

 

The heap is made up of the memory left over after linker has allocated all the memory for your program variables. This memory may be fragmented, ie it may be in small pieces. Also on PIC16 targets the biggest allocation on the heap will be 1 bytes less than the largest block of contigious memory block that is not used. No PIC16 targets have a full bank (128 byte) just as RAM, so you best case senario on a PIC16 limited by the biggest contigious memory block.

 

I hope that helps explain things.

 

Regards

Dave

Link to post
Share on other sites

Thank you Pavel

sizeof(xxx) was giving me a small value, yet I still had a problem, but now I am sure it's correct.

 

I have another problem which shows up when I allocate a large dummy variable inside the class and causes the lcd or uart routines to crash. So now it looks like I might have some bad pointers.

 

I wrote this routine earlier that helps bail out if the class is too big:

 

unsigned char heap_max_chunk() {
//since there is a difference between max single allocation and
//heap available we have to get the max chunk first
#define INCR	1	// larger number will make routine run faster but less accurate
unsigned char x=INCR;	// start testing at INCR bytes  
void * p_mem;

p_mem = alloc(x);
do {
	free(p_mem);		// give back before we ask for a bigger chunk
	x +=INCR;
	p_mem = alloc(x);	
} while (p_mem);		// if last alloc fails we fall through


return(x-INCR);	// subtract because last alloc had failed
}

 

And to use:

	if ((heap_max_chunk() - sizeof(ds1820)) > 0 )
	ds1820 *q = new ds1820(); //create ds1820 object.. or else.
else
	puts("** PROGRAM FAIL ** unable to allocate memory for class ds1820");

Link to post
Share on other sites
Thank you Pavel

sizeof(xxx) was giving me a small value, yet I still had a problem, but now I am sure it's correct.

 

I have another problem which shows up when I allocate a large dummy variable inside the class and causes the lcd or uart routines to crash. So now it looks like I might have some bad pointers.

 

I wrote this routine earlier that helps bail out if the class is too big:

 

unsigned char heap_max_chunk() {
//since there is a difference between max single allocation and
//heap available we have to get the max chunk first
#define INCR	1	// larger number will make routine run faster but less accurate
unsigned char x=INCR;	// start testing at INCR bytes  
void * p_mem;

p_mem = alloc(x);
do {
	free(p_mem);		// give back before we ask for a bigger chunk
	x +=INCR;
	p_mem = alloc(x);	
} while (p_mem);		// if last alloc fails we fall through


return(x-INCR);	// subtract because last alloc had failed
}

 

And to use:

	if ((heap_max_chunk() - sizeof(ds1820)) > 0 )
	ds1820 *q = new ds1820(); //create ds1820 object.. or else.
else
	puts("** PROGRAM FAIL ** unable to allocate memory for class ds1820");

 

Your code looks good. Don't see any problems except the if statement could get changed to if (((signed char)heap_max_chunk() - sizeof(ds1820)) >= 0 ) to catch the case when sizeof of your class is equal to the max chunk size. The crash problem you mentioned must happen somewhere else.

 

Regards,

Pavel

Link to post
Share on other sites
  • 1 month later...

Pavel,

Your new version 6.91 works perfectly. I do not see any of the problems I was having before. I have about 50 more bytes to play with inside the class now.

 

I am so pleased thank you for your work ;)

 

Regards

Tim

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...