Skip to content

Commit 635fc7b

Browse files
authored
Use more musl code for time to avoid static allocations in JS (#12043)
Use musl code for non-reentrant time functions to avoid a static allocation in JS. Instead, C does the allocation and sends the buffer to the _r versions of the time functions. This is better for code size too. Musl calls __ prefixed versions, so add aliases for them. Add a fix from upstream musl to asctime.c. Add ctime_r and asctime_r to libstandalone, which were missing. Helps WebAssembly/binaryen#3043
1 parent cd4dad7 commit 635fc7b

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

src/library.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,8 @@ LibraryManager.library = {
10601060
return time1 - time0;
10611061
},
10621062

1063-
// Statically allocated time struct.
1064-
__tm_current: '{{{ makeStaticAlloc(C_STRUCTS.tm.__size__) }}}',
10651063
// Statically allocated copy of the string "GMT" for gmtime() to point to
10661064
__tm_timezone: '{{{ makeStaticString("GMT") }}}',
1067-
// Statically allocated time strings.
1068-
__tm_formatted: '{{{ makeStaticAlloc(C_STRUCTS.tm.__size__) }}}',
10691065
mktime__deps: ['tzset'],
10701066
mktime__sig: 'ii',
10711067
mktime: function(tmPtr) {
@@ -1105,12 +1101,8 @@ LibraryManager.library = {
11051101
},
11061102
timelocal: 'mktime',
11071103

1108-
gmtime__deps: ['__tm_current', 'gmtime_r'],
1109-
gmtime: function(time) {
1110-
return _gmtime_r(time, ___tm_current);
1111-
},
1112-
11131104
gmtime_r__deps: ['__tm_timezone'],
1105+
gmtime_r__sig: 'iii',
11141106
gmtime_r: function(time, tmPtr) {
11151107
var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000);
11161108
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getUTCSeconds()', 'i32') }}};
@@ -1129,6 +1121,8 @@ LibraryManager.library = {
11291121

11301122
return tmPtr;
11311123
},
1124+
__gmtime_r: 'gmtime_r',
1125+
11321126
timegm__deps: ['tzset'],
11331127
timegm: function(tmPtr) {
11341128
_tzset();
@@ -1149,12 +1143,8 @@ LibraryManager.library = {
11491143
return (date.getTime() / 1000)|0;
11501144
},
11511145

1152-
localtime__deps: ['__tm_current', 'localtime_r'],
1153-
localtime: function(time) {
1154-
return _localtime_r(time, ___tm_current);
1155-
},
1156-
11571146
localtime_r__deps: ['__tm_timezone', 'tzset'],
1147+
localtime_r__sig: 'iii',
11581148
localtime_r: function(time, tmPtr) {
11591149
_tzset();
11601150
var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000);
@@ -1182,13 +1172,10 @@ LibraryManager.library = {
11821172

11831173
return tmPtr;
11841174
},
1175+
__localtime_r: 'localtime_r',
11851176

1186-
asctime__deps: ['__tm_formatted', 'asctime_r'],
1187-
asctime: function(tmPtr) {
1188-
return _asctime_r(tmPtr, ___tm_formatted);
1189-
},
1190-
1191-
asctime_r__deps: ['__tm_formatted', 'mktime'],
1177+
asctime_r__deps: ['mktime'],
1178+
asctime_r__sig: 'iii',
11921179
asctime_r: function(tmPtr, buf) {
11931180
var date = {
11941181
tm_sec: {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'i32') }}},
@@ -1216,19 +1203,17 @@ LibraryManager.library = {
12161203
stringToUTF8(s, buf, 26);
12171204
return buf;
12181205
},
1219-
1220-
ctime__deps: ['__tm_current', 'ctime_r'],
1221-
ctime: function(timer) {
1222-
return _ctime_r(timer, ___tm_current);
1223-
},
1206+
__asctime_r: 'asctime_r',
12241207

12251208
ctime_r__deps: ['localtime_r', 'asctime_r'],
1209+
ctime_r__sig: 'iii',
12261210
ctime_r: function(time, buf) {
12271211
var stack = stackSave();
12281212
var rv = _asctime_r(_localtime_r(time, stackAlloc({{{ C_STRUCTS.tm.__size__ }}})), buf);
12291213
stackRestore(stack);
12301214
return rv;
12311215
},
1216+
__ctime_r: 'ctime_r',
12321217

12331218
dysize: function(year) {
12341219
var leap = ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)));
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <time.h>
22

3-
char *__asctime(const struct tm *, char *);
3+
char *__asctime_r(const struct tm *, char *);
44

55
char *asctime(const struct tm *tm)
66
{
77
static char buf[26];
8-
return __asctime(tm, buf);
8+
return __asctime_r(tm, buf);
99
}

tools/system_libs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def get_files(self):
728728
# Allowed files from ignored modules
729729
libc_files += files_in_path(
730730
path_components=['system', 'lib', 'libc', 'musl', 'src', 'time'],
731-
filenames=['clock_settime.c'])
731+
filenames=['clock_settime.c', 'asctime.c', 'ctime.c', 'gmtime.c', 'localtime.c'])
732732
libc_files += files_in_path(
733733
path_components=['system', 'lib', 'libc', 'musl', 'src', 'legacy'],
734734
filenames=['getpagesize.c', 'err.c'])
@@ -1331,14 +1331,14 @@ def get_files(self):
13311331
'__tm_to_secs.c',
13321332
'__tz.c',
13331333
'__year_to_secs.c',
1334+
'asctime_r.c',
13341335
'clock.c',
13351336
'clock_gettime.c',
1337+
'ctime_r.c',
13361338
'difftime.c',
13371339
'gettimeofday.c',
1338-
'localtime.c',
1339-
'localtime_r.c',
1340-
'gmtime.c',
13411340
'gmtime_r.c',
1341+
'localtime_r.c',
13421342
'nanosleep.c',
13431343
'mktime.c',
13441344
'time.c'])

0 commit comments

Comments
 (0)