source: list.c @ c323405

release-1.10release-1.8release-1.9
Last change on this file since c323405 was d427f08, checked in by Nelson Elhage <nelhage@mit.edu>, 13 years ago
Use G_GNUC_WARN_UNUSED_RESULT Have gcc warn us when we ignore the result of a function that requires the caller to free the result, or an initilization function that can fail. This might help (slightly) with preventing leaks and segfaults. Additionally changed some functions that should never fail to not return values. (The owl_list_* functions changed only fail if list->size < 0, which we assume is not the case elsewhere.)
  • Property mode set to 100644
File size: 1.4 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
55void owl_list_append_element(owl_list *l, void *element)
56{
57  owl_list_insert_element(l, l->size, element);
58}
59
60void owl_list_prepend_element(owl_list *l, void *element)
61{
62  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.