2 * Copyright (C) 1990-2003 by Digital Mars, www.digitalmars.com
4 * Written by Walter Bright
11 extern size_t _x386_break;
13 extern size_t _pastdata;
17 /*******************************************
18 * Allocate data from the caller's stack frame.
19 * This is a 'magic' function that needs help from the compiler to
20 * work right, do not change its name, do not call it from other compilers.
22 * nbytes number of bytes to allocate
23 * ECX address of variable with # of bytes in locals
24 * This is adjusted upon return to reflect the additional
25 * size of the stack frame.
27 * EAX allocated data, null if stack overflows
30 extern (C) void* __alloca(int nbytes)
36 mov EAX,4[ESP] ; // get nbytes
41 and EAX,0xFFFFFFFC ; // round up to dword
43 mov EAX,4 ; // allow zero bytes allocation, 0 rounded to dword is 4..
45 mov ESI,EAX ; // ESI = nbytes
47 add EAX,ESP ; // EAX is now what the new ESP will be.
54 // We need to be careful about the guard page
55 // Thus, for every 4k page, touch it to cause the OS to load it in.
56 mov ECX,EAX ; // ECX is new location for stack
57 mov EBX,ESI ; // EBX is size to "grow" stack
59 test [ECX+EBX],EBX ; // bring in page
60 sub EBX,0x1000 ; // next 4K page down
61 jae L1 ; // if more pages
62 test [ECX],EBX ; // bring in last page
79 jbe Aoverflow ; // Unlikely - ~2 Gbytes under UNIX
84 // Copy down to [ESP] the temps on the stack.
85 // The number of temps is (EBP - ESP - locals).
88 sub ECX,[EDX] ; // ECX = number of temps (bytes) to move.
89 add [EDX],ESI ; // adjust locals by nbytes for next call to alloca()
90 mov ESP,EAX ; // Set up new stack pointer.
91 add EAX,ECX ; // Return value = ESP + temps.
92 mov EDI,ESP ; // Destination of copy of temps.
93 add ESI,ESP ; // Source of copy.
94 shr ECX,2 ; // ECX to count of dwords in temps
95 // Always at least 4 (nbytes, EIP, ESI,and EDI).
101 // Overflowed the stack. Return null