star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit be26d47952eac27e367b304713a491a2fa7aa693
parent 5e7b47c71078d5fba901d7bf3e57e0a6914f66a2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sun, 30 Mar 2025 16:12:47 +0200

Fix problems relating to a null state cache capacity

This capacity disables the use of the state cache. However, this
internal functionality was buggy since internal memory was used to
manage cached states instead of temporary files.

Diffstat:
Msrc/ssp_rng_proxy.c | 22++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/ssp_rng_proxy.c b/src/ssp_rng_proxy.c @@ -200,10 +200,12 @@ rng_state_cache_init cache->end_of_cache = STATE_CACHE_CAPACITY; cache->state_pitch = state_pitch; - cache->buffer = MEM_CALLOC(allocator, 1, STATE_CACHE_CAPACITY); - if(!cache->buffer) { - res = RES_MEM_ERR; - goto error; + if(STATE_CACHE_CAPACITY != 0) { + cache->buffer = MEM_CALLOC(allocator, 1, STATE_CACHE_CAPACITY); + if(!cache->buffer) { res = RES_MEM_ERR; goto error; } + cache->stream = fmemopen(cache->buffer, STATE_CACHE_CAPACITY, "w+"); + if(!cache->stream) { res = RES_IO_ERR; goto error; } + cache->read = cache->write = ftell(cache->stream); } cache->is_init = 1; @@ -219,8 +221,13 @@ static res_T rng_state_cache_clear(struct rng_state_cache* cache) { ASSERT(cache); - rewind(cache->stream); - cache->read = cache->write = ftell(cache->stream); + if(cache->stream) { + rewind(cache->stream); + cache->read = cache->write = ftell(cache->stream); + } else { + ASSERT(!STATE_CACHE_CAPACITY); + cache->read = cache->write = 0; + } cache->nstates = 0; cache->no_wstream = 0; cache->no_rstream = 0; @@ -397,8 +404,6 @@ rng_state_cache_write(struct rng_state_cache* cache, struct ssp_rng* rng) cache->write = 0; } - fseek(cache->stream, cache->write, SEEK_SET); - /* Calculate remaining cache space */ if(rng_state_cache_is_empty(cache) || cache->write > cache->read) { remaining_space = STATE_CACHE_CAPACITY - cache->write; @@ -414,6 +419,7 @@ rng_state_cache_write(struct rng_state_cache* cache, struct ssp_rng* rng) } else { size_t sz = 0; + fseek(cache->stream, cache->write, SEEK_SET); sz = fwrite(darray_char_cdata_get(&cache->state), 1, len, cache->stream); if(sz != len) { res = RES_IO_ERR; goto error; } cache->write = ftell(cache->stream);