Partager via


Stack (Windows Embedded CE 6.0)

1/6/2010

The stack is the storage area for variables referenced in a function. Windows Embedded CE manages a separate stack for every thread in the system.

The architecture of the stack depends on the microprocessor, but typically, the default stack limit is 64 KB, with 8 KB, or two pages, reserved for overflow error control. You can override the stack limit at compile time with the linker option /STACK.

Note

Exceeding the stack limit causes a system access violation that shuts down your application. In addition, stack frames can be no larger than one page each, or structured exception handling fails.

  • When the system creates a new thread, the kernel reserves memory for the stack. The amount of memory that the system reserves for the stack is determined by the /STACK parameter passed to the linker when the module is built. The stack size can be set for newly created threads with the CreateThread function by setting cbStack to the appropriate size and fdwCreate to STACK_SIZE_PARAM_IS_A_RESERVATION.

When the system schedules a thread for the first time, the thread's stack memory is committed one page at a time when needed. To prevent the initial commitment of stack memory from affecting performance, ensure that your thread is scheduled at least once before performing real-time processing. In the case of an interrup service thread (IST), allocation of stack memory generally occurs because the thread performs several actions before settling down into a WaitForSingleObject loop. You can avoid allocating more stack memory if your thread requires no more than the initial stack allocation. Because memory is typically limited in a Windows Embedded CE environment, at times the kernel takes back stack memory from threads that are no longer needed.

Reclaiming unused stack memory is controlled by the OEM and the parameters passed to the SetOOMEvent function. If the values passed to SetOOMEvent** cause the system to search for stack memory, the kernel suspends each thread that has a free stack while memory from the stack is removed. To avoid this situation, carefully set the memory usage parameters for SetOOMEvent.

You can set an OEM adaptation level (OAL) variable that tells the kernel at what priority to recover stack space when memory is low.

Make the following declaration in your OAL:

extern DWORD dwNKMaxPrioNoScav;

The value of dwNKMaxPrioNoScav defines the highest priority for a thread that should not incur stack space recovery. Any thread with a priority lower than dwNKMaxPrioNoScav can have its stack space recovered by the kernel. The default value for dwNKMaxPrioNoScav is 247. Use this variable in combination with the SetOOMEvent function to control when stack recovery is run. If stack recovery is run, dwNKMaxPrioNoScav, in combination with SetOOMEvent, determines which priority stack recovery should be applied.**

See Also

Concepts

Memory Architecture