summaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-11-24 14:54:32 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-11-24 14:54:32 -0200
commit7dc3ca7b8ee87d5264f7edb2a1ad425a34048aa1 (patch)
tree274aad205bdf28574673dc0549b30a48fe945790 /ltablib.c
parent71344b5cacce22a7a2a5dcdaccf4340420b0afa1 (diff)
downloadlua-github-7dc3ca7b8ee87d5264f7edb2a1ad425a34048aa1.tar.gz
handling 'clock_t' and 'time_t' correctly in ISO C point of view
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/ltablib.c b/ltablib.c
index 55a12c7a..ef39d5df 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltablib.c,v 1.87 2015/11/23 11:09:27 roberto Exp roberto $
+** $Id: ltablib.c,v 1.86 2015/11/20 12:30:20 roberto Exp roberto $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@@ -12,6 +12,7 @@
#include <limits.h>
#include <stddef.h>
+#include <string.h>
#include "lua.h"
@@ -298,14 +299,22 @@ static unsigned int partition (lua_State *L, unsigned int lo,
*/
#if !defined(l_sortpivot)
/* Use 'time' and 'clock' as sources of "randomness" */
-
#include <time.h>
+#define szi (sizeof(unsigned int))
+#define sof(e) (sizeof(e)/szi)
+
static unsigned int choosePivot (unsigned int lo, unsigned int up) {
- unsigned int t = (unsigned int)(unsigned long)time(NULL); /* time */
- unsigned int c = (unsigned int)(unsigned long)clock(); /* clock */
+ clock_t c = clock();
+ time_t t = time(NULL);
+ unsigned int buff[sof(c) + sof(t)];
unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */
- unsigned int p = (c + t) % (r4 * 2) + (lo + r4);
+ unsigned int p, i, h = 0;
+ memcpy(buff, &c, sof(c) * szi);
+ memcpy(buff + sof(c), &t, sof(t) * szi);
+ for (i = 0; i < sof(buff); i++)
+ h += buff[i];
+ p = h % (r4 * 2) + (lo + r4);
lua_assert(lo + r4 <= p && p <= up - r4);
return p;
}