root/openpgpsdk/trunk/src/lists.c

Revision 371 (checked in by ben, 7 years ago)

Make sure dmalloc happens last.

Line 
1 /**
2  * \file
3  *
4  * Set of functions to manage a dynamic list
5  */
6
7 #include <openpgpsdk/lists.h>
8
9 #include <stdlib.h>
10
11 #include <openpgpsdk/final.h>
12
13 /**
14  * Initialises ulong list
15  * \param *list Pointer to existing list structure
16  */
17 void ops_ulong_list_init(ops_ulong_list_t *list)
18     {
19     list->size=0;
20     list->used=0;
21     list->ulongs=NULL;
22     }
23  
24 /**
25  * Frees allocated memory in ulong list. Does not free *list itself.
26  *
27  * \param *list
28  */
29 void ops_ulong_list_free(ops_ulong_list_t *list)
30     {
31     if (list->ulongs)
32         free(list->ulongs);
33     ops_ulong_list_init(list);
34     }
35
36 /**
37  * Resizes ulong list.
38  *
39  * We only resize in one direction - upwards.
40  * Algorithm used : double the current size then add 1
41  *
42  * \param *list Pointer to list
43  * \return 1 if success, else 0
44  */
45
46 static unsigned int ops_ulong_list_resize(ops_ulong_list_t *list)
47     {
48
49     int newsize=0;
50
51     newsize=list->size*2 + 1;
52     list->ulongs=realloc(list->ulongs,newsize*sizeof *list->ulongs);
53     if (list->ulongs)
54         {
55         list->size=newsize;
56         return 1;
57         }
58     else
59         {
60         /* xxx - realloc failed. error message? - rachel */
61         return 0;
62         }
63     }
64
65 /**
66  * Adds entry to ulong list
67  *
68  * \param *list
69  * \param *ulong
70  *
71  * \return 1 if success, else 0
72  */
73 unsigned int ops_ulong_list_add(ops_ulong_list_t *list, unsigned long *ulong)
74     {
75     if (list->size==list->used)
76         if (!ops_ulong_list_resize(list))
77             return 0;
78
79     list->ulongs[list->used]=*ulong;
80     list->used++;
81     return 1;
82     }
83
Note: See TracBrowser for help on using the browser.