stackava.gml

来自「开放源码的编译器open watcom 1.6.0版的源代码」· GML 代码 · 共 57 行

GML
57
字号
.func stackavail
#include <malloc.h>
size_t stackavail(void);
.ixfunc2 '&Memory' &func
.funcend
.desc begin
The &func function returns the number of bytes currently available
in the stack.
This value is usually used to determine an appropriate amount to
allocate using alloca.
.desc end
.return begin
The &func function returns the number of bytes currently available
in the stack.
.return end
.see begin
.seelist stackavail alloca calloc Functions malloc Functions
.see end
.exmp begin
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <fcntl.h>
#include <&iohdr>

long char_count( FILE *fp )
  {
     char *buffer;
     size_t bufsiz;
     long count;

     /* allocate half of stack for temp buffer */
     bufsiz = stackavail() >> 1;
     buffer = (char *) alloca( bufsiz );
     setvbuf( fp, buffer, _IOFBF, bufsiz );
     count = 0L;
     while( fgetc( fp ) != EOF ) ++count;
     fclose( fp );
     return( count );
  }
.exmp break
void main()
  {
    FILE *fp;

    fp = fopen( "file", "rb" );
    if( fp != NULL ) {
      setmode( fileno( fp ), O_BINARY );
      printf( "File contains %lu characters\n",
          char_count( fp ) );
      fclose( fp );
    }
  }
.exmp end
.class WATCOM
.system

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?