00001 /* 00002 * This file is part of libbluray 00003 * Copyright (C) 2010 hpi1 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library. If not, see 00017 * <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "pes_buffer.h" 00021 00022 #include "util/macro.h" 00023 00024 #include <stdlib.h> 00025 #include <string.h> 00026 00027 PES_BUFFER *pes_buffer_alloc(int size) 00028 { 00029 PES_BUFFER *p = calloc(1, sizeof(*p)); 00030 00031 if (p) { 00032 p->size = size; 00033 p->buf = malloc(size); 00034 } 00035 00036 return p; 00037 } 00038 00039 void pes_buffer_free(PES_BUFFER **p) 00040 { 00041 if (p && *p) { 00042 if ((*p)->next) { 00043 pes_buffer_free(&(*p)->next); 00044 } 00045 X_FREE ((*p)->buf); 00046 X_FREE (*p); 00047 } 00048 } 00049 00050 void pes_buffer_append(PES_BUFFER **head, PES_BUFFER *buf) 00051 { 00052 if (!head) { 00053 return; 00054 } 00055 00056 if (!*head) { 00057 *head = buf; 00058 return; 00059 } 00060 00061 if (buf) { 00062 PES_BUFFER *tail = *head; 00063 for (; tail->next; tail = tail->next) ; 00064 tail->next = buf; 00065 } 00066 } 00067 00068 static PES_BUFFER *_prev_buffer(PES_BUFFER *head, PES_BUFFER *buf) 00069 { 00070 while (head) { 00071 if (head->next == buf) { 00072 return head; 00073 } 00074 head = head->next; 00075 } 00076 00077 return NULL; 00078 } 00079 00080 void pes_buffer_remove(PES_BUFFER **head, PES_BUFFER *p) 00081 { 00082 if (head && *head && p) { 00083 if (*head == p) { 00084 *head = (*head)->next; 00085 p->next = NULL; 00086 pes_buffer_free(&p); 00087 } else { 00088 PES_BUFFER *prev = _prev_buffer(*head, p); 00089 if (prev) { 00090 prev->next = p->next; 00091 p->next = NULL; 00092 pes_buffer_free(&p); 00093 } 00094 } 00095 } 00096 }
1.6.3