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