/** * @brief Shallow copy of the list passed in * @param list The list to copy * @return the newly created list */ struct dlist *dlist_copy(struct dlist *list) { struct dlist *next = NULL, *last = NULL; /* first one */ if (list) { next = dlist_alloc(); next->data = list->data; next->prev = NULL; last = next; list = list->next; /* the rest */ while (list) { next->next = dlist_alloc(); next->next->prev = next; next = next->next; next->data = list->data; list = list->next; } next->next = NULL; } return last; }
/* * alloc_list * * helper function to allocate a list for scamper_fds_init */ static dlist_t *alloc_list(char *name) { dlist_t *list; if((list = dlist_alloc()) == NULL) printerror(errno, strerror, __func__, "alloc %s failed", name); return list; }
int scamper_task_init(void) { if((tx_ip = splaytree_alloc(tx_ip_cmp)) == NULL) return -1; if((tx_nd = splaytree_alloc(tx_nd_cmp)) == NULL) return -1; if((sniff = dlist_alloc()) == NULL) return -1; return 0; }
int scamper_rtsock_init() { #ifndef _WIN32 if((pairs = dlist_alloc()) == NULL) { printerror(errno, strerror, __func__, "could not allocate pair list"); return -1; } pid = getpid(); #endif return 0; }
scamper_task_anc_t *scamper_task_anc_add(scamper_task_t *task, void *data, void (*freedata)(void *)) { scamper_task_anc_t *anc = NULL; if(task->ancillary == NULL && (task->ancillary = dlist_alloc()) == NULL) return NULL; if((anc = malloc_zero(sizeof(scamper_task_anc_t))) == NULL) return NULL; anc->data = data; anc->freedata = freedata; if((anc->node = dlist_tail_push(task->ancillary, anc)) == NULL) { free(anc); return NULL; } return anc; }
/** * @brief Add a list element to the start of the list * @param list The list list pointer * @param data The data to add to the list * @return The new list list */ struct dlist *dlist_prepend(struct dlist *list, void *data) { struct dlist *next = dlist_alloc(); next->data = data; next->next = list; if (list) { next->prev = list->prev; if (next->prev) next->prev->next = next; list->prev = next; } else next->prev = NULL; return next; }
/** * @brief Append a list element to the end of the list * @param list The list list pointer * @param data The data to add to the list * @return The new list list */ struct dlist *dlist_append(struct dlist *list, void *data) { struct dlist *last = NULL; struct dlist *next = dlist_alloc(); next->data = data; next->next = NULL; if (list) { last = dlist_last(list); last->next = next; next->prev = last; return list; } else { next->prev = NULL; return next; } }
void *scamper_task_onhold(scamper_task_t *task, void *param, void (*unhold)(void *param)) { task_onhold_t *toh = NULL; dlist_node_t *cookie; if(task->onhold == NULL && (task->onhold = dlist_alloc()) == NULL) goto err; if((toh = malloc_zero(sizeof(task_onhold_t))) == NULL) goto err; if((cookie = dlist_tail_push(task->onhold, toh)) == NULL) goto err; toh->param = param; toh->unhold = unhold; return cookie; err: if(toh != NULL) free(toh); return NULL; }
/** * @brief Insert a list element to the specified position * @param list The list list pointer * @param data The data for the new list * @param pos The position to put the new list * @return The new list list */ struct dlist *dlist_insert(struct dlist *list, void *data, int pos) { struct dlist *tmp = NULL, *next = NULL; if (pos < 0) return dlist_append(list, data); else if (pos == 0) return dlist_prepend(list, data); tmp = dlist_nth(list, pos); if (!tmp) return dlist_append(list, data); next = dlist_alloc(); next->data = data; next->prev = tmp->prev; if (tmp->prev) tmp->prev->next = next; next->next = tmp; tmp->prev = next; return (tmp == list) ? next : list; }