BarryServer : Git

All the code for all my projects
// BarryServer : Git / libBLOC / blob / cc6f863bfd2b1003781bff2cdb5d9ea4b8a9d4a9 / list / list_remove.c

// Related

libBLOC

Barry Initial commit b3106d3 (2 years, 11 months ago)
/*
 * Copyright (C) 2023 Barry
 *
 * This file is part of Barry's Little Objects in C Library (libBLOC).
 *
 * libBLOC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <BLOC/object.h>
#include <BLOC/list.h>
#include "../assert.h"
#include "../object.h"
#include "list.h"

/* Remove an object from a list */
void
list_remove(List *list, void *obj)
{
	ASSERT(list);
	ASSERT(obj);
	struct ObjectHeader *header = object_header(obj);
	ASSERT(header->magic == OBJECT_MAGIC);
	if (!list->header.start)
		return;
	if (list->type && list->type != header->type)
		return;
	obj_lock(list);

	/* Search for object */
	struct IterableEntry *entry;
	for (entry = list->header.start; entry; entry = entry->next) {
		if (entry->obj == obj)
			break;
	}
	if (!entry) {
		obj_unlock(list);
		return;
	}

	/* Unlink */
	if (list->header.start == entry)
		list->header.start = entry->next;
	if (list->header.end == entry)
		list->header.end = entry->prev;
	if (entry->prev)
		entry->prev->next = entry->next;
	if (entry->next)
		entry->next->prev = entry->prev;

	obj_put(entry->obj);
	list->header.entries--;
	free(entry);

	obj_unlock(list);
}