Permalink
Browse files

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

  • Loading branch information...
1 parent 88ad4d2 commit 1573547e163eb327e4898accb2649adebe347b0b @ngerakines committed Jan 31, 2010
Showing with 201 additions and 168 deletions.
  1. +6 −3 Makefile
  2. +14 −161 barbershop.c
  3. +48 −0 barbershop.h
  4. +3 −2 bst.c
  5. +0 −2 bst.h
  6. +110 −0 scores.c
  7. +20 −0 scores.h
View
@@ -15,17 +15,20 @@ endif
CCOPT= $(CFLAGS) $(CCLINK) $(ARCH) $(PROF)
DEBUG?= -g -rdynamic -ggdb
-OBJ = barbershop.o
+OBJ = barbershop.o bst.o scores.o
CLIOBJ = client.o
PRGNAME = barbershop
CLIPRGNAME = client
-all: barbershop client
+all: barbershop
# Deps (use make dep to generate this)
client.o: client.c
-barbershop.o: barbershop.c bst.c bst.h
+barbershop.o: barbershop.c
+bst.o: bst.c bst.h
+scores.o: scores.c scores.h
+benchmark.o: benchmark.c
barbershop: $(OBJ)
$(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ)
View
@@ -15,67 +15,11 @@ Copyright (c) 2010 Nick Gerakines <nick at gerakines dot net>
#include <errno.h>
#include <err.h>
-#include "bst.c"
+#include "scores.h"
+#include "bst.h"
+#include "barbershop.h"
#include <event.h>
-/* Port to listen on. */
-#define SERVER_PORT 8002
-
-struct client {
- struct event ev_read;
-};
-
-SearchTree items;
-
-typedef struct member_el {
- int item;
- struct member_el *next;
-};
-
-typedef struct member_el *MemberBucket;
-
-typedef struct bucket_el {
- int score;
- int count;
- MemberBucket members;
- struct bucket_el *next;
-};
-
-typedef struct bucket_el *ScoreBucket;
-
-ScoreBucket scores;
-
-ScoreBucket PrepScoreBucket(ScoreBucket bucket);
-ScoreBucket initScorePool(int score, int item_id);
-ScoreBucket PurgeThenAddScoreToPool(ScoreBucket bucket, int score, int item_id, int old_score);
-ScoreBucket AddScoreToPool(ScoreBucket bucket, int score, int item_id);
-ScoreBucket AddScoreMember(ScoreBucket bucket, int item);
-ScoreBucket doesPoolExist(ScoreBucket bucket, int score);
-int IsScoreMember(MemberBucket head, int item);
-void DumpScores(ScoreBucket head);
-void DumpMembers(MemberBucket head);
-MemberBucket DeleteMember(MemberBucket head, int item);
-
-int setnonblock(int fd) {
- int flags;
- flags = fcntl(fd, F_GETFL);
- if (flags < 0) { return flags; }
- flags |= O_NONBLOCK;
- if (fcntl(fd, F_SETFL, flags) < 0) { return -1; }
- return 0;
-}
-
-typedef struct token_s {
- char *value;
- size_t length;
-} token_t;
-
-#define COMMAND_TOKEN 0
-#define SUBCOMMAND_TOKEN 1
-#define KEY_TOKEN 1
-#define VALUE_TOKEN 2
-#define MAX_TOKENS 8
-
static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) {
char *s, *e;
size_t ntokens = 0;
@@ -104,12 +48,6 @@ static size_t tokenize_command(char *command, token_t *tokens, const size_t max_
return ntokens;
}
-void reply(int fd, char *buffer) {
- int n = write(fd, buffer, strlen(buffer));
- if (n < 0 || n < strlen(buffer))
- printf("ERROR writing to socket");
-}
-
void on_read(int fd, short ev, void *arg) {
struct client *client = (struct client *)arg;
char buf[8196];
@@ -212,102 +150,17 @@ int main(int argc, char **argv) {
return 0;
}
-// If the bucket is empty/null, create first and exit
-// If the score pool exists, add member to it
-// if the score pool does not exist, add to create last and exit
-ScoreBucket PurgeThenAddScoreToPool(ScoreBucket bucket, int score, int item_id, int old_score) {
- ScoreBucket lookup = doesPoolExist(bucket, old_score);
- lookup->members = DeleteMember(lookup->members, item_id);
- lookup->members -= 1;
- return AddScoreToPool(bucket, score, item_id);
-}
-
-ScoreBucket AddScoreToPool(ScoreBucket bucket, int score, int item_id) {
- if (bucket == NULL) {
- return initScorePool(score, item_id);
- }
- ScoreBucket lookup = doesPoolExist(bucket, score);
- if (lookup == NULL) {
- ScoreBucket head = initScorePool(score, item_id);
- head->next = bucket;
- return head;
- }
- lookup = AddScoreMember(lookup, item_id);
- return bucket;
-}
-
-ScoreBucket initScorePool(int score, int item_id) {
- ScoreBucket head = malloc(sizeof(struct bucket_el));
- head = malloc( sizeof( struct bucket_el ) );
- if (head == NULL ) {
- exit(1);
- } else {
- MemberBucket member = malloc( sizeof( struct member_el ) );
- member->item = item_id;
- member->next = NULL;
- head->members = member;
- head->score = score;
- head->count = 1;
- head->next = NULL;
- return head;
- }
- return NULL;
-}
-
-ScoreBucket AddScoreMember(ScoreBucket bucket, int item) {
- if (IsScoreMember(bucket->members, item)) {
- return bucket;
- }
- MemberBucket head = malloc(sizeof(struct member_el));
- head = malloc( sizeof( struct member_el ) );
- if (head == NULL ) {
- exit(1);
- } else {
- head->next = bucket->members;
- head->item = item;
- bucket->members = head;
- bucket->count += 1;
- return bucket;
- }
- return NULL;
-}
-
-ScoreBucket PrepScoreBucket(ScoreBucket bucket) {
- if (bucket != NULL) {
- free(bucket);
- }
- return NULL;
-}
-
-int IsScoreMember(MemberBucket head, int item) {
- if (head == NULL) { return 0; }
- if (head->item == item) {return 1; }
- return IsScoreMember(head->next, item);
-}
-
-ScoreBucket doesPoolExist(ScoreBucket bucket, int score) {
- if (bucket == NULL) { return NULL; }
- if (bucket->score == score) { return bucket; }
- return doesPoolExist(bucket->next, score);
-}
-
-void DumpScores(ScoreBucket head) {
- if (head == NULL) { return; }
- printf("Score %d (%d)", head->score, head->count);
- DumpMembers(head->members);
- printf("\n");
- DumpScores(head->next);
-}
-
-void DumpMembers(MemberBucket head) {
- if (head == NULL) { return; }
- printf(" %d", head->item);
- DumpMembers(head->next);
+int setnonblock(int fd) {
+ int flags;
+ flags = fcntl(fd, F_GETFL);
+ if (flags < 0) { return flags; }
+ flags |= O_NONBLOCK;
+ if (fcntl(fd, F_SETFL, flags) < 0) { return -1; }
+ return 0;
}
-MemberBucket DeleteMember(MemberBucket head, int item) {
- if (head == NULL) { return NULL; }
- if (head->item == item) { return head->next; }
- head->next = DeleteMember(head->next, item);
- return head;
+void reply(int fd, char *buffer) {
+ int n = write(fd, buffer, strlen(buffer));
+ if (n < 0 || n < strlen(buffer))
+ printf("ERROR writing to socket");
}
View
@@ -0,0 +1,48 @@
+
+#include <event.h>
+
+#define COMMAND_TOKEN 0
+#define SUBCOMMAND_TOKEN 1
+#define KEY_TOKEN 1
+#define VALUE_TOKEN 2
+#define MAX_TOKENS 8
+#define SERVER_PORT 8002
+
+struct client {
+ struct event ev_read;
+};
+
+struct TreeNode {
+ int item;
+ int score;
+ SearchTree left;
+ SearchTree right;
+};
+
+struct member_el {
+ int item;
+ MemberBucket next;
+};
+
+struct bucket_el {
+ int score;
+ int count;
+ MemberBucket members;
+ ScoreBucket next;
+};
+
+typedef struct token_s {
+ char *value;
+ size_t length;
+} token_t;
+
+SearchTree items;
+ScoreBucket scores;
+
+static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens);
+void on_read(int fd, short ev, void *arg);
+void on_accept(int fd, short ev, void *arg);
+int main(int argc, char **argv);
+int setnonblock(int fd);
+void reply(int fd, char *buffer);
+
View
@@ -1,11 +1,12 @@
#include "bst.h"
#include <stdlib.h>
+#include <stdio.h>
struct TreeNode {
int item;
int score;
- SearchTree left;
- SearchTree right;
+ SearchTree left;
+ SearchTree right;
};
SearchTree MakeEmpty(SearchTree tree) {
View
@@ -1,5 +1,3 @@
-typedef int ElementType;
-
#ifndef _Tree_H
#define _Tree_H
Oops, something went wrong.

0 comments on commit 1573547

Please sign in to comment.