summaryrefslogtreecommitdiff
path: root/src/xine-utils/list.c
blob: 2c790c3dbd19e1ae99f60ad84c05c256dfa303f6 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* 
 * Copyright (C) 2000-2006 the xine project
 * 
 * This file is part of xine, a free video player.
 * 
 * xine 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.
 * 
 * xine 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * $Id: list.c,v 1.12 2006/09/26 05:19:49 dgp85 Exp $
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include "attributes.h"
#include "list.h"

#define MIN_CHUNK_SIZE    32
#define MAX_CHUNK_SIZE 65536

/* list element struct */
typedef struct xine_list_elem_s xine_list_elem_t;
struct xine_list_elem_s {
  xine_list_elem_t *prev;
  xine_list_elem_t *next;
  void            *value;
};

/* chunk of list elements */
typedef struct xine_list_chunk_s xine_list_chunk_t;
struct xine_list_chunk_s {
  xine_list_chunk_t *next_chunk;            /* singly linked list of chunks */

  xine_list_elem_t *elem_array;             /* the allocated elements */
  int chunk_size;                          /* element count in the chunk */
  int current_elem_id;                     /* next free elem in the chunk */
};

/* list struct */
struct xine_list_s {
  /* list of chunks */
  xine_list_chunk_t *chunk_list;
  size_t            chunk_list_size;
  xine_list_chunk_t *last_chunk;

  /* list elements */
  xine_list_elem_t  *elem_list_front;
  xine_list_elem_t  *elem_list_back;
  size_t            elem_list_size;

  /* list of free elements */
  xine_list_elem_t  *free_elem_list;
  size_t            free_elem_list_size;
};

/* Allocates a new chunk of n elements
 * One malloc call is used to allocate the struct and the elements.
 */
static xine_list_chunk_t *xine_list_alloc_chunk(size_t size) {
  xine_list_chunk_t *new_chunk;
  size_t chunk_mem_size;;

  chunk_mem_size  = sizeof(xine_list_chunk_t);
  chunk_mem_size += sizeof(xine_list_elem_t) * size;

  new_chunk = (xine_list_chunk_t *)malloc(chunk_mem_size);
  new_chunk->elem_array = (xine_list_elem_t*)(new_chunk + 1);
  new_chunk->next_chunk = NULL;
  new_chunk->current_elem_id = 0;
  new_chunk->chunk_size = size;

  return new_chunk;
}

/* Delete a chunk */
static void xine_list_delete_chunk(xine_list_chunk_t *chunk) {
  free(chunk);
}

/* Get a new element either from the free list either from the current chunk.
   Allocate a new chunk if needed */
static xine_list_elem_t *xine_list_alloc_elem(xine_list_t *list) {
  xine_list_elem_t *new_elem;

  /* check the free list */
  if (list->free_elem_list_size > 0) {
    new_elem = list->free_elem_list;
    list->free_elem_list = list->free_elem_list->next;
    list->free_elem_list_size--;
  } else {
    /* check current chunk */
    if (list->last_chunk->current_elem_id < list->last_chunk->chunk_size) {
      /* take the next elem in the chunk */
      new_elem = &list->last_chunk->elem_array[list->last_chunk->current_elem_id];
      list->last_chunk->current_elem_id++;
    } else {
      /* a new chunk is needed */
      xine_list_chunk_t *new_chunk;
      int chunk_size;

      chunk_size = list->last_chunk->chunk_size * 2;
      if (chunk_size > MAX_CHUNK_SIZE)
        chunk_size = MAX_CHUNK_SIZE;

      new_chunk = xine_list_alloc_chunk(chunk_size);

      list->last_chunk->next_chunk = new_chunk;
      list->last_chunk = new_chunk;
      list->chunk_list_size++;

      new_elem = &new_chunk->elem_array[0];
      new_chunk->current_elem_id++;
    }
  }
  return new_elem;
}

/* Push the elem into the free list */
static void xine_list_recycle_elem(xine_list_t *list,  xine_list_elem_t *elem) {
  elem->next = list->free_elem_list;
  elem->prev = NULL;

  list->free_elem_list = elem;
  list->free_elem_list_size++;
}

/* List constructor */
xine_list_t *xine_list_new(void) {
  xine_list_t *new_list;

  new_list = (xine_list_t*)malloc(sizeof(xine_list_t));
  new_list->chunk_list = xine_list_alloc_chunk(MIN_CHUNK_SIZE);
  new_list->chunk_list_size = 1;
  new_list->last_chunk = new_list->chunk_list;
  new_list->free_elem_list = NULL;
  new_list->free_elem_list_size = 0;
  new_list->elem_list_front = NULL;
  new_list->elem_list_back = NULL;
  new_list->elem_list_size = 0;

  return new_list;
}

