source: list.c @ 72146c7

release-1.10release-1.8release-1.9
Last change on this file since 72146c7 was fda61d3, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
g_strdup and g_new0 never fail Remove pointless failure checks cluttering the code.
  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3
4#define INITSIZE 10
5#define GROWBY 3 / 2
6
7void owl_list_create(owl_list *l)
8{
9  l->size=0;
10  l->list=g_new(void *, INITSIZE);
11  l->avail=INITSIZE;
12}
13
14int owl_list_get_size(const owl_list *l)
15{
16  return(l->size);
17}
18
19
20static void owl_list_grow(owl_list *l, int n)
21{
22  void *ptr;
23
24  if ((l->size+n) > l->avail) {
25    int avail = MAX(l->avail * GROWBY, l->size + n);
26    ptr = g_renew(void *, l->list, avail);
27    if (ptr==NULL) abort();
28    l->list=ptr;
29    l->avail = avail;
30  }
31
32}
33
34void *owl_list_get_element(const owl_list *l, int n)
35{
36  if (n>l->size-1) return(NULL);
37  return(l->list[n]);
38}
39
40int owl_list_insert_element(owl_list *l, int at, void *element)
41{
42  int i;
43  if(at < 0 || at > l->size) return -1;
44  owl_list_grow(l, 1);
45
46  for (i=l->size; i>at; i--) {
47    l->list[i]=l->list[i-1];
48  }
49
50  l->list[at] = element;
51  l->size++;
52  return(0);
53}
54
55int owl_list_append_element(owl_list *l, void *element)
56{
57  return owl_list_insert_element(l, l->size, element);
58}
59
60int owl_list_prepend_element(owl_list *l, void *element)
61{
62  return owl_list_insert_element(l, 0, element);
63}
64
65int owl_list_remove_element(owl_list *l, int n)
66{
67  int i;
68
69  if (n>l->size-1) return(-1);
70  for (i=n; i<l->size-1; i++) {
71    l->list[i]=l->list[i+1];
72  }
73  l->size--;
74  return(0);
75}
76
77void owl_list_cleanup(owl_list *l, void (*elefree)(void *))
78{
79  int i;
80
81  if (elefree) {
82    for (i = 0; i < l->size; i++) {
83      (elefree)(l->list[i]);
84    }
85  }
86  g_free(l->list);
87}
Note: See TracBrowser for help on using the repository browser.