Permalink
Browse files

- bugfix: compile error when MySQL enabled

- optimize: malloced ape_socket doesn't free at disconnect
  • Loading branch information...
1 parent 5216626 commit 33c5d1d74ca83d716e8f92d079f2d39a0597afef @harmer harmer committed with paraboul May 23, 2010
Showing with 28 additions and 46 deletions.
  1. +6 −20 modules/libape-spidermonkey.c
  2. +1 −2 src/dns.c
  3. +1 −0 src/dns.h
  4. +19 −24 src/sock.c
  5. +1 −0 src/sock.h
@@ -2509,13 +2509,11 @@ APE_JS_NATIVE(ape_sm_mysql_constructor)
MYSAC *my;
int fd;
struct _ape_mysql_data *myhandle;
- ape_socket *co;
if (!JS_ConvertArguments(cx, argc, argv, "ssss", &host, &login, &pass, &db)) {
return JS_TRUE;
}
- co = g_ape->co;
myhandle = xmalloc(sizeof(*myhandle));
my = mysac_new(1024*1024);
@@ -2535,27 +2533,15 @@ APE_JS_NATIVE(ape_sm_mysql_constructor)
JS_SetPrivate(cx, obj, myhandle);
fd = mysac_get_fd(my);
-
- co[fd].buffer_in.data = NULL;
- co[fd].buffer_in.size = 0;
- co[fd].buffer_in.length = 0;
-
- co[fd].attach = NULL;
- co[fd].idle = 0;
- co[fd].burn_after_writing = 0;
- co[fd].fd = fd;
- co[fd].stream_type = STREAM_DELEGATE;
+ prepare_ape_socket (fd, g_ape);
- co[fd].callbacks.on_accept = NULL;
- co[fd].callbacks.on_connect = NULL;
- co[fd].callbacks.on_disconnect = NULL;
- co[fd].callbacks.on_read_lf = NULL;
- co[fd].callbacks.on_data_completly_sent = NULL;
+ g_ape->co[fd]->fd = fd;
+ g_ape->co[fd]->stream_type = STREAM_DELEGATE;
- co[fd].callbacks.on_read = ape_mysql_io_read;
- co[fd].callbacks.on_write = ape_mysql_io_write;
- co[fd].data = myhandle;
+ g_ape->co[fd]->callbacks.on_read = ape_mysql_io_read;
+ g_ape->co[fd]->callbacks.on_write = ape_mysql_io_write;
+ g_ape->co[fd]->data = myhandle;
events_add(g_ape->events, fd, EVENT_READ|EVENT_WRITE);
View
@@ -191,8 +191,7 @@ void ape_dns_init(acetables *g_ape)
{
int sock = dns_init(NULL, 1);
- g_ape->co[sock] = xmalloc(sizeof(*g_ape->co[sock]));
- memset(g_ape->co[sock], 0, sizeof(*g_ape->co[sock]));
+ prepare_ape_socket (sock, g_ape);
g_ape->co[sock]->fd = sock;
g_ape->co[sock]->stream_type = STREAM_DELEGATE;
View
@@ -23,6 +23,7 @@
#define _DNS_H
#include "main.h"
+#include "sock.h"
#include <udns.h>
struct query {
View
@@ -94,13 +94,7 @@ ape_socket *ape_listen(unsigned int port, char *listen_ip, acetables *g_ape)
setnonblocking(sock);
- while (sock + 4 >= g_ape->basemem) {
- /* Increase connection & events size */
- growup(&g_ape->basemem, &g_ape->co, g_ape->events, &g_ape->bufout);
- }
-
- g_ape->co[sock] = xmalloc(sizeof(*g_ape->co[sock]));
- memset(g_ape->co[sock], 0, sizeof(*g_ape->co[sock]));
+ prepare_ape_socket (sock, g_ape);
g_ape->co[sock]->fd = sock;
g_ape->co[sock]->state = STREAM_ONLINE;
@@ -133,13 +127,7 @@ ape_socket *ape_connect(char *ip, int port, acetables *g_ape)
return NULL;
}
- while (sock + 4 >= g_ape->basemem) {
- /* Increase connection & events size */
- growup(&g_ape->basemem, &g_ape->co, g_ape->events, &g_ape->bufout);
- }
-
- g_ape->co[sock] = xmalloc(sizeof(*g_ape->co[sock]));
- memset(g_ape->co[sock], 0, sizeof(*g_ape->co[sock]));
+ prepare_ape_socket (sock, g_ape);
g_ape->co[sock]->buffer_in.data = xmalloc(sizeof(char) * (DEFAULT_BUFFER_SIZE + 1));
g_ape->co[sock]->buffer_in.size = DEFAULT_BUFFER_SIZE;
@@ -204,6 +192,7 @@ void setnonblocking(int fd)
fcntl(fd, F_SETFL, old_flags);
}
+/* Close socket but preserve ape_socket struct */
void close_socket(int fd, acetables *g_ape)
{
ape_socket *co = g_ape->co[fd];
@@ -223,12 +212,24 @@ void close_socket(int fd, acetables *g_ape)
parser_destroy(&co->parser);
}
- free(co);
- g_ape->co[fd] = NULL;
-
close(fd);
}
+/* Create socket struct if not exists */
+void prepare_ape_socket(int fd, acetables *g_ape)
+{
+ while (fd >= g_ape->basemem) {
+ /* Increase connection & events size */
+ growup(&g_ape->basemem, &g_ape->co, g_ape->events, &g_ape->bufout);
+ }
+
+ if (g_ape->co[fd] == NULL) {
+ g_ape->co[fd] = xmalloc(sizeof(*g_ape->co[fd]));
+ }
+
+ memset(g_ape->co[fd], 0, sizeof(*g_ape->co[fd]));
+}
+
#if 0
static void check_idle(struct _socks_list *sl)
{
@@ -301,13 +302,7 @@ unsigned int sockroutine(acetables *g_ape)
break;
}
- while (new_fd + 4 >= g_ape->basemem) {
- /* Increase connection & events size */
- growup(&g_ape->basemem, &g_ape->co, g_ape->events, &g_ape->bufout);
- }
-
- g_ape->co[new_fd] = xmalloc(sizeof(*g_ape->co[new_fd]));
- memset(g_ape->co[new_fd], 0, sizeof(*g_ape->co[new_fd]));
+ prepare_ape_socket (new_fd, g_ape);
strncpy(g_ape->co[new_fd]->ip_client, inet_ntoa(their_addr.sin_addr), 16);
View
@@ -58,6 +58,7 @@ struct _ape_sock_connect_async
ape_socket *ape_listen(unsigned int port, char *listen_ip, acetables *g_ape);
ape_socket *ape_connect(char *ip, int port, acetables *g_ape);
void ape_connect_name(char *name, int port, ape_socket *pattern, acetables *g_ape);
+void prepare_ape_socket(int fd, acetables *g_ape);
void setnonblocking(int fd);
int sendf(int sock, acetables *g_ape, char *buf, ...);
int sendbin(int sock, const char *bin, unsigned int len, unsigned int burn_after_writing, acetables *g_ape);

0 comments on commit 33c5d1d

Please sign in to comment.