Repositories » libk0
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Tuned memory deallocations.

Changeset 148f8144e7c9

Parent 9b8bb97f199d

by Rup Palchowdhury

Changes to 2 files · Browse files at 148f8144e7c9 Showing diff from parent 9b8bb97f199d Diff from another changeset...

Change 1 of 2 Show Entire File khash.c Stacked
 
10
11
12
 
13
14
15
16
17
 
29
30
31
32
 
33
34
35
 
36
37
38
 
10
11
12
13
14
 
15
16
17
 
29
30
31
 
32
33
34
 
35
36
37
38
@@ -10,8 +10,8 @@
  h->tab = (Node **)malloc(sizeof(Node *) * n);   for (int i = 0; i < n; i++)   h->tab[i] = NULL; + h->nel = 0;   h->n = n; - h->nel = 0;   h->cmp = cmp;   h->hash = hash;   return h; @@ -29,10 +29,10 @@
 {   static unsigned MULTIPLIER = 31;   unsigned h; - unsigned char *p, *s; + unsigned char *s;   s = (unsigned char *)data;   h = 0; - for (p = (unsigned char *)s; *p != '\0'; p++) + for (unsigned char *p = (unsigned char *)s; *p != '\0'; p++)   h = MULTIPLIER * h + *p;   return h % size;  }
Change 1 of 2 Show Entire File klist.c Stacked
 
13
14
15
16
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
19
20
 
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
13
14
15
 
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
78
79
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
82
83
@@ -13,8 +13,21 @@
   void freenode(Node *n, void (*freedata)(void*))  { - if (n != NULL) { - (*freedata)(n->data); + if (n == NULL || freedata == NULL) + return; + (*freedata)(n->data); + free(n); +} + +void freelist(Node *n, void (*freedata)(void*)) +{ + if (n == NULL) + return; + Node *next; + for (; n != NULL; n = next) { + next = n->next; + if (freedata != NULL) + (*freedata)(n->data);   free(n);   }  } @@ -65,20 +78,6 @@
  return NULL;  }   -void freelist(Node *p, void (*freedata)(void*)) -{ - if (p == NULL) - return; - Node *next; - for (; p != NULL; p = next) { - next = p->next; - if (freedata == NULL) - free(p); - else - freenode(p, freedata); - } -} -  void apply(Node *p, void (*fn)(void*, void*), void *arg)  {   for (; p != NULL; p = p->next) {