From e1c0b55cbcc602bdff8b9778d911c35515f48cc3 Mon Sep 17 00:00:00 2001 From: Pierre-Luc St-Charles Date: Wed, 27 Nov 2013 07:48:33 -0800 Subject: [PATCH] Android: implement rand_r() stdlib.h doesn't implement rand_r() on Android. Signed-off-by: Charles Briere Signed-off-by: Mathieu Desnoyers --- Makefile.am | 1 + tests/benchmark/test_urcu_hash.h | 1 + urcu/rand-compat.h | 63 ++++++++++++++++++++++++++++++++ urcu/static/urcu.h | 1 + 4 files changed, 66 insertions(+) create mode 100644 urcu/rand-compat.h diff --git a/Makefile.am b/Makefile.am index d169ef0..938027c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,6 +23,7 @@ nobase_dist_include_HEADERS = urcu/compiler.h urcu/hlist.h urcu/list.h \ urcu/lfstack.h urcu/syscall-compat.h \ $(top_srcdir)/urcu/map/*.h \ $(top_srcdir)/urcu/static/*.h \ + urcu/rand-compat.h \ urcu/tls-compat.h nobase_nodist_include_HEADERS = urcu/arch.h urcu/uatomic.h urcu/config.h diff --git a/tests/benchmark/test_urcu_hash.h b/tests/benchmark/test_urcu_hash.h index c0727b7..b35acdb 100644 --- a/tests/benchmark/test_urcu_hash.h +++ b/tests/benchmark/test_urcu_hash.h @@ -37,6 +37,7 @@ #include #include +#include #include "cpuset.h" #include "thread-id.h" diff --git a/urcu/rand-compat.h b/urcu/rand-compat.h new file mode 100644 index 0000000..2c57751 --- /dev/null +++ b/urcu/rand-compat.h @@ -0,0 +1,63 @@ +#ifndef _URCU_RAND_COMPAT_H +#define _URCU_RAND_COMPAT_H + +/* + * urcu/rand-compat.h + * + * Userspace RCU library - rand/rand_r Compatibility Header + * + * Copyright 1996 - Ulrich Drepper + * Copyright 2013 - Pierre-Luc St-Charles + * + * Note: this file is only used to simplify the code required to + * use the 'rand_r(...)' system function across multiple platforms, + * which might not always be referenced the same way. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef __ANDROID__ +/* + * Reentrant random function from POSIX.1c. + * Copyright (C) 1996, 1999 Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * Contributed by Ulrich Drepper >, 1996. + */ +static inline int rand_r(unsigned int *seed) +{ + unsigned int next = *seed; + int result; + + next *= 1103515245; + next += 12345; + result = (unsigned int) (next / 65536) % 2048; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (unsigned int) (next / 65536) % 1024; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (unsigned int) (next / 65536) % 1024; + + *seed = next; + + return result; +} +#endif /* __ANDROID__ */ + +#endif /* _URCU_RAND_COMPAT_H */ diff --git a/urcu/static/urcu.h b/urcu/static/urcu.h index 53d2610..33545b7 100644 --- a/urcu/static/urcu.h +++ b/urcu/static/urcu.h @@ -41,6 +41,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { -- 2.34.1