[pypy-commit] pypy remove-PYPY_NOT_MAIN_FILE: Split stack.h: header and implementation.

amauryfa noreply at buildbot.pypy.org
Tue Oct 2 16:37:52 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: remove-PYPY_NOT_MAIN_FILE
Changeset: r57729:6519032ac08f
Date: 2012-10-02 15:34 +0200
http://bitbucket.org/pypy/pypy/changeset/6519032ac08f/

Log:	Split stack.h: header and implementation.

diff --git a/pypy/translator/c/genc.py b/pypy/translator/c/genc.py
--- a/pypy/translator/c/genc.py
+++ b/pypy/translator/c/genc.py
@@ -914,6 +914,7 @@
         srcdir / 'profiling.c',
         srcdir / 'debug_print.c',
         srcdir / 'debug_traceback.c',
+        srcdir / 'stack.c',
         srcdir / 'thread.c',
         srcdir / 'asm.c',
         srcdir / 'instrument.c',
diff --git a/pypy/translator/c/src/stack.c b/pypy/translator/c/src/stack.c
new file mode 100644
--- /dev/null
+++ b/pypy/translator/c/src/stack.c
@@ -0,0 +1,69 @@
+/* Stack operation */
+#include <src/stack.h>
+#include <src/thread.h>
+#include <stdio.h>
+
+
+/* the current stack is in the interval [end-length:end].  We assume a
+   stack that grows downward here. */
+char *_LLstacktoobig_stack_end = NULL;
+long _LLstacktoobig_stack_length = MAX_STACK_SIZE;
+char _LLstacktoobig_report_error = 1;
+static RPyThreadStaticTLS end_tls_key;
+
+void LL_stack_set_length_fraction(double fraction)
+{
+	_LLstacktoobig_stack_length = (long)(MAX_STACK_SIZE * fraction);
+}
+
+char LL_stack_too_big_slowpath(long current)
+{
+	long diff, max_stack_size;
+	char *baseptr, *curptr = (char*)current;
+
+	/* The stack_end variable is updated to match the current value
+	   if it is still 0 or if we later find a 'curptr' position
+	   that is above it.  The real stack_end pointer is stored in
+	   thread-local storage, but we try to minimize its overhead by
+	   keeping a local copy in _LLstacktoobig_stack_end. */
+
+	if (_LLstacktoobig_stack_end == NULL) {
+		/* not initialized */
+		/* XXX We assume that initialization is performed early,
+		   when there is still only one thread running.  This
+		   allows us to ignore race conditions here */
+		char *errmsg = RPyThreadStaticTLS_Create(&end_tls_key);
+		if (errmsg) {
+			/* XXX should we exit the process? */
+			fprintf(stderr, "Internal PyPy error: %s\n", errmsg);
+			return 1;
+		}
+	}
+
+	baseptr = (char *) RPyThreadStaticTLS_Get(end_tls_key);
+	max_stack_size = _LLstacktoobig_stack_length;
+	if (baseptr == NULL) {
+		/* first time we see this thread */
+	}
+	else {
+		diff = baseptr - curptr;
+		if (((unsigned long)diff) <= (unsigned long)max_stack_size) {
+			/* within bounds, probably just had a thread switch */
+			_LLstacktoobig_stack_end = baseptr;
+			return 0;
+		}
+		if (((unsigned long)-diff) <= (unsigned long)max_stack_size) {
+			/* stack underflowed: the initial estimation of
+			   the stack base must be revised */
+		}
+		else {	/* stack overflow (probably) */
+			return _LLstacktoobig_report_error;
+		}
+	}
+
+	/* update the stack base pointer to the current value */
+	baseptr = curptr;
+	RPyThreadStaticTLS_Set(end_tls_key, baseptr);
+	_LLstacktoobig_stack_end = baseptr;
+	return 0;
+}
diff --git a/pypy/translator/c/src/stack.h b/pypy/translator/c/src/stack.h
--- a/pypy/translator/c/src/stack.h
+++ b/pypy/translator/c/src/stack.h
@@ -35,71 +35,3 @@
 #endif
 
 
-#ifdef PYPY_MAIN_IMPLEMENTATION_FILE
-#include <stdio.h>
-
-/* the current stack is in the interval [end-length:end].  We assume a
-   stack that grows downward here. */
-char *_LLstacktoobig_stack_end = NULL;
-long _LLstacktoobig_stack_length = MAX_STACK_SIZE;
-char _LLstacktoobig_report_error = 1;
-static RPyThreadStaticTLS end_tls_key;
-
-void LL_stack_set_length_fraction(double fraction)
-{
-	_LLstacktoobig_stack_length = (long)(MAX_STACK_SIZE * fraction);
-}
-
-char LL_stack_too_big_slowpath(long current)
-{
-	long diff, max_stack_size;
-	char *baseptr, *curptr = (char*)current;
-
-	/* The stack_end variable is updated to match the current value
-	   if it is still 0 or if we later find a 'curptr' position
-	   that is above it.  The real stack_end pointer is stored in
-	   thread-local storage, but we try to minimize its overhead by
-	   keeping a local copy in _LLstacktoobig_stack_end. */
-
-	if (_LLstacktoobig_stack_end == NULL) {
-		/* not initialized */
-		/* XXX We assume that initialization is performed early,
-		   when there is still only one thread running.  This
-		   allows us to ignore race conditions here */
-		char *errmsg = RPyThreadStaticTLS_Create(&end_tls_key);
-		if (errmsg) {
-			/* XXX should we exit the process? */
-			fprintf(stderr, "Internal PyPy error: %s\n", errmsg);
-			return 1;
-		}
-	}
-
-	baseptr = (char *) RPyThreadStaticTLS_Get(end_tls_key);
-	max_stack_size = _LLstacktoobig_stack_length;
-	if (baseptr == NULL) {
-		/* first time we see this thread */
-	}
-	else {
-		diff = baseptr - curptr;
-		if (((unsigned long)diff) <= (unsigned long)max_stack_size) {
-			/* within bounds, probably just had a thread switch */
-			_LLstacktoobig_stack_end = baseptr;
-			return 0;
-		}
-		if (((unsigned long)-diff) <= (unsigned long)max_stack_size) {
-			/* stack underflowed: the initial estimation of
-			   the stack base must be revised */
-		}
-		else {	/* stack overflow (probably) */
-			return _LLstacktoobig_report_error;
-		}
-	}
-
-	/* update the stack base pointer to the current value */
-	baseptr = curptr;
-	RPyThreadStaticTLS_Set(end_tls_key, baseptr);
-	_LLstacktoobig_stack_end = baseptr;
-	return 0;
-}
-
-#endif
diff --git a/pypy/translator/c/src/thread.h b/pypy/translator/c/src/thread.h
--- a/pypy/translator/c/src/thread.h
+++ b/pypy/translator/c/src/thread.h
@@ -34,6 +34,7 @@
 #define RPyThreadStaticTLS_Create(key) RPyThreadTLS_Create(key)
 #define RPyThreadStaticTLS_Get(key)    RPyThreadTLS_Get(key)
 #define RPyThreadStaticTLS_Set(key, value) RPyThreadTLS_Set(key, value)
+char *RPyThreadTLS_Create(RPyThreadTLS *result);
 
 #endif
 


More information about the pypy-commit mailing list