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

Added error handling.

Changeset 57c05feedb89

Parent 2ee7a6ba1424

by Rup Palchowdhury

Changes to 10 files · Browse files at 57c05feedb89 Showing diff from parent 2ee7a6ba1424 Diff from another changeset...

Change 1 of 2 Show Entire File Makefile Stacked
 
14
15
16
17
 
18
19
20
21
22
23
 
57
58
59
60
 
 
14
15
16
 
17
18
19
 
20
21
22
 
56
57
58
 
59
@@ -14,10 +14,9 @@
 POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d    SRC = \ - kbuffer.c \ + eprintf.c \   khash.c \   klist.c \ - klog.c \   kpost.c \   kstack.c \   ktree.c @@ -57,4 +56,4 @@
   .PHONY: clean  clean: - rm -f $(O)/* $(DEPDIR)/* libk.a test + rm -f $(O)/*.o $(DEPDIR)/* libk.a test
Change 1 of 1 Show Entire File kcommon.h Stacked
 
1
2
 
 
 
 
 
1
2
@@ -1,2 +1,2 @@
-typedef int (*fn_cmp)(void*, void*); -typedef unsigned (*fn_hash)(void*, unsigned); +typedef int (*fn_cmp)(void *, void *); +typedef unsigned (*fn_hash)(void *, unsigned);
Change 1 of 2 Show Entire File khash.c Stacked
 
1
2
3
 
4
5
6
7
8
9
10
11
 
 
12
13
14
15
16
 
 
 
17
18
19
 
85
86
87
88
89
90
91
92
 
1
2
3
4
5
6
7
8
9
10
 
 
11
12
13
14
 
 
 
15
16
17
18
19
20
 
86
87
88
 
 
89
90
91
@@ -1,19 +1,20 @@
 #include <stdio.h>  #include <stdlib.h>  #include <string.h> +#include "eprintf.h"  #include "kcommon.h"  #include "klist.h"  #include "khash.h"    Hash *newhash(unsigned n, fn_cmp cmp, fn_hash hash) {   Hash *h; - h = (Hash *)malloc(sizeof(Hash)); - h->tab = (Node **)malloc(sizeof(Node *) * n); + h = (Hash *)emalloc(sizeof(Hash)); + h->tab = (Node **)emalloc(sizeof(Node *) * n);   for (int i = 0; i < n; i++)   h->tab[i] = NULL; - h->nel = 0; - h->n = n; - h->cmp = cmp; + h->nel = 0; + h->n = n; + h->cmp = cmp;   h->hash = hash;   return h;  } @@ -85,8 +86,6 @@
 {   int flag;   for (int i = 0; i < h->n; i++) { - if (h->tab[i] == NULL) - continue;   flag = 0;   for (Node *np = h->tab[i]; np != NULL; np = np->next) {   if (flag == 0) {
Change 1 of 1 Show Entire File khash.h Stacked
 
7
8
9
10
11
12
13
14
15
16
 
 
 
 
 
 
 
 
7
8
9
 
 
 
 
 
 
 
10
11
12
13
14
15
16
@@ -7,10 +7,10 @@
  fn_hash hash;  };   -Hash *newhash(unsigned, fn_cmp, fn_hash); -void freehash(Hash*, void (*freedata)(void*)); -Node *hlookup(Hash*, Node*, int); -int _strcmp(void *d1, void *d2); -unsigned _strhash(void*, unsigned); -void fprinthashstats(FILE*, Hash*); -void fprinthash(FILE*, Hash*, void (*printdata)(FILE*, void*)); +Hash *newhash(unsigned, fn_cmp, fn_hash); +void freehash(Hash *, void (*freedata)(void *)); +Node *hlookup(Hash *, Node *, int); +int _strcmp(void *, void *); +unsigned _strhash(void *, unsigned); +void fprinthashstats(FILE *, Hash *); +void fprinthash(FILE *, Hash *, void (*printdata)(FILE *, void *));
Change 1 of 3 Show Entire File klist.c Stacked
 
1
2
3
 
4
5
6
7
8
 
9
10
11
 
13
14
15
16
 
17
18
19
 
47
48
49
50
 
51
52
53
 
54
55
56
57
58
59
60
 
61
62
63
64
65
 
66
67
68
69
70
 
 
71
72
73
74
75
76
77
78
79
80
81
82
 
 
 
83
84
85
86
87
 
1
2
3
4
5
6
7
8
 
9
10
11
12
 
14
15
16
 
17
18
19
20
 
48
49
50
 
51
52
53
 
54
55
56
57
58
59
60
 
61
62
63
64
65
 
66
67
68
69
70
 
71
72
73
74
75
76
 
77
78
79
80
81
 
 
82
83
84
85
 
86
87
88
@@ -1,11 +1,12 @@
 #include <stdio.h>  #include <stdlib.h>  #include <string.h> +#include "eprintf.h"  #include "klist.h"    Node *newnode(void *data)  { - Node *n = (Node *)malloc(sizeof(Node)); + Node *n = (Node *)emalloc(sizeof(Node));   n->data = data;   n->next = NULL;   return n; @@ -13,7 +14,7 @@
   void freenode(Node *n, void (*freedata)(void*))  { - if (n == NULL || freedata == NULL) + if (freedata == NULL)   return;   (*freedata)(n->data);   free(n); @@ -47,41 +48,41 @@
  return p;  }   -Node *lookup(Node *p, Node *n, int (*nodecmp)(void*, void*)) +Node *lookup(Node *p, Node *n, int (*cmp)(void*, void*))  {   for (; p != NULL; p = p->next) { - if ((*nodecmp)(p->data, n->data) == 0) + if ((*cmp)(p->data, n->data) == 0)   return p;   }   return NULL;  }    Node *delnode(Node *p, Node *n, - int (*nodecmp)(void*, void*), + int (*cmp)(void*, void*),   void (*freedata)(void*))  {   Node *i, *prev;   for (i = p; i->next != NULL; i = i->next) { - if ((*nodecmp)(i->data, n->data) == 0) { + if ((*cmp)(i->data, n->data) == 0) {   if (prev == NULL)   p = i->next;   else   prev->next = i->next; - freenode(i, freedata); + if (freedata != NULL) + freenode(i, freedata);   return p;   }   prev = i;   } - fprintf(stderr, "delnode: matching node not found");   return NULL;  }    void apply(Node *p, void (*fn)(void*, void*), void *arg)  { - for (; p != NULL; p = p->next) { - /* printf("%p %p\n", p, p->next); */ + if (fn == NULL) + return; + for (; p != NULL; p = p->next)   (*fn)(p->data, arg); - }  }    void inccounter(void *p, void *arg)
