File: list.c

package info (click to toggle)
samdump2 3.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 308 kB
  • sloc: ansic: 3,249; makefile: 228
file content (129 lines) | stat: -rw-r--r-- 3,243 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/** @file list.c
    Implementation of a doubly linked list.
    
    This program 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 2
    of the License, or (at your option) any later version.
    
    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 General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    This program is released under the GPL with the additional exemption 
    that compiling, linking, and/or using OpenSSL is allowed.
    
    Copyright (C) 2008 Bertrand Mesot <http://www.objectif-securite.ch>
*/

#include <stdlib.h>
#include <assert.h>

#include "list.h"
/*-------------------------------------------------------------------------*/
list_t *list_alloc(void) {
  list_t *l = (list_t*)malloc(sizeof(list_t));

  l->size = 0;
  l->head = 0;
  l->tail = 0;

  return l;
}
/*-------------------------------------------------------------------------*/
void list_free(list_t *l) {
  list_clean(l);
  free(l);
}
/*-------------------------------------------------------------------------*/
void list_clean(list_t *l) {
  while (l->size)
    list_rem_head(l);
}
/*-------------------------------------------------------------------------*/
void list_add_head(list_t *l, void *data) {
  list_nd_t *nd  = list_nd_alloc(data);
  list_nd_t *old = l->head;

  nd->prev = old;
  l->head  = nd;
  l->size += 1;

  if (old != 0) 
    old->next = nd;
  else
    l->tail = nd;
}
/*-------------------------------------------------------------------------*/
void list_add_tail(list_t *l, void *data) {
  list_nd_t *nd  = list_nd_alloc(data);
  list_nd_t *old = l->tail;

  nd->next = old;
  l->tail  = nd;
  l->size += 1;

  if (old != 0) 
    old->prev = nd;
  else
    l->head = nd;
}
/*-------------------------------------------------------------------------*/
void *list_rem_head(list_t *l) {
  list_nd_t *nd = l->head;

  if (nd == 0) return 0;

  list_nd_t *old = nd->prev;

  l->head  = old;
  l->size -= 1;

  if (old != 0) 
    old->next = 0;
  else
    l->tail = 0;
  
  return list_nd_free(nd);
}
/*-------------------------------------------------------------------------*/
void *list_rem_tail(list_t *l) {
  list_nd_t *nd = l->tail;

  if (nd == 0) return 0;

  list_nd_t *old = nd->next;

  l->tail  = old;
  l->size -= 1;

  if (old != 0) 
    old->prev = 0;
  else
    l->head = 0;

  return list_nd_free(nd);
}
/*-------------------------------------------------------------------------*/
list_nd_t *list_nd_alloc(void *data) {
  list_nd_t *nd = (list_nd_t*)malloc(sizeof(list_nd_t));

  nd->prev = 0;
  nd->next = 0;
  nd->data = data;

  return nd;
}
/*-------------------------------------------------------------------------*/
void *list_nd_free(list_nd_t *nd) {
  void *data = nd->data;

  free(nd);

  return data;
}