guacamole-spice-protocol/src/common/guac_list.c

82 lines
2.1 KiB
C
Raw Normal View History

2014-01-06 23:53:22 +00:00
/*
2016-03-25 19:59:40 +00:00
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
2014-01-06 23:53:22 +00:00
*
2016-03-25 19:59:40 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2014-01-06 23:53:22 +00:00
*
2016-03-25 19:59:40 +00:00
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
2014-01-06 23:53:22 +00:00
*/
#include "config.h"
2014-01-07 00:00:07 +00:00
#include "guac_list.h"
2014-01-06 23:53:22 +00:00
#include <stdlib.h>
#include <pthread.h>
guac_common_list* guac_common_list_alloc() {
guac_common_list* list = malloc(sizeof(guac_common_list));
pthread_mutex_init(&list->_lock, NULL);
list->head = NULL;
return list;
}
void guac_common_list_free(guac_common_list* list) {
free(list);
}
guac_common_list_element* guac_common_list_add(guac_common_list* list,
void* data) {
/* Allocate element, initialize as new head */
guac_common_list_element* element =
malloc(sizeof(guac_common_list_element));
element->data = data;
element->next = list->head;
element->_ptr = &(list->head);
/* If head already existed, point it at this element */
if (list->head != NULL)
list->head->_ptr = &(element->next);
/* Set as new head */
list->head = element;
return element;
}
void guac_common_list_remove(guac_common_list* list,
guac_common_list_element* element) {
/* Point previous (or head) to next */
*(element->_ptr) = element->next;
2014-03-02 18:39:36 +00:00
if (element->next != NULL)
element->next->_ptr = element->_ptr;
2014-01-06 23:53:22 +00:00
free(element);
}
void guac_common_list_lock(guac_common_list* list) {
pthread_mutex_lock(&list->_lock);
}
void guac_common_list_unlock(guac_common_list* list) {
pthread_mutex_unlock(&list->_lock);
}