aboutsummaryrefslogtreecommitdiff
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-7dc3ca7b8ee87d5264f7edb2a1ad425a34048aa1.tar.gz
lua-7dc3ca7b8ee87d5264f7edb2a1ad425a34048aa1.tar.bz2
lua-7dc3ca7b8ee87d5264f7edb2a1ad425a34048aa1.zip
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 @@
1/* 1/*
2** $Id: ltablib.c,v 1.87 2015/11/23 11:09:27 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.86 2015/11/20 12:30:20 roberto Exp roberto $
3** Library for Table Manipulation 3** Library for Table Manipulation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -12,6 +12,7 @@
12 12
13#include <limits.h> 13#include <limits.h>
14#include <stddef.h> 14#include <stddef.h>
15#include <string.h>
15 16
16#include "lua.h" 17#include "lua.h"
17 18
@@ -298,14 +299,22 @@ static unsigned int partition (lua_State *L, unsigned int lo,
298*/ 299*/
299#if !defined(l_sortpivot) 300#if !defined(l_sortpivot)
300/* Use 'time' and 'clock' as sources of "randomness" */ 301/* Use 'time' and 'clock' as sources of "randomness" */
301
302#include <time.h> 302#include <time.h>
303 303
304#define szi (sizeof(unsigned int))
305#define sof(e) (sizeof(e)/szi)
306
304static unsigned int choosePivot (unsigned int lo, unsigned int up) { 307static unsigned int choosePivot (unsigned int lo, unsigned int up) {
305 unsigned int t = (unsigned int)(unsigned long)time(NULL); /* time */ 308 clock_t c = clock();
306 unsigned int c = (unsigned int)(unsigned long)clock(); /* clock */ 309 time_t t = time(NULL);
310 unsigned int buff[sof(c) + sof(t)];
307 unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ 311 unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */
308 unsigned int p = (c + t) % (r4 * 2) + (lo + r4); 312 unsigned int p, i, h = 0;
313 memcpy(buff, &c, sof(c) * szi);
314 memcpy(buff + sof(c), &t, sof(t) * szi);
315 for (i = 0; i < sof(buff); i++)
316 h += buff[i];
317 p = h % (r4 * 2) + (lo + r4);
309 lua_assert(lo + r4 <= p && p <= up - r4); 318 lua_assert(lo + r4 <= p && p <= up - r4);
310 return p; 319 return p;
311} 320}