/* should be faster than sorting the list */ void sort_sub_list (GHolder * h, GSort sort) { int i, j, k; GHolderItem *arr_items; for (i = 0; i < h->idx; i++) { GSubList *sub_list = h->items[i].sub_list; GSubItem *iter; arr_items = new_gholder_item (sub_list->size); /* copy items from the linked-list into an rray */ for (j = 0, iter = sub_list->head; iter; iter = iter->next) { arr_items[j].data = (char *) iter->data; arr_items[j++].hits = iter->hits; } sort_holder_items (arr_items, j, sort); delete_sub_list (sub_list); sub_list = new_gsublist (); for (k = 0; k < j; k++) { if (k > 0) sub_list = h->items[i].sub_list; add_sub_item_back (sub_list, h->module, arr_items[k].data, arr_items[k].hits, 0); h->items[i].sub_list = sub_list; } free (arr_items); } }
/* dynamically allocated holder fields */ static void free_holder_data (GHolderItem item) { if (item.sub_list != NULL) delete_sub_list (item.sub_list); if (item.metrics->data != NULL) free (item.metrics->data); if (item.metrics->method != NULL) free (item.metrics->method); if (item.metrics->protocol != NULL) free (item.metrics->protocol); if (item.metrics != NULL) free (item.metrics); }
/* Copy linked-list items to an array, sort, and move them back * to the list. Should be faster than sorting the list */ static void sort_sub_list (GHolder * h, GSort sort) { GHolderItem *arr; GSubItem *iter; GSubList *sub_list; int i, j, k; /* iterate over root-level nodes */ for (i = 0; i < h->idx; i++) { sub_list = h->items[i].sub_list; if (sub_list == NULL) continue; arr = new_gholder_item (sub_list->size); /* copy items from the linked-list into an array */ for (j = 0, iter = sub_list->head; iter; iter = iter->next, j++) { arr[j].metrics = new_gmetrics (); arr[j].metrics->bw.nbw = iter->metrics->bw.nbw; arr[j].metrics->data = xstrdup (iter->metrics->data); arr[j].metrics->hits = iter->metrics->hits; arr[j].metrics->id = iter->metrics->id; arr[j].metrics->visitors = iter->metrics->visitors; if (conf.serve_usecs) { arr[j].metrics->avgts.nts = iter->metrics->avgts.nts; arr[j].metrics->cumts.nts = iter->metrics->cumts.nts; arr[j].metrics->maxts.nts = iter->metrics->maxts.nts; } } sort_holder_items (arr, j, sort); delete_sub_list (sub_list); sub_list = new_gsublist (); for (k = 0; k < j; k++) { if (k > 0) sub_list = h->items[i].sub_list; add_sub_item_back (sub_list, h->module, arr[k].metrics); h->items[i].sub_list = sub_list; } free (arr); } }
/* free memory allocated in holder for specific module */ void free_holder_by_module (GHolder ** holder, GModule module) { if ((*holder) == NULL) return; GSubList *sub_list; int j; for (j = 0; j < (*holder)[module].holder_size; j++) { sub_list = (*holder)[module].items[j].sub_list; /* free the sub list */ if (sub_list != NULL) delete_sub_list (sub_list); if ((*holder)[module].items[j].data != NULL) free ((*holder)[module].items[j].data); } free ((*holder)[module].items); (*holder)[module].holder_size = 0; (*holder)[module].idx = 0; (*holder)[module].sub_items_size = 0; }
/* free allocated memory for holder */ void free_holder (GHolder ** holder) { if ((*holder) == NULL) return; GSubList *sub_list; int i, j; for (i = 0; i < TOTAL_MODULES; i++) { for (j = 0; j < (*holder)[i].holder_size; j++) { sub_list = (*holder)[i].items[j].sub_list; /* free the sub list */ if (sub_list != NULL) delete_sub_list (sub_list); if ((*holder)[i].items[j].data != NULL) free ((*holder)[i].items[j].data); } free ((*holder)[i].items); } free (*holder); (*holder) = NULL; }