Skip to content

Commit

Permalink
Reset memg.c to pre-ringbuffer version. We're I/O bound, so let's not…
Browse files Browse the repository at this point in the history
… complicate the code for nothing.
  • Loading branch information
grahamking committed Mar 13, 2012
1 parent b461764 commit dddcf3b
Showing 1 changed file with 22 additions and 88 deletions.
110 changes: 22 additions & 88 deletions memg.c
Expand Up @@ -12,17 +12,17 @@
#include <search.h> #include <search.h>
#include <pthread.h> #include <pthread.h>


#define BUFFER_SIZE 25 #define READ_SIZE 1024


// Arguments to our thread // Arguments to our thread
typedef struct { typedef struct {
int conn; int conn;
struct hsearch_data *htab; struct hsearch_data *htab;
} thdata; } thdata;


/**** /*
* Buffered IO on socket * Buffered IO on socket
****/ */


struct Buf { struct Buf {
int conn; int conn;
Expand All @@ -34,122 +34,57 @@ struct Buf {
struct Buf *new_buffer(int conn) { struct Buf *new_buffer(int conn) {
struct Buf *buf = malloc(sizeof(struct Buf)); struct Buf *buf = malloc(sizeof(struct Buf));
buf->conn = conn; buf->conn = conn;
buf->store = malloc(BUFFER_SIZE); buf->store = malloc(READ_SIZE + 1); // +1 for the \0
buf->start = 0;
buf->end = 0;
return buf; return buf;
} }


void buf_debug(struct Buf *buf);

// Buffer len: Number of characters in buffer
int buf_len(struct Buf *buf) {
if (buf->end >= buf->start) {
return buf->end - buf->start;
} else {
return BUFFER_SIZE - buf->start + buf->end;
}
}

// Space to the end of the buffer
int buf_contiguous_space(struct Buf *buf) {
if (buf->end >= buf->start) {
return BUFFER_SIZE - buf->end;
} else {
return buf->start - buf->end;
}
}

void buf_fill(struct Buf *buf) { void buf_fill(struct Buf *buf) {
memset(buf->store, '\0', READ_SIZE + 1);
recv(buf->conn, buf->store, READ_SIZE, 0);


printf("BUF_FILL\n"); //printf("* Buffer is now: '%s' (%d)\n", buf->store, strlen(buf->store));

int fetch_size = buf_contiguous_space(buf);
if (fetch_size == 0) {
printf("Error: Buffer is full!\n");
return;
}

//memset(buf->store, '\0', READ_SIZE + 1);
int num_fetched = recv(
buf->conn,
buf->store + buf->end,
fetch_size,
0);

buf->end += num_fetched;
buf->end %= BUFFER_SIZE;

buf_debug(buf);

if (num_fetched == fetch_size) {
// OS gave us all the bytes we asked for, ask for more
buf_fill(buf);
}
} }


/* Fetch until \n */ /* Fetch until \n */
char *buf_line(struct Buf *buf) { char *buf_line(struct Buf *buf) {


printf("BUF_LINE\n");

size_t str_end; size_t str_end;
char *line; char *line;


if (buf->start == buf->end) { if (strlen(buf->store) == 0) {
buf_fill(buf); buf_fill(buf);
} }


str_end = strcspn(buf->store + buf->start, "\n"); str_end = strcspn(buf->store, "\n");
line = strndup(buf->store + buf->start, str_end - 1); // -1 because of \r line = strndup(buf->store, str_end - 1); // -1 because of \r

//memmove(buf->store, buf->store + str_end + 1, BUFFER_SIZE - str_end);


printf("buf_line Found line: %s (%d)\n", line, strlen(line)); memmove(buf->store, buf->store + str_end + 1, READ_SIZE - str_end);

buf->start += str_end + 1; // +1 because of \n

buf_debug(buf);


//printf("buf_line Found line: %s (%d)\n", line, strlen(line));
//printf("buf_line Remain: %s (%d)\n", buf->store, strlen(buf->store));
return line; return line;
} }


/* Fetch exactly 'num' bytes */ /* Fetch exactly 'num' bytes */
char *buf_bytes(struct Buf *buf, int num) { char *buf_bytes(struct Buf *buf, int num) {


printf("BUF_BYTES\n");

char *line; char *line;


if (buf->start == buf->end) { if (strlen(buf->store) == 0) {
buf_fill(buf); buf_fill(buf);
} }


line = strndup(buf->store + buf->start, num); line = strndup(buf->store, num);
//memmove(buf->store, buf->store + num + 2, BUFFER_SIZE - num - 1); memmove(buf->store, buf->store + num + 2, READ_SIZE - num - 1);


printf("buf_bytes Found line: %s (%d)\n", line, strlen(line)); //printf("buf_bytes Found line: %s (%d)\n", line, strlen(line));
//printf("buf_bytes Remain: %s (%d)\n", buf->store, strlen(buf->store)); //printf("buf_bytes Remain: %s (%d)\n", buf->store, strlen(buf->store));

buf->start += num + 2; // +2 for \r\n
buf_debug(buf);

return line; return line;
} }


/* Display debug info */ /*
void buf_debug(struct Buf *buf) {

printf("* Buffer is now: '%s'\n", buf->store);
printf("* Start / End: %d / %d\n", buf->start, buf->end);
printf("* Len: %d\n", buf_len(buf));
printf("* Contiguous: %d\n", buf_contiguous_space(buf));
}

/****
* Connection handling. Implements 'get' and 'set'. * Connection handling. Implements 'get' and 'set'.
****/ */

void *handle_conn(void *void_param) {//int conn, struct hsearch_data *htab) { void *handle_conn(void *void_param) {//int conn, struct hsearch_data *htab) {


thdata *param = (thdata *)void_param; thdata *param = (thdata *)void_param;
Expand Down Expand Up @@ -226,10 +161,9 @@ int is_single(int argc, char *argv[]) {
return 0; return 0;
} }


/**** /*
* MAIN * MAIN
****/ */

int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {


int sock_fd; int sock_fd;
Expand Down Expand Up @@ -269,7 +203,7 @@ int main(int argc, char *argv[]) {
addr->sin_addr.s_addr = INADDR_ANY; addr->sin_addr.s_addr = INADDR_ANY;
err = bind(sock_fd, (struct sockaddr *)addr, sizeof(struct sockaddr)); err = bind(sock_fd, (struct sockaddr *)addr, sizeof(struct sockaddr));
if (err == -1) { if (err == -1) {
printf("Bind error on port 11211: %d. Maybe you have memcached running?\n", errno); printf("bind error: %d\n", errno);
} }


err = listen(sock_fd, 1); err = listen(sock_fd, 1);
Expand Down

0 comments on commit dddcf3b

Please sign in to comment.