From a9418c876a11bc7cf1e30b2157057526f6108d10 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Sun, 18 Mar 2007 14:12:20 +0000
Subject: [PATCH] add stack check for unix (same as the one we use in getdp)

---
 Box/Box.cpp   |  5 ++++-
 Box/Makefile  |  6 +++---
 Common/OS.cpp | 19 ++++++++++++++++++-
 Common/OS.h   |  1 +
 Fltk/Main.cpp |  5 ++++-
 Fltk/Makefile | 46 +++++++++++++++++++++++-----------------------
 6 files changed, 53 insertions(+), 29 deletions(-)

diff --git a/Box/Box.cpp b/Box/Box.cpp
index 0ae21e7bcd..2ef2359d7b 100644
--- a/Box/Box.cpp
+++ b/Box/Box.cpp
@@ -1,4 +1,4 @@
-// $Id: Box.cpp,v 1.33 2006-12-16 15:44:27 geuzaine Exp $
+// $Id: Box.cpp,v 1.34 2007-03-18 14:12:20 geuzaine Exp $
 //
 // Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 //
@@ -21,6 +21,7 @@
 
 #include <signal.h>
 #include "Gmsh.h"
+#include "OS.h"
 #include "Numeric.h"
 #include "Geo.h"
 #include "Generator.h"
@@ -95,6 +96,8 @@ int GMSHBOX(int argc, char *argv[])
   signal(SIGFPE, Signal);
 #endif
 
+  CheckResources();
+
   GMSH_PluginManager::instance()->registerDefaultPlugins();
 
   check_gsl();
diff --git a/Box/Makefile b/Box/Makefile
index b97af7b864..b8f25d8477 100644
--- a/Box/Makefile
+++ b/Box/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.34 2007-02-26 16:24:23 geuzaine Exp $
+# $Id: Makefile,v 1.35 2007-03-18 14:12:20 geuzaine Exp $
 #
 # Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 #
@@ -53,8 +53,8 @@ depend:
 # DO NOT DELETE THIS LINE
 Box.o: Box.cpp ../Common/Gmsh.h ../Common/Message.h ../DataStr/Malloc.h \
   ../DataStr/List.h ../DataStr/Tree.h ../DataStr/avl.h ../DataStr/Tools.h \
-  ../DataStr/List.h ../DataStr/Tree.h ../Numeric/Numeric.h ../Geo/Geo.h \
-  ../Common/GmshDefines.h ../Geo/gmshSurface.h ../Geo/Pair.h \
+  ../DataStr/List.h ../DataStr/Tree.h ../Common/OS.h ../Numeric/Numeric.h \
+  ../Geo/Geo.h ../Common/GmshDefines.h ../Geo/gmshSurface.h ../Geo/Pair.h \
   ../Geo/Range.h ../Geo/SPoint2.h ../Geo/SPoint3.h ../Geo/SVector3.h \
   ../Geo/SPoint3.h ../Geo/SBoundingBox3d.h ../Geo/SPoint3.h \
   ../Geo/SPoint2.h ../Geo/ExtrudeParams.h ../Common/SmoothData.h \
diff --git a/Common/OS.cpp b/Common/OS.cpp
index 4660bbe064..ef8fcd1818 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -1,4 +1,4 @@
-// $Id: OS.cpp,v 1.5 2006-11-27 22:22:07 geuzaine Exp $
+// $Id: OS.cpp,v 1.6 2007-03-18 14:12:20 geuzaine Exp $
 //
 // Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 //
@@ -91,6 +91,23 @@ void GetResources(double *s, long *mem)
 #endif
 }
 
