Replace malloc / free for ELIST

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-03-27 22:22:39 +01:00
parent 582260a9bf
commit 2c273c1b3b

View File

@ -103,33 +103,29 @@ int32_t ELIST::length() const { // count elements
void ELIST::sort( // sort elements void ELIST::sort( // sort elements
int comparator( // comparison routine int comparator( // comparison routine
const void *, const void *)) { const void *, const void *)) {
ELIST_ITERATOR it(this); // Allocate an array of pointers, one per list element.
int32_t count; auto count = length();
ELIST_LINK **base; // ptr array to sort
ELIST_LINK **current;
int32_t i;
/* Allocate an array of pointers, one per list element */ if (count > 0) {
count = length(); // ptr array to sort
base = static_cast<ELIST_LINK **>(malloc(count * sizeof(ELIST_LINK *))); std::vector<ELIST_LINK *> base;
base.reserve(count);
/* Extract all elements, putting the pointers in the array */ ELIST_ITERATOR it(this);
current = base;
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) { // Extract all elements, putting the pointers in the array.
*current = it.extract(); for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
current++; base.push_back(it.extract());
}
// Sort the pointer array.
qsort(&base[0], count, sizeof(base[0]), comparator);
// Rebuild the list from the sorted pointers.
for (auto current : base) {
it.add_to_end(current);
}
} }
/* Sort the pointer array */
qsort(base, count, sizeof(*base), comparator);
/* Rebuild the list from the sorted pointers */
current = base;
for (i = 0; i < count; i++) {
it.add_to_end(*current);
current++;
}
free(base);
} }
// Assuming list has been sorted already, insert new_link to // Assuming list has been sorted already, insert new_link to