Skip to content

StackSizeControl

Simon Wright edited this page Oct 29, 2022 · 7 revisions

There are two kinds of stack in Cortex GNAT RTS, with distinct uses.

  • The initial stack
    • used during program boot.
    • used for interrupt handlers (which may be nested).
  • task stacks
    • used in the environment task, mainly for elaboration but thereafter for the main program.
    • used in ordinary tasks.

You can control the stack sizes at run time, by declaring special constants (perhaps best in your main program), as below. The values shown are the defaults, which will be used if you don't declare the constants.

For the initial stack (also used for interrupt programs),

   Default_Initial_Stack : constant Natural := 1024
   with
     Export,
     Convention => Ada,
     External_Name => "_default_initial_stack";

For the environment task, the stack size

   Environment_Task_Storage_Size : constant Natural := 1536
   with
     Export,
     Convention => Ada,
     External_Name => "_environment_task_storage_size";

and the secondary stack size (the default here is actually the standard default, 10% of the environment task's stack)

   Environment_Task_Secondary_Stack_Size : constant Natural := 153
   with
     Export,
     Convention => Ada,
     External_Name => "_environment_task_secondary_stack_size";

For ordinary tasks,

   Default_Storage_Size : constant Natural := 1024
   with
     Export,
     Convention => Ada,
     External_Name => "_default_storage_size";

You can also control the minimum task stack size:

   Minimum_Storage_Size : constant Natural := 768
   with
     Export,
     Convention => Ada,
     External_Name => "_minimum_storage_size";

For the curious, the way this works can be seen in System.Parameters (s-parame.adb),

   --  If the link includes a symbol _default_storage_size,
   --  use this as the storage size: otherwise, use 1024.
   Default_Storage_Size : constant Size_Type
   with
     Import,
     Convention => Ada,
     External_Name => "_default_storage_size";
   pragma Weak_External (Default_Storage_Size);

   function Default_Stack_Size return Size_Type is
     (if Default_Storage_Size'Address = System.Null_Address
      then 1024
      else Default_Storage_Size);

See here for how you can measure the free stack for your program's tasks.

Clone this wiki locally