fixed memory leaks

This commit is contained in:
timerix 2022-10-24 15:59:01 +06:00
parent 4222c3ebf6
commit bbeb6bea1d
11 changed files with 50 additions and 6 deletions

View File

@ -12,11 +12,13 @@ extern "C" {
/*
You can choose any algorithm that has required functions:
some_alg32_state some_alg32_init(uint32 seed);
uint32 some_alg32_next(some_alg32_state);
some_alg32_statePtr some_alg32_init(uint32 seed);
uint32 some_alg32_next(some_alg32_statePtr);
void some_alg32_free(some_alg32_statePtr);
#define KRAND_ALG32_next some_alg32_next
#define KRAND_ALG32_init some_alg32_init
#define KRAND_ALG32_next some_alg32_next
#define KRAND_ALG32_free some_alg32_free
#include "kerep/random/krandom.h"
The same way it works for 64-bit RNGs
@ -29,12 +31,18 @@ The same way it works for 64-bit RNGs
#ifndef KRAND_ALG32_init
#define KRAND_ALG32_init xoshiro128plus##_init
#endif
#ifndef KRAND_ALG32_free
#define KRAND_ALG32_free xoshiro128plus##_free
#endif
#ifndef KRAND_ALG64_next
#define KRAND_ALG64_next xoshiro256plus##_next
#endif
#ifndef KRAND_ALG64_init
#define KRAND_ALG64_init xoshiro256plus##_init
#endif
#ifndef KRAND_ALG64_free
#define KRAND_ALG64_free xoshiro256plus##_free
#endif
typedef void* krand_statePtr;
#define KRAND_ALG32_initFromTime xoshiro128plus##_initFromTime

View File

@ -13,6 +13,9 @@ splitmix64_statePtr splitmix64_init(uint64 seed);
static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); }
uint64 splitmix64_next(splitmix64_statePtr);
static inline void splitmix64_free(splitmix64_statePtr state) {
free(state);
}
#if __cplusplus
}

View File

@ -24,6 +24,12 @@ static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoros
uint32 xoroshiro64star_next(xoroshiro64_statePtr);
uint32 xoroshiro64starstar_next(xoroshiro64_statePtr);
static inline void xoroshiro64_free(xoroshiro64_statePtr state) {
free(state);
}
#define xoroshiro64star_free xoroshiro64_free
#define xoroshiro64starstar_free xoroshiro64_free
#if __cplusplus
}
#endif

View File

@ -44,5 +44,6 @@ void* xoroshiro64_init(uint64 seed){
xoroshiro64_state* state=malloc(sizeof(xoroshiro64_state));
splitmix64_state* splitmix=splitmix64_init(seed);
state->merged=splitmix64_next(splitmix);
splitmix64_free(splitmix);
return state;
}

View File

@ -27,6 +27,13 @@ uint64 xoroshiro128plus_next(xoroshiro128_statePtr);
uint64 xoroshiro128plusplus_next(xoroshiro128_statePtr);
uint64 xoroshiro128starstar_next(xoroshiro128_statePtr);
static inline void xoroshiro128_free(xoroshiro128_statePtr state) {
free(state);
}
#define xoroshiro128plus_free xoroshiro128_free
#define xoroshiro128plusplus_free xoroshiro128_free
#define xoroshiro128starstar_free xoroshiro128_free
#if __cplusplus
}
#endif

View File

@ -54,5 +54,6 @@ void* xoroshiro128_init(uint64 seed){
splitmix64_state* splitmix=splitmix64_init(seed);
state->s[0]=splitmix64_next(splitmix);
state->s[1]=splitmix64_next(splitmix);
splitmix64_free(splitmix);
return state;
}

View File

@ -28,6 +28,13 @@ uint32 xoshiro128plus_next(xoshiro128_statePtr);
uint32 xoshiro128plusplus_next(xoshiro128_statePtr);
uint32 xoshiro128starstar_next(xoshiro128_statePtr);
static inline void xoshiro128_free(xoshiro128_statePtr state) {
free(state);
}
#define xoshiro128plus_free xoshiro128_free
#define xoshiro128plusplus_free xoshiro128_free
#define xoshiro128starstar_free xoshiro128_free
#if __cplusplus
}
#endif

View File

@ -49,5 +49,6 @@ void* xoshiro128_init(uint64 seed){
splitmix64_state* splitmix=splitmix64_init(seed);
state->merged[0]=splitmix64_next(splitmix);
state->merged[1]=splitmix64_next(splitmix);
splitmix64_free(splitmix);
return state;
}

View File

@ -27,6 +27,13 @@ uint64 xoshiro256plus_next(xoshiro256_statePtr);
uint64 xoshiro256plusplus_next(xoshiro256_statePtr);
uint64 xoshiro256starstar_next(xoshiro256_statePtr);
static inline void xoshiro256_free(xoshiro256_statePtr state) {
free(state);
}
#define xoshiro256plus_free xoshiro256_free
#define xoshiro256plusplus_free xoshiro256_free
#define xoshiro256starstar_free xoshiro256_free
#if __cplusplus
}
#endif

View File

@ -53,5 +53,6 @@ void* xoshiro256_init(uint64 seed){
state->s[1]=splitmix64_next(splitmix);
state->s[2]=splitmix64_next(splitmix);
state->s[3]=splitmix64_next(splitmix);
splitmix64_free(splitmix);
return state;
}

View File

@ -8,13 +8,15 @@
uint##VALUE_SIZE r=ALG##_next(s);\
printf("\e[97m next from zero seed:");\
if(r!=EXPECTED_FROM_ZERO){\
printf("\e[91m %llu\n", (uint64)r);\
printf("\e[91m " IFWIN("%llu\n","%lu\n"), (uint64)r);\
throw(ERR_UNEXPECTEDVAL);\
}\
printf("\e[92m %llu\n", (uint64)r);\
printf("\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\
ALG##_free(s);\
s= ALG##_initFromTime();\
r=ALG##_next(s);\
printf("\e[97m next from time seed:\e[92m %llu\n", (uint64)r);\
ALG##_free(s);\
printf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\
}
void test_rng_algorithms(){