Message-ID: <334CEC36.76D9@cs.com> Date: Thu, 10 Apr 1997 06:33:42 -0700 From: "John M. Aldrich" Reply-To: fighteer AT cs DOT com Organization: Two pounds of chaos and a pinch of salt MIME-Version: 1.0 To: Oberhumer Markus CC: djgpp-workers Subject: Re: stackavail() ? References: <199704102250 DOT SAA19142 AT delorie DOT com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Oberhumer Markus wrote: > > I'm searching for a fast and reliable implementation to determine the > available stack space, e.g. > > unsigned stackavail( void ); It's an extremely difficult thing to do in DPMI, because the DPMI spec does not support any direct method of detecting the available stack space. There was a thread about it several months ago on the newsgroup in which several suggestions were made by myself or others, but nothing definite was decided. If you do want to attempt to determine the available stack, you'll need to know the following things: - The size of the stack. This may be possible to find by examining the value of the global variable _stklen, but I don't know if this has a reliable value unless you set it yourself in a program. I also have no idea what happens if the stack size is modified externally in the stub (using stubedit). There may also be a value that the DPMI host stores that you can access via DPMI functions. - You need to record the virtual address of the top of the stack. To do this, you either have to do a detailed examination of the stack itself (the way a debugger would), or find a way to store the value of the stack pointer after it's been allocated but before anything is pushed onto it. I guess you could fudge this by examining the stack at the beginning of main(), where only a tiny amount has been used. - If you can determine those two things, then you can make a function which detects its own position on the stack and performs simple math to determine the remaining space. Gcc may want to make things difficult for you by optimizing the variables that you are using to test the stack. You may need to declare anything you use for this purpose 'volatile'. Grabbing the virtual address of the stack may also require hacking into the startup code in 'crt0.s'. What's more, there's no guarantee that any of these methods will work in a debugger. It's an uphill climb, but if you can manage it, a lot of people would probably be very grateful. ;) -- John M. Aldrich * Anything that happens, happens. * Anything that, in happening, causes something else to happen, causes something else to happen. * Anything that, in happening, causes itself to happen again, happens again. * It doesn't necessarily do it in chronological order, though. --- Douglas Adams