Skip to content

Commit 485065f

Browse files
committed
Refactor time APIs, IO abstraction, and malloc error handling
Update multiple low-level C library modules: - gd32/time_ptp/time.cpp, time_systick/time.cpp, time_timer/time.cpp: bump copyright years, add includes (cstdint/ctime), implement/normalize gettimeofday, settimeofday and time() with improved type safety (static_cast), use renamed gd32 PTP helpers, and unify brace/formatting style. Use UINT32_MAX where appropriate and rename g_Seconds -> gv_seconds for timer backend. - getchar.cpp, putchar.cpp, printf.cpp, puts.cpp: introduce conditional IO backend selection (CONFIG_CLIB_USE_UART0 / CONFIG_CLIB_USE_NULL) and unify use of PutChar/GetChar/Puts via using-declarations to decouple console implementation. - malloc.cpp: centralize heap error reporting by adding a static Error(func, s) helper and ERROR macro, adjust messages, and ensure diagnostic pop at end. Other small cleanups: consistent brace placement, removal of redundant namespaces, minor macro/constant fixes, and defensive assert/nullptr checks. These changes improve type safety, backend configurability, and readability.
1 parent 3da5ba8 commit 485065f

8 files changed

Lines changed: 218 additions & 194 deletions

File tree

lib-clib/src/gd32/time_ptp/time.cpp

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file time.cpp
33
*
44
*/
5-
/* Copyright (C) 2024-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2024-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -23,6 +23,7 @@
2323
* THE SOFTWARE.
2424
*/
2525

26+
#include <cstdint>
2627
#pragma GCC push_options
2728
#pragma GCC optimize("O2")
2829

@@ -38,66 +39,60 @@
3839
#define enet_ptp_system_time_get(x) enet_ptp_system_time_get(ENETx, x)
3940
#endif
4041

41-
extern "C"
42-
{
43-
/*
44-
* number of seconds and microseconds since the Epoch,
45-
* 1970-01-01 00:00:00 +0000 (UTC).
46-
*/
42+
extern "C" {
43+
/*
44+
* number of seconds and microseconds since the Epoch,
45+
* 1970-01-01 00:00:00 +0000 (UTC).
46+
*/
4747

48-
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz)
49-
{
50-
assert(tv != 0);
48+
int gettimeofday(struct timeval* tv, [[maybe_unused]] struct timezone* tz) {
49+
assert(tv != 0);
5150

52-
enet_ptp_systime_struct systime;
53-
enet_ptp_system_time_get(&systime);
51+
enet_ptp_systime_struct systime;
52+
enet_ptp_system_time_get(&systime);
5453

55-
tv->tv_sec = systime.second;
54+
tv->tv_sec = static_cast<time_t>(systime.second);
5655

5756
#if !defined(GD32F4XX)
58-
const auto kNanoSecond = systime.nanosecond;
57+
const auto kNanoSecond = systime.nanosecond;
5958
#else
60-
const auto kNanoSecond = gd32::ptp_subsecond_2_nanosecond(systime.subsecond);
59+
const auto kNanoSecond = gd32::PtpSubsecond2Nanosecond(systime.subsecond);
6160
#endif
6261

63-
tv->tv_usec = kNanoSecond / 1000U;
64-
65-
return 0;
66-
}
62+
tv->tv_usec = static_cast<time_t>(kNanoSecond / 1000U);
6763

68-
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz)
69-
{
70-
assert(tv != 0);
64+
return 0;
65+
}
7166

72-
const uint32_t kSign = ENET_PTP_ADD_TO_TIME;
73-
const uint32_t kSecond = tv->tv_sec;
74-
const uint32_t kNanoSecond = tv->tv_usec * 1000U;
75-
const auto kSubSecond = gd32::ptp_nanosecond_2_subsecond(kNanoSecond);
67+
int settimeofday(const struct timeval* tv, [[maybe_unused]] const struct timezone* tz) {
68+
assert(tv != 0);
7669

77-
enet_ptp_timestamp_update_config(kSign, kSecond, kSubSecond);
70+
const uint32_t kSign = ENET_PTP_ADD_TO_TIME;
71+
const uint32_t kSecond = static_cast<uint32_t>(tv->tv_sec);
72+
const uint32_t kNanoSecond = static_cast<uint32_t>(tv->tv_usec) * 1000U;
73+
const auto kSubSecond = gd32::PtpNanosecond2Subsecond(kNanoSecond);
7874

79-
if (SUCCESS == enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT))
80-
{
81-
return 0;
82-
}
75+
enet_ptp_timestamp_update_config(kSign, kSecond, kSubSecond);
8376

84-
return -1;
77+
if (SUCCESS == enet_ptp_timestamp_function_config(ENET_PTP_SYSTIME_INIT)) {
78+
return 0;
8579
}
8680

87-
/*
88-
* time() returns the time as the number of seconds since the Epoch,
89-
1970-01-01 00:00:00 +0000 (UTC).
90-
*/
91-
time_t time(time_t* __timer) // NOLINT
92-
{
93-
struct timeval tv;
94-
gettimeofday(&tv, nullptr);
95-
96-
if (__timer != nullptr)
97-
{
98-
*__timer = tv.tv_sec;
99-
}
100-
101-
return tv.tv_sec;
81+
return -1;
82+
}
83+
84+
/*
85+
* time() returns the time as the number of seconds since the Epoch,
86+
1970-01-01 00:00:00 +0000 (UTC).
87+
*/
88+
time_t time(time_t* __timer) { // NOLINT
89+
struct timeval tv;
90+
gettimeofday(&tv, nullptr);
91+
92+
if (__timer != nullptr) {
93+
*__timer = tv.tv_sec;
10294
}
95+
96+
return tv.tv_sec;
97+
}
10398
}

