1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-02 15:40:38 +02:00

Allocate a static tc16 for random states

* libguile/random.h (scm_t_rstate): Put a tag word in the beginning.
(scm_is_random_state, scm_to_random_state, scm_from_random_state): New
helpers.
(SCM_RSTATEP, SCM_RSTATE): Use the new helpers.
(scm_masktab): Make private.
* libguile/random.c: Adapt random states to not be a smob.
* libguile/eq.c:
* libguile/print.c:
* libguile/scm.h:
* module/oop/goops.scm: Add new random-state cases.  Fix a number of
classes for other types that were recently changed to not be smobs.
This commit is contained in:
Andy Wingo 2025-06-17 10:36:32 +02:00
parent 63317ff480
commit ccaff3da39
7 changed files with 57 additions and 45 deletions

View file

@ -1,7 +1,7 @@
#ifndef SCM_RANDOM_H
#define SCM_RANDOM_H
/* Copyright 1999-2001,2006,2008,2010,2018
/* Copyright 1999-2001,2006,2008,2010,2018,2025
Free Software Foundation, Inc.
This file is part of Guile.
@ -38,6 +38,7 @@
*/
typedef struct scm_t_rstate {
scm_t_bits tag;
struct scm_t_rng *rng;
double normal_next; /* For scm_c_normal01 */
/* Custom fields follow here */
@ -73,15 +74,32 @@ SCM_API SCM scm_c_random_bignum (scm_t_rstate *, SCM m);
/*
* Scheme level interface
*/
SCM_API scm_t_bits scm_tc16_rstate;
#define SCM_RSTATEP(obj) SCM_SMOB_PREDICATE (scm_tc16_rstate, obj)
#define SCM_RSTATE(obj) ((scm_t_rstate *) SCM_SMOB_DATA (obj))
static inline int
scm_is_random_state (SCM x)
{
return SCM_HAS_TYP16 (x, scm_tc16_random_state);
}
static inline struct scm_t_rstate *
scm_to_random_state (SCM x)
{
if (!scm_is_random_state (x))
abort ();
return (struct scm_t_rstate *) SCM_UNPACK_POINTER (x);
}
static inline SCM
scm_from_random_state (struct scm_t_rstate *x)
{
return SCM_PACK_POINTER (x);
}
#define SCM_RSTATEP(obj) scm_is_random_state (obj)
#define SCM_RSTATE(obj) scm_to_random_state (obj)
#define SCM_VALIDATE_RSTATE(pos, v) \
SCM_MAKE_VALIDATE_MSG (pos, v, RSTATEP, "random-generator-state")
SCM_API unsigned char scm_masktab[256];
SCM_API SCM scm_var_random_state;
SCM_API SCM scm_random (SCM n, SCM state);
SCM_API SCM scm_copy_random_state (SCM state);