universe@503: /* universe@503: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@503: * universe@503: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@503: * universe@503: * Redistribution and use in source and binary forms, with or without universe@503: * modification, are permitted provided that the following conditions are met: universe@503: * universe@503: * 1. Redistributions of source code must retain the above copyright universe@503: * notice, this list of conditions and the following disclaimer. universe@503: * universe@503: * 2. Redistributions in binary form must reproduce the above copyright universe@503: * notice, this list of conditions and the following disclaimer in the universe@503: * documentation and/or other materials provided with the distribution. universe@503: * universe@503: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@503: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@503: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@503: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@503: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@503: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@503: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@503: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@503: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@503: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@503: * POSSIBILITY OF SUCH DAMAGE. universe@503: */ universe@503: universe@503: #include "cx/list.h" universe@503: universe@503: CxList *cxListDestroy(CxList *list) { universe@503: if (list->content_destructor == NULL) { universe@503: if (list->autofree_contents) { universe@503: CxIterator iter = cxListBegin(list); universe@503: cx_foreach(void*, elem, iter) { universe@503: cxFree(list->allocator, elem); universe@503: } universe@503: } universe@503: } else { universe@503: // avoid checking this condition every loop iteration universe@503: if (list->autofree_contents) { universe@503: CxIterator iter = cxListBegin(list); universe@503: cx_foreach(void*, elem, iter) { universe@503: cxFree(list->allocator, list->content_destructor(elem)); universe@503: } universe@503: } else { universe@503: CxIterator iter = cxListBegin(list); universe@503: cx_foreach(void*, elem, iter) { universe@503: elem = list->content_destructor(elem); universe@503: } universe@503: } universe@503: } universe@503: if (list->autofree) { universe@503: cxFree(list->allocator, list->list_destructor(list)); universe@503: return NULL; universe@503: } else { universe@503: return list->list_destructor(list); universe@503: } universe@503: } universe@519: universe@519: void cxListMemoryMgmt( universe@519: CxList *list, universe@519: cx_destructor_func list_destructor, universe@519: cx_destructor_func content_destructor, universe@519: bool list_autofree, universe@519: bool content_autofree universe@519: ) { universe@519: if (list_destructor != NULL) list->list_destructor = list_destructor; universe@519: list->content_destructor = content_destructor; universe@519: list->autofree = list_autofree; universe@519: list->autofree_contents = content_autofree; universe@519: }