1  // Copyright 2018 The Go Authors. All rights reserved.
       2  // Use of this source code is governed by a BSD-style
       3  // license that can be found in the LICENSE file.
       4  
       5  // This test source is used by both TestBigStackCallbackCgo (linked
       6  // directly into the Go binary) and TestBigStackCallbackSyscall
       7  // (compiled into a DLL).
       8  
       9  #include <windows.h>
      10  #include <stdio.h>
      11  
      12  #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
      13  #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
      14  #endif
      15  
      16  typedef void callback(char*);
      17  
      18  // Allocate a stack that's much larger than the default.
      19  static const int STACK_SIZE = 16<<20;
      20  
      21  static callback *bigStackCallback;
      22  
      23  static void useStack(int bytes) {
      24  	// Windows doesn't like huge frames, so we grow the stack 64k at a time.
      25  	char x[64<<10];
      26  	if (bytes < sizeof x) {
      27  		bigStackCallback(x);
      28  	} else {
      29  		useStack(bytes - sizeof x);
      30  	}
      31  }
      32  
      33  static DWORD WINAPI threadEntry(LPVOID lpParam) {
      34  	useStack(STACK_SIZE - (128<<10));
      35  	return 0;
      36  }
      37  
      38  void bigStack(callback *cb) {
      39  	bigStackCallback = cb;
      40  	HANDLE hThread = CreateThread(NULL, STACK_SIZE, threadEntry, NULL, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL);
      41  	if (hThread == NULL) {
      42  		fprintf(stderr, "CreateThread failed\n");
      43  		exit(1);
      44  	}
      45  	WaitForSingleObject(hThread, INFINITE);
      46  }