2010-03-27 20:18:01     Thread safe malloc()

Document created by Aaronwu Employee on Sep 26, 2013
Version 1Show Document
  • View in full screen mode

2010-03-27 20:18:01     Thread safe malloc()

Takemasa Nakamura (JAPAN)

Message: 87765   




I am developing a RTOS port for the Blackfin processor. Recently, I am planning to add C++ compatibility to this RTOS. To achieve this, I need thread safe malloc for creating objects during execution.


I am wondering how com I can obtain the thread safe malloc. According to the simple check with C program, the linked newlib to bfin-elf is not the thread safe.


Would you kindly tell me if there are any build option to link the thread-safe malloc ( or newlib ), please?


    Blackfin Toolchain 2009R1.1


    Ubuntu 9.04


Following is the list of my investigation.


takemasa@hilbert:~/foo$ bfin-elf-gcc -v

Using built-in specs.

Target: bfin-elf

コンフィグオプション: /usr/src/packages/BUILD/blackfin-toolchain-09r1.1/../gcc-4.1/configure --build=i686-pc-linux-gnu --host=i686-pc-linux-gnu --target=bfin-elf --prefix=/opt/uClinux/bfin-elf --with-newlib --disable-libstdcxx-pch --enable-languages=c,c++ --disable-symvers --disable-libssp --disable-libffi --enable-clocale=generic --enable-version-specific-runtime-libs --enable-__cxa_atexit --with-bugurl=URL:http://blackfin.uclinux.org/gf/project/toolchain/tracker --with-pkgversion=ADI-09r1.1-2

スレッドモデル: single

gcc バージョン 4.1.2 (ADI svn)

takemasa@hilbert:~/foo$ cat main.c

#include <stdlib.h>


int main(void)


    int *p;


    p = malloc(sizeof(int)*100);

    return 3;



takemasa@hilbert:~/foo$ bfin-elf-gcc -g2 main.c -msim

takemasa@hilbert:~/foo$ bfin-elf-gdb -quiet a.out

(gdb) target sim

Connected to the simulator.

(gdb) load

Loading section .init, size 0x12 vma 0x0

Loading section .text, size 0x1060 vma 0x14

Loading section .fini, size 0xe vma 0x1074

Loading section .rodata, size 0x8 vma 0x1084

Loading section .eh_frame, size 0x4 vma 0x108c

Loading section .ctors, size 0x8 vma 0x2090

Loading section .dtors, size 0x8 vma 0x2098

Loading section .jcr, size 0x4 vma 0x20a0

Loading section .data, size 0x81c vma 0x20a4

Start address 0x14

Transfer rate: 50656 bits in <1 sec.

(gdb) disassemble __malloc_lock

Dump of assembler code for function __malloc_lock:

0x00000744 <__malloc_lock+0>:    NOP;

0x00000746 <__malloc_lock+2>:    NOP;

0x00000748 <__malloc_lock+4>:    LINK 0x0;        /* (0) */

0x0000074c <__malloc_lock+8>:    UNLINK;

0x00000750 <__malloc_lock+12>:    RTS;

End of assembler dump.

(gdb) b __malloc_lock

Breakpoint 1 at 0x74c: file /usr/src/packages/BUILD/blackfin-toolchain-09r1.1/../gcc-4.1/newlib/libc/stdlib/mlock.c, line 53.

(gdb) run

Starting program: /home/takemasa/foo/a.out


Breakpoint 1, __malloc_lock (ptr=0x20b0)

    at /usr/src/packages/BUILD/blackfin-toolchain-09r1.1/../gcc-4.1/newlib/libc/stdlib/mlock.c:53

53    /usr/src/packages/BUILD/blackfin-toolchain-09r1.1/../gcc-4.1/newlib/libc/stdlib/mlock.c: No such file or directory.

    in /usr/src/packages/BUILD/blackfin-toolchain-09r1.1/../gcc-4.1/newlib/libc/stdlib/mlock.c








2010-03-27 20:27:45     Re: Thread safe malloc()

Mike Frysinger (UNITED STATES)

Message: 87766   


please read the newlib documentation:


If you have multiple threads of execution which may call any of these routines, or if any of these routines may be called reentrantly, then you must provide implementations of the <<__malloc_lock>> and <<__malloc_unlock>> functions for your system.  See the documentation for those functions.




2010-03-27 20:50:59     Re: Thread safe malloc()

Takemasa Nakamura (JAPAN)

Message: 87767   


Thanks, Mike


I will read Cygnus Newlib documentation. If you have any Blackfin specific Newlib documentation, please tell me. I checked Blackfin Koop documentation page before, but newlib page was empty.






2010-03-29 19:53:11     Re: Thread safe malloc()


Message: 87805   




There isn't any Blackfin specific newlib docs - that is why the page is blank.