eloop_none: Update sample to be closer to the current eloop.c

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-02-03 16:50:45 +02:00
parent 5617809c93
commit fc9d96a78a

View file

@ -55,7 +55,7 @@ static struct eloop_data eloop;
int eloop_init(void) int eloop_init(void)
{ {
memset(&eloop, 0, sizeof(eloop)); os_memset(&eloop, 0, sizeof(eloop));
dl_list_init(&eloop.timeout); dl_list_init(&eloop.timeout);
return 0; return 0;
} }
@ -66,9 +66,8 @@ int eloop_register_read_sock(int sock, eloop_sock_handler handler,
{ {
struct eloop_sock *tmp; struct eloop_sock *tmp;
tmp = (struct eloop_sock *) tmp = os_realloc_array(eloop.readers, eloop.reader_count + 1,
realloc(eloop.readers, sizeof(struct eloop_sock));
(eloop.reader_count + 1) * sizeof(struct eloop_sock));
if (tmp == NULL) if (tmp == NULL)
return -1; return -1;
@ -100,9 +99,9 @@ void eloop_unregister_read_sock(int sock)
if (i == eloop.reader_count) if (i == eloop.reader_count)
return; return;
if (i != eloop.reader_count - 1) { if (i != eloop.reader_count - 1) {
memmove(&eloop.readers[i], &eloop.readers[i + 1], os_memmove(&eloop.readers[i], &eloop.readers[i + 1],
(eloop.reader_count - i - 1) * (eloop.reader_count - i - 1) *
sizeof(struct eloop_sock)); sizeof(struct eloop_sock));
} }
eloop.reader_count--; eloop.reader_count--;
eloop.reader_table_changed = 1; eloop.reader_table_changed = 1;
@ -114,12 +113,27 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
void *eloop_data, void *user_data) void *eloop_data, void *user_data)
{ {
struct eloop_timeout *timeout, *tmp; struct eloop_timeout *timeout, *tmp;
os_time_t now_sec;
timeout = (struct eloop_timeout *) malloc(sizeof(*timeout)); timeout = os_zalloc(sizeof(*timeout));
if (timeout == NULL) if (timeout == NULL)
return -1; return -1;
os_get_time(&timeout->time); if (os_get_time(&timeout->time) < 0) {
os_free(timeout);
return -1;
}
now_sec = timeout->time.sec;
timeout->time.sec += secs; timeout->time.sec += secs;
if (timeout->time.sec < now_sec) {
/*
* Integer overflow - assume long enough timeout to be assumed
* to be infinite, i.e., the timeout would never happen.
*/
wpa_printf(MSG_DEBUG, "ELOOP: Too long timeout (secs=%u) to "
"ever happen - ignore it", secs);
os_free(timeout);
return 0;
}
timeout->time.usec += usecs; timeout->time.usec += usecs;
while (timeout->time.usec >= 1000000) { while (timeout->time.usec >= 1000000) {
timeout->time.sec++; timeout->time.sec++;
@ -142,6 +156,13 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
} }
static void eloop_remove_timeout(struct eloop_timeout *timeout)
{
dl_list_del(&timeout->list);
os_free(timeout);
}
int eloop_cancel_timeout(eloop_timeout_handler handler, int eloop_cancel_timeout(eloop_timeout_handler handler,
void *eloop_data, void *user_data) void *eloop_data, void *user_data)
{ {
@ -155,8 +176,7 @@ int eloop_cancel_timeout(eloop_timeout_handler handler,
eloop_data == ELOOP_ALL_CTX) && eloop_data == ELOOP_ALL_CTX) &&
(timeout->user_data == user_data || (timeout->user_data == user_data ||
user_data == ELOOP_ALL_CTX)) { user_data == ELOOP_ALL_CTX)) {
dl_list_del(&timeout->list); eloop_remove_timeout(timeout);
free(timeout);
removed++; removed++;
} }
} }
@ -225,10 +245,8 @@ int eloop_register_signal(int sig, eloop_signal_handler handler,
{ {
struct eloop_signal *tmp; struct eloop_signal *tmp;
tmp = (struct eloop_signal *) tmp = os_realloc_array(eloop.signals, eloop.signal_count + 1,
realloc(eloop.signals, sizeof(struct eloop_signal));
(eloop.signal_count + 1) *
sizeof(struct eloop_signal));
if (tmp == NULL) if (tmp == NULL)
return -1; return -1;
@ -302,10 +320,12 @@ void eloop_run(void)
if (timeout) { if (timeout) {
os_get_time(&now); os_get_time(&now);
if (!os_time_before(&now, &timeout->time)) { if (!os_time_before(&now, &timeout->time)) {
dl_list_del(&timeout->list); void *eloop_data = timeout->eloop_data;
timeout->handler(timeout->eloop_data, void *user_data = timeout->user_data;
timeout->user_data); eloop_timeout_handler handler =
free(timeout); timeout->handler;
eloop_remove_timeout(timeout);
handler(eloop_data, user_data);
} }
} }
@ -341,11 +361,10 @@ void eloop_destroy(void)
dl_list_for_each_safe(timeout, prev, &eloop.timeout, dl_list_for_each_safe(timeout, prev, &eloop.timeout,
struct eloop_timeout, list) { struct eloop_timeout, list) {
dl_list_del(&timeout->list); eloop_remove_timeout(timeout);
free(timeout);
} }
free(eloop.readers); os_free(eloop.readers);
free(eloop.signals); os_free(eloop.signals);
} }