Change 1 of 1 Show Entire File klist.h Stacked
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
 
 
 
 
 
 
 
 
 
 
 
5
6
7
 
 
 
 
 
 
 
 
 
 
 
8
9
10
11
12
13
14
15
16
17
18
@@ -5,14 +5,14 @@
  Node *next;  };   -Node *newnode(void*); -void freenode(Node*, void (*freedata)(void*)); -Node *addfront(Node*, Node*); -Node *addend(Node*, Node*); -Node *lookup(Node*, Node*, int (*nodecmp)(void*, void*)); -void apply(Node*, void (*fn)(void*, void*), void*); -Node *delnode(Node*, Node*, - int (*nodecmp)(void*, void*), - void (*freedata)(void*)); -void freelist(Node*, void (*freedata)(void*)); -void inccounter(void*, void*); +Node *newnode(void *); +void freenode(Node *, void (*freedata)(void *)); +Node *addfront(Node *, Node *); +Node *addend(Node *, Node *); +Node *lookup(Node *, Node *, int (*cmp)(void *, void *)); +void apply(Node *, void (*fn)(void *, void *), void *); +Node *delnode(Node *, Node *, + int (*cmp)(void *, void *), + void (*freedata)(void *)); +void freelist(Node *, void (*freedata)(void *)); +void inccounter(void *, void *);
