cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

list.c (2688B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21#include "common/list.h"
     22
     23#include <guacamole/mem.h>
     24
     25#include <stdlib.h>
     26#include <pthread.h>
     27
     28guac_common_list* guac_common_list_alloc() {
     29
     30    guac_common_list* list = guac_mem_alloc(sizeof(guac_common_list));
     31
     32    pthread_mutex_init(&list->_lock, NULL);
     33    list->head = NULL;
     34
     35    return list;
     36
     37}
     38
     39void guac_common_list_free(
     40        guac_common_list* list,
     41        guac_common_list_element_free_handler* free_element_handler) {
     42
     43    /* Free every element of the list */
     44    guac_common_list_element* element = list->head;
     45    while(element != NULL) {
     46
     47        guac_common_list_element* next = element->next;
     48
     49        if (free_element_handler != NULL)
     50            free_element_handler(element->data);
     51
     52        guac_mem_free(element);
     53        element = next;
     54    }
     55
     56    /* Free the list itself */
     57    guac_mem_free(list);
     58
     59}
     60
     61guac_common_list_element* guac_common_list_add(guac_common_list* list,
     62        void* data) {
     63
     64    /* Allocate element, initialize as new head */
     65    guac_common_list_element* element =
     66        guac_mem_alloc(sizeof(guac_common_list_element));
     67    element->data = data;
     68    element->next = list->head;
     69    element->_ptr = &(list->head);
     70
     71    /* If head already existed, point it at this element */
     72    if (list->head != NULL)
     73        list->head->_ptr = &(element->next);
     74
     75    /* Set as new head */
     76    list->head = element;
     77    return element;
     78
     79}
     80
     81void guac_common_list_remove(guac_common_list* list,
     82        guac_common_list_element* element) {
     83
     84    /* Point previous (or head) to next */
     85    *(element->_ptr) = element->next;
     86
     87    if (element->next != NULL)
     88        element->next->_ptr = element->_ptr;
     89
     90    guac_mem_free(element);
     91
     92}
     93
     94void guac_common_list_lock(guac_common_list* list) {
     95    pthread_mutex_lock(&list->_lock);
     96}
     97
     98void guac_common_list_unlock(guac_common_list* list) {
     99    pthread_mutex_unlock(&list->_lock);
    100}
    101