Source
xxxxxxxxxx
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/******************************************************************************
* *
* Purpose: create singly linked list (with custom memory functions) *
* *
* Parameters: list - [IN/OUT] *
* mem_malloc_func - [IN] callback for malloc *
* mem_free_func - [IN] callback for free *
* *
******************************************************************************/
void zbx_list_create_ext(zbx_list_t *list, zbx_mem_malloc_func_t mem_malloc_func, zbx_mem_free_func_t mem_free_func)
{
memset(list, 0, sizeof(*list));
list->mem_malloc_func = mem_malloc_func;
list->mem_free_func = mem_free_func;
}
/******************************************************************************
* *
* Purpose: create singly linked list *
* *
* Parameters: list - [IN] *
* *
******************************************************************************/
void zbx_list_create(zbx_list_t *list)
{
zbx_list_create_ext(list, ZBX_DEFAULT_MEM_MALLOC_FUNC, ZBX_DEFAULT_MEM_FREE_FUNC);
}
void zbx_list_destroy(zbx_list_t *list)
{
while (FAIL != zbx_list_pop(list, NULL))
;
}
/******************************************************************************
* *
* Purpose: allocate memory and initialize a new list item *
* *
* Parameters: list - [IN] *
* value - [IN] Data to be stored. Ownership of 'value' goes *
* to list item. *
* created - [OUT] pointer to the created list item *
* *
******************************************************************************/
static void list_create_item(zbx_list_t *list, void *value, zbx_list_item_t **created)
{
zbx_list_item_t *item;
if (NULL != (item = (zbx_list_item_t *)list->mem_malloc_func(NULL, sizeof(zbx_list_item_t))))
{
item->next = NULL;
item->data = value;
}
*created = item;
}
/******************************************************************************
* *
* Purpose: insert value after specified position in the list *
* *
* Parameters: list - [IN] *
* after - [IN] specified position (can be NULL to insert at *
* the end of the list) *
* value - [IN] Value to be inserted. Ownership of 'value' *
* goes to list item. *
* inserted - [OUT] pointer to the inserted list item *
* *
* Return value: SUCCEED - the item was prepended successfully *
* FAIL - memory allocation error *
* *
******************************************************************************/
int zbx_list_insert_after(zbx_list_t *list, zbx_list_item_t *after, void *value, zbx_list_item_t **inserted)
{