Skip to content

Use more musl code for time to avoid static allocations in JS #12043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,12 +1060,8 @@ LibraryManager.library = {
return time1 - time0;
},

// Statically allocated time struct.
__tm_current: '{{{ makeStaticAlloc(C_STRUCTS.tm.__size__) }}}',
// Statically allocated copy of the string "GMT" for gmtime() to point to
__tm_timezone: '{{{ makeStaticString("GMT") }}}',
// Statically allocated time strings.
__tm_formatted: '{{{ makeStaticAlloc(C_STRUCTS.tm.__size__) }}}',
mktime__deps: ['tzset'],
mktime__sig: 'ii',
mktime: function(tmPtr) {
Expand Down Expand Up @@ -1105,12 +1101,8 @@ LibraryManager.library = {
},
timelocal: 'mktime',

gmtime__deps: ['__tm_current', 'gmtime_r'],
gmtime: function(time) {
return _gmtime_r(time, ___tm_current);
},

gmtime_r__deps: ['__tm_timezone'],
gmtime_r__sig: 'iii',
gmtime_r: function(time, tmPtr) {
var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000);
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getUTCSeconds()', 'i32') }}};
Expand All @@ -1129,6 +1121,8 @@ LibraryManager.library = {

return tmPtr;
},
__gmtime_r: 'gmtime_r',

timegm__deps: ['tzset'],
timegm: function(tmPtr) {
_tzset();
Expand All @@ -1149,12 +1143,8 @@ LibraryManager.library = {
return (date.getTime() / 1000)|0;
},

localtime__deps: ['__tm_current', 'localtime_r'],
localtime: function(time) {
return _localtime_r(time, ___tm_current);
},

localtime_r__deps: ['__tm_timezone', 'tzset'],
localtime_r__sig: 'iii',
localtime_r: function(time, tmPtr) {
_tzset();
var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000);
Expand Down Expand Up @@ -1182,13 +1172,10 @@ LibraryManager.library = {

return tmPtr;
},
__localtime_r: 'localtime_r',

asctime__deps: ['__tm_formatted', 'asctime_r'],
asctime: function(tmPtr) {
return _asctime_r(tmPtr, ___tm_formatted);
},

asctime_r__deps: ['__tm_formatted', 'mktime'],
asctime_r__deps: ['mktime'],
asctime_r__sig: 'iii',
asctime_r: function(tmPtr, buf) {
var date = {
tm_sec: {{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'i32') }}},
Expand Down Expand Up @@ -1216,19 +1203,17 @@ LibraryManager.library = {
stringToUTF8(s, buf, 26);
return buf;
},

ctime__deps: ['__tm_current', 'ctime_r'],
ctime: function(timer) {
return _ctime_r(timer, ___tm_current);
},
__asctime_r: 'asctime_r',

ctime_r__deps: ['localtime_r', 'asctime_r'],
ctime_r__sig: 'iii',
ctime_r: function(time, buf) {
var stack = stackSave();
var rv = _asctime_r(_localtime_r(time, stackAlloc({{{ C_STRUCTS.tm.__size__ }}})), buf);
stackRestore(stack);
return rv;
},
__ctime_r: 'ctime_r',

dysize: function(year) {
var leap = ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)));
Expand Down
4 changes: 2 additions & 2 deletions system/lib/libc/musl/src/time/asctime.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <time.h>

char *__asctime(const struct tm *, char *);
char *__asctime_r(const struct tm *, char *);

char *asctime(const struct tm *tm)
{
static char buf[26];
return __asctime(tm, buf);
return __asctime_r(tm, buf);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is updated to latest upstream musl, the lack of _r was a bug apparently.

btw, this file is basically the same as the other non-reentrant ones - they allocate a buffer, and then call the reentrant version (which we implement in JS).

8 changes: 4 additions & 4 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def get_files(self):
# Allowed files from ignored modules
libc_files += files_in_path(
path_components=['system', 'lib', 'libc', 'musl', 'src', 'time'],
filenames=['clock_settime.c'])
filenames=['clock_settime.c', 'asctime.c', 'ctime.c', 'gmtime.c', 'localtime.c'])
libc_files += files_in_path(
path_components=['system', 'lib', 'libc', 'musl', 'src', 'legacy'],
filenames=['getpagesize.c', 'err.c'])
Expand Down Expand Up @@ -1330,14 +1330,14 @@ def get_files(self):
'__tm_to_secs.c',
'__tz.c',
'__year_to_secs.c',
'asctime_r.c',
'clock.c',
'clock_gettime.c',
'ctime_r.c',
'difftime.c',
'gettimeofday.c',
'localtime.c',
'localtime_r.c',
'gmtime.c',
'gmtime_r.c',
'localtime_r.c',
'nanosleep.c',
'mktime.c',
'time.c'])
Expand Down