lib-clib/src/gd32/time_systick/time.cpp

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file time.cpp
33
*
44
*/
5-
/* Copyright (C) 2021-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2021-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -35,79 +35,70 @@ extern volatile uint32_t gv_nSysTickMillis;
3535
static uint32_t previous_systick_millis;
3636
static struct timeval s_tv;
3737

38-
extern "C"
39-
{
40-
/*
41-
* number of seconds and microseconds since the Epoch,
42-
* 1970-01-01 00:00:00 +0000 (UTC).
43-
*/
38+
extern "C" {
39+
/*
40+
* number of seconds and microseconds since the Epoch,
41+
* 1970-01-01 00:00:00 +0000 (UTC).
42+
*/
4443

45-
int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz)
46-
{
47-
assert(tv != 0);
44+
int gettimeofday(struct timeval* tv, __attribute__((unused)) struct timezone* tz) {
45+
assert(tv != 0);
4846

49-
const auto kCurrentSysTickMillis = gv_nSysTickMillis;
47+
const auto kCurrentSysTickMillis = gv_nSysTickMillis;
5048

51-
uint32_t millis_elapsed;
49+
uint32_t millis_elapsed;
5250

53-
if (kCurrentSysTickMillis >= previous_systick_millis)
54-
{
55-
millis_elapsed = kCurrentSysTickMillis - previous_systick_millis;
56-
}
57-
else
58-
{
59-
millis_elapsed = (UINT32_MAX - previous_systick_millis) + kCurrentSysTickMillis + 1;
60-
}
51+
if (kCurrentSysTickMillis >= previous_systick_millis) {
52+
millis_elapsed = kCurrentSysTickMillis - previous_systick_millis;
53+
} else {
54+
millis_elapsed = (UINT32_MAX - previous_systick_millis) + kCurrentSysTickMillis + 1;
55+
}
6156

62-
previous_systick_millis = kCurrentSysTickMillis;
57+
previous_systick_millis = kCurrentSysTickMillis;
6358

64-
const auto kSeconds = millis_elapsed / 1000U;
65-
const auto kMicroSeconds = (millis_elapsed % 1000U) * 1000U;
59+
const auto kSeconds = millis_elapsed / 1000U;
60+
const auto kMicroSeconds = (millis_elapsed % 1000U) * 1000U;
6661

67-
s_tv.tv_sec += static_cast<time_t>(kSeconds);
68-
s_tv.tv_usec += static_cast<suseconds_t>(kMicroSeconds);
62+
s_tv.tv_sec += static_cast<time_t>(kSeconds);
63+
s_tv.tv_usec += static_cast<suseconds_t>(kMicroSeconds);
6964

70-
if (s_tv.tv_usec >= 1000000)
71-
{
72-
s_tv.tv_sec++;
73-
s_tv.tv_usec -= 1000000;
74-
}
65+
if (s_tv.tv_usec >= 1000000) {
66+
s_tv.tv_sec++;
67+
s_tv.tv_usec -= 1000000;
68+
}
7569

76-
tv->tv_sec = s_tv.tv_sec;
77-
tv->tv_usec = s_tv.tv_usec;
70+
tv->tv_sec = s_tv.tv_sec;
71+
tv->tv_usec = s_tv.tv_usec;
7872

79-
return 0;
80-
}
73+
return 0;
74+
}
8175

82-
int settimeofday(const struct timeval* tv, __attribute__((unused)) const struct timezone* tz)
83-
{
84-
assert(tv != 0);
76+
int settimeofday(const struct timeval* tv, __attribute__((unused)) const struct timezone* tz) {
77+
assert(tv != 0);
8578

86-
struct timeval g;
87-
gettimeofday(&g, nullptr);
79+
struct timeval g;
80+
gettimeofday(&g, nullptr);
8881

89-
previous_systick_millis = gv_nSysTickMillis;
82+
previous_systick_millis = gv_nSysTickMillis;
9083

91-
s_tv.tv_sec = tv->tv_sec;
92-
s_tv.tv_usec = tv->tv_usec;
84+
s_tv.tv_sec = tv->tv_sec;
85+
s_tv.tv_usec = tv->tv_usec;
9386

94-
return 0;
95-
}
87+
return 0;
88+
}
89+
90+
/*
91+
* time() returns the time as the number of seconds since the Epoch,
92+
1970-01-01 00:00:00 +0000 (UTC).
93+
*/
94+
time_t time(time_t* __timer) { // NOLINT
95+
struct timeval tv;
96+
gettimeofday(&tv, nullptr);
9697

97-
/*
98-
* time() returns the time as the number of seconds since the Epoch,
99-
1970-01-01 00:00:00 +0000 (UTC).
100-
*/
101-
time_t time(time_t* __timer) //NOLINT
102-
{
103-
struct timeval tv;
104-
gettimeofday(&tv, nullptr);
105-
106-
if (__timer != nullptr)
107-
{
108-
*__timer = tv.tv_sec;
109-
}
110-
111-
return tv.tv_sec;
98+
if (__timer != nullptr) {
99+
*__timer = tv.tv_sec;
112100
}
101+
102+
return tv.tv_sec;
103+
}
113104
}

0 commit comments

Comments
 (0)