blob: b65bf103b6d3aec2c01ac2f83714666125fe26da [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
3 */
Benny Prijonodd859a62005-11-01 16:42:51 +00004/* $Log: /pjproject-0.3/pjlib/include/pj/compat/rand.h $
5 *
6 * 3 10/14/05 12:26a Bennylp
7 * Finished error code framework, some fixes in ioqueue, etc. Pretty
8 * major.
9 *
10 * 2 9/21/05 1:39p Bennylp
11 * Periodic checkin for backup.
12 *
13 * 1 9/17/05 10:36a Bennylp
14 * Created.
15 *
16 */
17#ifndef __PJ_COMPAT_RAND_H__
18#define __PJ_COMPAT_RAND_H__
19
20/**
21 * @file rand.h
22 * @brief Provides platform_rand() and platform_srand() functions.
23 */
24
25#if defined(PJ_HAS_STDLIB_H) && PJ_HAS_STDLIB_H != 0
26 /*
27 * Use stdlib based rand() and srand().
28 */
29# include <stdlib.h>
30# define platform_srand srand
31# if defined(RAND_MAX) && RAND_MAX <= 0xFFFF
32 /*
33 * When rand() is only 16 bit strong, double the strength
34 * by calling it twice!
35 */
36 PJ_INLINE(int) platform_rand(void)
37 {
38 return ((rand() & 0xFFFF) << 16) | (rand() & 0xFFFF);
39 }
40# else
41# define platform_rand rand
42# endif
43
44#elif defined(PJ_LINUX_KERNEL) && PJ_LINUX_KERNEL != 0
45 /*
46 * Linux kernel mode random number generator.
47 */
48# include <linux/random.h>
49# define platform_srand(seed)
50
51 PJ_INLINE(int) platform_rand(void)
52 {
53 int value;
54 get_random_bytes((void*)&value, sizeof(value));
55 return value;
56 }
57
58#else
59# warning "platform_rand() is not implemented"
60# define platform_rand() 1
61# define platform_srand(seed)
62
63#endif
64
65
66#endif /* __PJ_COMPAT_RAND_H__ */
67