BarryServer : Git

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

// Related

libBLOC

Barry Adding object metadata functions c5a8d16 (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 "list.h"

/* Add an object to a list */
void
list_add(List *list, void *obj)
{
	ASSERT(list);
	ASSERT(obj);
	ASSERT(obj_verify(obj));
	if (list->type && list->type != obj_type(obj))
		return;

	struct IterableEntry *entry;
	entry = malloc(sizeof(struct IterableEntry));
	entry->prev = entry->next = NULL;
	entry->obj = obj_get(obj);

	obj_lock(list);
	list->header.entries++;
	if (!list->header.start) {
		list->header.start = list->header.end = entry;
	} else {
		list->header.end->next = entry;
		entry->prev = list->header.end;
		list->header.end = entry;
	}
	obj_unlock(list);
}