Change 1 of 1 Show Entire File kpost.c Stacked
 
1
2
3
 
4
5
6
7
8
9
 
10
11
12
 
1
2
3
4
5
6
7
8
9
 
10
11
12
13
@@ -1,12 +1,13 @@
 #include <stdio.h>  #include <stdlib.h>  #include <string.h> +#include "eprintf.h"  #include "kpost.h"    KPost *newkpost(char *s, int f)  {   KPost *t; - t = (KPost *)malloc(sizeof(KPost)); + t = (KPost *)emalloc(sizeof(KPost));   t->s = s;   t->f = f;   return t;
Change 1 of 1 Show Entire File kstack.h Stacked
 
5
6
7
8
9
10
11
 
 
 
 
 
5
6
7
 
 
 
 
8
9
10
11
@@ -5,7 +5,7 @@
  int n;  };   -void initstack(Stack*); -void push(Stack*, void*); -void* pop(Stack*); -void purge(Stack*); +void initstack(Stack *); +void push(Stack *, void *); +void *pop(Stack *); +void purge(Stack *);
Change 1 of 2 Show Entire File ktree.c Stacked
 
 
1
 
2
3
4
5
6
7
 
8
9
10
 
 
11
12
13
14
15
16
 
17
18
19
 
20
21
22
23
 
24
25
26
 
27
28
29
30
 
 
31
32
33
34
35
 
 
36
37
 
38
39
40
41
42
 
43
44
45
46
 
47
48
49
50
51
52
 
 
53
54
55
56
57
 
58
59
60
 
63
64
65
66
 
67
68
 
69
70
71
72
 
 
73
74
75
 
 
76
77
 
78
79
80
 
81
82
 
83
84
85
86
87
 
 
88
89
 
90
91
92
 
1
2
3
4
5
6
7
8
 
9
10
 
 
11
12
13
14
15
16
17
 
18
19
 
 
20
21
22
23
 
24
25
26
 
27
28
29
 
 
30
31
32
33
34
 
 
35
36
37
 
38
39
40
41
42
 
43
44
45
46
 
47
48
49
50
51
 
 
52
53
54
55
56
57
 
58
59
60
61
 
64
65
66
 
67
68
 
69
70
71
 
 
72
73
74
 
 
75
76
77
 
78
79
80
 
81
82
 
83
84
85
86
 
 
87
88
89
 
