Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Starting to clean up the code and look for obvious leaks.

  • Loading branch information...
commit 1573547e163eb327e4898accb2649adebe347b0b 1 parent 88ad4d2
Nick Gerakines authored
9 Makefile
@@ -15,17 +15,20 @@ endif
15 15 CCOPT= $(CFLAGS) $(CCLINK) $(ARCH) $(PROF)
16 16 DEBUG?= -g -rdynamic -ggdb
17 17
18   -OBJ = barbershop.o
  18 +OBJ = barbershop.o bst.o scores.o
19 19 CLIOBJ = client.o
20 20
21 21 PRGNAME = barbershop
22 22 CLIPRGNAME = client
23 23
24   -all: barbershop client
  24 +all: barbershop
25 25
26 26 # Deps (use make dep to generate this)
27 27 client.o: client.c
28   -barbershop.o: barbershop.c bst.c bst.h
  28 +barbershop.o: barbershop.c
  29 +bst.o: bst.c bst.h
  30 +scores.o: scores.c scores.h
  31 +benchmark.o: benchmark.c
29 32
30 33 barbershop: $(OBJ)
31 34 $(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ)
175 barbershop.c
@@ -15,67 +15,11 @@ Copyright (c) 2010 Nick Gerakines <nick at gerakines dot net>
15 15 #include <errno.h>
16 16 #include <err.h>
17 17
18   -#include "bst.c"
  18 +#include "scores.h"
  19 +#include "bst.h"
  20 +#include "barbershop.h"
