Skip to content
Browse files

ape_pool : destroy opti

  • Loading branch information...
1 parent 46d799c commit a36abf54074aa006ed8f21ddcf13696b8cb55da9 @paraboul committed Dec 30, 2010
Showing with 103 additions and 27 deletions.
  1. +49 −1 src/core/ape_pool.c
  2. +2 −0 src/core/ape_pool.h
  3. +3 −0 src/core/common.h
  4. +1 −1 src/core/entry.c
  5. +1 −1 src/core/events_loop.c
  6. +47 −24 src/core/socket.c
View
50 src/core/ape_pool.c
@@ -31,7 +31,18 @@ void ape_init_pool_list(ape_pool_list_t *list, size_t size, size_t n)
list->head = pool;
list->current = pool;
- list->queue = ((void *)&pool[0])+((n-1)*size);
+ list->pPool = pool;
+ list->queue = ((void *)&pool[0])+((n-1)*size);
+}
+
+void ape_pool_head_to_queue(ape_pool_list_t *list)
+{
+ ape_pool_t *head = list->head;
+
+ list->head = head->next;
+ list->queue->next = head;
+ list->queue = head;
+ head->next = NULL;
}
ape_pool_t *ape_grow_pool(ape_pool_list_t *list, size_t size, size_t n)
@@ -45,24 +56,61 @@ ape_pool_t *ape_grow_pool(ape_pool_list_t *list, size_t size, size_t n)
return pool;
}
+#if 0
void ape_destroy_pool(ape_pool_t *pool)
{
ape_pool_t *tPool = NULL;
while (pool != NULL) {
/* TODO : callback ? (cleaner) */
if (pool->flags & APE_POOL_ALLOC) {
+ printf("Alloc detected\n");
if (tPool != NULL) {
+ printf("Free previous\n");
free(tPool);
}
tPool = pool;
+ } else {
+ printf("Not allocated block\n");
}
pool = pool->next;
}
if (tPool != NULL) {
+ printf("Free final\n");
free(tPool);
}
}
+#endif
+
+void ape_destroy_pool(ape_pool_t *pool)
+{
+ ape_pool_t *tPool = NULL, *fPool = NULL;
+
+ while (pool != NULL) {
+ if (pool->flags & APE_POOL_ALLOC) {
+ if (fPool == NULL) {
+ fPool = pool;
+ }
+ if (tPool != NULL) {
+ fPool->next = pool->next;
+ pool->next = tPool;
+ tPool = pool;
+ pool = fPool->next;
+ continue;
+ }
+ tPool = pool;
+ }
+ pool = pool->next;
+ }
+ fPool->next = NULL;
+ pool = tPool;
+
+ while (pool != NULL && pool->flags & APE_POOL_ALLOC) {
+ tPool = pool->next;
+ free(pool);
+ pool = tPool;
+ }
+}
void ape_destroy_pool_list(ape_pool_list_t *list)
{
View
2 src/core/ape_pool.h
@@ -17,6 +17,7 @@ typedef struct _ape_pool_list {
ape_pool_t *head;
ape_pool_t *queue;
ape_pool_t *current;
+ ape_pool_t *pPool;
} ape_pool_list_t;
ape_pool_t *ape_new_pool(size_t size, size_t n);
@@ -26,5 +27,6 @@ ape_pool_t *ape_grow_pool(ape_pool_list_t *list, size_t size, size_t n);
void ape_init_pool_list(ape_pool_list_t *list, size_t size, size_t n);
void ape_destroy_pool(ape_pool_t *pool);
void ape_destroy_pool_list(ape_pool_list_t *list);
+void ape_pool_head_to_queue(ape_pool_list_t *list);
#endif
View
3 src/core/common.h
@@ -15,6 +15,9 @@
#define CONST_STR_LEN(x) x, x ? sizeof(x) - 1 : 0
#define CONST_STR_LEN2(x) x ? sizeof(x) - 1 : 0, x
+#define _APE_ABS_MASK(val) (val >> sizeof(int) * 8 - 1)
+#define APE_ABS(val) (val + _APE_ABS_MASK(val)) ^ _APE_ABS_MASK(val)
+
typedef struct _ape_global ape_global;
#include "ape_config.h"
View
2 src/core/entry.c
@@ -88,7 +88,7 @@ int main(int argc, char **argv)
}
ape->conf = ape_read_config("../../etc/ape.conf");
-
+
events_loop(ape);
return 0;
View
2 src/core/events_loop.c
@@ -44,7 +44,7 @@ void events_loop(ape_global *ape)
if (APE_SOCKET(attach)->states.state == APE_SOCKET_ST_ONLINE &&
!(APE_SOCKET(attach)->states.flags & APE_SOCKET_WOULD_BLOCK)) {
- printf("[Socket] %d is writable\n", APE_SOCKET(attach)->s.fd);
+ //printf("[Socket] %d is writable\n", APE_SOCKET(attach)->s.fd);
ape_socket_do_jobs(APE_SOCKET(attach));
//printf("[Socket] Rdy to send %i\n", APE_SOCKET(attach)->s.fd);
} else if (APE_SOCKET(attach)->states.state == APE_SOCKET_ST_PROGRESS) {
View
71 src/core/socket.c
@@ -212,7 +212,7 @@ int APE_socket_write(ape_socket *socket, char *data, size_t len)
if (errno == EAGAIN && r_bytes != 0) {
socket->states.flags |= APE_SOCKET_WOULD_BLOCK;
ape_socket_queue_data(socket, data, len, t_bytes);
- printf("Write not finished %d\n", r_bytes);
+ printf("Write not finished %d (done : %d)\n", r_bytes, t_bytes);
return r_bytes;
} else {
@@ -223,7 +223,7 @@ int APE_socket_write(ape_socket *socket, char *data, size_t len)
t_bytes += n;
r_bytes -= n;
}
- printf("Success %d\n", t_bytes);
+ //printf("Success %d\n", t_bytes);
return 0;
}
@@ -261,41 +261,60 @@ int ape_socket_do_jobs(ape_socket *socket)
struct iovec chunks[max_chunks];
job = socket->jobs.head;
+
+ printf("Jobs to do\n");
+
while(job != NULL && job->flags & APE_SOCKET_JOB_ACTIVE) {
switch(job->flags & ~(APE_POOL_ALL_FLAGS | APE_SOCKET_JOB_ACTIVE)) {
case APE_SOCKET_JOB_WRITEV:
{
- int i;
- ssize_t n;
- ape_socket_packet_t *packet = job->ptr;
- printf("wut\n");
- for (i = 0; packet != NULL && i < max_chunks;) {
+ int i, y;
+ ssize_t n, total_len = 0;
+ ape_socket_packet_t *packet = (ape_socket_packet_t *)((ape_pool_list_t *)job->ptr)->pPool;
+ for (i = 0; packet != NULL && i < max_chunks; i++) {
if (packet->pool.ptr == NULL) {
- packet = (ape_socket_packet_t *)packet->pool.next;
- continue;
+ break;
}
- printf("while?\n");
chunks[i].iov_base = packet->pool.ptr + packet->offset;
chunks[i].iov_len = packet->len - packet->offset;
- packet = (ape_socket_packet_t *)packet->pool.next;
+ //total_len += chunks[i].iov_len;
- /* TODO: rewrite the queue system (pool offset) */
- i++;
+ packet = (ape_socket_packet_t *)packet->pool.next;
+
}
- while (1) {
- n = writev(socket->s.fd, chunks, i);
- if (n == -1) {
- if (errno == EAGAIN) {
- printf("EAGAIN\n");
- }
+ n = writev(socket->s.fd, chunks, i);
+ if (n == -1) {
+ job = job->next;
+ continue;
+ }
+
+ for (packet = (ape_socket_packet_t *)((ape_pool_list_t *)job->ptr)->pPool;
+ packet != NULL && packet->pool.ptr != NULL;
+ packet = (ape_socket_packet_t *)packet->pool.next) {
+
+ n -= packet->len - packet->offset;
+
+ if (n == 0) {
+
+ free(packet->pool.ptr);
+ packet->pool.ptr = NULL;
+ ((ape_pool_list_t *)job->ptr)->pPool = ((ape_pool_list_t *)job->ptr)->head;
+ break;
+ }
+ if (n < 0) {
+ packet->offset = packet->len + n;
+ ((ape_pool_list_t *)job->ptr)->pPool = (ape_pool_t *)packet;
+ break;
}
- printf("fail : %d\n", n);
+ free(packet->pool.ptr);
+ packet->pool.ptr = NULL;
}
+
/* TODO: remove jobs */
- printf("writev written : %d %d\n", i, n);
+
}
break;
case APE_SOCKET_JOB_SENDFILE:
@@ -309,6 +328,7 @@ int ape_socket_do_jobs(ape_socket *socket)
printf("[Job] :(\n");
break;
}
+
job = job->next;
}
@@ -330,7 +350,7 @@ static int ape_socket_queue_data(ape_socket *socket,
job = ape_socket_job_get_slot(socket, APE_SOCKET_JOB_WRITEV);
list = job->ptr;
-
+
if (list == NULL) {
list = ape_socket_new_packet_queue(8);
job->ptr = list;
@@ -460,8 +480,11 @@ static ape_socket_jobs_t *ape_socket_job_get_slot(ape_socket *socket, int type)
ape_socket_jobs_t *jobs = socket->jobs.current;
/* If we request a write job we can push the data to the iov list */
- if (type == APE_SOCKET_JOB_WRITEV &&
- jobs->flags & APE_SOCKET_JOB_WRITEV) {
+ if ((type == APE_SOCKET_JOB_WRITEV &&
+ jobs->flags & APE_SOCKET_JOB_WRITEV) ||
+ !(jobs->flags & APE_SOCKET_JOB_ACTIVE)) {
+
+ jobs->flags |= APE_SOCKET_JOB_ACTIVE | type;
return jobs;
}

0 comments on commit a36abf5

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