void xine_list_delete(xine_list_t *list) {
  /* Delete each chunk */
  xine_list_chunk_t *current_chunk = list->chunk_list;

  while (current_chunk) {
    xine_list_chunk_t *next_chunk = current_chunk->next_chunk;

    xine_list_delete_chunk(current_chunk);
    current_chunk = next_chunk;
  }
  free(list);
}

unsigned int xine_list_size(xine_list_t *list) {
  return list->elem_list_size;
}

unsigned int xine_list_empty(xine_list_t *list) {
  return (list->elem_list_size == 0);
}

xine_list_iterator_t xine_list_front(xine_list_t *list) {
  return list->elem_list_front;
}

xine_list_iterator_t xine_list_back(xine_list_t *list) {
  return list->elem_list_back;
}

void xine_list_push_back(xine_list_t *list, void *value) {
  xine_list_elem_t *new_elem;

  new_elem = xine_list_alloc_elem(list);
  new_elem->value = value;

  if (list->elem_list_back) {
    new_elem->next = NULL;
    new_elem->prev = list->elem_list_back;
    list->elem_list_back->next = new_elem;
    list->elem_list_back = new_elem;
  } else {
    /* first elem in the list */
    list->elem_list_front = list->elem_list_back = new_elem;
    new_elem->next = NULL;
    new_elem->prev = NULL;
  }
  list->elem_list_size++;
}

void xine_list_push_front(xine_list_t *list, void *value) {
  xine_list_elem_t *new_elem;

  new_elem = xine_list_alloc_elem(list);
  new_elem->value = value;

  if (list->elem_list_front) {
    new_elem->next = list->elem_list_front;
    new_elem->prev = NULL;
    list->elem_list_front->prev = new_elem;
    list->elem_list_front = new_elem;
  } else {
    /* first elem in the list */
    list->elem_list_front = list->elem_list_back = new_elem;
    new_elem->next = NULL;
    new_elem->prev = NULL;
  }
  list->elem_list_size++;
}

void xine_list_clear(xine_list_t *list) {
  xine_list_elem_t *elem = list->elem_list_front;
  while (elem) {
    xine_list_elem_t *elem_next = elem->next;
    xine_list_recycle_elem(list, elem);
    elem = elem_next;
  }

  list->elem_list_front = NULL;
  list->elem_list_back = NULL;
  list->elem_list_size = 0;
}

xine_list_iterator_t xine_list_next(xine_list_t *list, xine_list_iterator_t ite) {
  xine_list_elem_t *elem = (xine_list_elem_t*)ite;

  if (ite == NULL)
    return list->elem_list_front;
  else
    return (xine_list_iterator_t)elem->next;
}

xine_list_iterator_t xine_list_prev(xine_list_t *list, xine_list_iterator_t ite) {
  xine_list_elem_t *elem = (xine_list_elem_t*)ite;

  if (ite == NULL)
    return list->elem_list_back;
  else
    return (xine_list_iterator_t)elem->prev;
}

void *xine_list_get_value(xine_list_t *list, xine_list_iterator_t ite) {
  xine_list_elem_t *elem = (xine_list_elem_t*)ite;

  return elem->value;
}

void xine_list_remove(xine_list_t *list, xine_list_iterator_t position) {
  xine_list_elem_t *elem = (xine_list_elem_t*)position;

  if (elem) {
    xine_list_elem_t *prev = elem->prev;
    xine_list_elem_t *next = elem->next;

    if (prev)
      prev->next = next;
    else
      list->elem_list_front = next;

    if (next)
      next->prev = prev;
    else
      list->elem_list_back = prev;

    xine_list_recycle_elem(list, elem);
    list->elem_list_size--;
  }
}

xine_list_iterator_t xine_list_insert(xine_list_t *list,
                                    xine_list_iterator_t position,
                                    void *value) {
  xine_list_elem_t *elem = (xine_list_elem_t*)position;
  xine_list_iterator_t new_position = NULL;

  if (elem == NULL) {
    /* insert at the end */
    xine_list_push_back(list, value);
    new_position = list->elem_list_back;
  } else {
    if (elem->prev == NULL) {
      /* insert at the beginning */
      xine_list_push_front(list, value);
      new_position = list->elem_list_front;
    } else {
      xine_list_elem_t *new_elem = xine_list_alloc_elem(list);
      xine_list_elem_t *prev = elem->prev;

      new_elem->next = elem;
      new_elem->prev = prev;
      new_elem->value = value;

      elem->prev = new_elem;
      prev->next = new_elem;

      new_position = (xine_list_iterator_t)elem;
    }
  }
  return new_position;
}

xine_list_iterator_t xine_list_find(xine_list_t *list, void *value) {

  xine_list_elem_t *elem;

  for (elem = list->elem_list_front; elem; elem = elem->next) {
    if (elem->value == value)
      break;
  }
  return elem;
}