Topic : dynamic allocation using raisonance

Forum : ST7/STM8

Original Post
Post Information Post
July 26, 2010 - 2:55pm
Guest

Hi everybody,

I want to use malloc() to allcaote dynamically memory space.
I found that I have to include "alloc.h" but the allocation fails and I don't understand the reason.
Is there something that I have to add ?

NB: I am using raisonance compiler with STDV.

I am very thankful.

#include "alloc.h"

uint8_t* tab;

/* Allocate momory space */
tab = malloc(10);

if (tab == NULL)
{
/* Memory space can't be allocated */
checkstatus = ERROR;
}

Replies
Post Information Post
+1
0
-1
July 27, 2010 - 9:51am
Raisonance Support Team

Hi,

The dynamic memory manager has been extensively described in the documentation which will be released with the next RKit-STM8. I copy the interesting part from it below.

Actually you must NOT include alloc.h, but stdlib.h, but you have to call an initialization function mempool_init at the beginning of your program.

Let me know if you still have problems.
Regards,
Bruno

4.15 Using the dynamic memory manager

RCSTM8 includes support for allocating and freeing dynamic memory.
The dynamic memory handling functions are defined in the standard header file as follows:

void *malloc(size_t size); allocates an object of size 'size', and returns the address of the object if successful; otherwise, it returns a null pointer. The values stored in the object are indeterminate.

void *calloc(size_t nelem, size_t size); allocates an array object containing 'nelem' elements each of size 'size', stores zeros in all bytes of the array, and returns the address of the first element of the array if successful; otherwise, it returns a null pointer.

void *realloc(void *ptr, size_t size); allocates an object of size 'size', possibly obtaining initial stored values from the object whose address is 'ptr'. It returns the address of the new object if successful; otherwise, it returns a null pointer.

void free(void *ptr); If 'ptr' is not a null pointer, the function deallocates the object whose address is 'ptr'; otherwise, it does nothing. You can deallocate only objects that have been allocated by calling calloc, malloc, or realloc.

The dynamic memory comes from a "heap" of available space, which must first be granted to the dynamic memory manager. This must be done using the mempool_init function before any call to dynamic memory functions is performed.

void mempool_init(void *ptr, size_t size); initializes the dynamic memory manager and provides a memory buffer of size 'size' at the address pointed to by 'ptr' for the heap.

Note: The dynamic memory manager requires at least 32 bytes to be functional. This is the
minimal size of the buffer to be passed to mempool_init().

4.15.1 Dynamic memory manager example

Here is an example that shows how the dynamic memory manager can be used:

#include 
#include 
#include 

/* Buffer for the dynamic memory manager (32 bytes minimum) */
unsigned char mempool_buffer[100];

char* buffer;

void main( void )
{
    /* Initialization of the dynamic memory manager */
    mempool_init( mempool_buffer, sizeof( mempool_buffer ) );

    /* Allocates a 40-byte buffer */
    buffer = (char *)malloc( 40 );
    if( buffer != NULL )
    {
        /* Write something in the buffer and then print it */
        strcpy( buffer, "hello" );
        printf( "40 chars buffer at address 0x%04x, contains \"%s\"\n", buffer, buffer );
        free( buffer );
    }
    else
    {
        printf( "Allocation error: Returned 0x%04x\n", buffer );
    }

    while(1);
}
+1
0
-1
July 29, 2010 - 4:20pm
Guest

Hi Bruno,

Thanks Bruno for this reply.
I have tested it and it is working fine now !

But I still have a question: why should I have to call mempool_init() ?
When I've learned programming c language (using borland c or Visual studio) I never required to call a such function.
Is there a tip that borland or visual studio is hiding or what is the matter ?

Thanks for your help.

With regards,
bada380

+1
0
-1
July 30, 2010 - 10:03am
Raisonance Support Team

Hi Bada,

Yes of course, there is some hidden stuff: The dynamic memory always has to be initialized.
In the C startup code for Borland or MSVC there is some code in their startup file to initialize various things, amonst them the dynamic memory manager and the printf facility.

In the embedded world we want to reduce the applications footprint as much as possible; so the startup is extremely limited and performs only global variables initilization before calling main().
This is why initilization of the dynamic memory requires an explicit initialization through mempool_init.

Regards,
Bruno

+1
0
-1
August 5, 2010 - 1:01pm
Guest

Hi Brunio

I have used it and it is working fine now but I still have a warning that I hope that it disappears.

#define BUFFER_SIZE 100

uint8_t mempool_buffer[BUFFER_SIZE];

/* Initialization of the dynamic memory manager */
/* line 102 */ mempool_init(mempool_buffer, sizeof(mempool_buffer));

I got the following message:
*** WARNING C207 IN LINE 102 OF ..\..\..\main.c : suspicious pointer conversion

Thanks in advance.

Thanks and best regards,
bada380

+1
0
-1
August 6, 2010 - 12:08pm
Guest

bada380 wrote:
Hi Brunio

I have used it and it is working fine now but I still have a warning that I hope that it disappears.

#define BUFFER_SIZE 100

uint8_t mempool_buffer[BUFFER_SIZE];

/* Initialization of the dynamic memory manager */
/* line 102 */ mempool_init(mempool_buffer, sizeof(mempool_buffer));

I got the following message:
*** WARNING C207 IN LINE 102 OF ..\..\..\main.c : suspicious pointer conversion

Thanks in advance.

Thanks and best regards,
bada380

Compiler Warnings like that generally arent bad... EEPROM functions also give the same warning but work well.

I think it occur when there are pointers passed as function values... it mean "control if are correct or I'll do some mistake" ;)

+1
0
-1
August 13, 2010 - 11:06am
Guest

Hi GremlinC5,

I think that it should be a solution to hide this warning, shouldn't it ?
My customer will worry if he sees the warning message and surely he will ask me to hide it (pretext that it could lead to a disaster).

Regards,
bada380

+1
0
-1
August 23, 2010 - 11:12am
Raisonance Support Team

Hi,

The compiler emits a warning because the mempool_init() function requires a pointer to a dynamic_allocator_struct and gets a pointer to void. A cast will remove the warning.

You can change the line as follows to remove the warning:

mempool_init( (dynamic_allocator_struct allocmem*)mempool_buffer, sizeof( mempool_buffer ) );

Regards,