/** Release a Connection and all memory associated with it. * The connection's DNS reply field is freed, its file descriptor is * closed, its msgq and sendq are cleared, and its associated Listener * is dereferenced. Then it is prepended to #connectionFreeList. * @param[in] con Connection to free. */ static void dealloc_connection(struct Connection* con) { assert(con_verify(con)); assert(!t_active(&(con_proc(con)))); assert(!t_onqueue(&(con_proc(con)))); Debug((DEBUG_LIST, "Deallocating connection %p", con)); if (con_dns_reply(con)) --(con_dns_reply(con)->ref_count); if (con_dnsbl_reply(con)) --(con_dnsbl_reply(con)->ref_count); if (-1 < con_fd(con)) close(con_fd(con)); MsgQClear(&(con_sendQ(con))); client_drop_sendq(con); DBufClear(&(con_recvQ(con))); if (con_listener(con)) release_listener(con_listener(con)); if (con_loc(con)) MyFree(con_loc(con)); --connections.inuse; con_next(con) = connectionFreeList; connectionFreeList = con; con_magic(con) = 0; }
/** Allocate a new Connection structure. * If #connectionFreeList != NULL, use the head of that list. * Otherwise, allocate a new structure. * @return Newly allocated Connection. */ static struct Connection* alloc_connection(void) { struct Connection* con = connectionFreeList; if (!con) { con = (struct Connection*) MyMalloc(sizeof(struct Connection)); connections.alloc++; } else connectionFreeList = con_next(con); connections.inuse++; memset(con, 0, sizeof(struct Connection)); timer_init(&(con_proc(con))); return con; }
static struct Connection* alloc_connection(void) { struct Connection* con = connectionFreeList; if (!con) { con = (struct Connection*) MyMalloc(sizeof(struct Connection)); ++connectionAllocCount; } else connectionFreeList = con_next(con); #ifdef DEBUGMODE connections.inuse++; #endif memset(con, 0, sizeof(struct Connection)); timer_init(&(con_proc(con))); return con; }