blob: c9329157a441f78455fbd81794f44aa1479bbd61 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_COMPAT_RAND_H__
20#define __PJ_COMPAT_RAND_H__
21
22/**
23 * @file rand.h
24 * @brief Provides platform_rand() and platform_srand() functions.
25 */
26
27#if defined(PJ_HAS_STDLIB_H) && PJ_HAS_STDLIB_H != 0
28 /*
29 * Use stdlib based rand() and srand().
30 */
31# include <stdlib.h>
32# define platform_srand srand
33# if defined(RAND_MAX) && RAND_MAX <= 0xFFFF
34 /*
35 * When rand() is only 16 bit strong, double the strength
36 * by calling it twice!
37 */
38 PJ_INLINE(int) platform_rand(void)
39 {
40 return ((rand() & 0xFFFF) << 16) | (rand() & 0xFFFF);
41 }
42# else
43# define platform_rand rand
44# endif
45
46#elif defined(PJ_LINUX_KERNEL) && PJ_LINUX_KERNEL != 0
47 /*
48 * Linux kernel mode random number generator.
49 */
50# include <linux/random.h>
51# define platform_srand(seed)
52
53 PJ_INLINE(int) platform_rand(void)
54 {
55 int value;
56 get_random_bytes((void*)&value, sizeof(value));
57 return value;
58 }
59
60#else
61# warning "platform_rand() is not implemented"
62# define platform_rand() 1
63# define platform_srand(seed)
64
65#endif
66
67
68#endif /* __PJ_COMPAT_RAND_H__ */
69