Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

We don't need hashmap at all

  • Loading branch information...
commit 15653c4924df9e25b5ff0bbfd7cc18f6abef0a4c 1 parent fcbb3c4
@cloudwu authored
Showing with 24 additions and 180 deletions.
  1. +1 −2  Makefile
  2. +0 −139 map.c
  3. +0 −13 map.h
  4. +22 −25 mread.c
  5. +1 −1  mread.h
View
3  Makefile
@@ -1,6 +1,5 @@
all:
- gcc -g -o mread -Wall mread.c ringbuffer.c map.c main.c
+ gcc -g -o mread -Wall mread.c ringbuffer.c main.c
test:
- gcc -g -o testmap -Wall map.c testmap.c
gcc -g -o testrb -Wall ringbuffer.c testringbuffer.c
View
139 map.c
@@ -1,139 +0,0 @@
-#include "map.h"
-
-#include <stdlib.h>
-#include <assert.h>
-#include <stdio.h>
-
-struct node {
- int fd;
- int id;
- struct node * next;
-};
-
-struct map {
- int size;
- struct node * hash;
-};
-
-struct map *
-map_new(int max) {
- int sz = 1;
- while (sz <= max) {
- sz *= 2;
- }
- struct map * m = malloc(sizeof(*m));
- m->size = sz;
- m->hash = malloc(sizeof(struct node) * sz);
- int i;
- for (i=0;i<sz;i++) {
- m->hash[i].fd = -1;
- m->hash[i].id = 0;
- m->hash[i].next = NULL;
- }
- return m;
-}
-
-void
-map_delete(struct map * m) {
- free(m->hash);
- free(m);
-}
-
-int
-map_search(struct map * m, int fd) {
- int hash = fd & (m->size-1);
- struct node * n = &m->hash[hash];
- do {
- if (n->fd == fd)
- return n->id;
- n = n->next;
- } while(n);
- return -1;
-}
-
-void
-map_insert(struct map * m, int fd, int id) {
- int hash = fd & (m->size-1);
- struct node * n = &m->hash[hash];
- if (n->fd < 0) {
- n->fd = fd;
- n->id = id;
- return;
- }
- int ohash = n->fd & (m->size-1);
- if (hash != ohash) {
- struct node * last = &m->hash[ohash];
- while (last->next != &m->hash[hash]) {
- last = last->next;
- }
- last->next = n->next;
-
- int ofd = n->fd;
- int oid = n->id;
- n->fd = fd;
- n->id = id;
- n->next = NULL;
- map_insert(m,ofd, oid);
- return;
- }
-
- int last = (n - m->hash) * 2;
- int i;
- for (i=0;i<m->size;i++) {
- int idx = (i + last + 1) & (m->size - 1);
- struct node * temp = &m->hash[idx];
- if (temp->fd < 0) {
- temp->fd = fd;
- temp->id = id;
- temp->next = n->next;
- n->next = temp;
- return;
- }
- }
- assert(0);
-}
-
-void
-map_erase(struct map *m , int fd) {
- int hash = fd & (m->size-1);
- struct node * n = &m->hash[hash];
- if (n->fd == fd) {
- if (n->next == NULL) {
- n->fd = -1;
- return;
- }
- struct node * next = n->next;
- n->fd = next->fd;
- n->id = next->id;
- n->next = next->next;
- next->fd = -1;
- next->next = NULL;
- return;
- }
- if (n->next == NULL) {
- return;
- }
- struct node * last = n;
- n = n->next;
- for(;;) {
- if (n->fd == fd) {
- n->fd = -1;
- last->next = n->next;
- n->next = NULL;
- return;
- }
- if (n->next == NULL)
- return;
- last = n;
- n = n->next;
- }
-}
-
-void
-map_dump(struct map *m) {
- int i;
- for (i=0;i<m->size;i++) {
- struct node * n = &(m->hash[i]);
- printf("[%d] fd = %d , id = %d , next = %d\n",i,n->fd,n->id,(int)(n->next - m->hash));
- }
-}
View
13 map.h
@@ -1,13 +0,0 @@
-#ifndef MREAD_MAP_H
-#define MREAD_MAP_H
-
-struct map;
-
-struct map * map_new(int max);
-void map_delete(struct map *);
-int map_search(struct map * , int fd);
-void map_insert(struct map * , int fd, int id);
-void map_erase(struct map *, int fd);
-void map_dump(struct map *m);
-
-#endif
View
47 mread.c
@@ -28,6 +28,8 @@
#define SOCKET_ALIVE SOCKET_SUSPEND
+#define LISTENSOCKET (void *)((intptr_t)~0)
+
struct socket {
int fd;
struct ringbuffer_block * node;
@@ -44,7 +46,6 @@ struct mread_pool {
int skip;
struct socket * sockets;
struct socket * free_socket;
- struct map * socket_hash;
int queue_len;
int queue_head;
struct epoll_event ev[READQUEUE];
@@ -98,9 +99,9 @@ mread_create(int port , int max , int buffer_size) {
if (listen_fd == -1) {
return NULL;
}
- if ( -1 == _set_nonblocking(listen_fd) ) {
- return NULL;
- }
+ if ( -1 == _set_nonblocking(listen_fd) ) {
+ return NULL;
+ }
int reuse = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
@@ -109,7 +110,7 @@ mread_create(int port , int max , int buffer_size) {
memset(&my_addr, 0, sizeof(struct sockaddr_in));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
- my_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // INADDR_ANY;
+ my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // INADDR_LOOPBACK
printf("MREAD bind %s:%u\n",inet_ntoa(my_addr.sin_addr),ntohs(my_addr.sin_port));
if (bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
close(listen_fd);
@@ -125,9 +126,11 @@ mread_create(int port , int max , int buffer_size) {
close(listen_fd);
return NULL;
}
+
struct epoll_event ev;
ev.events = EPOLLIN;
- ev.data.fd = listen_fd;
+ ev.data.ptr = LISTENSOCKET;
+
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev) == -1) {
close(listen_fd);
close(epoll_fd);
@@ -135,6 +138,7 @@ mread_create(int port , int max , int buffer_size) {
}
struct mread_pool * self = malloc(sizeof(*self));
+
self->listen_fd = listen_fd;
self->epoll_fd = epoll_fd;
self->max_connection = max;
@@ -143,7 +147,6 @@ mread_create(int port , int max , int buffer_size) {
self->skip = 0;
self->sockets = _create_sockets(max);
self->free_socket = &self->sockets[0];
- self->socket_hash = map_new(max * 3 / 2);
self->queue_len = 0;
self->queue_head = 0;
if (buffer_size == 0) {
@@ -172,7 +175,6 @@ mread_close(struct mread_pool *self) {
}
close(self->epoll_fd);
_release_rb(self->rb);
- map_delete(self->socket_hash);
free(self);
}
@@ -188,12 +190,12 @@ _read_queue(struct mread_pool * self, int timeout) {
return n;
}
-inline static int
+inline static struct socket *
_read_one(struct mread_pool * self) {
if (self->queue_head >= self->queue_len) {
- return -1;
+ return NULL;
}
- return self->ev[self->queue_head ++].data.fd;
+ return self->ev[self->queue_head ++].data.ptr;
}
static struct socket *
@@ -220,7 +222,7 @@ _add_client(struct mread_pool * self, int fd) {
}
struct epoll_event ev;
ev.events = EPOLLIN;
- ev.data.fd = fd;
+ ev.data.ptr = s;
if (epoll_ctl(self->epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
close(fd);
return;
@@ -229,8 +231,6 @@ _add_client(struct mread_pool * self, int fd) {
s->fd = fd;
s->node = NULL;
s->status = SOCKET_SUSPEND;
- int id = s - self->sockets;
- map_insert(self->socket_hash , fd , id);
}
static int
@@ -265,12 +265,12 @@ mread_poll(struct mread_pool * self , int timeout) {
}
}
for (;;) {
- int fd = _read_one(self);
- if (fd == -1) {
+ struct socket * s = _read_one(self);
+ if (s == NULL) {
self->active = -1;
return -1;
}
- if (fd == self->listen_fd) {
+ if (s == LISTENSOCKET) {
struct sockaddr_in remote_addr;
socklen_t len = sizeof(struct sockaddr_in);
int client_fd = accept(self->listen_fd , (struct sockaddr *)&remote_addr , &len);
@@ -279,13 +279,11 @@ mread_poll(struct mread_pool * self , int timeout) {
_add_client(self, client_fd);
}
} else {
- int index = map_search(self->socket_hash , fd);
- if (index >= 0) {
- self->active = index;
- struct socket * s = &self->sockets[index];
- s->status = SOCKET_POLLIN;
- return index;
- }
+ int index = s - self->sockets;
+ assert(index >=0 && index < self->max_connection);
+ self->active = index;
+ s->status = SOCKET_POLLIN;
+ return index;
}
}
}
@@ -456,7 +454,6 @@ mread_yield(struct mread_pool * self) {
if (s->status == SOCKET_CLOSED && s->node == NULL) {
--self->closed;
s->status = SOCKET_INVALID;
- map_erase(self->socket_hash , s->fd);
s->fd = self->free_socket - self->sockets;
self->free_socket = s;
self->skip = 0;
View
2  mread.h
@@ -9,8 +9,8 @@ void mread_close(struct mread_pool *m);
int mread_poll(struct mread_pool *m , int timeout);
void * mread_pull(struct mread_pool *m , int size);
void mread_yield(struct mread_pool *m);
-void mread_close_client(struct mread_pool *m, int id);
int mread_closed(struct mread_pool *m);
+void mread_close_client(struct mread_pool *m, int id);
int mread_socket(struct mread_pool *m , int index);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.