Home | History | Annotate | Download | only in src
      1 /*---------------------------------------------------------------------------*
      2  *  PStackSize.c  *
      3  *                                                                           *
      4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
      5  *                                                                           *
      6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
      7  *  you may not use this file except in compliance with the License.         *
      8  *                                                                           *
      9  *  You may obtain a copy of the License at                                  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
     11  *                                                                           *
     12  *  Unless required by applicable law or agreed to in writing, software      *
     13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     15  *  See the License for the specific language governing permissions and      *
     16  *  limitations under the License.                                           *
     17  *                                                                           *
     18  *---------------------------------------------------------------------------*/
     19 
     20 #include "pstdio.h"
     21 #include "PStackSize.h"
     22 
     23 #ifdef _WIN32
     24 
     25 static const char * PSTACK_BASE = NULL;
     26 
     27 /** Initialize the stack base.  This should be done in the main() function.
     28  * Note that the local variables of main() are not taken into account in the
     29  * stack size computation.  To overcome this problem, rewrite main() as a
     30  * simple function that first invokes PSTACK_SIZE_INIT and then invokes the
     31  * original main().
     32 */
     33 void PSTACK_SIZE_INIT()
     34 {
     35   PSTACK_BASE = (const char *) _alloca(0);
     36 }
     37 
     38 /**
     39  * Computes the current stack size.  The returned value is the number of bytes
     40  * in the stack.
     41  **/
     42 size_t PSTACK_SIZE_GET()
     43 {
     44   return (PSTACK_BASE - ((const char *) _alloca(0)));
     45 }
     46 
     47 #else
     48 
     49 /* Insert other platform implementations here... */
     50 /*
     51 #error not supported at this time
     52 */
     53 void PSTACK_SIZE_INIT()
     54 {}
     55 
     56 size_t PSTACK_SIZE_GET()
     57 {
     58   return 0;
     59 }
     60 
     61 #endif
     62