input/Curl: use std::forward_list instead of GSList
This commit is contained in:
@@ -42,6 +42,8 @@ extern "C" {
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <forward_list>
|
||||||
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
@@ -137,7 +139,7 @@ static struct {
|
|||||||
* A linked list of all active HTTP requests. An active
|
* A linked list of all active HTTP requests. An active
|
||||||
* request is one that doesn't have the "eof" flag set.
|
* request is one that doesn't have the "eof" flag set.
|
||||||
*/
|
*/
|
||||||
GSList *requests;
|
std::forward_list<input_curl *> requests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The GMainLoop source used to poll all CURL file
|
* The GMainLoop source used to poll all CURL file
|
||||||
@@ -151,7 +153,7 @@ static struct {
|
|||||||
guint source_id;
|
guint source_id;
|
||||||
|
|
||||||
/** a linked list of all registered GPollFD objects */
|
/** a linked list of all registered GPollFD objects */
|
||||||
GSList *fds;
|
std::forward_list<GPollFD> fds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Did CURL give us a timeout? If yes, then we need to call
|
* Did CURL give us a timeout? If yes, then we need to call
|
||||||
@@ -183,11 +185,9 @@ input_curl_find_request(CURL *easy)
|
|||||||
{
|
{
|
||||||
assert(io_thread_inside());
|
assert(io_thread_inside());
|
||||||
|
|
||||||
for (GSList *i = curl.requests; i != NULL; i = g_slist_next(i)) {
|
for (auto c : curl.requests)
|
||||||
struct input_curl *c = (struct input_curl *)i->data;
|
|
||||||
if (c->easy == easy)
|
if (c->easy == easy)
|
||||||
return c;
|
return c;
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -260,17 +260,14 @@ curl_update_fds(void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GSList *fds = curl.fds;
|
for (auto prev = curl.fds.before_begin(), end = curl.fds.end(),
|
||||||
curl.fds = NULL;
|
i = std::next(prev);
|
||||||
|
i != end; i = std::next(prev)) {
|
||||||
while (fds != NULL) {
|
const auto poll_fd = &*i;
|
||||||
GPollFD *poll_fd = (GPollFD *)fds->data;
|
|
||||||
gushort events = input_curl_fd_events(poll_fd->fd, &rfds,
|
|
||||||
&wfds, &efds);
|
|
||||||
|
|
||||||
assert(poll_fd->events != 0);
|
assert(poll_fd->events != 0);
|
||||||
|
|
||||||
fds = g_slist_remove(fds, poll_fd);
|
gushort events = input_curl_fd_events(poll_fd->fd, &rfds,
|
||||||
|
&wfds, &efds);
|
||||||
|
|
||||||
if (events != poll_fd->events)
|
if (events != poll_fd->events)
|
||||||
g_source_remove_poll(curl.source, poll_fd);
|
g_source_remove_poll(curl.source, poll_fd);
|
||||||
@@ -281,20 +278,20 @@ curl_update_fds(void)
|
|||||||
g_source_add_poll(curl.source, poll_fd);
|
g_source_add_poll(curl.source, poll_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl.fds = g_slist_prepend(curl.fds, poll_fd);
|
prev = i;
|
||||||
} else {
|
} else {
|
||||||
g_free(poll_fd);
|
curl.fds.erase_after(prev);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int fd = 0; fd <= max_fd; ++fd) {
|
for (int fd = 0; fd <= max_fd; ++fd) {
|
||||||
gushort events = input_curl_fd_events(fd, &rfds, &wfds, &efds);
|
gushort events = input_curl_fd_events(fd, &rfds, &wfds, &efds);
|
||||||
if (events != 0) {
|
if (events != 0) {
|
||||||
GPollFD *poll_fd = g_new(GPollFD, 1);
|
curl.fds.push_front(GPollFD());
|
||||||
|
const auto poll_fd = &curl.fds.front();
|
||||||
poll_fd->fd = fd;
|
poll_fd->fd = fd;
|
||||||
poll_fd->events = events;
|
poll_fd->events = events;
|
||||||
g_source_add_poll(curl.source, poll_fd);
|
g_source_add_poll(curl.source, poll_fd);
|
||||||
curl.fds = g_slist_prepend(curl.fds, poll_fd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -310,7 +307,7 @@ input_curl_easy_add(struct input_curl *c, GError **error_r)
|
|||||||
assert(c->easy != NULL);
|
assert(c->easy != NULL);
|
||||||
assert(input_curl_find_request(c->easy) == NULL);
|
assert(input_curl_find_request(c->easy) == NULL);
|
||||||
|
|
||||||
curl.requests = g_slist_prepend(curl.requests, c);
|
curl.requests.push_front(c);
|
||||||
|
|
||||||
CURLMcode mcode = curl_multi_add_handle(curl.multi, c->easy);
|
CURLMcode mcode = curl_multi_add_handle(curl.multi, c->easy);
|
||||||
if (mcode != CURLM_OK) {
|
if (mcode != CURLM_OK) {
|
||||||
@@ -375,7 +372,7 @@ input_curl_easy_free(struct input_curl *c)
|
|||||||
if (c->easy == NULL)
|
if (c->easy == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
curl.requests = g_slist_remove(curl.requests, c);
|
curl.requests.remove(c);
|
||||||
|
|
||||||
curl_multi_remove_handle(curl.multi, c->easy);
|
curl_multi_remove_handle(curl.multi, c->easy);
|
||||||
curl_easy_cleanup(c->easy);
|
curl_easy_cleanup(c->easy);
|
||||||
@@ -423,9 +420,8 @@ input_curl_abort_all_requests(GError *error)
|
|||||||
assert(io_thread_inside());
|
assert(io_thread_inside());
|
||||||
assert(error != NULL);
|
assert(error != NULL);
|
||||||
|
|
||||||
while (curl.requests != NULL) {
|
while (!curl.requests.empty()) {
|
||||||
struct input_curl *c =
|
struct input_curl *c = curl.requests.front();
|
||||||
(struct input_curl *)curl.requests->data;
|
|
||||||
assert(c->postponed_error == NULL);
|
assert(c->postponed_error == NULL);
|
||||||
|
|
||||||
input_curl_easy_free(c);
|
input_curl_easy_free(c);
|
||||||
@@ -586,11 +582,9 @@ input_curl_source_check(G_GNUC_UNUSED GSource *source)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (GSList *i = curl.fds; i != NULL; i = i->next) {
|
for (const auto &i : curl.fds)
|
||||||
GPollFD *poll_fd = (GPollFD *)i->data;
|
if (i.revents != 0)
|
||||||
if (poll_fd->revents != 0)
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -682,7 +676,7 @@ curl_destroy_sources(G_GNUC_UNUSED gpointer data)
|
|||||||
static void
|
static void
|
||||||
input_curl_finish(void)
|
input_curl_finish(void)
|
||||||
{
|
{
|
||||||
assert(curl.requests == NULL);
|
assert(curl.requests.empty());
|
||||||
|
|
||||||
io_thread_call(curl_destroy_sources, NULL);
|
io_thread_call(curl_destroy_sources, NULL);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user