+void CheckResources(){
+#if !defined (WIN32) || defined(__CYGWIN__)
+  static struct rlimit r;
+
+  getrlimit(RLIMIT_STACK, &r);
+
+  // Try to get at least 1 MB of stack. Running with too small a stack
+  // can cause crashes in the recursive calls (e.g. for tet
+  // classification in 3D Delaunay)
+  if(r.rlim_cur < 1024 * 1024){
+    Msg(INFO, "Increasing process stack size (%d kB < 1 MB)", r.rlim_cur / 1024);
+    r.rlim_cur = r.rlim_max;
+    setrlimit(RLIMIT_STACK, &r);
+  }
+#endif
+}
+
 double Cpu()
 {
   long mem = 0;
diff --git a/Common/OS.h b/Common/OS.h
index 00e194f09e..57232f7328 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -23,6 +23,7 @@
 double GetTimeInSeconds();
 void SleepInSeconds(double s);
 void GetResources(double *s, long *mem);
+void CheckResources();
 double Cpu();
 int GetProcessId();
 int UnlinkFile(char *name);
diff --git a/Fltk/Main.cpp b/Fltk/Main.cpp
index 106199bf4a..31ad694461 100644
--- a/Fltk/Main.cpp
+++ b/Fltk/Main.cpp
@@ -1,4 +1,4 @@
-// $Id: Main.cpp,v 1.104 2007-02-04 11:24:55 geuzaine Exp $
+// $Id: Main.cpp,v 1.105 2007-03-18 14:12:20 geuzaine Exp $
 //
 // Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 //
@@ -25,6 +25,7 @@
 #include "GUI.h"
 #include "Gmsh.h"
 #include "GmshUI.h"
+#include "OS.h"
 #include "Geo.h"
 #include "Generator.h"
 #include "CreateFile.h"
@@ -105,6 +106,8 @@ int main(int argc, char *argv[])
   signal(SIGFPE, Signal);
 #endif
 
+  CheckResources();
+  
   // Initialize the default plugins
 
   GMSH_PluginManager::instance()->registerDefaultPlugins();
diff --git a/Fltk/Makefile b/Fltk/Makefile
index c798c81961..74ff3aca3f 100644
--- a/Fltk/Makefile
+++ b/Fltk/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.126 2007-02-28 06:58:46 geuzaine Exp $
+# $Id: Makefile,v 1.127 2007-03-18 14:12:20 geuzaine Exp $
 #
 # Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 #
@@ -68,28 +68,28 @@ Main.o: Main.cpp GUI.h Opengl_Window.h Colorbar_Window.h \
   SpherePosition_Widget.h ../Common/Gmsh.h ../Common/Message.h \
   ../DataStr/Malloc.h ../DataStr/List.h ../DataStr/Tree.h \
   ../DataStr/avl.h ../DataStr/Tools.h ../DataStr/List.h ../DataStr/Tree.h \
-  ../Geo/Geo.h ../Common/GmshDefines.h ../Geo/gmshSurface.h ../Geo/Pair.h \
-  ../Geo/Range.h ../Geo/SPoint2.h ../Geo/SPoint3.h ../Geo/SVector3.h \
-  ../Geo/SPoint3.h ../Geo/SBoundingBox3d.h ../Geo/SPoint3.h \
-  ../Geo/SPoint2.h ../Geo/ExtrudeParams.h ../Common/SmoothData.h \
-  ../Numeric/Numeric.h ../Mesh/Generator.h ../Parser/CreateFile.h \
-  ../Graphics/Draw.h ../Common/Views.h ../Common/ColorTable.h \
-  ../Common/VertexArray.h ../Common/SmoothData.h \
-  ../Common/AdaptiveViews.h ../Common/GmshMatrix.h ../Common/Context.h \
-  ../Common/Options.h ../Parser/Parser.h ../Parser/OpenFile.h \
-  ../Common/CommandLine.h Solvers.h ../Plugin/PluginManager.h \
-  ../Plugin/Plugin.h ../Geo/GModel.h ../Geo/GVertex.h ../Geo/GEntity.h \
-  ../Geo/Range.h ../Geo/SPoint3.h ../Geo/SBoundingBox3d.h \
-  ../Geo/MVertex.h ../Geo/SPoint3.h ../Geo/GPoint.h ../Geo/SPoint2.h \
-  ../Geo/GEdge.h ../Geo/GEntity.h ../Geo/GVertex.h ../Geo/SVector3.h \
-  ../Geo/SPoint3.h ../Geo/SPoint2.h ../Geo/MElement.h ../Geo/MVertex.h \
-  ../Geo/MEdge.h ../Geo/MVertex.h ../Geo/SVector3.h ../Common/Hash.h \
-  ../Geo/MFace.h ../Geo/MVertex.h ../Geo/SVector3.h \
-  ../Geo/ExtrudeParams.h ../Geo/GFace.h ../Geo/GPoint.h ../Geo/GEntity.h \
-  ../Geo/GEdgeLoop.h ../Geo/GEdge.h ../Geo/MElement.h ../Geo/SPoint2.h \
-  ../Geo/SVector3.h ../Geo/Pair.h ../Geo/ExtrudeParams.h ../Geo/GRegion.h \
-  ../Geo/GEntity.h ../Geo/MElement.h ../Geo/ExtrudeParams.h \
-  ../Geo/SBoundingBox3d.h
+  ../Common/OS.h ../Geo/Geo.h ../Common/GmshDefines.h \
+  ../Geo/gmshSurface.h ../Geo/Pair.h ../Geo/Range.h ../Geo/SPoint2.h \
+  ../Geo/SPoint3.h ../Geo/SVector3.h ../Geo/SPoint3.h \
+  ../Geo/SBoundingBox3d.h ../Geo/SPoint3.h ../Geo/SPoint2.h \
+  ../Geo/ExtrudeParams.h ../Common/SmoothData.h ../Numeric/Numeric.h \
+  ../Mesh/Generator.h ../Parser/CreateFile.h ../Graphics/Draw.h \
+  ../Common/Views.h ../Common/ColorTable.h ../Common/VertexArray.h \
+  ../Common/SmoothData.h ../Common/AdaptiveViews.h ../Common/GmshMatrix.h \
+  ../Common/Context.h ../Common/Options.h ../Parser/Parser.h \
+  ../Parser/OpenFile.h ../Common/CommandLine.h Solvers.h \
+  ../Plugin/PluginManager.h ../Plugin/Plugin.h ../Geo/GModel.h \
+  ../Geo/GVertex.h ../Geo/GEntity.h ../Geo/Range.h ../Geo/SPoint3.h \
+  ../Geo/SBoundingBox3d.h ../Geo/MVertex.h ../Geo/SPoint3.h \
+  ../Geo/GPoint.h ../Geo/SPoint2.h ../Geo/GEdge.h ../Geo/GEntity.h \
+  ../Geo/GVertex.h ../Geo/SVector3.h ../Geo/SPoint3.h ../Geo/SPoint2.h \
+  ../Geo/MElement.h ../Geo/MVertex.h ../Geo/MEdge.h ../Geo/MVertex.h \
+  ../Geo/SVector3.h ../Common/Hash.h ../Geo/MFace.h ../Geo/MVertex.h \
+  ../Geo/SVector3.h ../Geo/ExtrudeParams.h ../Geo/GFace.h ../Geo/GPoint.h \
+  ../Geo/GEntity.h ../Geo/GEdgeLoop.h ../Geo/GEdge.h ../Geo/MElement.h \
+  ../Geo/SPoint2.h ../Geo/SVector3.h ../Geo/Pair.h ../Geo/ExtrudeParams.h \
+  ../Geo/GRegion.h ../Geo/GEntity.h ../Geo/MElement.h \
+  ../Geo/ExtrudeParams.h ../Geo/SBoundingBox3d.h
 Message.o: Message.cpp ../Common/Gmsh.h ../Common/Message.h \
   ../DataStr/Malloc.h ../DataStr/List.h ../DataStr/Tree.h \
   ../DataStr/avl.h ../DataStr/Tools.h ../DataStr/List.h ../DataStr/Tree.h \
-- 
GitLab