90
91
92
93
@@ -1,60 +1,61 @@
+#include <stdio.h>  #include <stdlib.h> +#include "eprintf.h"  #include "ktree.h"    TNode *newtnode(void *data)  {   TNode *n; - n = (TNode *)malloc(sizeof(TNode)); + n = (TNode *)emalloc(sizeof(TNode));   n->data = data; - n->l = NULL; - n->r = NULL; + n->l = NULL; + n->r = NULL;   return n;  }    void freetnode(TNode *n, void (*freedata)(void*))  { - if (n != NULL) { + if (freedata != NULL)   (*freedata)(n->data); - free(n); - } + free(n);  }    TNode *insert(TNode *treep, TNode *n, - int (*tnodecmp)(void*, void*), + int (*cmp)(void*, void*),   int (*match_handler)(void*, void*))  { - int cmp; + int ret;   if (treep == NULL)   return n; - cmp = (*tnodecmp)(n->data, treep->data); - if (cmp == 0) { + ret = (*cmp)(n->data, treep->data); + if (ret == 0) {   if (match_handler != NULL)   (*match_handler)(n->data, treep->data);   } - else if (cmp < 0) - treep->l = insert(treep->l, n, tnodecmp, match_handler); + else if (ret < 0) + treep->l = insert(treep->l, n, cmp, match_handler);   else - treep->r = insert(treep->r, n, tnodecmp, match_handler); + treep->r = insert(treep->r, n, cmp, match_handler);   return treep;  }    TNode *nrinsert(TNode *treep, TNode *newp, - int (*tnodecmp)(void*, void*), + int (*cmp)(void*, void*),   int (*match_handler)(void*, void*))  {   TNode **p; - int cmp; + int ret;   if (treep == NULL)   return newp;   p = &treep;   while (*p != NULL) { - cmp = (*tnodecmp)(newp->data, (*p)->data); - if (cmp == 0) { + ret = (*cmp)(newp->data, (*p)->data); + if (ret == 0) {   if (match_handler != NULL)   (*match_handler)(newp->data, (*p)->data);   return treep;   } - else if (cmp < 0) + else if (ret < 0)   p = &((*p)->l);   else   p = &((*p)->r); @@ -63,30 +64,30 @@
  return treep;  }   -TNode *tlookup(TNode *p, TNode *n, int (*tnodecmp)(void*, void*)) +TNode *tlookup(TNode *p, TNode *n, int (*cmp)(void*, void*))  { - int cmp; + int ret;   if (p == NULL)   return NULL; - cmp = (*tnodecmp)(n->data, p->data); - if (cmp == 0) + ret = (*cmp)(n->data, p->data); + if (ret == 0)   return p; - else if (cmp < 0) - return tlookup(p->l, n, tnodecmp); + else if (ret < 0) + return tlookup(p->l, n, cmp);   else - return tlookup(p->r, n, tnodecmp); + return tlookup(p->r, n, cmp);  }   -TNode *nrtlookup(TNode *treep, TNode *newp, int (*tnodecmp)(void*, void*)) +TNode *nrtlookup(TNode *treep, TNode *newp, int (*cmp)(void*, void*))  { - int cmp; + int ret;   TNode **p;   p = &treep;   while (*p != NULL) { - cmp = (*tnodecmp)(newp->data, (*p)->data); - if (cmp == 0) + ret = (*cmp)(newp->data, (*p)->data); + if (ret == 0)   return *p; - else if (cmp < 0) + else if (ret < 0)   p = &((*p)->l);   else   p = &((*p)->r);
Change 1 of 1 Show Entire File ktree.h Stacked
 
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
 
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
9
10
11
12
13
14
15
16
17
18
19
20
@@ -6,15 +6,15 @@
  TNode *r;  };   -TNode *newtnode(void*); -void freetnode(TNode*, void (*freedata)(void*)); -TNode *insert(TNode*, TNode*, - int (*tnodecmp)(void*, void*), - int (*match_handler)(void*, void*)); -TNode *nrinsert(TNode*, TNode*, - int (*tnodecmp)(void*, void*), - int (*match_handler)(void*, void*)); -TNode *tlookup(TNode*, TNode*, int (*tnodecmp)(void*, void*)); -TNode *nrtlookup(TNode*, TNode*, int (*tnodecmp)(void*, void*)); -void applyinorder(TNode*, void (*fn)(void*, void*), void*); -void applypostorder(TNode*, void (*fn)(void*, void*), void*); +TNode *newtnode(void *); +void freetnode(TNode *, void (*freedata)(void *)); +TNode *insert(TNode *, TNode *, + int (*cmp)(void *, void *), + int (*match_handler)(void *, void *)); +TNode *nrinsert(TNode *, TNode *, + int (*cmp)(void *, void *), + int (*match_handler)(void *, void *)); +TNode *tlookup(TNode *, TNode *, int (*cmp)(void *, void *)); +TNode *nrtlookup(TNode *, TNode *, int (*cmp)(void *, void *)); +void applyinorder(TNode *, void (*fn)(void *, void *), void *); +void applypostorder(TNode *, void (*fn)(void *, void *), void *);