diff options
author | Nick Mathewson <nickm@torproject.org> | 2008-04-25 01:18:08 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2008-04-25 01:18:08 +0000 |
commit | 49868b618a55c375d6c378edc174cd81e478f213 (patch) | |
tree | 43d062032c9e61bc720d71e79de3560fbb61b05f | |
parent | a55a67d56e989b9f7fa2027f2c75bec8a55a9a87 (diff) | |
download | libevent-49868b618a55c375d6c378edc174cd81e478f213.tar.gz |
r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Rename internal memory management functions from event_malloc() etc to mm_malloc() etc.
svn:r725
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | buffer.c | 20 | ||||
-rw-r--r-- | devpoll.c | 32 | ||||
-rw-r--r-- | epoll.c | 20 | ||||
-rw-r--r-- | evbuffer.c | 8 | ||||
-rw-r--r-- | evdns.c | 309 | ||||
-rw-r--r-- | event.c | 66 | ||||
-rw-r--r-- | event_tagging.c | 2 | ||||
-rw-r--r-- | evport.c | 14 | ||||
-rw-r--r-- | evrpc.c | 58 | ||||
-rw-r--r-- | http.c | 130 | ||||
-rw-r--r-- | kqueue.c | 30 | ||||
-rw-r--r-- | mm-internal.h | 10 | ||||
-rw-r--r-- | poll.c | 20 | ||||
-rw-r--r-- | select.c | 28 | ||||
-rw-r--r-- | signal.c | 12 |
16 files changed, 410 insertions, 350 deletions
@@ -68,6 +68,7 @@ Changes in current version: o Make name_from_addr() threadsafe in http.c o Add new thread-safe interfaces to evdns functions. o Make all event_tagging interfaces threadsafe. + o Rename internal memory management functions. Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. @@ -86,7 +86,7 @@ evbuffer_chain_new(size_t size) to_alloc <<= 1; /* we get everything in one chunk */ - if ((chain = event_malloc(to_alloc)) == NULL) + if ((chain = mm_malloc(to_alloc)) == NULL) return (NULL); memset(chain, 0, EVBUFFER_CHAIN_SIZE); @@ -101,7 +101,7 @@ evbuffer_new(void) { struct evbuffer *buffer; - buffer = event_calloc(1, sizeof(struct evbuffer)); + buffer = mm_calloc(1, sizeof(struct evbuffer)); return (buffer); } @@ -112,9 +112,9 @@ evbuffer_free(struct evbuffer *buffer) struct evbuffer_chain *chain, *next; for (chain = buffer->first; chain != NULL; chain = next) { next = chain->next; - event_free(chain); + mm_free(chain); } - event_free(buffer); + mm_free(buffer); } size_t @@ -218,7 +218,7 @@ evbuffer_drain(struct evbuffer *buf, size_t len) for (chain = buf->first; chain != NULL; chain = next) { next = chain->next; - event_free(chain); + mm_free(chain); } ZERO_CHAIN(buf); @@ -229,7 +229,7 @@ evbuffer_drain(struct evbuffer *buf, size_t len) next = chain->next; len -= chain->off; - event_free(chain); + mm_free(chain); } buf->first = chain; @@ -269,7 +269,7 @@ evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen) tmp = chain; chain = chain->next; - event_free(tmp); + mm_free(tmp); } buf->first = chain; @@ -404,7 +404,7 @@ evbuffer_pullup(struct evbuffer *buf, int size) size -= chain->off; buffer += chain->off; - event_free(chain); + mm_free(chain); } if (chain != NULL) { @@ -582,7 +582,7 @@ evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out, return (NULL); } - if ((line = event_malloc(n_to_copy+1)) == NULL) { + if ((line = mm_malloc(n_to_copy+1)) == NULL) { event_warn("%s: out of memory\n", __func__); evbuffer_drain(buffer, n_to_copy + extra_drain); return (NULL); @@ -762,7 +762,7 @@ evbuffer_expand(struct evbuffer *buf, size_t datlen) buf->previous_to_last->next = tmp; buf->last = tmp; - event_free(chain); + mm_free(chain); return (0); } @@ -135,7 +135,7 @@ devpoll_init(struct event_base *base) if (getenv("EVENT_NODEVPOLL")) return (NULL); - if (!(devpollop = event_calloc(1, sizeof(struct devpollop)))) + if (!(devpollop = mm_calloc(1, sizeof(struct devpollop)))) return (NULL); if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && @@ -145,35 +145,35 @@ devpoll_init(struct event_base *base) /* Initialize the kernel queue */ if ((dpfd = open("/dev/poll", O_RDWR)) == -1) { event_warn("open: /dev/poll"); - event_free(devpollop); + mm_free(devpollop); return (NULL); } devpollop->dpfd = dpfd; /* Initialize fields */ - devpollop->events = event_calloc(nfiles, sizeof(struct pollfd)); + devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd)); if (devpollop->events == NULL) { - event_free(devpollop); + mm_free(devpollop); close(dpfd); return (NULL); } devpollop->nevents = nfiles; - devpollop->fds = event_calloc(nfiles, sizeof(struct evdevpoll)); + devpollop->fds = mm_calloc(nfiles, sizeof(struct evdevpoll)); if (devpollop->fds == NULL) { - event_free(devpollop->events); - event_free(devpollop); + mm_free(devpollop->events); + mm_free(devpollop); close(dpfd); return (NULL); } devpollop->nfds = nfiles; - devpollop->changes = event_calloc(nfiles, sizeof(struct pollfd)); + devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd)); if (devpollop->changes == NULL) { - event_free(devpollop->fds); - event_free(devpollop->events); - event_free(devpollop); + mm_free(devpollop->fds); + mm_free(devpollop->events); + mm_free(devpollop); close(dpfd); return (NULL); } @@ -196,7 +196,7 @@ devpoll_recalc(struct event_base *base, void *arg, int max) while (nfds < max) nfds <<= 1; - fds = event_realloc(devpollop->fds, nfds * sizeof(struct evdevpoll)); + fds = mm_realloc(devpollop->fds, nfds * sizeof(struct evdevpoll)); if (fds == NULL) { event_warn("realloc"); return (-1); @@ -405,14 +405,14 @@ devpoll_dealloc(struct event_base *base, void *arg) evsignal_dealloc(base); if (devpollop->fds) - event_free(devpollop->fds); + mm_free(devpollop->fds); if (devpollop->events) - event_free(devpollop->events); + mm_free(devpollop->events); if (devpollop->changes) - event_free(devpollop->changes); + mm_free(devpollop->changes); if (devpollop->dpfd >= 0) close(devpollop->dpfd); memset(devpollop, 0, sizeof(struct devpollop)); - event_free(devpollop); + mm_free(devpollop); } @@ -136,23 +136,23 @@ epoll_init(struct event_base *base) FD_CLOSEONEXEC(epfd); - if (!(epollop = event_calloc(1, sizeof(struct epollop)))) + if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) return (NULL); epollop->epfd = epfd; /* Initalize fields */ - epollop->events = event_malloc(nfiles * sizeof(struct epoll_event)); + epollop->events = mm_malloc(nfiles * sizeof(struct epoll_event)); if (epollop->events == NULL) { - event_free(epollop); + mm_free(epollop); return (NULL); } epollop->nevents = nfiles; - epollop->fds = event_calloc(nfiles, sizeof(struct evepoll)); + epollop->fds = mm_calloc(nfiles, sizeof(struct evepoll)); if (epollop->fds == NULL) { - event_free(epollop->events); - event_free(epollop); + mm_free(epollop->events); + mm_free(epollop); return (NULL); } epollop->nfds = nfiles; @@ -175,7 +175,7 @@ epoll_recalc(struct event_base *base, void *arg, int max) while (nfds < max) nfds <<= 1; - fds = event_realloc(epollop->fds, nfds * sizeof(struct evepoll)); + fds = mm_realloc(epollop->fds, nfds * sizeof(struct evepoll)); if (fds == NULL) { event_warn("realloc"); return (-1); @@ -360,12 +360,12 @@ epoll_dealloc(struct event_base *base, void *arg) evsignal_dealloc(base); if (epollop->fds) - event_free(epollop->fds); + mm_free(epollop->fds); if (epollop->events) - event_free(epollop->events); + mm_free(epollop->events); if (epollop->epfd >= 0) close(epollop->epfd); memset(epollop, 0, sizeof(struct epollop)); - event_free(epollop); + mm_free(epollop); } @@ -233,17 +233,17 @@ bufferevent_new(evutil_socket_t fd, evbuffercb readcb, evbuffercb writecb, { struct bufferevent *bufev; - if ((bufev = event_calloc(1, sizeof(struct bufferevent))) == NULL) + if ((bufev = mm_calloc(1, sizeof(struct bufferevent))) == NULL) return (NULL); if ((bufev->input = evbuffer_new()) == NULL) { - event_free(bufev); + mm_free(bufev); return (NULL); } if ((bufev->output = evbuffer_new()) == NULL) { evbuffer_free(bufev->input); - event_free(bufev); + mm_free(bufev); return (NULL); } @@ -288,7 +288,7 @@ bufferevent_free(struct bufferevent *bufev) evbuffer_free(bufev->input); evbuffer_free(bufev->output); - event_free(bufev); + mm_free(bufev); } /* @@ -293,8 +293,16 @@ struct server_request { }; struct evdns_base { - struct request *req_head, *req_waiting_head; + /* An array of n_req_heads circular lists for inflight requests. + * Each inflight request req is in req_heads[req->trans_id % n_req_heads]. + */ + struct request **req_heads; + /* A circular list of requests that we're waiting to send, but haven't + * sent yet because there are too many requests inflight */ + struct request *req_waiting_head; + /* A circular list of nameservers. */ struct nameserver *server_head; + int n_req_heads; struct event_base *event_base; @@ -330,6 +338,8 @@ static struct evdns_base *current_base = NULL; ((struct server_request*) \ (((char*)(base_ptr) - OFFSET_OF(struct server_request, base)))) +#define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads]) + /* These are the timeout values for nameservers. If we find a nameserver is down */ /* we try to probe it at intervals as given below. Values are in seconds. */ static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}}; @@ -453,7 +463,8 @@ _evdns_log(int warn, const char *fmt, ...) /* failure */ static struct request * request_find_from_trans_id(struct evdns_base *base, u16 trans_id) { - struct request *req = base->req_head, *const started_at = base->req_head; + struct request *req = REQ_HEAD(base, trans_id); + struct request *const started_at = req; if (req) { do { @@ -511,6 +522,7 @@ static void nameserver_failed(struct nameserver *const ns, const char *msg) { struct request *req, *started_at; struct evdns_base *base = ns->base; + int i; /* if this nameserver has already been marked as failed */ /* then don't do anything */ if (!ns->state) return; @@ -544,16 +556,18 @@ nameserver_failed(struct nameserver *const ns, const char *msg) { /* trying to reassign requests to one */ if (!base->global_good_nameservers) return; - req = started_at = base->req_head; - if (req) { - do { - if (req->tx_count == 0 && req->ns == ns) { - /* still waiting to go out, can be moved */ - /* to another server */ - req->ns = nameserver_pick(base); - } - req = req->next; - } while (req != started_at); + for (i = 0; i < base->n_req_heads; ++i) { + req = started_at = base->req_heads[i]; + if (req) { + do { + if (req->tx_count == 0 && req->ns == ns) { + /* still waiting to go out, can be moved */ + /* to another server */ + req->ns = nameserver_pick(base); + } + req = req->next; + } while (req != started_at); + } } } @@ -601,13 +615,13 @@ request_finished(struct request *const req, struct request **head) { if (!req->request_appended) { /* need to free the request data on it's own */ - event_free(req->request); + mm_free(req->request); } else { /* the request data is appended onto the header */ /* so everything gets free()ed when we: */ } - event_free(req); + mm_free(req); evdns_requests_pump_waiting_queue(base); } @@ -665,7 +679,7 @@ evdns_requests_pump_waiting_queue(struct evdns_base *base) { req->ns = nameserver_pick(base); request_trans_id_set(req, transaction_id_pick(base)); - evdns_request_insert(req, &base->req_head); + evdns_request_insert(req, &REQ_HEAD(base, req->trans_id)); evdns_request_transmit(req); evdns_transmit(base); } @@ -757,19 +771,19 @@ reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) /* a new request was issued so this request is finished and */ /* the user callback will be made when that request (or a */ /* child of it) finishes. */ - request_finished(req, &req->base->req_head); + request_finished(req, &REQ_HEAD(req->base, req->trans_id)); return; } } /* all else failed. Pass the failure up */ reply_callback(req, 0, error, NULL); - request_finished(req, &req->base->req_head); + request_finished(req, &REQ_HEAD(req->base, req->trans_id)); } else { /* all ok, tell the user */ reply_callback(req, ttl, 0, reply); nameserver_up(req->ns); - request_finished(req, &req->base->req_head); + request_finished(req, &REQ_HEAD(req->base, req->trans_id)); } } @@ -986,7 +1000,7 @@ request_parse(u8 *packet, int length, struct evdns_server_port *port, struct soc if (flags & 0x8000) return -1; /* Must not be an answer. */ flags &= 0x0110; /* Only RD and CD get preserved. */ - server_req = event_malloc(sizeof(struct server_request)); + server_req = mm_malloc(sizeof(struct server_request)); if (server_req == NULL) return -1; memset(server_req, 0, sizeof(struct server_request)); @@ -996,7 +1010,7 @@ request_parse(u8 *packet, int length, struct evdns_server_port *port, struct soc server_req->base.flags = flags; server_req->base.nquestions = 0; - server_req->base.questions = event_malloc(sizeof(struct evdns_server_question *) * questions); + server_req->base.questions = mm_malloc(sizeof(struct evdns_server_question *) * questions); if (server_req->base.questions == NULL) goto err; @@ -1009,7 +1023,7 @@ request_parse(u8 *packet, int length, struct evdns_server_port *port, struct soc GET16(type); GET16(class); namelen = strlen(tmp_name); - q = event_malloc(sizeof(struct evdns_server_question) + namelen); + q = mm_malloc(sizeof(struct evdns_server_question) + namelen); if (!q) goto err; q->type = type; @@ -1036,10 +1050,10 @@ err: if (server_req) { if (server_req->base.questions) { for (i = 0; i < server_req->base.nquestions; ++i) - event_free(server_req->base.questions[i]); - event_free(server_req->base.questions); + mm_free(server_req->base.questions[i]); + mm_free(server_req->base.questions); } - event_free(server_req); + mm_free(server_req); } return -1; @@ -1106,20 +1120,12 @@ evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void)) static u16 transaction_id_pick(struct evdns_base *base) { for (;;) { - const struct request *req, *started_at; u16 trans_id = trans_id_function(); if (trans_id == 0xffff) continue; /* now check to see if that id is already inflight */ - req = started_at = base->req_head; - if (req) { - do { - if (req->trans_id == trans_id) break; - req = req->next; - } while (req != started_at); - } - /* we didn't find it, so this is a good id */ - if (req == started_at) return trans_id; + if (request_find_from_trans_id(base, trans_id) == NULL) + return trans_id; } } @@ -1312,7 +1318,7 @@ dnslabel_clear(struct dnslabel_table *table) { int i; for (i = 0; i < table->n_labels; ++i) - event_free(table->labels[i].v); + mm_free(table->labels[i].v); table->n_labels = 0; } @@ -1337,7 +1343,7 @@ dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos) int p; if (table->n_labels == MAX_LABELS) return (-1); - v = event_strdup(label); + v = mm_strdup(label); if (v == NULL) return (-1); p = table->n_labels++; @@ -1470,7 +1476,7 @@ struct evdns_server_port * evdns_add_server_port_with_base(struct event_base *base, int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data) { struct evdns_server_port *port; - if (!(port = event_malloc(sizeof(struct evdns_server_port)))) + if (!(port = mm_malloc(sizeof(struct evdns_server_port)))) return NULL; memset(port, 0, sizeof(struct evdns_server_port)); @@ -1537,12 +1543,12 @@ evdns_server_request_add_reply(struct evdns_server_request *_req, int section, c while (*itemp) { itemp = &((*itemp)->next); } - item = event_malloc(sizeof(struct server_reply_item)); + item = mm_malloc(sizeof(struct server_reply_item)); if (!item) return -1; item->next = NULL; - if (!(item->name = event_strdup(name))) { - event_free(item); + if (!(item->name = mm_strdup(name))) { + mm_free(item); return -1; } item->type = type; @@ -1553,16 +1559,16 @@ evdns_server_request_add_reply(struct evdns_server_request *_req, int section, c item->data = NULL; if (data) { if (item->is_name) { - if (!(item->data = event_strdup(data))) { - event_free(item->name); - event_free(item); + if (!(item->data = mm_strdup(data))) { + mm_free(item->name); + mm_free(item); return -1; } item->datalen = (u16)-1; } else { - if (!(item->data = event_malloc(datalen))) { - event_free(item->name); - event_free(item); + if (!(item->data = mm_malloc(datalen))) { + mm_free(item->name); + mm_free(item); return -1; } item->datalen = datalen; @@ -1711,7 +1717,7 @@ overflow: req->response_len = j; - if (!(req->response = event_malloc(req->response_len))) { + if (!(req->response = mm_malloc(req->response_len))) { server_request_free_answers(req); dnslabel_clear(&table); return (-1); @@ -1790,10 +1796,10 @@ server_request_free_answers(struct server_request *req) victim = *list; while (victim) { next = victim->next; - event_free(victim->name); + mm_free(victim->name); if (victim->data) - event_free(victim->data); - event_free(victim); + mm_free(victim->data); + mm_free(victim); victim = next; } *list = NULL; @@ -1808,8 +1814,8 @@ server_request_free(struct server_request *req) int i, rc=1; if (req->base.questions) { for (i = 0; i < req->base.nquestions; ++i) - event_free(req->base.questions[i]); - event_free(req->base.questions); + mm_free(req->base.questions[i]); + mm_free(req->base.questions); } if (req->port) { @@ -1823,7 +1829,7 @@ server_request_free(struct server_request *req) } if (req->response) { - event_free(req->response); + mm_free(req->response); } server_request_free_answers(req); @@ -1835,10 +1841,10 @@ server_request_free(struct server_request *req) if (rc == 0) { server_port_free(req->port); - event_free(req); + mm_free(req); return (1); } - event_free(req); + mm_free(req); return (0); } @@ -1900,7 +1906,7 @@ evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg) { if (req->tx_count >= req->base->global_max_retransmits) { /* this request has failed */ reply_callback(req, 0, DNS_ERR_TIMEOUT, NULL); - request_finished(req, &req->base->req_head); + request_finished(req, &REQ_HEAD(req->base, req->trans_id)); } else { /* retransmit it */ evdns_request_transmit(req); @@ -2015,18 +2021,21 @@ nameserver_send_probe(struct nameserver *const ns) { static int evdns_transmit(struct evdns_base *base) { char did_try_to_transmit = 0; + int i; - if (base->req_head) { - struct request *const started_at = base->req_head, *req = base->req_head; - /* first transmit all the requests which are currently waiting */ - do { - if (req->transmit_me) { - did_try_to_transmit = 1; - evdns_request_transmit(req); - } + for (i = 0; i < base->n_req_heads; ++i) { + if (base->req_heads[i]) { + struct request *const started_at = base->req_heads[i], *req = started_at; + /* first transmit all the requests which are currently waiting */ + do { + if (req->transmit_me) { + did_try_to_transmit = 1; + evdns_request_transmit(req); + } - req = req->next; - } while (req != started_at); + req = req->next; + } while (req != started_at); + } } return did_try_to_transmit; @@ -2058,7 +2067,7 @@ int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base) { struct nameserver *server = base->server_head, *started_at = base->server_head; - struct request *req = base->req_head, *req_started_at = base->req_head; + int i; if (!server) return 0; @@ -2069,7 +2078,7 @@ evdns_base_clear_nameservers_and_suspend(struct evdns_base *base) (void) evtimer_del(&server->timeout_event); if (server->socket >= 0) CLOSE_SOCKET(server->socket); - event_free(server); + mm_free(server); if (next == started_at) break; server = next; @@ -2077,28 +2086,33 @@ evdns_base_clear_nameservers_and_suspend(struct evdns_base *base) base->server_head = NULL; base->global_good_nameservers = 0; - while (req) { - struct request *next = req->next; - req->tx_count = req->reissue_count = 0; - req->ns = NULL; - /* ???? What to do about searches? */ - (void) evtimer_del(&req->timeout_event); - req->trans_id = 0; - req->transmit_me = 0; - - base->global_requests_waiting++; - evdns_request_insert(req, &base->req_waiting_head); - /* We want to insert these suspended elements at the front of - * the waiting queue, since they were pending before any of - * the waiting entries were added. This is a circular list, - * so we can just shift the start back by one.*/ - base->req_waiting_head = base->req_waiting_head->prev; - - if (next == req_started_at) - break; - req = next; + for (i = 0; i < base->n_req_heads; ++i) { + struct request *req, *req_started_at; + req = req_started_at = base->req_heads[i]; + while (req) { + struct request *next = req->next; + req->tx_count = req->reissue_count = 0; + req->ns = NULL; + /* ???? What to do about searches? */ + (void) evtimer_del(&req->timeout_event); + req->trans_id = 0; + req->transmit_me = 0; + + base->global_requests_waiting++; + evdns_request_insert(req, &base->req_waiting_head); + /* We want to insert these suspended elements at the front of + * the waiting queue, since they were pending before any of + * the waiting entries were added. This is a circular list, + * so we can just shift the start back by one.*/ + base->req_waiting_head = base->req_waiting_head->prev; + + if (next == req_started_at) + break; + req = next; + } + base->req_heads[i] = NULL; } - base->req_head = NULL; + base->global_requests_inflight = 0; return 0; @@ -2140,7 +2154,7 @@ _evdns_nameserver_add_impl(struct evdns_base *base, unsigned long int address, i } while (server != started_at); } - ns = (struct nameserver *) event_malloc(sizeof(struct nameserver)); + ns = (struct nameserver *) mm_malloc(sizeof(struct nameserver)); if (!ns) return -1; memset(ns, 0, sizeof(struct nameserver)); @@ -2189,7 +2203,7 @@ _evdns_nameserver_add_impl(struct evdns_base *base, unsigned long int address, i out2: CLOSE_SOCKET(ns->socket); out1: - event_free(ns); + mm_free(ns); log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d", debug_ntoa(address), err); return err; } @@ -2280,7 +2294,7 @@ request_new(struct evdns_base *base, int type, const char *name, int flags, const u16 trans_id = issuing_now ? transaction_id_pick(base) : 0xffff; /* the request data is alloced in a single block with the header */ struct request *const req = - (struct request *) event_malloc(sizeof(struct request) + request_max_len); + (struct request *) mm_malloc(sizeof(struct request) + request_max_len); int rlen; (void) flags; @@ -2307,7 +2321,7 @@ request_new(struct evdns_base *base, int type, const char *name, int flags, return req; err1: - event_free(req); + mm_free(req); return NULL; } @@ -2317,7 +2331,7 @@ request_submit(struct request *const req) { if (req->ns) { /* if it has a nameserver assigned then this is going */ /* straight into the inflight queue */ - evdns_request_insert(req, &base->req_head); + evdns_request_insert(req, &REQ_HEAD(base, req->trans_id)); base->global_requests_inflight++; evdns_request_transmit(req); } else { @@ -2455,15 +2469,15 @@ search_state_decref(struct search_state *const state) { struct search_domain *next, *dom; for (dom = state->head; dom; dom = next) { next = dom->next; - event_free(dom); + mm_free(dom); } - event_free(state); + mm_free(state); } } static struct search_state * search_state_new(void) { - struct search_state *state = (struct search_state *) event_malloc(sizeof(struct search_state)); + struct search_state *state = (struct search_state *) mm_malloc(sizeof(struct search_state)); if (!state) return NULL; memset(state, 0, sizeof(struct search_state)); state->refcount = 1; @@ -2501,7 +2515,7 @@ search_postfix_add(struct evdns_base *base, const char *domain) { if (!base->global_search_state) return; base->global_search_state->num_domains++; - sdomain = (struct search_domain *) event_malloc(sizeof(struct search_domain) + domain_len); + sdomain = (struct search_domain *) mm_malloc(sizeof(struct search_domain) + domain_len); if (!sdomain) return; memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len); sdomain->next = base->global_search_state->head; @@ -2572,7 +2586,7 @@ search_make_new(const struct search_state *const state, int n, const char *const /* the actual postfix string is kept at the end of the structure */ const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain); const int postfix_len = dom->len; - char *const newname = (char *) event_malloc(base_len + need_to_append_dot + postfix_len + 1); + char *const newname = (char *) mm_malloc(base_len + need_to_append_dot + postfix_len + 1); if (!newname) return NULL; memcpy(newname, base_name, base_len); if (need_to_append_dot) newname[base_len] = '.'; @@ -2603,11 +2617,11 @@ search_request_new(struct evdns_base *base, int type, const char *const name, in char *const new_name = search_make_new(base->global_search_state, 0, name); if (!new_name) return 1; req = request_new(base, type, new_name, flags, user_callback, user_arg); - event_free(new_name); + mm_free(new_name); if (!req) return 1; req->search_index = 0; } - req->search_origname = event_strdup(name); + req->search_origname = mm_strdup(name); req->search_state = base->global_search_state; req->search_flags = flags; base->global_search_state->refcount++; @@ -2653,7 +2667,7 @@ search_try_next(struct request *const req) { if (!new_name) return 1; log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, req->search_index); newreq = request_new(base, req->request_type, new_name, req->search_flags, req->user_callback, req->user_pointer); - event_free(new_name); + mm_free(new_name); if (!newreq) return 1; newreq->search_origname = req->search_origname; req->search_origname = NULL; @@ -2674,7 +2688,7 @@ search_request_finished(struct request *const req) { req->search_state = NULL; } if (req->search_origname) { - event_free(req->search_origname); + mm_free(req->search_origname); req->search_origname = NULL; } } @@ -2735,6 +2749,43 @@ strtoint_clipped(const char *const str, int min, int max) return r; } +static int +evdns_base_set_max_requests_inflight(struct evdns_base *base, int maxinflight) +{ + int old_n_heads = base->n_req_heads, n_heads; + struct request **old_heads = base->req_heads, **new_heads, *req; + int i; + if (maxinflight < 1) + maxinflight = 1; + n_heads = (maxinflight+4) / 5; + assert(n_heads > 0); + new_heads = mm_malloc(n_heads * sizeof(struct request*)); + if (!new_heads) + return (-1); + for (i=0; i < n_heads; ++i) + new_heads[i] = NULL; + if (old_heads) { + for (i = 0; i < old_n_heads; ++i) { + while (old_heads[i]) { + req = old_heads[i]; + if (req->next == req) { + old_heads[i] = NULL; + } else { + old_heads[i] = req->next; + req->next->prev = req->prev; + req->prev->next = req->next; + } + evdns_request_insert(req, &new_heads[req->trans_id % n_heads]); + } + } + mm_free(old_heads); + } + base->req_heads = new_heads; + base->n_req_heads = n_heads; + base->global_max_requests_inflight = maxinflight; + return (0); +} + /* exported function */ int evdns_base_set_option(struct evdns_base *base, @@ -2767,7 +2818,7 @@ evdns_base_set_option(struct evdns_base *base, if (!(flags & DNS_OPTION_MISC)) return 0; log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d", maxinflight); - base->global_max_requests_inflight = maxinflight; + evdns_base_set_max_requests_inflight(base, maxinflight); } else if (!strncmp(option, "attempts:", 9)) { int retries = strtoint(val); if (retries == -1) return -1; @@ -2858,7 +2909,7 @@ evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *con } if (st.st_size > 65535) { err = 3; goto out1; } /* no resolv.conf should be any bigger */ - resolv = (u8 *) event_malloc((size_t)st.st_size + 1); + resolv = (u8 *) mm_malloc((size_t)st.st_size + 1); if (!resolv) { err = 4; goto out1; } n = 0; @@ -2894,7 +2945,7 @@ evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *con } out2: - event_free(resolv); + mm_free(resolv); out1: close(fd); return err; @@ -2918,12 +2969,12 @@ evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) { addr = ips; while (ISDIGIT(*ips) || *ips == '.' || *ips == ':') ++ips; - buf = event_malloc(ips-addr+1); + buf = mm_malloc(ips-addr+1); if (!buf) return 4; memcpy(buf, addr, ips-addr); buf[ips-addr] = '\0'; r = evdns_nameserver_ip_add(buf); - event_free(buf); + mm_free(buf); if (r) return r; } return 0; @@ -2956,7 +3007,7 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base) goto done; } - buf = event_malloc(size); + buf = mm_malloc(size); if (!buf) { status = 4; goto done; } fixed = buf; r = fn(fixed, &size); @@ -2965,8 +3016,8 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base) goto done; } if (r != ERROR_SUCCESS) { - event_free(buf); - buf = event_malloc(size); + mm_free(buf); + buf = mm_malloc(size); if (!buf) { status = 4; goto done; } fixed = buf; r = fn(fixed, &size); @@ -3002,7 +3053,7 @@ load_nameservers_with_getnetworkparams(struct evdns_base *base) done: if (buf) - event_free(buf); + mm_free(buf); if (handle) FreeLibrary(handle); return status; @@ -3018,7 +3069,7 @@ config_nameserver_from_reg_key(struct evdns_base *base, HKEY key, const char *su if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz) != ERROR_MORE_DATA) return -1; - if (!(buf = event_malloc(bufsz))) + if (!(buf = mm_malloc(bufsz))) return -1; if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz) @@ -3026,7 +3077,7 @@ config_nameserver_from_reg_key(struct evdns_base *base, HKEY key, const char *su status = evdns_nameserver_ip_add_line(base,buf); } - event_free(buf); + mm_free(buf); return status; } @@ -3101,16 +3152,21 @@ struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers) { struct evdns_base *base; - base = event_malloc(sizeof(struct evdns_base)); + base = mm_malloc(sizeof(struct evdns_base)); if (base == NULL) return (NULL); memset(base, 0, sizeof(struct evdns_base)); - base->req_head = base->req_waiting_head = NULL; + base->req_waiting_head = NULL; + + /* Set max requests inflight and allocate req_heads. */ + base->req_heads = NULL; + evdns_base_set_max_requests_inflight(base, 64); + base->server_head = NULL; base->event_base = event_base; base->global_good_nameservers = base->global_requests_inflight = base->global_requests_waiting = 0; - base->global_max_requests_inflight = 64; + base->global_timeout.tv_sec = 5; base->global_timeout.tv_usec = 0; base->global_max_reissues = 1; @@ -3168,11 +3224,14 @@ evdns_base_free(struct evdns_base *base, int fail_requests) { struct nameserver *server, *server_next; struct search_domain *dom, *dom_next; + int i; - while (base->req_head) { - if (fail_requests) - reply_callback(base->req_head, 0, DNS_ERR_SHUTDOWN, NULL); - request_finished(base->req_head, &base->req_head); + for (i = 0; i < base->n_req_heads; ++i) { + while (base->req_heads[i]) { + if (fail_requests) + reply_callback(base->req_heads[i], 0, DNS_ERR_SHUTDOWN, NULL); + request_finished(base->req_heads[i], &REQ_HEAD(base, base->req_heads[i]->trans_id)); + } } while (base->req_waiting_head) { if (fail_requests) @@ -3188,7 +3247,7 @@ evdns_base_free(struct evdns_base *base, int fail_requests) (void) event_del(&server->event); if (server->state == 0) (void) event_del(&server->timeout_event); - event_free(server); + mm_free(server); if (server_next == base->server_head) break; } @@ -3198,12 +3257,12 @@ evdns_base_free(struct evdns_base *base, int fail_requests) if (base->global_search_state) { for (dom = base->global_search_state->head; dom; dom = dom_next) { dom_next = dom->next; - event_free(dom); + mm_free(dom); } - event_free(base->global_search_state); + mm_free(base->global_search_state); base->global_search_state = NULL; } - event_free(base); + mm_free(base); } void @@ -183,7 +183,7 @@ event_base_new(void) int i; struct event_base *base; - if ((base = event_calloc(1, sizeof(struct event_base))) == NULL) + if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) event_err(1, "%s: calloc", __func__); event_sigcb = NULL; @@ -274,12 +274,12 @@ event_base_free(struct event_base *base) min_heap_dtor(&base->timeheap); for (i = 0; i < base->nactivequeues; ++i) - event_free(base->activequeues[i]); - event_free(base->activequeues); + mm_free(base->activequeues[i]); + mm_free(base->activequeues); assert(TAILQ_EMPTY(&base->eventqueue)); - event_free(base); + mm_free(base); } /* reinitialized the event base after a fork */ @@ -326,21 +326,21 @@ event_base_priority_init(struct event_base *base, int npriorities) if (base->nactivequeues && npriorities != base->nactivequeues) { for (i = 0; i < base->nactivequeues; ++i) { - event_free(base->activequeues[i]); + mm_free(base->activequeues[i]); } - event_free(base->activequeues); + mm_free(base->activequeues); } /* Allocate our priority queues */ base->nactivequeues = npriorities; - base->activequeues = (struct event_list **)event_calloc( + base->activequeues = (struct event_list **)mm_calloc( base->nactivequeues, npriorities * sizeof(struct event_list *)); if (base->activequeues == NULL) event_err(1, "%s: calloc", __func__); for (i = 0; i < base->nactivequeues; ++i) { - base->activequeues[i] = event_malloc(sizeof(struct event_list)); + base->activequeues[i] = mm_malloc(sizeof(struct event_list)); if (base->activequeues[i] == NULL) event_err(1, "%s: malloc", __func__); TAILQ_INIT(base->activequeues[i]); @@ -568,7 +568,7 @@ event_once_cb(evutil_socket_t fd, short events, void *arg) struct event_once *eonce = arg; (*eonce->cb)(fd, events, eonce->arg); - event_free(eonce); + mm_free(eonce); } /* not threadsafe, event scheduled once. */ @@ -594,7 +594,7 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, if (events & EV_SIGNAL) return (-1); - if ((eonce = event_calloc(1, sizeof(struct event_once))) == NULL) + if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL) return (-1); eonce->cb = callback; @@ -613,7 +613,7 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, event_set(&eonce->ev, fd, events, event_once_cb, eonce); } else { /* Bad event combination */ - event_free(eonce); + mm_free(eonce); return (-1); } @@ -621,7 +621,7 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, if (res == 0) res = event_add(&eonce->ev, tv); if (res != 0) { - event_free(eonce); + mm_free(eonce); return (res); } @@ -1089,25 +1089,25 @@ event_get_method(void) return (current_base->evsel->name); } -static void *(*_event_malloc_fn)(size_t sz) = NULL; -static void *(*_event_realloc_fn)(void *p, size_t sz) = NULL; -static void (*_event_free_fn)(void *p) = NULL; +static void *(*_mm_malloc_fn)(size_t sz) = NULL; +static void *(*_mm_realloc_fn)(void *p, size_t sz) = NULL; +static void (*_mm_free_fn)(void *p) = NULL; void * -event_malloc(size_t sz) +mm_malloc(size_t sz) { - if (_event_malloc_fn) - return _event_malloc_fn(sz); + if (_mm_malloc_fn) + return _mm_malloc_fn(sz); else return malloc(sz); } void * -event_calloc(size_t count, size_t size) +mm_calloc(size_t count, size_t size) { - if (_event_malloc_fn) { + if (_mm_malloc_fn) { size_t sz = count * size; - void *p = _event_malloc_fn(sz); + void *p = _mm_malloc_fn(sz); if (p) memset(p, 0, sz); return p; @@ -1116,11 +1116,11 @@ event_calloc(size_t count, size_t size) } char * -event_strdup(const char *str) +mm_strdup(const char *str) { - if (_event_malloc_fn) { + if (_mm_malloc_fn) { size_t ln = strlen(str); - void *p = _event_malloc_fn(ln+1); + void *p = _mm_malloc_fn(ln+1); if (p) memcpy(p, str, ln+1); return p; @@ -1133,19 +1133,19 @@ event_strdup(const char *str) } void * -event_realloc(void *ptr, size_t sz) +mm_realloc(void *ptr, size_t sz) { - if (_event_realloc_fn) - return _event_realloc_fn(ptr, sz); + if (_mm_realloc_fn) + return _mm_realloc_fn(ptr, sz); else return realloc(ptr, sz); } void -event_free(void *ptr) +mm_free(void *ptr) { - if (_event_realloc_fn) - _event_free_fn(ptr); + if (_mm_realloc_fn) + _mm_free_fn(ptr); else free(ptr); } @@ -1155,9 +1155,9 @@ event_set_mem_functions(void *(*malloc_fn)(size_t sz), void *(*realloc_fn)(void *ptr, size_t sz), void (*free_fn)(void *ptr)) { - _event_malloc_fn = malloc_fn; - _event_realloc_fn = realloc_fn; - _event_free_fn = free_fn; + _mm_malloc_fn = malloc_fn; + _mm_realloc_fn = realloc_fn; + _mm_free_fn = free_fn; } /* support for threading */ diff --git a/event_tagging.c b/event_tagging.c index 87016523..62fc4b22 100644 --- a/event_tagging.c +++ b/event_tagging.c @@ -445,7 +445,7 @@ evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag, tag != need_tag) return (-1); - *pstring = event_malloc(tag_len + 1); + *pstring = mm_malloc(tag_len + 1); if (*pstring == NULL) event_err(1, "%s: malloc", __func__); evbuffer_remove(evbuf, *pstring, tag_len); @@ -147,21 +147,21 @@ evport_init(struct event_base *base) if (getenv("EVENT_NOEVPORT")) return (NULL); - if (!(evpd = event_calloc(1, sizeof(struct evport_data)))) + if (!(evpd = mm_calloc(1, sizeof(struct evport_data)))) return (NULL); if ((evpd->ed_port = port_create()) == -1) { - event_free(evpd); + mm_free(evpd); return (NULL); } /* * Initialize file descriptor structure */ - evpd->ed_fds = event_calloc(DEFAULT_NFDS, sizeof(struct fd_info)); + evpd->ed_fds = mm_calloc(DEFAULT_NFDS, sizeof(struct fd_info)); if (evpd->ed_fds == NULL) { close(evpd->ed_port); - event_free(evpd); + mm_free(evpd); return (NULL); } evpd->ed_nevents = DEFAULT_NFDS; @@ -242,7 +242,7 @@ grow(struct evport_data *epdp, int factor) check_evportop(epdp); - tmp = event_realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize); + tmp = mm_realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize); if (NULL == tmp) return -1; epdp->ed_fds = tmp; @@ -509,6 +509,6 @@ evport_dealloc(struct event_base *base, void *arg) close(evpd->ed_port); if (evpd->ed_fds) - event_free(evpd->ed_fds); - event_free(evpd); + mm_free(evpd->ed_fds); + mm_free(evpd); } @@ -66,7 +66,7 @@ struct evrpc_base * evrpc_init(struct evhttp *http_server) { - struct evrpc_base* base = event_calloc(1, sizeof(struct evrpc_base)); + struct evrpc_base* base = mm_calloc(1, sizeof(struct evrpc_base)); if (base == NULL) return (NULL); @@ -99,7 +99,7 @@ evrpc_free(struct evrpc_base *base) while ((hook = TAILQ_FIRST(&base->output_hooks)) != NULL) { assert(evrpc_remove_hook(base, OUTPUT, hook)); } - event_free(base); + mm_free(base); } void * @@ -122,7 +122,7 @@ evrpc_add_hook(void *vbase, assert(hook_type == INPUT || hook_type == OUTPUT); } - hook = event_calloc(1, sizeof(struct evrpc_hook)); + hook = mm_calloc(1, sizeof(struct evrpc_hook)); assert(hook != NULL); hook->process = cb; @@ -139,7 +139,7 @@ evrpc_remove_hook_internal(struct evrpc_hook_list *head, void *handle) TAILQ_FOREACH(hook, head, next) { if (hook == handle) { TAILQ_REMOVE(head, hook, next); - event_free(hook); + mm_free(hook); return (1); } } @@ -201,7 +201,7 @@ evrpc_construct_uri(const char *uri) int constructed_uri_len; constructed_uri_len = strlen(EVRPC_URI_PREFIX) + strlen(uri) + 1; - if ((constructed_uri = event_malloc(constructed_uri_len)) == NULL) + if ((constructed_uri = mm_malloc(constructed_uri_len)) == NULL) event_err(1, "%s: failed to register rpc at %s", __func__, uri); memcpy(constructed_uri, EVRPC_URI_PREFIX, strlen(EVRPC_URI_PREFIX)); @@ -228,7 +228,7 @@ evrpc_register_rpc(struct evrpc_base *base, struct evrpc *rpc, evrpc_request_cb, rpc); - event_free(constructed_uri); + mm_free(constructed_uri); return (0); } @@ -250,15 +250,15 @@ evrpc_unregister_rpc(struct evrpc_base *base, const char *name) } TAILQ_REMOVE(&base->registered_rpcs, rpc, next); - event_free((char *)rpc->uri); - event_free(rpc); + mm_free((char *)rpc->uri); + mm_free(rpc); registered_uri = evrpc_construct_uri(name); /* remove the http server callback */ assert(evhttp_del_cb(base->http_server, registered_uri) == 0); - event_free(registered_uri); + mm_free(registered_uri); return (0); } @@ -277,7 +277,7 @@ evrpc_request_cb(struct evhttp_request *req, void *arg) EVBUFFER_LENGTH(req->input_buffer) <= 0) goto error; - rpc_state = event_calloc(1, sizeof(struct evrpc_req_generic)); + rpc_state = mm_calloc(1, sizeof(struct evrpc_req_generic)); if (rpc_state == NULL) goto error; rpc_state->rpc = rpc; @@ -377,7 +377,7 @@ evrpc_reqstate_free(struct evrpc_req_generic* rpc_state) rpc->reply_free(rpc_state->reply); if (rpc_state->rpc_data != NULL) evbuffer_free(rpc_state->rpc_data); - event_free(rpc_state); + mm_free(rpc_state); } static void @@ -473,7 +473,7 @@ static int evrpc_schedule_request(struct evhttp_connection *connection, struct evrpc_pool * evrpc_pool_new(struct event_base *base) { - struct evrpc_pool *pool = event_calloc(1, sizeof(struct evrpc_pool)); + struct evrpc_pool *pool = mm_calloc(1, sizeof(struct evrpc_pool)); if (pool == NULL) return (NULL); @@ -496,8 +496,8 @@ evrpc_request_wrapper_free(struct evrpc_request_wrapper *request) { if (request->hook_meta != NULL) evrpc_hook_context_free(request->hook_meta); - event_free(request->name); - event_free(request); + mm_free(request->name); + mm_free(request); } void @@ -515,7 +515,7 @@ evrpc_pool_free(struct evrpc_pool *pool) while ((pause = TAILQ_FIRST(&pool->paused_requests)) != NULL) { TAILQ_REMOVE(&pool->paused_requests, pause, next); - event_free(pause); + mm_free(pause); } while ((connection = TAILQ_FIRST(&pool->connections)) != NULL) { @@ -531,7 +531,7 @@ evrpc_pool_free(struct evrpc_pool *pool) assert(evrpc_remove_hook(pool, OUTPUT, hook)); } - event_free(pool); + mm_free(pool); } /* @@ -699,7 +699,7 @@ evrpc_schedule_request_closure(void *arg, enum EVRPC_HOOK_RESULT hook_res) /* start the request over the connection */ res = evhttp_make_request(connection, req, EVHTTP_REQ_POST, uri); - event_free(uri); + mm_free(uri); if (res == -1) goto error; @@ -719,7 +719,7 @@ evrpc_pause_request(void *vbase, void *ctx, void (*cb)(void *, enum EVRPC_HOOK_RESULT)) { struct _evrpc_hooks *base = vbase; - struct evrpc_hook_ctx *pause = event_malloc(sizeof(*pause)); + struct evrpc_hook_ctx *pause = mm_malloc(sizeof(*pause)); if (pause == NULL) return (-1); @@ -786,16 +786,16 @@ evrpc_send_request_generic( void *cbarg) { struct evrpc_request_wrapper *ctx = (struct evrpc_request_wrapper *) - event_malloc(sizeof(struct evrpc_request_wrapper)); + mm_malloc(sizeof(struct evrpc_request_wrapper)); if (ctx == NULL) return (NULL); ctx->pool = pool; ctx->hook_meta = NULL; ctx->evcon = NULL; - ctx->name = event_strdup(rpcname); + ctx->name = mm_strdup(rpcname); if (ctx->name == NULL) { - event_free(ctx); + mm_free(ctx); return (NULL); } ctx->cb = cb; @@ -945,9 +945,9 @@ evrpc_meta_data_free(struct evrpc_meta_list *meta_data) while ((entry = TAILQ_FIRST(meta_data)) != NULL) { TAILQ_REMOVE(meta_data, entry, next); - event_free(entry->key); - event_free(entry->data); - event_free(entry); + mm_free(entry->key); + mm_free(entry->data); + mm_free(entry); } } @@ -955,7 +955,7 @@ static struct evrpc_hook_meta * evrpc_hook_meta_new(void) { struct evrpc_hook_meta *ctx; - ctx = event_malloc(sizeof(struct evrpc_hook_meta)); + ctx = mm_malloc(sizeof(struct evrpc_hook_meta)); assert(ctx != NULL); TAILQ_INIT(&ctx->meta_data); @@ -978,7 +978,7 @@ static void evrpc_hook_context_free(struct evrpc_hook_meta *ctx) { evrpc_meta_data_free(&ctx->meta_data); - event_free(ctx); + mm_free(ctx); } /* Adds meta data */ @@ -993,10 +993,10 @@ evrpc_hook_add_meta(void *ctx, const char *key, if ((store = req->hook_meta) == NULL) store = req->hook_meta = evrpc_hook_meta_new(); - assert((meta = event_malloc(sizeof(struct evrpc_meta))) != NULL); - assert((meta->key = event_strdup(key)) != NULL); + assert((meta = mm_malloc(sizeof(struct evrpc_meta))) != NULL); + assert((meta->key = mm_strdup(key)) != NULL); meta->data_size = data_size; - assert((meta->data = event_malloc(data_size)) != NULL); + assert((meta->data = mm_malloc(data_size)) != NULL); memcpy(meta->data, data, data_size); TAILQ_INSERT_TAIL(&store->meta_data, meta, next); @@ -120,7 +120,7 @@ fake_getaddrinfo(const char *hostname, struct addrinfo *ai) ai->ai_socktype = SOCK_STREAM; ai->ai_protocol = 0; ai->ai_addrlen = sizeof(struct sockaddr_in); - if (NULL == (ai->ai_addr = event_malloc(ai->ai_addrlen))) + if (NULL == (ai->ai_addr = mm_malloc(ai->ai_addrlen))) return (-1); sa = (struct sockaddr_in*)ai->ai_addr; memset(sa, 0, ai->ai_addrlen); @@ -137,7 +137,7 @@ fake_getaddrinfo(const char *hostname, struct addrinfo *ai) static void fake_freeaddrinfo(struct addrinfo *ai) { - event_free(ai->ai_addr); + mm_free(ai->ai_addr); } #endif @@ -230,7 +230,7 @@ evhttp_htmlescape(const char *html) for (i = 0; i < old_size; ++i) new_size += strlen(html_replace(html[i], scratch_space)); - p = escaped_html = event_malloc(new_size + 1); + p = escaped_html = mm_malloc(new_size + 1); if (escaped_html == NULL) event_err(1, "%s: malloc(%d)", __func__, new_size + 1); for (i = 0; i < old_size; ++i) { @@ -540,7 +540,7 @@ evhttp_connection_incoming_fail(struct evhttp_request *req, default: /* xxx: probably should just error on default */ /* the callback looks at the uri to determine errors */ if (req->uri) { - event_free(req->uri); + mm_free(req->uri); req->uri = NULL; } @@ -708,12 +708,12 @@ evhttp_handle_chunked_read(struct evhttp_request *req, struct evbuffer *buf) break; /* the last chunk is on a new line? */ if (strlen(p) == 0) { - event_free(p); + mm_free(p); continue; } req->ntoread = evutil_strtoll(p, &endp, 16); error = *p == '\0' || (*endp != '\0' && *endp != ' '); - event_free(p); + mm_free(p); if (error) { /* could not get chunk size */ return (-1); @@ -859,10 +859,10 @@ evhttp_connection_free(struct evhttp_connection *evcon) EVUTIL_CLOSESOCKET(evcon->fd); if (evcon->bind_address != NULL) - event_free(evcon->bind_address); + mm_free(evcon->bind_address); if (evcon->address != NULL) - event_free(evcon->address); + mm_free(evcon->address); if (evcon->input_buffer != NULL) evbuffer_free(evcon->input_buffer); @@ -870,7 +870,7 @@ evhttp_connection_free(struct evhttp_connection *evcon) if (evcon->output_buffer != NULL) evbuffer_free(evcon->output_buffer); - event_free(evcon); + mm_free(evcon); } void @@ -879,8 +879,8 @@ evhttp_connection_set_local_address(struct evhttp_connection *evcon, { assert(evcon->state == EVCON_DISCONNECTED); if (evcon->bind_address) - event_free(evcon->bind_address); - if ((evcon->bind_address = event_strdup(address)) == NULL) + mm_free(evcon->bind_address); + if ((evcon->bind_address = mm_strdup(address)) == NULL) event_err(1, "%s: strdup", __func__); } @@ -1080,7 +1080,7 @@ evhttp_parse_response_line(struct evhttp_request *req, char *line) return (-1); } - if ((req->response_code_line = event_strdup(readable)) == NULL) + if ((req->response_code_line = mm_strdup(readable)) == NULL) event_err(1, "%s: strdup", __func__); return (0); @@ -1135,7 +1135,7 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line) return (-1); } - if ((req->uri = event_strdup(uri)) == NULL) { + if ((req->uri = mm_strdup(uri)) == NULL) { event_debug(("%s: evhttp_decode_uri", __func__)); return (-1); } @@ -1169,9 +1169,9 @@ evhttp_clear_headers(struct evkeyvalq *headers) header != NULL; header = TAILQ_FIRST(headers)) { TAILQ_REMOVE(headers, header, next); - event_free(header->key); - event_free(header->value); - event_free(header); + mm_free(header->key); + mm_free(header->value); + mm_free(header); } } @@ -1195,9 +1195,9 @@ evhttp_remove_header(struct evkeyvalq *headers, const char *key) /* Free and remove the header that we found */ TAILQ_REMOVE(headers, header, next); - event_free(header->key); - event_free(header->value); - event_free(header); + mm_free(header->key); + mm_free(header->value); + mm_free(header); return (0); } @@ -1217,19 +1217,19 @@ evhttp_add_header(struct evkeyvalq *headers, return (-1); } - header = event_calloc(1, sizeof(struct evkeyval)); + header = mm_calloc(1, sizeof(struct evkeyval)); if (header == NULL) { event_warn("%s: calloc", __func__); return (-1); } - if ((header->key = event_strdup(key)) == NULL) { - event_free(header); + if ((header->key = mm_strdup(key)) == NULL) { + mm_free(header); event_warn("%s: strdup", __func__); return (-1); } - if ((header->value = event_strdup(value)) == NULL) { - event_free(header->key); - event_free(header); + if ((header->value = mm_strdup(value)) == NULL) { + mm_free(header->key); + mm_free(header); event_warn("%s: strdup", __func__); return (-1); } @@ -1261,7 +1261,7 @@ evhttp_parse_lines(struct evhttp_request *req, struct evbuffer* buffer) if (*line == '\0') { /* Last header - Done */ done = 1; - event_free(line); + mm_free(line); break; } @@ -1293,13 +1293,13 @@ evhttp_parse_lines(struct evhttp_request *req, struct evbuffer* buffer) goto error; } - event_free(line); + mm_free(line); } return (done); error: - event_free(line); + mm_free(line); return (-1); } @@ -1450,7 +1450,7 @@ evhttp_connection_new(const char *address, unsigned short port) event_debug(("Attempting connection to %s:%d\n", address, port)); - if ((evcon = event_calloc(1, sizeof(struct evhttp_connection))) == NULL) { + if ((evcon = mm_calloc(1, sizeof(struct evhttp_connection))) == NULL) { event_warn("%s: calloc failed", __func__); goto error; } @@ -1461,7 +1461,7 @@ evhttp_connection_new(const char *address, unsigned short port) evcon->timeout = -1; evcon->retry_cnt = evcon->retry_max = 0; - if ((evcon->address = event_strdup(address)) == NULL) { + if ((evcon->address = mm_strdup(address)) == NULL) { event_warn("%s: strdup failed", __func__); goto error; } @@ -1573,8 +1573,8 @@ evhttp_make_request(struct evhttp_connection *evcon, req->kind = EVHTTP_REQUEST; req->type = type; if (req->uri != NULL) - event_free(req->uri); - if ((req->uri = event_strdup(uri)) == NULL) + mm_free(req->uri); + if ((req->uri = mm_strdup(uri)) == NULL) event_err(1, "%s: strdup", __func__); /* Set the protocol version if it is not supplied */ @@ -1765,8 +1765,8 @@ evhttp_response_code(struct evhttp_request *req, int code, const char *reason) req->kind = EVHTTP_RESPONSE; req->response_code = code; if (req->response_code_line != NULL) - event_free(req->response_code_line); - req->response_code_line = event_strdup(reason); + mm_free(req->response_code_line); + req->response_code_line = mm_strdup(reason); } void @@ -1827,7 +1827,7 @@ evhttp_encode_uri(const char *uri) } } evbuffer_add(buf, "", 1); - p = event_strdup((char *)EVBUFFER_DATA(buf)); + p = mm_strdup((char *)EVBUFFER_DATA(buf)); evbuffer_free(buf); return (p); @@ -1839,7 +1839,7 @@ evhttp_decode_uri(const char *uri) char c, *ret; int i, j, in_query = 0; - ret = event_malloc(strlen(uri) + 1); + ret = mm_malloc(strlen(uri) + 1); if (ret == NULL) event_err(1, "%s: malloc(%lu)", __func__, (unsigned long)(strlen(uri) + 1)); @@ -1882,7 +1882,7 @@ evhttp_parse_query(const char *uri, struct evkeyvalq *headers) if (strchr(uri, '?') == NULL) return; - if ((line = event_strdup(uri)) == NULL) + if ((line = mm_strdup(uri)) == NULL) event_err(1, "%s: strdup", __func__); @@ -1904,11 +1904,11 @@ evhttp_parse_query(const char *uri, struct evkeyvalq *headers) value = evhttp_decode_uri(value); event_debug(("Query Param: %s -> %s\n", key, value)); evhttp_add_header(headers, key, value); - event_free(value); + mm_free(value); } error: - event_free(line); + mm_free(line); } static struct evhttp_cb * @@ -1974,7 +1974,7 @@ evhttp_handle_request(struct evhttp_request *req, void *arg) evbuffer_add_printf(buf, ERR_FORMAT, escaped_html); - event_free(escaped_html); + mm_free(escaped_html); evhttp_send_page(req, buf); @@ -2041,7 +2041,7 @@ evhttp_new_object(void) { struct evhttp *http = NULL; - if ((http = event_calloc(1, sizeof(struct evhttp))) == NULL) { + if ((http = mm_calloc(1, sizeof(struct evhttp))) == NULL) { event_warn("%s: calloc", __func__); return (NULL); } @@ -2074,7 +2074,7 @@ evhttp_start(const char *address, u_short port) struct evhttp *http = evhttp_new_object(); if (evhttp_bind_socket(http, address, port) == -1) { - event_free(http); + mm_free(http); return (NULL); } @@ -2099,11 +2099,11 @@ evhttp_free(struct evhttp* http) while ((http_cb = TAILQ_FIRST(&http->callbacks)) != NULL) { TAILQ_REMOVE(&http->callbacks, http_cb, next); - event_free(http_cb->what); - event_free(http_cb); + mm_free(http_cb->what); + mm_free(http_cb); } - event_free(http); + mm_free(http); } void @@ -2118,10 +2118,10 @@ evhttp_set_cb(struct evhttp *http, const char *uri, { struct evhttp_cb *http_cb; - if ((http_cb = event_calloc(1, sizeof(struct evhttp_cb))) == NULL) + if ((http_cb = mm_calloc(1, sizeof(struct evhttp_cb))) == NULL) event_err(1, "%s: calloc", __func__); - http_cb->what = event_strdup(uri); + http_cb->what = mm_strdup(uri); http_cb->cb = cb; http_cb->cbarg = cbarg; @@ -2141,8 +2141,8 @@ evhttp_del_cb(struct evhttp *http, const char *uri) return (-1); TAILQ_REMOVE(&http->callbacks, http_cb, next); - event_free(http_cb->what); - event_free(http_cb); + mm_free(http_cb->what); + mm_free(http_cb); return (0); } @@ -2165,20 +2165,20 @@ evhttp_request_new(void (*cb)(struct evhttp_request *, void *), void *arg) struct evhttp_request *req = NULL; /* Allocate request structure */ - if ((req = event_calloc(1, sizeof(struct evhttp_request))) == NULL) { + if ((req = mm_calloc(1, sizeof(struct evhttp_request))) == NULL) { event_warn("%s: calloc", __func__); goto error; } req->kind = EVHTTP_RESPONSE; - req->input_headers = event_calloc(1, sizeof(struct evkeyvalq)); + req->input_headers = mm_calloc(1, sizeof(struct evkeyvalq)); if (req->input_headers == NULL) { event_warn("%s: calloc", __func__); goto error; } TAILQ_INIT(req->input_headers); - req->output_headers = event_calloc(1, sizeof(struct evkeyvalq)); + req->output_headers = mm_calloc(1, sizeof(struct evkeyvalq)); if (req->output_headers == NULL) { event_warn("%s: calloc", __func__); goto error; @@ -2210,17 +2210,17 @@ void evhttp_request_free(struct evhttp_request *req) { if (req->remote_host != NULL) - event_free(req->remote_host); + mm_free(req->remote_host); if (req->uri != NULL) - event_free(req->uri); + mm_free(req->uri); if (req->response_code_line != NULL) - event_free(req->response_code_line); + mm_free(req->response_code_line); evhttp_clear_headers(req->input_headers); - event_free(req->input_headers); + mm_free(req->input_headers); evhttp_clear_headers(req->output_headers); - event_free(req->output_headers); + mm_free(req->output_headers); if (req->input_buffer != NULL) evbuffer_free(req->input_buffer); @@ -2228,7 +2228,7 @@ evhttp_request_free(struct evhttp_request *req) if (req->output_buffer != NULL) evbuffer_free(req->output_buffer); - event_free(req); + mm_free(req); } void @@ -2276,8 +2276,8 @@ evhttp_get_request_connection( name_from_addr(sa, salen, &hostname, &portname); if (hostname==NULL || portname==NULL) { - if (hostname) event_free(hostname); - if (portname) event_free(portname); + if (hostname) mm_free(hostname); + if (portname) mm_free(portname); return (NULL); } @@ -2286,8 +2286,8 @@ evhttp_get_request_connection( /* we need a connection object to put the http request on */ evcon = evhttp_connection_new(hostname, atoi(portname)); - event_free(hostname); - event_free(portname); + mm_free(hostname); + mm_free(portname); if (evcon == NULL) return (NULL); @@ -2317,7 +2317,7 @@ evhttp_associate_new_request_with_connection(struct evhttp_connection *evcon) req->kind = EVHTTP_REQUEST; - if ((req->remote_host = event_strdup(evcon->address)) == NULL) + if ((req->remote_host = mm_strdup(evcon->address)) == NULL) event_err(1, "%s: strdup", __func__); req->remote_port = evcon->port; @@ -2402,8 +2402,8 @@ name_from_addr(struct sockaddr *sa, socklen_t salen, return; } - *phost = event_strdup(ntop); - *pport = event_strdup(strport); + *phost = mm_strdup(ntop); + *pport = mm_strdup(strport); #else /* XXXX */ #endif @@ -103,14 +103,14 @@ kq_init(struct event_base *base) if (getenv("EVENT_NOKQUEUE")) return (NULL); - if (!(kqueueop = event_calloc(1, sizeof(struct kqop)))) + if (!(kqueueop = mm_calloc(1, sizeof(struct kqop)))) return (NULL); /* Initalize the kernel queue */ if ((kq = kqueue()) == -1) { event_warn("kqueue"); - event_free (kqueueop); + mm_free (kqueueop); return (NULL); } @@ -119,15 +119,15 @@ kq_init(struct event_base *base) kqueueop->pid = getpid(); /* Initalize fields */ - kqueueop->changes = event_malloc(NEVENT * sizeof(struct kevent)); + kqueueop->changes = mm_malloc(NEVENT * sizeof(struct kevent)); if (kqueueop->changes == NULL) { - event_free (kqueueop); + mm_free (kqueueop); return (NULL); } - kqueueop->events = event_malloc(NEVENT * sizeof(struct kevent)); + kqueueop->events = mm_malloc(NEVENT * sizeof(struct kevent)); if (kqueueop->events == NULL) { - event_free (kqueueop->changes); - event_free (kqueueop); + mm_free (kqueueop->changes); + mm_free (kqueueop); return (NULL); } kqueueop->nevents = NEVENT; @@ -146,9 +146,9 @@ kq_init(struct event_base *base) kqueueop->events[0].ident != -1 || kqueueop->events[0].flags != EV_ERROR) { event_warn("%s: detected broken kqueue; not using.", __func__); - event_free(kqueueop->changes); - event_free(kqueueop->events); - event_free(kqueueop); + mm_free(kqueueop->changes); + mm_free(kqueueop->events); + mm_free(kqueueop); close(kq); return (NULL); } @@ -167,7 +167,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev) nevents *= 2; - newchange = event_realloc(kqop->changes, + newchange = mm_realloc(kqop->changes, nevents * sizeof(struct kevent)); if (newchange == NULL) { event_warn("%s: malloc", __func__); @@ -175,7 +175,7 @@ kq_insert(struct kqop *kqop, struct kevent *kev) } kqop->changes = newchange; - newresult = event_realloc(kqop->events, + newresult = mm_realloc(kqop->events, nevents * sizeof(struct kevent)); /* @@ -412,11 +412,11 @@ kq_dealloc(struct event_base *base, void *arg) struct kqop *kqop = arg; if (kqop->changes) - event_free(kqop->changes); + mm_free(kqop->changes); if (kqop->events) - event_free(kqop->events); + mm_free(kqop->events); if (kqop->kq >= 0 && kqop->pid == getpid()) close(kqop->kq); memset(kqop, 0, sizeof(struct kqop)); - event_free(kqop); + mm_free(kqop); } diff --git a/mm-internal.h b/mm-internal.h index 52a6fa5b..070ff6f0 100644 --- a/mm-internal.h +++ b/mm-internal.h @@ -9,11 +9,11 @@ extern "C" { #endif /* Internal use only: Memory allocation functions. */ -void *event_malloc(size_t sz); -void *event_calloc(size_t count, size_t size); -char *event_strdup(const char *s); -void *event_realloc(void *p, size_t sz); -void event_free(void *p); +void *mm_malloc(size_t sz); +void *mm_calloc(size_t count, size_t size); +char *mm_strdup(const char *s); +void *mm_realloc(void *p, size_t sz); +void mm_free(void *p); #ifdef __cplusplus } @@ -91,7 +91,7 @@ poll_init(struct event_base *base) if (getenv("EVENT_NOPOLL")) return (NULL); - if (!(pollop = event_calloc(1, sizeof(struct pollop)))) + if (!(pollop = mm_calloc(1, sizeof(struct pollop)))) return (NULL); evsignal_init(base); @@ -222,7 +222,7 @@ poll_add(void *arg, struct event *ev) tmp_event_count = pop->event_count * 2; /* We need more file descriptors */ - tmp_event_set = event_realloc(pop->event_set, + tmp_event_set = mm_realloc(pop->event_set, tmp_event_count * sizeof(struct pollfd)); if (tmp_event_set == NULL) { event_warn("realloc"); @@ -230,7 +230,7 @@ poll_add(void *arg, struct event *ev) } pop->event_set = tmp_event_set; - tmp_event_r_back = event_realloc(pop->event_r_back, + tmp_event_r_back = mm_realloc(pop->event_r_back, tmp_event_count * sizeof(struct event *)); if (tmp_event_r_back == NULL) { /* event_set overallocated; that's okay. */ @@ -239,7 +239,7 @@ poll_add(void *arg, struct event *ev) } pop->event_r_back = tmp_event_r_back; - tmp_event_w_back = event_realloc(pop->event_w_back, + tmp_event_w_back = mm_realloc(pop->event_w_back, tmp_event_count * sizeof(struct event *)); if (tmp_event_w_back == NULL) { /* event_set and event_r_back overallocated; that's @@ -261,7 +261,7 @@ poll_add(void *arg, struct event *ev) while (new_count <= ev->ev_fd) new_count *= 2; tmp_idxplus1_by_fd = - event_realloc(pop->idxplus1_by_fd, new_count * sizeof(int)); + mm_realloc(pop->idxplus1_by_fd, new_count * sizeof(int)); if (tmp_idxplus1_by_fd == NULL) { event_warn("realloc"); return (-1); @@ -362,14 +362,14 @@ poll_dealloc(struct event_base *base, void *arg) evsignal_dealloc(base); if (pop->event_set) - event_free(pop->event_set); + mm_free(pop->event_set); if (pop->event_r_back) - event_free(pop->event_r_back); + mm_free(pop->event_r_back); if (pop->event_w_back) - event_free(pop->event_w_back); + mm_free(pop->event_w_back); if (pop->idxplus1_by_fd) - event_free(pop->idxplus1_by_fd); + mm_free(pop->idxplus1_by_fd); memset(pop, 0, sizeof(struct pollop)); - event_free(pop); + mm_free(pop); } @@ -98,7 +98,7 @@ select_init(struct event_base *base) if (getenv("EVENT_NOSELECT")) return (NULL); - if (!(sop = event_calloc(1, sizeof(struct selectop)))) + if (!(sop = mm_calloc(1, sizeof(struct selectop)))) return (NULL); select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask)); @@ -210,23 +210,23 @@ select_resize(struct selectop *sop, int fdsz) if (sop->event_readset_in) check_selectop(sop); - if ((readset_in = event_realloc(sop->event_readset_in, fdsz)) == NULL) + if ((readset_in = mm_realloc(sop->event_readset_in, fdsz)) == NULL) goto error; sop->event_readset_in = readset_in; - if ((readset_out = event_realloc(sop->event_readset_out, fdsz)) == NULL) + if ((readset_out = mm_realloc(sop->event_readset_out, fdsz)) == NULL) goto error; sop->event_readset_out = readset_out; - if ((writeset_in = event_realloc(sop->event_writeset_in, fdsz)) == NULL) + if ((writeset_in = mm_realloc(sop->event_writeset_in, fdsz)) == NULL) goto error; sop->event_writeset_in = writeset_in; - if ((writeset_out = event_realloc(sop->event_writeset_out, fdsz)) == NULL) + if ((writeset_out = mm_realloc(sop->event_writeset_out, fdsz)) == NULL) goto error; sop->event_writeset_out = writeset_out; - if ((r_by_fd = event_realloc(sop->event_r_by_fd, + if ((r_by_fd = mm_realloc(sop->event_r_by_fd, n_events*sizeof(struct event*))) == NULL) goto error; sop->event_r_by_fd = r_by_fd; - if ((w_by_fd = event_realloc(sop->event_w_by_fd, + if ((w_by_fd = mm_realloc(sop->event_w_by_fd, n_events * sizeof(struct event*))) == NULL) goto error; sop->event_w_by_fd = w_by_fd; @@ -336,18 +336,18 @@ select_dealloc(struct event_base *base, void *arg) evsignal_dealloc(base); if (sop->event_readset_in) - event_free(sop->event_readset_in); + mm_free(sop->event_readset_in); if (sop->event_writeset_in) - event_free(sop->event_writeset_in); + mm_free(sop->event_writeset_in); if (sop->event_readset_out) - event_free(sop->event_readset_out); + mm_free(sop->event_readset_out); if (sop->event_writeset_out) - event_free(sop->event_writeset_out); + mm_free(sop->event_writeset_out); if (sop->event_r_by_fd) - event_free(sop->event_r_by_fd); + mm_free(sop->event_r_by_fd); if (sop->event_w_by_fd) - event_free(sop->event_w_by_fd); + mm_free(sop->event_w_by_fd); memset(sop, 0, sizeof(struct selectop)); - event_free(sop); + mm_free(sop); } @@ -142,7 +142,7 @@ _evsignal_set_handler(struct event_base *base, event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing", __func__, evsignal, sig->sh_old_max)); sig->sh_old_max = evsignal + 1; - p = event_realloc(sig->sh_old, sig->sh_old_max * sizeof *sig->sh_old); + p = mm_realloc(sig->sh_old, sig->sh_old_max * sizeof *sig->sh_old); if (p == NULL) { event_warn("realloc"); return (-1); @@ -151,7 +151,7 @@ _evsignal_set_handler(struct event_base *base, } /* allocate space for previous handler out of dynamic array */ - sig->sh_old[evsignal] = event_malloc(sizeof *sig->sh_old[evsignal]); + sig->sh_old[evsignal] = mm_malloc(sizeof *sig->sh_old[evsignal]); if (sig->sh_old[evsignal] == NULL) { event_warn("malloc"); return (-1); @@ -166,13 +166,13 @@ _evsignal_set_handler(struct event_base *base, if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) { event_warn("sigaction"); - event_free(sig->sh_old[evsignal]); + mm_free(sig->sh_old[evsignal]); return (-1); } #else if ((sh = signal(evsignal, handler)) == SIG_ERR) { event_warn("signal"); - event_free(sig->sh_old[evsignal]); + mm_free(sig->sh_old[evsignal]); return (-1); } *sig->sh_old[evsignal] = sh; @@ -234,7 +234,7 @@ _evsignal_restore_handler(struct event_base *base, int evsignal) } #endif - event_free(sh); + mm_free(sh); return ret; } @@ -304,5 +304,5 @@ evsignal_dealloc(struct event_base *base) base->sig.sh_old_max = 0; /* per index frees are handled in evsignal_del() */ - event_free(base->sig.sh_old); + mm_free(base->sig.sh_old); } |