summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Goldblatt <davidgoldblatt@fb.com>2017-04-19 19:48:50 (GMT)
committerDavid Goldblatt <davidtgoldblatt@gmail.com>2017-04-24 17:33:21 (GMT)
commitfa3ad730c492c50f19fc68050ea5d5175b1df3cb (patch)
treebe56df044f81f9ead8f1a0f8791916cff5826946
parent4d2e4bf5ebb1e37a9348fdbf51af0b63304d7c98 (diff)
downloadjemalloc-fa3ad730c492c50f19fc68050ea5d5175b1df3cb.zip
jemalloc-fa3ad730c492c50f19fc68050ea5d5175b1df3cb.tar.gz
jemalloc-fa3ad730c492c50f19fc68050ea5d5175b1df3cb.tar.bz2
Header refactoring: prng module - remove from the catchall and unify.
-rw-r--r--include/jemalloc/internal/extent_inlines.h1
-rw-r--r--include/jemalloc/internal/jemalloc_internal_includes.h2
-rw-r--r--include/jemalloc/internal/prng.h (renamed from include/jemalloc/internal/prng_inlines.h)50
-rw-r--r--include/jemalloc/internal/prng_types.h29
-rw-r--r--include/jemalloc/internal/prof_structs.h1
-rw-r--r--src/ckh.c1
6 files changed, 49 insertions, 35 deletions
diff --git a/include/jemalloc/internal/extent_inlines.h b/include/jemalloc/internal/extent_inlines.h
index 22d45ce..a73b653 100644
--- a/include/jemalloc/internal/extent_inlines.h
+++ b/include/jemalloc/internal/extent_inlines.h
@@ -1,6 +1,7 @@
#ifndef JEMALLOC_INTERNAL_EXTENT_INLINES_H
#define JEMALLOC_INTERNAL_EXTENT_INLINES_H
+#include "jemalloc/internal/prng.h"
#include "jemalloc/internal/ql.h"
static inline arena_t *
diff --git a/include/jemalloc/internal/jemalloc_internal_includes.h b/include/jemalloc/internal/jemalloc_internal_includes.h
index 669194d..5e80f96 100644
--- a/include/jemalloc/internal/jemalloc_internal_includes.h
+++ b/include/jemalloc/internal/jemalloc_internal_includes.h
@@ -40,7 +40,6 @@
/* TYPES */
/******************************************************************************/
-#include "jemalloc/internal/prng_types.h"
#include "jemalloc/internal/ticker_types.h"
#include "jemalloc/internal/ckh_types.h"
#include "jemalloc/internal/size_classes.h"
@@ -108,7 +107,6 @@
/* INLINES */
/******************************************************************************/
-#include "jemalloc/internal/prng_inlines.h"
#include "jemalloc/internal/ticker_inlines.h"
#include "jemalloc/internal/tsd_inlines.h"
#include "jemalloc/internal/witness_inlines.h"
diff --git a/include/jemalloc/internal/prng_inlines.h b/include/jemalloc/internal/prng.h
index 0275dfc..15cc2d1 100644
--- a/include/jemalloc/internal/prng_inlines.h
+++ b/include/jemalloc/internal/prng.h
@@ -1,9 +1,37 @@
-#ifndef JEMALLOC_INTERNAL_PRNG_INLINES_H
-#define JEMALLOC_INTERNAL_PRNG_INLINES_H
+#ifndef JEMALLOC_INTERNAL_PRNG_H
+#define JEMALLOC_INTERNAL_PRNG_H
#include "jemalloc/internal/atomic.h"
#include "jemalloc/internal/bit_util.h"
+/*
+ * Simple linear congruential pseudo-random number generator:
+ *
+ * prng(y) = (a*x + c) % m
+ *
+ * where the following constants ensure maximal period:
+ *
+ * a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4.
+ * c == Odd number (relatively prime to 2^n).
+ * m == 2^32
+ *
+ * See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints.
+ *
+ * This choice of m has the disadvantage that the quality of the bits is
+ * proportional to bit position. For example, the lowest bit has a cycle of 2,
+ * the next has a cycle of 4, etc. For this reason, we prefer to use the upper
+ * bits.
+ */
+
+/******************************************************************************/
+/* INTERNAL DEFINITIONS -- IGNORE */
+/******************************************************************************/
+#define PRNG_A_32 UINT32_C(1103515241)
+#define PRNG_C_32 UINT32_C(12347)
+
+#define PRNG_A_64 UINT64_C(6364136223846793005)
+#define PRNG_C_64 UINT64_C(1442695040888963407)
+
JEMALLOC_ALWAYS_INLINE uint32_t
prng_state_next_u32(uint32_t state) {
return (state * PRNG_A_32) + PRNG_C_32;
@@ -25,6 +53,16 @@ prng_state_next_zu(size_t state) {
#endif
}
+/******************************************************************************/
+/* BEGIN PUBLIC API */
+/******************************************************************************/
+
+/*
+ * The prng_lg_range functions give a uniform int in the half-open range [0,
+ * 2**lg_range). If atomic is true, they do so safely from multiple threads.
+ * Multithreaded 64-bit prngs aren't supported.
+ */
+
JEMALLOC_ALWAYS_INLINE uint32_t
prng_lg_range_u32(atomic_u32_t *state, unsigned lg_range, bool atomic) {
uint32_t ret, state0, state1;
@@ -48,7 +86,6 @@ prng_lg_range_u32(atomic_u32_t *state, unsigned lg_range, bool atomic) {
return ret;
}
-/* 64-bit atomic operations cannot be supported on all relevant platforms. */
JEMALLOC_ALWAYS_INLINE uint64_t
prng_lg_range_u64(uint64_t *state, unsigned lg_range) {
uint64_t ret, state1;
@@ -86,6 +123,11 @@ prng_lg_range_zu(atomic_zu_t *state, unsigned lg_range, bool atomic) {
return ret;
}
+/*
+ * The prng_range functions behave like the prng_lg_range, but return a result
+ * in [0, range) instead of [0, 2**lg_range).
+ */
+
JEMALLOC_ALWAYS_INLINE uint32_t
prng_range_u32(atomic_u32_t *state, uint32_t range, bool atomic) {
uint32_t ret;
@@ -140,4 +182,4 @@ prng_range_zu(atomic_zu_t *state, size_t range, bool atomic) {
return ret;
}
-#endif /* JEMALLOC_INTERNAL_PRNG_INLINES_H */
+#endif /* JEMALLOC_INTERNAL_PRNG_H */
diff --git a/include/jemalloc/internal/prng_types.h b/include/jemalloc/internal/prng_types.h
deleted file mode 100644
index 3e8e183..0000000
--- a/include/jemalloc/internal/prng_types.h
+++ /dev/null
@@ -1,29 +0,0 @@
-#ifndef JEMALLOC_INTERNAL_PRNG_TYPES_H
-#define JEMALLOC_INTERNAL_PRNG_TYPES_H
-
-/*
- * Simple linear congruential pseudo-random number generator:
- *
- * prng(y) = (a*x + c) % m
- *
- * where the following constants ensure maximal period:
- *
- * a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4.
- * c == Odd number (relatively prime to 2^n).
- * m == 2^32
- *
- * See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints.
- *
- * This choice of m has the disadvantage that the quality of the bits is
- * proportional to bit position. For example, the lowest bit has a cycle of 2,
- * the next has a cycle of 4, etc. For this reason, we prefer to use the upper
- * bits.
- */
-
-#define PRNG_A_32 UINT32_C(1103515241)
-#define PRNG_C_32 UINT32_C(12347)
-
-#define PRNG_A_64 UINT64_C(6364136223846793005)
-#define PRNG_C_64 UINT64_C(1442695040888963407)
-
-#endif /* JEMALLOC_INTERNAL_PRNG_TYPES_H */
diff --git a/include/jemalloc/internal/prof_structs.h b/include/jemalloc/internal/prof_structs.h
index e193676..82080aa 100644
--- a/include/jemalloc/internal/prof_structs.h
+++ b/include/jemalloc/internal/prof_structs.h
@@ -1,6 +1,7 @@
#ifndef JEMALLOC_INTERNAL_PROF_STRUCTS_H
#define JEMALLOC_INTERNAL_PROF_STRUCTS_H
+#include "jemalloc/internal/prng.h"
#include "jemalloc/internal/rb.h"
struct prof_bt_s {
diff --git a/src/ckh.c b/src/ckh.c
index 6576740..db52a84 100644
--- a/src/ckh.c
+++ b/src/ckh.c
@@ -40,6 +40,7 @@
#include "jemalloc/internal/assert.h"
#include "jemalloc/internal/malloc_io.h"
+#include "jemalloc/internal/prng.h"
#include "jemalloc/internal/util.h"
/******************************************************************************/