ntpc: optimize stack used

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong 2022-01-05 11:14:49 +08:00 committed by Masayuki Ishikawa
parent b9a6dc2a85
commit 64ffdb46bb

View File

@ -1222,8 +1222,8 @@ sock_error:
static int ntpc_daemon(int argc, FAR char **argv) static int ntpc_daemon(int argc, FAR char **argv)
{ {
struct ntp_sample_s samples[CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES]; FAR struct ntp_sample_s *samples;
struct ntp_servers_s srvs; FAR struct ntp_servers_s *srvs;
int exitcode = EXIT_SUCCESS; int exitcode = EXIT_SUCCESS;
int retries = 0; int retries = 0;
int nsamples; int nsamples;
@ -1233,7 +1233,19 @@ static int ntpc_daemon(int argc, FAR char **argv)
DEBUGASSERT(argc > 1 && argv[1] != NULL && *argv[1] != '\0'); DEBUGASSERT(argc > 1 && argv[1] != NULL && *argv[1] != '\0');
memset(&srvs, 0, sizeof(srvs)); samples = malloc(sizeof(struct ntp_sample_s) *
CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES);
if (samples == NULL)
{
return EXIT_FAILURE;
}
srvs = calloc(1, sizeof(*srvs));
if (srvs == NULL)
{
free(samples);
return EXIT_FAILURE;
}
/* Indicate that we have started */ /* Indicate that we have started */
@ -1268,15 +1280,17 @@ static int ntpc_daemon(int argc, FAR char **argv)
sched_lock(); sched_lock();
while (g_ntpc_daemon.state != NTP_STOP_REQUESTED) while (g_ntpc_daemon.state != NTP_STOP_REQUESTED)
{ {
struct timespec start_realtime, start_monotonic; struct timespec start_realtime;
struct timespec start_monotonic;
int errval = 0; int errval = 0;
int i; int i;
free(srvs.hostlist_str); free(srvs->hostlist_str);
memset(&srvs, 0, sizeof(srvs)); memset(srvs, 0, sizeof(*srvs));
srvs.ntp_servers = argv[1]; srvs->ntp_servers = argv[1];
memset(samples, 0, sizeof(samples)); memset(samples, 0, sizeof(*samples) *
CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES);
clock_gettime(CLOCK_REALTIME, &start_realtime); clock_gettime(CLOCK_REALTIME, &start_realtime);
#ifdef CONFIG_CLOCK_MONOTONIC #ifdef CONFIG_CLOCK_MONOTONIC
@ -1290,7 +1304,7 @@ static int ntpc_daemon(int argc, FAR char **argv)
{ {
/* Get next sample. */ /* Get next sample. */
ret = ntpc_get_ntp_sample(&srvs, samples, nsamples); ret = ntpc_get_ntp_sample(srvs, samples, nsamples);
if (ret < 0) if (ret < 0)
{ {
errval = errno; errval = errno;
@ -1417,7 +1431,9 @@ static int ntpc_daemon(int argc, FAR char **argv)
g_ntpc_daemon.state = NTP_STOPPED; g_ntpc_daemon.state = NTP_STOPPED;
sem_post(&g_ntpc_daemon.sync); sem_post(&g_ntpc_daemon.sync);
free(srvs.hostlist_str); free(srvs->hostlist_str);
free(srvs);
free(samples);
return exitcode; return exitcode;
} }