Refactor `time.c` regarding memory allocation.

parent 83071843
...@@ -205,17 +205,18 @@ static struct mrb_time* ...@@ -205,17 +205,18 @@ static struct mrb_time*
time_update_datetime(mrb_state *mrb, struct mrb_time *self, int dealloc) time_update_datetime(mrb_state *mrb, struct mrb_time *self, int dealloc)
{ {
struct tm *aid; struct tm *aid;
time_t t = self->sec;
if (self->timezone == MRB_TIMEZONE_UTC) { if (self->timezone == MRB_TIMEZONE_UTC) {
aid = gmtime_r(&self->sec, &self->datetime); aid = gmtime_r(&t, &self->datetime);
} }
else { else {
aid = localtime_r(&self->sec, &self->datetime); aid = localtime_r(&t, &self->datetime);
} }
if (!aid) { if (!aid) {
mrb_float sec = (mrb_float)self->sec; mrb_float sec = (mrb_float)t;
mrb_free(mrb, self); if (dealloc) mrb_free(mrb, self);
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec)); mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
/* not reached */ /* not reached */
return NULL; return NULL;
...@@ -292,24 +293,24 @@ mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mr ...@@ -292,24 +293,24 @@ mrb_time_make(mrb_state *mrb, struct RClass *c, double sec, double usec, enum mr
static struct mrb_time* static struct mrb_time*
current_mrb_time(mrb_state *mrb) current_mrb_time(mrb_state *mrb)
{ {
struct mrb_time tmzero = {0};
struct mrb_time *tm; struct mrb_time *tm;
time_t sec, usec;
tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
#if defined(TIME_UTC) && !defined(__ANDROID__) #if defined(TIME_UTC) && !defined(__ANDROID__)
{ {
struct timespec ts; struct timespec ts;
if (timespec_get(&ts, TIME_UTC) == 0) { if (timespec_get(&ts, TIME_UTC) == 0) {
mrb_free(mrb, tm);
mrb_raise(mrb, E_RUNTIME_ERROR, "timespec_get() failed for unknown reasons"); mrb_raise(mrb, E_RUNTIME_ERROR, "timespec_get() failed for unknown reasons");
} }
tm->sec = ts.tv_sec; sec = ts.tv_sec;
tm->usec = ts.tv_nsec / 1000; usec = ts.tv_nsec / 1000;
} }
#elif defined(NO_GETTIMEOFDAY) #elif defined(NO_GETTIMEOFDAY)
{ {
static time_t last_sec = 0, last_usec = 0; static time_t last_sec = 0, last_usec = 0;
tm->sec = time(NULL); sec = time(NULL);
if (tm->sec != last_sec) { if (tm->sec != last_sec) {
last_sec = tm->sec; last_sec = tm->sec;
last_usec = 0; last_usec = 0;
...@@ -318,17 +319,20 @@ current_mrb_time(mrb_state *mrb) ...@@ -318,17 +319,20 @@ current_mrb_time(mrb_state *mrb)
/* add 1 usec to differentiate two times */ /* add 1 usec to differentiate two times */
last_usec += 1; last_usec += 1;
} }
tm->usec = last_usec; usec = last_usec;
} }
#else #else
{ {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
tm->sec = tv.tv_sec; sec = tv.tv_sec;
tm->usec = tv.tv_usec; usec = tv.tv_usec;
} }
#endif #endif
tm = (struct mrb_time *)mrb_malloc(mrb, sizeof(*tm));
*tm = tmzero;
tm->sec = sec; tm->usec = usec;
tm->timezone = MRB_TIMEZONE_LOCAL; tm->timezone = MRB_TIMEZONE_LOCAL;
time_update_datetime(mrb, tm, TRUE); time_update_datetime(mrb, tm, TRUE);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment