Tom Lane e04a8059a7 Simplify declaring variables exported from libpgcommon and libpgport.
This reverts commits c2d1eea9e and 11b500072, as well as similar hacks
elsewhere, in favor of setting up the PGDLLIMPORT macro so that it can
just be used unconditionally.  That can work because in frontend code,
we need no marking in either the defining or consuming files for a
variable exported from these libraries; and frontend code has no need
to access variables exported from the core backend, either.

While at it, write some actual documentation about the PGDLLIMPORT
and PGDLLEXPORT macros.

Patch by me, based on a suggestion from Robert Haas.

Discussion: https://postgr.es/m/1160385.1638165449@sss.pgh.pa.us
2021-11-29 11:00:00 -05:00

61 lines
2.1 KiB
C

/*-------------------------------------------------------------------------
*
* Pseudo-Random Number Generator
*
* Copyright (c) 2021, PostgreSQL Global Development Group
*
* src/include/common/pg_prng.h
*
*-------------------------------------------------------------------------
*/
#ifndef PG_PRNG_H
#define PG_PRNG_H
/*
* State vector for PRNG generation. Callers should treat this as an
* opaque typedef, but we expose its definition to allow it to be
* embedded in other structs.
*/
typedef struct pg_prng_state
{
uint64 s0,
s1;
} pg_prng_state;
/*
* Callers not needing local PRNG series may use this global state vector,
* after initializing it with one of the pg_prng_...seed functions.
*/
extern PGDLLIMPORT pg_prng_state pg_global_prng_state;
extern void pg_prng_seed(pg_prng_state *state, uint64 seed);
extern void pg_prng_fseed(pg_prng_state *state, double fseed);
extern bool pg_prng_seed_check(pg_prng_state *state);
/*
* Initialize the PRNG state from the pg_strong_random source,
* taking care that we don't produce all-zeroes. If this returns false,
* caller should initialize the PRNG state from some other random seed,
* using pg_prng_[f]seed.
*
* We implement this as a macro, so that the pg_strong_random() call is
* in the caller. If it were in pg_prng.c, programs using pg_prng.c
* but not needing strong seeding would nonetheless be forced to pull in
* pg_strong_random.c and thence OpenSSL.
*/
#define pg_prng_strong_seed(state) \
(pg_strong_random((void *) (state), sizeof(pg_prng_state)) ? \
pg_prng_seed_check(state) : false)
extern uint64 pg_prng_uint64(pg_prng_state *state);
extern uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax);
extern int64 pg_prng_int64(pg_prng_state *state);
extern int64 pg_prng_int64p(pg_prng_state *state);
extern uint32 pg_prng_uint32(pg_prng_state *state);
extern int32 pg_prng_int32(pg_prng_state *state);
extern int32 pg_prng_int32p(pg_prng_state *state);
extern double pg_prng_double(pg_prng_state *state);
extern bool pg_prng_bool(pg_prng_state *state);
#endif /* PG_PRNG_H */