2007-02-28 Kieran Mansley (based on patch from Simon Goldschmidt)

* api_lib.c, tcpip.c, memp.c, memp.h: make API msg structs allocated
    on the stack and remove the API msg type from memp
This commit is contained in:
kieranm 2007-02-28 14:15:43 +00:00
parent 5ace5976a1
commit 527d18a526
5 changed files with 24 additions and 60 deletions

View File

@ -27,6 +27,10 @@ HISTORY
++ New features: ++ New features:
2007-02-28 Kieran Mansley (based on patch from Simon Goldschmidt)
* api_lib.c, tcpip.c, memp.c, memp.h: make API msg structs allocated
on the stack and remove the API msg type from memp
2007-02-26 Jonathan Larmour (based on patch from Simon Goldschmidt) 2007-02-26 Jonathan Larmour (based on patch from Simon Goldschmidt)
* sockets.h, sockets.c: Move socket initialization to new * sockets.h, sockets.c: Move socket initialization to new
lwip_socket_init() function. lwip_socket_init() function.

View File

@ -201,7 +201,8 @@ netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto,
void (*callback)(struct netconn *, enum netconn_evt, u16_t len)) void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
{ {
struct netconn *conn; struct netconn *conn;
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
conn = memp_malloc(MEMP_NETCONN); conn = memp_malloc(MEMP_NETCONN);
if (conn == NULL) { if (conn == NULL) {
@ -228,17 +229,11 @@ netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto,
conn->callback = callback; conn->callback = callback;
conn->recv_avail = 0; conn->recv_avail = 0;
if((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
memp_free(MEMP_NETCONN, conn);
return NULL;
}
msg->type = API_MSG_NEWCONN; msg->type = API_MSG_NEWCONN;
msg->msg.msg.bc.port = proto; /* misusing the port field */ msg->msg.msg.bc.port = proto; /* misusing the port field */
msg->msg.conn = conn; msg->msg.conn = conn;
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
if ( conn->err != ERR_OK ) { if ( conn->err != ERR_OK ) {
memp_free(MEMP_NETCONN, conn); memp_free(MEMP_NETCONN, conn);
@ -266,22 +261,18 @@ netconn *netconn_new_with_callback(enum netconn_type t,
err_t err_t
netconn_delete(struct netconn *conn) netconn_delete(struct netconn *conn)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
void *mem; void *mem;
if (conn == NULL) { if (conn == NULL) {
return ERR_OK; return ERR_OK;
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return ERR_MEM;
}
msg->type = API_MSG_DELCONN; msg->type = API_MSG_DELCONN;
msg->msg.conn = conn; msg->msg.conn = conn;
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
/* Drain the recvmbox. */ /* Drain the recvmbox. */
if (conn->recvmbox != SYS_MBOX_NULL) { if (conn->recvmbox != SYS_MBOX_NULL) {
@ -378,7 +369,8 @@ err_t
netconn_bind(struct netconn *conn, struct ip_addr *addr, netconn_bind(struct netconn *conn, struct ip_addr *addr,
u16_t port) u16_t port)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
@ -391,16 +383,12 @@ netconn_bind(struct netconn *conn, struct ip_addr *addr,
} }
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM);
}
msg->type = API_MSG_BIND; msg->type = API_MSG_BIND;
msg->msg.conn = conn; msg->msg.conn = conn;
msg->msg.msg.bc.ipaddr = addr; msg->msg.msg.bc.ipaddr = addr;
msg->msg.msg.bc.port = port; msg->msg.msg.bc.port = port;
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
@ -409,7 +397,8 @@ err_t
netconn_connect(struct netconn *conn, struct ip_addr *addr, netconn_connect(struct netconn *conn, struct ip_addr *addr,
u16_t port) u16_t port)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
@ -422,36 +411,29 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr,
} }
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return ERR_MEM;
}
msg->type = API_MSG_CONNECT; msg->type = API_MSG_CONNECT;
msg->msg.conn = conn; msg->msg.conn = conn;
msg->msg.msg.bc.ipaddr = addr; msg->msg.msg.bc.ipaddr = addr;
msg->msg.msg.bc.port = port; msg->msg.msg.bc.port = port;
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
err_t err_t
netconn_disconnect(struct netconn *conn) netconn_disconnect(struct netconn *conn)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return ERR_MEM;
}
msg->type = API_MSG_DISCONNECT; msg->type = API_MSG_DISCONNECT;
msg->msg.conn = conn; msg->msg.conn = conn;
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
@ -459,7 +441,8 @@ netconn_disconnect(struct netconn *conn)
err_t err_t
netconn_listen(struct netconn *conn) netconn_listen(struct netconn *conn)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
@ -472,14 +455,10 @@ netconn_listen(struct netconn *conn)
} }
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM);
}
msg->type = API_MSG_LISTEN; msg->type = API_MSG_LISTEN;
msg->msg.conn = conn; msg->msg.conn = conn;
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
@ -503,7 +482,8 @@ netconn_accept(struct netconn *conn)
struct netbuf * struct netbuf *
netconn_recv(struct netconn *conn) netconn_recv(struct netconn *conn)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
struct netbuf *buf; struct netbuf *buf;
struct pbuf *p; struct pbuf *p;
u16_t len; u16_t len;
@ -564,10 +544,6 @@ netconn_recv(struct netconn *conn)
buf->fromaddr = NULL; buf->fromaddr = NULL;
/* Let the stack know that we have taken the data. */ /* Let the stack know that we have taken the data. */
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
conn->err = ERR_MEM;
return buf;
}
msg->type = API_MSG_RECV; msg->type = API_MSG_RECV;
msg->msg.conn = conn; msg->msg.conn = conn;
if (buf != NULL) { if (buf != NULL) {
@ -578,7 +554,6 @@ netconn_recv(struct netconn *conn)
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
} else { } else {
sys_mbox_fetch(conn->recvmbox, (void *)&buf); sys_mbox_fetch(conn->recvmbox, (void *)&buf);
conn->recv_avail -= buf->p->tot_len; conn->recv_avail -= buf->p->tot_len;
@ -599,7 +574,8 @@ netconn_recv(struct netconn *conn)
err_t err_t
netconn_send(struct netconn *conn, struct netbuf *buf) netconn_send(struct netconn *conn, struct netbuf *buf)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
@ -609,10 +585,6 @@ netconn_send(struct netconn *conn, struct netbuf *buf)
return conn->err; return conn->err;
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM);
}
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len)); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len));
msg->type = API_MSG_SEND; msg->type = API_MSG_SEND;
msg->msg.conn = conn; msg->msg.conn = conn;
@ -620,14 +592,14 @@ netconn_send(struct netconn *conn, struct netbuf *buf)
api_msg_post(msg); api_msg_post(msg);
sys_mbox_fetch(conn->mbox, NULL); sys_mbox_fetch(conn->mbox, NULL);
memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
err_t err_t
netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy) netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
u16_t len; u16_t len;
if (conn == NULL) { if (conn == NULL) {
@ -638,9 +610,6 @@ netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
return conn->err; return conn->err;
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM);
}
msg->type = API_MSG_WRITE; msg->type = API_MSG_WRITE;
msg->msg.conn = conn; msg->msg.conn = conn;
@ -683,7 +652,6 @@ netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
} }
} }
ret: ret:
memp_free(MEMP_API_MSG, msg);
conn->state = NETCONN_NONE; conn->state = NETCONN_NONE;
return conn->err; return conn->err;
@ -692,14 +660,12 @@ netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
err_t err_t
netconn_close(struct netconn *conn) netconn_close(struct netconn *conn)
{ {
struct api_msg *msg; struct api_msg _msg;
struct api_msg *msg = &_msg;
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
} }
if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM);
}
conn->state = NETCONN_CLOSE; conn->state = NETCONN_CLOSE;
again: again:
@ -713,7 +679,6 @@ netconn_close(struct netconn *conn)
goto again; goto again;
} }
conn->state = NETCONN_NONE; conn->state = NETCONN_NONE;
memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }

View File

@ -176,7 +176,6 @@ tcpip_apimsg(struct api_msg *apimsg)
struct tcpip_msg *msg; struct tcpip_msg *msg;
msg = memp_malloc(MEMP_TCPIP_MSG); msg = memp_malloc(MEMP_TCPIP_MSG);
if (msg == NULL) { if (msg == NULL) {
memp_free(MEMP_API_MSG, apimsg);
return; return;
} }
msg->type = TCPIP_MSG_API; msg->type = TCPIP_MSG_API;

View File

@ -62,7 +62,6 @@ static const u16_t memp_sizes[MEMP_MAX] = {
MEM_ALIGN_SIZE(sizeof(struct tcp_seg)), MEM_ALIGN_SIZE(sizeof(struct tcp_seg)),
MEM_ALIGN_SIZE(sizeof(struct netbuf)), MEM_ALIGN_SIZE(sizeof(struct netbuf)),
MEM_ALIGN_SIZE(sizeof(struct netconn)), MEM_ALIGN_SIZE(sizeof(struct netconn)),
MEM_ALIGN_SIZE(sizeof(struct api_msg)),
MEM_ALIGN_SIZE(sizeof(struct tcpip_msg)), MEM_ALIGN_SIZE(sizeof(struct tcpip_msg)),
MEM_ALIGN_SIZE(sizeof(struct sys_timeo)) MEM_ALIGN_SIZE(sizeof(struct sys_timeo))
}; };
@ -76,7 +75,6 @@ static const u16_t memp_num[MEMP_MAX] = {
MEMP_NUM_TCP_SEG, MEMP_NUM_TCP_SEG,
MEMP_NUM_NETBUF, MEMP_NUM_NETBUF,
MEMP_NUM_NETCONN, MEMP_NUM_NETCONN,
MEMP_NUM_API_MSG,
MEMP_NUM_TCPIP_MSG, MEMP_NUM_TCPIP_MSG,
MEMP_NUM_SYS_TIMEOUT MEMP_NUM_SYS_TIMEOUT
}; };
@ -93,7 +91,6 @@ static u8_t memp_memory[MEM_ALIGNMENT - 1 +
MEMP_TYPE_SIZE(MEMP_NUM_TCP_SEG, struct tcp_seg) + MEMP_TYPE_SIZE(MEMP_NUM_TCP_SEG, struct tcp_seg) +
MEMP_TYPE_SIZE(MEMP_NUM_NETBUF, struct netbuf) + MEMP_TYPE_SIZE(MEMP_NUM_NETBUF, struct netbuf) +
MEMP_TYPE_SIZE(MEMP_NUM_NETCONN, struct netconn) + MEMP_TYPE_SIZE(MEMP_NUM_NETCONN, struct netconn) +
MEMP_TYPE_SIZE(MEMP_NUM_API_MSG, struct api_msg) +
MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG, struct tcpip_msg) + MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG, struct tcpip_msg) +
MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)]; MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)];

View File

@ -45,7 +45,6 @@ typedef enum {
MEMP_NETBUF, MEMP_NETBUF,
MEMP_NETCONN, MEMP_NETCONN,
MEMP_API_MSG,
MEMP_TCPIP_MSG, MEMP_TCPIP_MSG,
MEMP_SYS_TIMEOUT, MEMP_SYS_TIMEOUT,