19 21 #include <event.h>
20 22
21   -/* Port to listen on. */
22   -#define SERVER_PORT 8002
23   -
24   -struct client {
25   - struct event ev_read;
26   -};
27   -
28   -SearchTree items;
29   -
30   -typedef struct member_el {
31   - int item;
32   - struct member_el *next;
33   -};
34   -
35   -typedef struct member_el *MemberBucket;
36   -
37   -typedef struct bucket_el {
38   - int score;
39   - int count;
40   - MemberBucket members;
41   - struct bucket_el *next;
42   -};
43   -
44   -typedef struct bucket_el *ScoreBucket;
45   -
46   -ScoreBucket scores;
47   -
48   -ScoreBucket PrepScoreBucket(ScoreBucket bucket);
49   -ScoreBucket initScorePool(int score, int item_id);
50   -ScoreBucket PurgeThenAddScoreToPool(ScoreBucket bucket, int score, int item_id, int old_score);
51   -ScoreBucket AddScoreToPool(ScoreBucket bucket, int score, int item_id);
52   -ScoreBucket AddScoreMember(ScoreBucket bucket, int item);
53   -ScoreBucket doesPoolExist(ScoreBucket bucket, int score);
54   -int IsScoreMember(MemberBucket head, int item);
55   -void DumpScores(ScoreBucket head);
56   -void DumpMembers(MemberBucket head);
57   -MemberBucket DeleteMember(MemberBucket head, int item);
58   -
59   -int setnonblock(int fd) {
60   - int flags;
61   - flags = fcntl(fd, F_GETFL);
62   - if (flags < 0) { return flags; }
63   - flags |= O_NONBLOCK;
64   - if (fcntl(fd, F_SETFL, flags) < 0) { return -1; }
65   - return 0;
66   -}
67   -
68   -typedef struct token_s {
69   - char *value;
70   - size_t length;
71   -} token_t;
72   -
73   -#define COMMAND_TOKEN 0
74   -#define SUBCOMMAND_TOKEN 1
75   -#define KEY_TOKEN 1
76   -#define VALUE_TOKEN 2
77   -#define MAX_TOKENS 8
78   -
79 23 static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) {
80 24 char *s, *e;
81 25 size_t ntokens = 0;
@@ -104,12 +48,6 @@ static size_t tokenize_command(char *command, token_t *tokens, const size_t max_
104 48 return ntokens;
105 49 }
106 50
107   -void reply(int fd, char *buffer) {
108   - int n = write(fd, buffer, strlen(buffer));
109   - if (n < 0 || n < strlen(buffer))
110   - printf("ERROR writing to socket");
111   -}
112   -
113 51 void on_read(int fd, short ev, void *arg) {
114 52 struct client *client = (struct client *)arg;
115 53 char buf[8196];
@@ -212,102 +150,17 @@ int main(int argc, char **argv) {
212 150 return 0;
213 151 }
214 152
215   -// If the bucket is empty/null, create first and exit
216   -// If the score pool exists, add member to it
217   -// if the score pool does not exist, add to create last and exit
218   -ScoreBucket PurgeThenAddScoreToPool(ScoreBucket bucket, int score, int item_id, int old_score) {
219   - ScoreBucket lookup = doesPoolExist(bucket, old_score);
220   - lookup->members = DeleteMember(lookup->members, item_id);
221   - lookup->members -= 1;
222   - return AddScoreToPool(bucket, score, item_id);
223   -}
224   -
225   -ScoreBucket AddScoreToPool(ScoreBucket bucket, int score, int item_id) {
226   - if (bucket == NULL) {
227   - return initScorePool(score, item_id);
228   - }
229   - ScoreBucket lookup = doesPoolExist(bucket, score);
230   - if (lookup == NULL) {
231   - ScoreBucket head = initScorePool(score, item_id);
232   - head->next = bucket;
233   - return head;
234   - }
235   - lookup = AddScoreMember(lookup, item_id);
236   - return bucket;
237   -}
238   -
239   -ScoreBucket initScorePool(int score, int item_id) {
240   - ScoreBucket head = malloc(sizeof(struct bucket_el));
241   - head = malloc( sizeof( struct bucket_el ) );
242   - if (head == NULL ) {
243   - exit(1);
244   - } else {
245   - MemberBucket member = malloc( sizeof( struct member_el ) );
246   - member->item = item_id;
247   - member->next = NULL;
248   - head->members = member;
249   - head->score = score;
250   - head->count = 1;
251   - head->next = NULL;
252   - return head;
253   - }
254   - return NULL;
255   -}
256   -
257   -ScoreBucket AddScoreMember(ScoreBucket bucket, int item) {
258   - if (IsScoreMember(bucket->members, item)) {
259   - return bucket;
260   - }
261   - MemberBucket head = malloc(sizeof(struct member_el));
262   - head = malloc( sizeof( struct member_el ) );
263   - if (head == NULL ) {
264   - exit(1);
265   - } else {
266   - head->next = bucket->members;
267   - head->item = item;
268   - bucket->members = head;
269   - bucket->count += 1;
270   - return bucket;
271   - }
272   - return NULL;
273   -}
274   -
275   -ScoreBucket PrepScoreBucket(ScoreBucket bucket) {
276   - if (bucket != NULL) {
277   - free(bucket);
278   - }
279   - return NULL;
280   -}
281   -
282   -int IsScoreMember(MemberBucket head, int item) {
283   - if (head == NULL) { return 0; }
284   - if (head->item == item) {return 1; }
285   - return IsScoreMember(head->next, item);
286   -}
287   -
288   -ScoreBucket doesPoolExist(ScoreBucket bucket, int score) {
289   - if (bucket == NULL) { return NULL; }
290   - if (bucket->score == score) { return bucket; }
291   - return doesPoolExist(bucket->next, score);
292   -}
293   -
294   -void DumpScores(ScoreBucket head) {
295   - if (head == NULL) { return; }
296   - printf("Score %d (%d)", head->score, head->count);
297   - DumpMembers(head->members);
298   - printf("\n");
299   - DumpScores(head->next);
300   -}
301   -
302   -void DumpMembers(MemberBucket head) {
303   - if (head == NULL) { return; }
304   - printf(" %d", head->item);
305   - DumpMembers(head->next);
  153 +int setnonblock(int fd) {
  154 + int flags;
  155 + flags = fcntl(fd, F_GETFL);
  156 + if (flags < 0) { return flags; }
  157 + flags |= O_NONBLOCK;
  158 + if (fcntl(fd, F_SETFL, flags) < 0) { return -1; }
  159 + return 0;
306 160 }
307 161
308   -MemberBucket DeleteMember(MemberBucket head, int item) {
309   - if (head == NULL) { return NULL; }
310   - if (head->item == item) { return head->next; }
311   - head->next = DeleteMember(head->next, item);
312   - return head;
  162 +void reply(int fd, char *buffer) {
  163 + int n = write(fd, buffer, strlen(buffer));
  164 + if (n < 0 || n < strlen(buffer))
  165 + printf("ERROR writing to socket");
313 166 }
48 barbershop.h
... ... @@ -0,0 +1,48 @@
  1 +
  2 +#include <event.h>
  3 +
  4 +#define COMMAND_TOKEN 0
  5 +#define SUBCOMMAND_TOKEN 1
  6 +#define KEY_TOKEN 1
  7 +#define VALUE_TOKEN 2
  8 +#define MAX_TOKENS 8
  9 +#define SERVER_PORT 8002
  10 +
  11 +struct client {
  12 + struct event ev_read;
  13 +};
  14 +
  15 +struct TreeNode {
  16 + int item;
  17 + int score;
  18 + SearchTree left;
  19 + SearchTree right;
  20 +};
  21 +
  22 +struct member_el {
  23 + int item;
  24 + MemberBucket next;
  25 +};
  26 +
  27 +struct bucket_el {
  28 + int score;
  29 + int count;
  30 + MemberBucket members;
  31 + ScoreBucket next;
  32 +};
  33 +
  34 +typedef struct token_s {
  35 + char *value;
  36 + size_t length;
  37 +} token_t;
  38 +
  39 +SearchTree items;
  40 +ScoreBucket scores;
  41 +
  42 +static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens);
  43 +void on_read(int fd, short ev, void *arg);
  44 +void on_accept(int fd, short ev, void *arg);
  45 +int main(int argc, char **argv);
  46 +int setnonblock(int fd);
  47 +void reply(int fd, char *buffer);
  48 +
5 bst.c
... ... @@ -1,11 +1,12 @@
1 1 #include "bst.h"
2 2 #include <stdlib.h>
  3 +#include <stdio.h>
3 4
4 5 struct TreeNode {
5 6 int item;
6 7 int score;
7   - SearchTree left;
8   - SearchTree right;
  8 + SearchTree left;
  9 + SearchTree right;
9 10 };
10 11
11 12 SearchTree MakeEmpty(SearchTree tree) {
2  bst.h
... ... @@ -1,5 +1,3 @@
1   -typedef int ElementType;
2   -
3 1 #ifndef _Tree_H
4 2 #define _Tree_H
5 3
110 scores.c
... ... @@ -0,0 +1,110 @@
  1 +#include "scores.h"
  2 +#include <stdlib.h>
  3 +#include <stdio.h>
  4 +
  5 +struct member_el {
  6 + int item;
  7 + MemberBucket next;
  8 +};
  9 +
  10 +struct bucket_el {
  11 + int score;
  12 + int count;
  13 + MemberBucket members;
  14 + ScoreBucket next;
  15 +};
  16 +
  17 +ScoreBucket PurgeThenAddScoreToPool(ScoreBucket bucket, int score, int item_id, int old_score) {
  18 + ScoreBucket lookup = doesPoolExist(bucket, old_score);
  19 + lookup->members = DeleteMember(lookup->members, item_id);
  20 + lookup->count -= 1;
  21 + return AddScoreToPool(bucket, score, item_id);
  22 +}
  23 +
  24 +ScoreBucket AddScoreToPool(ScoreBucket bucket, int score, int item_id) {
  25 + if (bucket == NULL) {
  26 + return initScorePool(score, item_id);
  27 + }
  28 + ScoreBucket lookup = doesPoolExist(bucket, score);
  29 + if (lookup == NULL) {
  30 + ScoreBucket head = initScorePool(score, item_id);
  31 + head->next = bucket;
  32 + return head;
  33 + }
  34 + lookup = AddScoreMember(lookup, item_id);
  35 + return bucket;
  36 +}
  37 +
  38 +ScoreBucket initScorePool(int score, int item_id) {
  39 + ScoreBucket head = malloc(sizeof(ScoreBucket));
  40 + if (head == NULL ) {
  41 + exit(1);
  42 + } else {
  43 + MemberBucket member = malloc( sizeof(MemberBucket ) );
  44 + member->item = item_id;
  45 + member->next = NULL;
  46 + head->members = member;
  47 + head->score = score;
  48 + head->count = 1;
  49 + head->next = NULL;
  50 + return head;
  51 + }
  52 + return NULL;
  53 +}
  54 +
  55 +ScoreBucket AddScoreMember(ScoreBucket bucket, int item) {
  56 + if (IsScoreMember(bucket->members, item)) {
  57 + return bucket;
  58 + }
  59 + MemberBucket member = malloc(sizeof(MemberBucket));
  60 + if (member == NULL ) {
  61 + exit(1);
  62 + } else {
  63 + member->next = bucket->members;
  64 + member->item = item;
  65 + bucket->members = member;
  66 + bucket->count += 1;
  67 + return bucket;
  68 + }
  69 + return NULL;
  70 +}
  71 +
  72 +ScoreBucket PrepScoreBucket(ScoreBucket bucket) {
  73 + if (bucket != NULL) {
  74 + free(bucket);
  75 + }
  76 + return NULL;
  77 +}
  78 +
  79 +int IsScoreMember(MemberBucket head, int item) {
  80 + if (head == NULL) { return 0; }
  81 + if (head->item == item) {return 1; }
  82 + return IsScoreMember(head->next, item);
  83 +}
  84 +
  85 +ScoreBucket doesPoolExist(ScoreBucket bucket, int score) {
  86 + if (bucket == NULL) { return NULL; }
  87 + if (bucket->score == score) { return bucket; }
  88 + return doesPoolExist(bucket->next, score);
  89 +}
  90 +
  91 +void DumpScores(ScoreBucket head) {
  92 + if (head == NULL) { return; }
  93 + printf("Score %d (%d)", head->score, head->count);
  94 + DumpMembers(head->members);
  95 + printf("\n");
  96 + DumpScores(head->next);
  97 +}
  98 +
  99 +void DumpMembers(MemberBucket head) {
  100 + if (head == NULL) { return; }
  101 + printf(" %d", head->item);
  102 + DumpMembers(head->next);
  103 +}
  104 +
  105 +MemberBucket DeleteMember(MemberBucket head, int item) {
  106 + if (head == NULL) { return NULL; }
  107 + if (head->item == item) { return head->next; }
  108 + head->next = DeleteMember(head->next, item);
  109 + return head;
  110 +}
20 scores.h
... ... @@ -0,0 +1,20 @@
  1 +#ifndef __SCORES_H__
  2 +#define __SCORES_H__
  3 +
  4 +struct member_el;
  5 +struct bucket_el;
  6 +typedef struct member_el *MemberBucket;
  7 +typedef struct bucket_el *ScoreBucket;
  8 +
  9 +ScoreBucket PurgeThenAddScoreToPool(ScoreBucket bucket, int score, int item_id, int old_score);
  10 +ScoreBucket AddScoreToPool(ScoreBucket bucket, int score, int item_id);
  11 +ScoreBucket initScorePool(int score, int item_id);
  12 +ScoreBucket AddScoreMember(ScoreBucket bucket, int item);
  13 +ScoreBucket PrepScoreBucket(ScoreBucket bucket);
  14 +int IsScoreMember(MemberBucket head, int item);
  15 +ScoreBucket doesPoolExist(ScoreBucket bucket, int score);
  16 +void DumpScores(ScoreBucket head);
  17 +void DumpMembers(MemberBucket head);
  18 +MemberBucket DeleteMember(MemberBucket head, int item);
  19 +
  20 +#endif

0 comments on commit 1573547

Please sign in to comment.
Something went wrong with that request. Please try again.