From: Ben Pfaff <blp@gnu.org>
Date: Sun, 11 Dec 2005 07:35:50 +0000 (+0000)
Subject: Separate random numbers from other settings because of GSL dependency.
X-Git-Tag: sav-api~2134
X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=836682e6cff19cce534966b7bab4a933833f9693;p=pspp

Separate random numbers from other settings because of GSL dependency.
---

diff --git a/src/ChangeLog b/src/ChangeLog
index 1fc5fef1f7..a94b5ef53e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,22 @@
+Sat Dec 10 23:30:19 2005  Ben Pfaff  <blp@gnu.org>
+
+	Separate random numbers from other settings because of GSL
+	dependency.
+
+	* Makefile.am: Add random.c, random.h to sources.
+
+	* glob.c: (init_glob) Call random_init().
+	(done_glob) Call random_done().
+
+	* settings.c: (static var rng) Move to random.c.
+	(done_settings) Move freeing of RNG to random_done().
+	(get_rng) Move to random.c
+	(set_rng) Ditto.
+
+	* random.c: New file.
+
+	* random.h: New file.
+
 Sat Dec 10 18:13:36 2005  Ben Pfaff  <blp@gnu.org>
 
 	Separate settings and the SET command, for modularity.
diff --git a/src/Makefile.am b/src/Makefile.am
index 857dd8a59b..5289af2b1f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -187,6 +187,8 @@ pspp_SOURCES =					\
 	pool.h					\
 	postscript.c				\
 	print.c					\
+	random.c				\
+	random.h				\
 	range-prs.c				\
 	range-prs.h				\
 	recode.c				\
diff --git a/src/expressions/helpers.h b/src/expressions/helpers.h
index 4011d843de..a3fbdb02f7 100644
--- a/src/expressions/helpers.h
+++ b/src/expressions/helpers.h
@@ -17,6 +17,7 @@
 #include "gsl-extras/gsl-extras.h"
 #include "misc.h"
 #include "moments.h"
+#include "random.h"
 #include "settings.h"
 #include "str.h"
 #include "val.h"
diff --git a/src/glob.c b/src/glob.c
index 47db10ea78..38dd323738 100644
--- a/src/glob.c
+++ b/src/glob.c
@@ -71,6 +71,7 @@ extern void stifle_history ();
 #include "lexer.h"
 #include "magic.h"
 #include "main.h"
+#include "random.h"
 #include "settings.h"
 #include "str.h"
 #include "var.h"
@@ -160,7 +161,8 @@ init_glob (int argc UNUSED, char **argv)
   }
 
 
-  init_settings();
+  init_settings ();
+  random_init ();
 
   /* log.h */
   logging = 1;
@@ -176,6 +178,7 @@ done_glob(void)
   cancel_transformations ();
   dict_destroy (default_dict);
   free (logfn);
+  random_done ();
   done_settings ();
   ds_destroy (&tokstr);
 
diff --git a/src/random.c b/src/random.c
new file mode 100644
index 0000000000..7420a82ece
--- /dev/null
+++ b/src/random.c
@@ -0,0 +1,57 @@
+/* PSPP - computes sample statistics.
+   Copyright (C) 1997-9, 2000, 2005 Free Software Foundation, Inc.
+   Written by Ben Pfaff <blp@gnu.org>.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
+
+#include <config.h>
+#include "random.h"
+#include <time.h>
+#include "xalloc.h"
+
+static gsl_rng *rng;
+
+void
+random_init (void) 
+{
+}
+
+void
+random_done (void) 
+{
+  if (rng != NULL) 
+    gsl_rng_free (rng);
+}
+
+/* Returns the current random number generator. */
+gsl_rng *
+get_rng (void)
+{
+  if (rng == NULL)
+    set_rng (time (0));
+  return rng;
+}
+
+/* Initializes or reinitializes the random number generator with
+   the given SEED. */
+void
+set_rng (unsigned long seed) 
+{
+  rng = gsl_rng_alloc (gsl_rng_mt19937);
+  if (rng == NULL)
+    xalloc_die ();
+  gsl_rng_set (rng, seed);
+}
diff --git a/src/random.h b/src/random.h
new file mode 100644
index 0000000000..85959677a8
--- /dev/null
+++ b/src/random.h
@@ -0,0 +1,31 @@
+/* PSPP - computes sample statistics.
+   Copyright (C) 1997-9, 2000, 2005 Free Software Foundation, Inc.
+   Written by Ben Pfaff <blp@gnu.org>.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
+
+#ifndef RANDOM_H
+#define RANDOM_H 1
+
+#include <gsl/gsl_rng.h>
+
+void random_init (void);
+void random_done (void);
+
+gsl_rng *get_rng (void);
+void set_rng (unsigned long seed);
+
+#endif /* random.h */
diff --git a/src/sample.c b/src/sample.c
index fceadbbbf3..0255ae79ee 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -26,7 +26,7 @@
 #include "command.h"
 #include "error.h"
 #include "lexer.h"
-#include "settings.h"
+#include "random.h"
 #include "str.h"
 #include "var.h"
 
diff --git a/src/set.q b/src/set.q
index 9e045fb7ce..8fb3a80768 100644
--- a/src/set.q
+++ b/src/set.q
@@ -32,6 +32,7 @@
 #include "magic.h"
 #include "log.h"
 #include "output.h"
+#include "random.h"
 #include "var.h"
 #include "format.h"
 #include "copyleft.h"
diff --git a/src/settings.c b/src/settings.c
index 749757bc6f..4961b564e8 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -20,6 +20,7 @@
 #include <config.h>
 #include "settings.h"
 #include <assert.h>
+#include <stdlib.h>
 #include <time.h>
 #include "format.h"
 #include "val.h"
@@ -77,8 +78,6 @@ static struct custom_currency cc[CC_CNT] =
     CC_INITIALIZER,
   };
 
-static gsl_rng *rng;
-
 static bool testing_mode = false;
 
 static int global_algorithm = ENHANCED;
@@ -92,9 +91,6 @@ static void init_viewport (void);
 void
 done_settings (void)
 {
-  if (rng != NULL) 
-    gsl_rng_free (rng);
-
   free (prompt);
   free (cprompt);
   free (dprompt);
@@ -494,7 +490,7 @@ set_endcmd (char endcmd_)
 /* Approximate maximum amount of memory to use for cases, in
    bytes. */
 size_t
-get_workspace(void)
+get_workspace (void)
 {
   return workspace;
 }
@@ -540,26 +536,6 @@ set_cc (int idx, const struct custom_currency *cc_)
   cc[idx] = *cc_;
 }
 
-/* Returns the current random number generator. */
-gsl_rng *
-get_rng (void)
-{
-  if (rng == NULL)
-    set_rng (time (0));
-  return rng;
-}
-
-/* Initializes or reinitializes the random number generator with
-   the given SEED. */
-void
-set_rng (unsigned long seed) 
-{
-  rng = gsl_rng_alloc (gsl_rng_mt19937);
-  if (rng == NULL)
-    xalloc_die ();
-  gsl_rng_set (rng, seed);
-}
-
 /* Are we in testing mode?  (e.g. --testing-mode command line
    option) */
 bool
diff --git a/src/settings.h b/src/settings.h
index 9e6209250d..0593f2d10c 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -116,11 +116,6 @@ struct custom_currency
 const struct custom_currency *get_cc (int idx);
 void set_cc (int idx, const struct custom_currency *);
 
-#include <gsl/gsl_rng.h>
-
-gsl_rng *get_rng (void);
-void set_rng (unsigned long seed);
-
 bool get_testing_mode (void);
 void set_testing_mode (bool);