What is the equavalint code of the memmory allocation

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

What is the equavalint code of the memmory allocation

Postby robu » Sat Nov 24, 2012 3:46 pm

I wrote a matrix class in c++ and I would like to test on a Raspberry Pi using ChibiOS/RT. Durring the compilation I hade problem with the memset

Code: Select all

Linking build/ch.elf
/opt/arm_gcc/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0x18): undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status


If I take out the memset than I can compile/linke the code. What can be the problem?

I also decided to use the ChibiOS/RT memory allocation, but I haven't found any example regarding this.
For example what is the equivalent code for this:

Code: Select all

double * t = (double *) malloc(2*2*sizeof(double))
memset((void *) t, 0, sizeof(double) * 4);
free(t);

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: What is the equavalint code of the memmory allocation

Postby Giovanni » Sat Nov 24, 2012 4:33 pm

Hi,

chHeapAlloc() is very similar to malloc(), see the documentation page for details, there are examples in the test suite under ./test.

Giovanni

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: What is the equavalint code of the memmory allocation

Postby robu » Sat Nov 24, 2012 7:00 pm

Thanks for your answer!

In my case a I have a matrix class

static MemoryHeap myheap;

Code: Select all

static MemoryHeap myheap;

Matrix::Matrix(size_t rows, size_t cols):rows(rows), cols(cols)
{
data = (double *) chHeapAlloc(&myheap, rows*cols*sizeof(double));
}
Matrix::~Matrix()
{
chHeapFree(data);
}

I'm a bit newbie and I don't understand what is the chHeapInit. My other question that before I used memset and memcpy, I can replace this with a for cycle to initialize or copy the data, I just wan to know that is there an other way to replace this commands?
According this documentation the heap has the same limitation as the C-runtime, what else are the advantages of the heap?
http://www.chibios.org/dokuwiki/doku.ph ... age_memory

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: What is the equavalint code of the memmory allocation

Postby robu » Sat Nov 24, 2012 7:05 pm

Also when I'm using memset and memcpy why do I get the following linking error:

Code: Select all

/opt/arm_gcc/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0x18): undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status
make: *** [build/ch.elf] Error 1

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: What is the equavalint code of the memmory allocation

Postby Giovanni » Sat Nov 24, 2012 7:09 pm

Hi,

That is a problem with the C library, you have to provide the implementation of _sbrk(), see the newlib documentation for this. Searching this forum could give you several hits about this.

Under ./os/various there is a file syscall.c, it should provide the required implementations but I never tested it with the RPI port.

Giovanni

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: What is the equavalint code of the memmory allocation

Postby robu » Sat Nov 24, 2012 7:13 pm

Hi,

I tried to include the -lstdc++ in this case I'm receiving a different error:

Code: Select all

/opt/arm_gcc/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld: error: no memory region specified for loadable section `.ARM.extab'
collect2: error: ld returned 1 exit status


Thanks,
Robert

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: What is the equavalint code of the memmory allocation

Postby Giovanni » Sat Nov 24, 2012 9:00 pm

Hi,

The linker script needs to be updated to allocate the missing sections, look at the standard linker scripts of your compiler, you should see those sections listed inside.

Giovanni

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: What is the equavalint code of the memmory allocation

Postby robu » Sun Nov 25, 2012 11:37 pm

Hi,
I'm really newbie, I just started to program embedded system a few days ago, I bought a Raspberry PI, and I don't know where can I find this section/file.
Thanks,
Robert

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: What is the equavalint code of the memmory allocation

Postby robu » Wed Nov 28, 2012 11:24 pm

Hi,
This is my linker script:

Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * BCM2835 memory setup.
 */
__und_stack_size__   = 0x0004;
__abt_stack_size__   = 0x0004;
__fiq_stack_size__   = 0x0010;
__irq_stack_size__   = 0x0080;
__svc_stack_size__   = 0x0004;
__sys_stack_size__   = 0x0400;
__stacks_total_size__   = __und_stack_size__ + __abt_stack_size__ + __fiq_stack_size__ + __irq_stack_size__ + __svc_stack_size__ + __sys_stack_size__;

MEMORY
{
    ram : org = 0x8000, len = 0x06000000 - 0x20
}

__ram_start__      = ORIGIN(ram);
__ram_size__      = LENGTH(ram);
__ram_end__      = __ram_start__ + __ram_size__;

SECTIONS
{
   . = 0;

   .text : ALIGN(16) SUBALIGN(16)
   {
        _text = .;
        KEEP(*(vectors))
        *(.text)
        *(.text.*)
        *(.rodata)
        *(.rodata.*)
        *(.glue_7t)
        *(.glue_7)
        *(.gcc*)
        *(.ctors)
        *(.dtors)
    } > ram

    .ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)}

    __exidx_start = .;
    .ARM.exidx : {*(.ARM.exidx* .gnu.linkonce.armexidx.*)} > ram
    __exidx_end = .;

    .eh_frame_hdr : {*(.eh_frame_hdr)}

    .eh_frame : ONLY_IF_RO {*(.eh_frame)}

    . = ALIGN(4);
    _etext = .;
    _textdata = _etext;

    .data :
    {
        _data = .;
        *(.data)
        . = ALIGN(4);
        *(.data.*)
        . = ALIGN(4);
        *(.ramtext)
        . = ALIGN(4);
        _edata = .;
    } > ram

    .bss :
    {
        _bss_start = .;
        *(.bss)
        . = ALIGN(4);
        *(.bss.*)
        . = ALIGN(4);
        *(COMMON)
        . = ALIGN(4);
        _bss_end = .;
    } > ram   
}

PROVIDE(end = .);
_end = .;

__heap_base__              = _end;
__heap_end__               = __ram_end__ - __stacks_total_size__;
__main_thread_stack_base__ = __ram_end__ - __stacks_total_size__;


What should I update?

Thank you for your help.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: What is the equavalint code of the memmory allocation

Postby Giovanni » Thu Nov 29, 2012 8:48 am

Try changing

.ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)}

in

.ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)} > flash

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 13 guests