annotate src/klist.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents aa1b2cac9edc
children
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: klist.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 24
diff changeset
4 * Copyright 2001-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 * A simple ADT for Key-Data pairs
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 * NOTE: this ADT is not perfect. The possibility of holding a Key, after
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 * its data has been removed, long enough for the key-counter to reset and
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 * reuse the same key is very low, but EXISTS. So, the responsibility
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 * remains with the caller.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include "klist.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 * Compare function for searching data by its key
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 static int Klist_node_by_key_cmp(const void *Node, const void *key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 return ((KlistNode_t *)Node)->Key - VOIDP2INT(key);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 /*
24
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
33 * Compare function for searching data by node
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
34 */
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
35 static int Klist_node_by_node_cmp(const void *Node1, const void *Node2)
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
36 {
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
37 return ((KlistNode_t *)Node1)->Key - ((KlistNode_t *)Node2)->Key;
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
38 }
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
39
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
40 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 * Return the data pointer for a given Key (or NULL if not found)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 void *a_Klist_get_data(Klist_t *Klist, int Key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 void *aux;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 if (!Klist)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 aux = dList_find_sorted(Klist->List, INT2VOIDP(Key), Klist_node_by_key_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 return (aux) ? ((KlistNode_t*)aux)->Data : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 * Insert a data pointer and return a key for it.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 int a_Klist_insert(Klist_t **Klist, void *Data)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 KlistNode_t *Node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 if (!*Klist) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 (*Klist) = dNew(Klist_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 (*Klist)->List = dList_new(32);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 (*Klist)->Clean = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 (*Klist)->Counter = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 /* This avoids repeated keys at the same time */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 do {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 if (++((*Klist)->Counter) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 (*Klist)->Counter = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 (*Klist)->Clean = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 } while (!((*Klist)->Clean) &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 a_Klist_get_data((*Klist), (*Klist)->Counter));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 Node = dNew(KlistNode_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 Node->Key = (*Klist)->Counter;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 Node->Data = Data;
24
a7fb418cbedc Fixed a subtle bug in klist that was affecting IO.
jcid
parents: 0
diff changeset
79 dList_insert_sorted((*Klist)->List, Node, Klist_node_by_node_cmp);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 return (*Klist)->Counter;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 * Remove data by Key
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 void a_Klist_remove(Klist_t *Klist, int Key)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 void *data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 data = dList_find_sorted(Klist->List, INT2VOIDP(Key),Klist_node_by_key_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 if (data) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 dList_remove(Klist->List, data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 dFree(data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 if (dList_length(Klist->List) == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 Klist->Clean = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 * Return the number of elements in the Klist
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 int a_Klist_length(Klist_t *Klist)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 return dList_length(Klist->List);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 * Free a Klist
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 void a_Klist_free(Klist_t **KlistPtr)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 void *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 Klist_t *Klist = *KlistPtr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 if (!Klist)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 while (dList_length(Klist->List) > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 node = dList_nth_data(Klist->List, 0);
1079
aa1b2cac9edc Fix a memory leak in a_Klist_free()
corvid <corvid@lavabit.com>
parents: 966
diff changeset
120 dList_remove_fast(Klist->List, node);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 dFree(node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 }
966
dfc29c3df4f7 [mq]: fix-klist-leak
Jeremy Henty <onepoint@starurchin.org>
parents: 84
diff changeset
123 dList_free(Klist->List);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 dFree(Klist);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 *KlistPtr = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127