Using AR
Using AR
Release information
The following changes have been made to this Application Note.
Change history
Proprietary notice
ARM, the ARM Powered logo, Thumb and StrongARM are registered trademarks of ARM Limited.
The ARM logo, AMBA, Angel, ARMulator, EmbeddedICE, ModelGen, Multi-ICE, ARM7TDMI, ARM9TDMI, TDMI
and STRONG are trademarks of ARM Limited.
All other products, or services, mentioned herein may be trademarks of their respective owners
Confidentiality status
This document is Open Access. This document has no restriction on distribution.
Table of Contents
1 Introduction.................................................................................................................2
1 Introduction
This application note gives a brief introduction to the use of ARM C++ in embedded
systems. It provides additional information on system initialization and should be used in
conjunction with the chapter Writing Code for ROM in the ARM Software Development
Toolkit User Guide (ARM DUI 0040). All information in this document relates to the use of
SDT 2.50 and C++ 1.10.
The ARM C++ libraries are used in conjunction with one of the following:
• the semihosted ANSI C library
• the Embedded C library.
This application note discusses the use of the ARM C++ libraries with the Embedded C
Library.
By default, the ARM C++ library makes use of the semihosted ANSI C library. When using
this library, all system initialization is performed by code within the library before your own
code is executed.
Although the semihosted ANSI C library is suitable for early development work, the
Embedded C library has a number of advantages:
• It is significantly smaller.
• It makes no assumptions about the underlying hardware.
• It is simpler to use with your own initialization code started from the reset vector.
Note Only a subset of ANSI C functions is present in the Embedded C library; functions that
depend on the underlying hardware or operating system are omitted. However, it is still
possible to use semihosted operations by invoking semihosted SWIs directly.
You need to write a small amount of assembler code to perform low-level initialization
tasks before jumping to your C++ code. As an absolute minimum, this low-level
initialization must set up the stack pointer for all modes used.
A full discussion of this task is beyond the scope of this document. This code is not C++
specific and further information with sample code can be found in the chapter Writing
Code for ROM in the ARM Software Development Toolkit User Guide (ARM DUI 0040).
For all but the simplest C/C++ programs, you will need to make use of the heap to
dynamically allocate memory. Functions that use the heap include malloc() and the new
operator.
The Embedded C Library provides heap management. As the location and size of the
heap varies between targets, your code must pass this information to the heap manager.
Initialization function
You initialize the embedded heap manager by calling the following function with the size
and location of the block of memory to be used as the heap:
struct Heap_Descriptor *__rt_embeddedalloc_init(void *base, size_t size);
This function returns a pointer to a heap descriptor, and this result should be stored.
Call-back function
The Embedded C Library is designed to be fully re-entrant, and so it contains no static
data. For this reason, your code must provide a call-back function:
struct Heap_Descriptor *__rt_heapdescriptor(void)
This allows the library to locate the heap descriptor when required. This function passes
back the result of the __rt_embeddedalloc_init() function.
When using the C++ libraries with the Embedded C Library, your code must call this
function in order to initialize top-level objects properly.
Before your application terminates, you should call the following function to call all the
destructors for top-level objects:
void __cpp_finalise(void)
Note This will not be needed in embedded systems that never exit the main application code.
void ApplicationCode(void);
hp = __rt_embeddedalloc_init((void *)0x2030000,0x20000);
This code is included in the associated example, which is downloadable from the
application notes area of the ARM website.