Eliminated memp_mallocp() and memp_freep() in favour of memp_malloc()

and memp_free() respectively. Protection is always used, except while
zeroing out memory ;-). Closes patch #1184. If you see any issues
with this change please email me.
This commit is contained in:
marcbou 2003-06-19 12:32:24 +00:00
parent d5e08d7d78
commit 6657b656f5
6 changed files with 58 additions and 70 deletions

View File

@ -44,7 +44,7 @@ netbuf *netbuf_new(void)
{ {
struct netbuf *buf; struct netbuf *buf;
buf = memp_mallocp(MEMP_NETBUF); buf = memp_malloc(MEMP_NETBUF);
if (buf != NULL) { if (buf != NULL) {
buf->p = NULL; buf->p = NULL;
buf->ptr = NULL; buf->ptr = NULL;
@ -62,7 +62,7 @@ netbuf_delete(struct netbuf *buf)
pbuf_free(buf->p); pbuf_free(buf->p);
buf->p = buf->ptr = NULL; buf->p = buf->ptr = NULL;
} }
memp_freep(MEMP_NETBUF, buf); memp_free(MEMP_NETBUF, buf);
} }
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -107,7 +107,7 @@ netbuf_chain(struct netbuf *head, struct netbuf *tail)
{ {
pbuf_chain(head->p, tail->p); pbuf_chain(head->p, tail->p);
head->ptr = head->p; head->ptr = head->p;
memp_freep(MEMP_NETBUF, tail); memp_free(MEMP_NETBUF, tail);
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
u16_t u16_t
@ -198,7 +198,7 @@ netconn *netconn_new(enum netconn_type t)
{ {
struct netconn *conn; struct netconn *conn;
conn = memp_mallocp(MEMP_NETCONN); conn = memp_malloc(MEMP_NETCONN);
if (conn == NULL) { if (conn == NULL) {
return NULL; return NULL;
} }
@ -206,7 +206,7 @@ netconn *netconn_new(enum netconn_type t)
conn->pcb.tcp = NULL; conn->pcb.tcp = NULL;
if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) { if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) {
memp_freep(MEMP_NETCONN, conn); memp_free(MEMP_NETCONN, conn);
return NULL; return NULL;
} }
conn->recvmbox = SYS_MBOX_NULL; conn->recvmbox = SYS_MBOX_NULL;
@ -243,7 +243,7 @@ netconn_delete(struct netconn *conn)
return ERR_OK; return ERR_OK;
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return ERR_MEM; return ERR_MEM;
} }
@ -251,7 +251,7 @@ netconn_delete(struct netconn *conn)
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_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
/* Drain the recvmbox. */ /* Drain the recvmbox. */
if (conn->recvmbox != SYS_MBOX_NULL) { if (conn->recvmbox != SYS_MBOX_NULL) {
@ -353,7 +353,7 @@ netconn_bind(struct netconn *conn, struct ip_addr *addr,
} }
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM); return (conn->err = ERR_MEM);
} }
msg->type = API_MSG_BIND; msg->type = API_MSG_BIND;
@ -362,7 +362,7 @@ netconn_bind(struct netconn *conn, struct ip_addr *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_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
@ -384,7 +384,7 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr,
} }
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return ERR_MEM; return ERR_MEM;
} }
msg->type = API_MSG_CONNECT; msg->type = API_MSG_CONNECT;
@ -393,7 +393,7 @@ netconn_connect(struct netconn *conn, struct ip_addr *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_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
@ -406,14 +406,14 @@ netconn_disconnect(struct netconn *conn)
return ERR_VAL; return ERR_VAL;
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return ERR_MEM; 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_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
@ -434,14 +434,14 @@ netconn_listen(struct netconn *conn)
} }
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM); 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_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -490,7 +490,7 @@ netconn_recv(struct netconn *conn)
} }
buf = memp_mallocp(MEMP_NETBUF); buf = memp_malloc(MEMP_NETBUF);
if (buf == NULL) { if (buf == NULL) {
conn->err = ERR_MEM; conn->err = ERR_MEM;
@ -514,7 +514,7 @@ netconn_recv(struct netconn *conn)
/* If we are closed, we indicate that we no longer wish to receive /* If we are closed, we indicate that we no longer wish to receive
data by setting conn->recvmbox to SYS_MBOX_NULL. */ data by setting conn->recvmbox to SYS_MBOX_NULL. */
if (p == NULL) { if (p == NULL) {
memp_freep(MEMP_NETBUF, buf); memp_free(MEMP_NETBUF, buf);
sys_mbox_free(conn->recvmbox); sys_mbox_free(conn->recvmbox);
conn->recvmbox = SYS_MBOX_NULL; conn->recvmbox = SYS_MBOX_NULL;
return NULL; return NULL;
@ -526,7 +526,7 @@ 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_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
conn->err = ERR_MEM; conn->err = ERR_MEM;
return buf; return buf;
} }
@ -540,7 +540,7 @@ 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_freep(MEMP_API_MSG, msg); 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;
@ -571,7 +571,7 @@ netconn_send(struct netconn *conn, struct netbuf *buf)
return conn->err; return conn->err;
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM); return (conn->err = ERR_MEM);
} }
@ -582,7 +582,7 @@ 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_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -607,7 +607,7 @@ netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
} }
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM); return (conn->err = ERR_MEM);
} }
msg->type = API_MSG_WRITE; msg->type = API_MSG_WRITE;
@ -652,7 +652,7 @@ netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
} }
} }
ret: ret:
memp_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
conn->state = NETCONN_NONE; conn->state = NETCONN_NONE;
if (conn->sem != SYS_SEM_NULL) { if (conn->sem != SYS_SEM_NULL) {
sys_sem_free(conn->sem); sys_sem_free(conn->sem);
@ -670,7 +670,7 @@ netconn_close(struct netconn *conn)
if (conn == NULL) { if (conn == NULL) {
return ERR_VAL; return ERR_VAL;
} }
if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
return (conn->err = ERR_MEM); return (conn->err = ERR_MEM);
} }
@ -686,7 +686,7 @@ netconn_close(struct netconn *conn)
goto again; goto again;
} }
conn->state = NETCONN_NONE; conn->state = NETCONN_NONE;
memp_freep(MEMP_API_MSG, msg); memp_free(MEMP_API_MSG, msg);
return conn->err; return conn->err;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -52,7 +52,7 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
return; return;
} }
if (conn->recvmbox != SYS_MBOX_NULL) { if (conn->recvmbox != SYS_MBOX_NULL) {
buf = memp_mallocp(MEMP_NETBUF); buf = memp_malloc(MEMP_NETBUF);
if (buf == NULL) { if (buf == NULL) {
pbuf_free(p); pbuf_free(p);
return; return;
@ -193,7 +193,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
#endif /* API_MSG_DEBUG */ #endif /* API_MSG_DEBUG */
conn = (struct netconn *)arg; conn = (struct netconn *)arg;
mbox = conn->acceptmbox; mbox = conn->acceptmbox;
newconn = memp_mallocp(MEMP_NETCONN); newconn = memp_malloc(MEMP_NETCONN);
if (newconn == NULL) { if (newconn == NULL) {
return ERR_MEM; return ERR_MEM;
} }

View File

@ -110,7 +110,7 @@ tcpip_thread(void *arg)
default: default:
break; break;
} }
memp_freep(MEMP_TCPIP_MSG, msg); memp_free(MEMP_TCPIP_MSG, msg);
} }
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -119,7 +119,7 @@ tcpip_input(struct pbuf *p, struct netif *inp)
{ {
struct tcpip_msg *msg; struct tcpip_msg *msg;
msg = memp_mallocp(MEMP_TCPIP_MSG); msg = memp_malloc(MEMP_TCPIP_MSG);
if (msg == NULL) { if (msg == NULL) {
pbuf_free(p); pbuf_free(p);
return ERR_MEM; return ERR_MEM;
@ -137,7 +137,7 @@ tcpip_callback(void (*f)(void *ctx), void *ctx)
{ {
struct tcpip_msg *msg; struct tcpip_msg *msg;
msg = memp_mallocp(MEMP_TCPIP_MSG); msg = memp_malloc(MEMP_TCPIP_MSG);
if (msg == NULL) { if (msg == NULL) {
return ERR_MEM; return ERR_MEM;
} }
@ -153,7 +153,7 @@ void
tcpip_apimsg(struct api_msg *apimsg) tcpip_apimsg(struct api_msg *apimsg)
{ {
struct tcpip_msg *msg; struct tcpip_msg *msg;
msg = memp_mallocp(MEMP_TCPIP_MSG); msg = memp_malloc(MEMP_TCPIP_MSG);
if (msg == NULL) { if (msg == NULL) {
memp_free(MEMP_API_MSG, apimsg); memp_free(MEMP_API_MSG, apimsg);
return; return;

View File

@ -182,9 +182,18 @@ memp_malloc(memp_t type)
{ {
struct memp *memp; struct memp *memp;
void *mem; void *mem;
#ifdef SYS_LIGHTWEIGHT_PROT
SYS_ARCH_DECL_PROTECT(old_level);
#endif
LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX); LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX);
#ifdef SYS_LIGHTWEIGHT_PROT
SYS_ARCH_PROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_wait(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */
memp = memp_tab[type]; memp = memp_tab[type];
if (memp != NULL) { if (memp != NULL) {
@ -196,6 +205,11 @@ memp_malloc(memp_t type)
lwip_stats.memp[type].max = lwip_stats.memp[type].used; lwip_stats.memp[type].max = lwip_stats.memp[type].used;
} }
#endif /* MEMP_STATS */ #endif /* MEMP_STATS */
#ifdef SYS_LIGHTWEIGHT_PROT
SYS_ARCH_UNPROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_signal(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */
LWIP_ASSERT("memp_malloc: memp properly aligned", LWIP_ASSERT("memp_malloc: memp properly aligned",
((u32_t)MEM_ALIGN((u8_t *)memp + sizeof(struct memp)) % MEM_ALIGNMENT) == 0); ((u32_t)MEM_ALIGN((u8_t *)memp + sizeof(struct memp)) % MEM_ALIGNMENT) == 0);
@ -208,41 +222,34 @@ memp_malloc(memp_t type)
#ifdef MEMP_STATS #ifdef MEMP_STATS
++lwip_stats.memp[type].err; ++lwip_stats.memp[type].err;
#endif /* MEMP_STATS */ #endif /* MEMP_STATS */
return NULL;
}
}
/*-----------------------------------------------------------------------------------*/
void *
memp_mallocp(memp_t type)
{
void *mem;
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_DECL_PROTECT(old_level);
SYS_ARCH_PROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_wait(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */
mem = memp_malloc(type);
#if SYS_LIGHTWEIGHT_PROT #if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_UNPROTECT(old_level); SYS_ARCH_UNPROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */ #else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_signal(mutex); sys_sem_signal(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */ #endif /* SYS_LIGHTWEIGHT_PROT */
return mem; return NULL;
}
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void void
memp_free(memp_t type, void *mem) memp_free(memp_t type, void *mem)
{ {
struct memp *memp; struct memp *memp;
#ifdef SYS_LIGHTWEIGHT_PROT
SYS_ARCH_DECL_PROTECT(old_level);
#endif /* SYS_LIGHTWEIGHT_PROT */
if (mem == NULL) { if (mem == NULL) {
return; return;
} }
memp = (struct memp *)((u8_t *)mem - sizeof(struct memp)); memp = (struct memp *)((u8_t *)mem - sizeof(struct memp));
#ifdef SYS_LIGHTWEIGHT_PROT
SYS_ARCH_PROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_wait(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */
#ifdef MEMP_STATS #ifdef MEMP_STATS
lwip_stats.memp[type].used--; lwip_stats.memp[type].used--;
#endif /* MEMP_STATS */ #endif /* MEMP_STATS */
@ -252,26 +259,10 @@ memp_free(memp_t type, void *mem)
LWIP_ASSERT("memp sanity", memp_sanity()); LWIP_ASSERT("memp sanity", memp_sanity());
return;
}
/*-----------------------------------------------------------------------------------*/
void
memp_freep(memp_t type, void *mem)
{
#if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_DECL_PROTECT(old_level);
SYS_ARCH_PROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_wait(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */
memp_free(type, mem);
#if SYS_LIGHTWEIGHT_PROT #if SYS_LIGHTWEIGHT_PROT
SYS_ARCH_UNPROTECT(old_level); SYS_ARCH_UNPROTECT(old_level);
#else /* SYS_LIGHTWEIGHT_PROT */ #else /* SYS_LIGHTWEIGHT_PROT */
sys_sem_signal(mutex); sys_sem_signal(mutex);
#endif /* SYS_LIGHTWEIGHT_PROT */ #endif /* SYS_LIGHTWEIGHT_PROT */
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -323,7 +323,7 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
/* pbuf references existing (externally allocated) RAM payload? */ /* pbuf references existing (externally allocated) RAM payload? */
case PBUF_REF: case PBUF_REF:
/* only allocate memory for the pbuf structure */ /* only allocate memory for the pbuf structure */
p = memp_mallocp(MEMP_PBUF); p = memp_malloc(MEMP_PBUF);
if (p == NULL) { if (p == NULL) {
LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", flag == PBUF_ROM?"ROM":"REF")); LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", flag == PBUF_ROM?"ROM":"REF"));
return NULL; return NULL;
@ -576,7 +576,7 @@ pbuf_free(struct pbuf *p)
PBUF_POOL_FREE(p); PBUF_POOL_FREE(p);
/* a ROM or RAM referencing pbuf */ /* a ROM or RAM referencing pbuf */
} else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) { } else if (p->flags == PBUF_FLAG_ROM || p->flags == PBUF_FLAG_REF) {
memp_freep(MEMP_PBUF, p); memp_free(MEMP_PBUF, p);
/* p->flags == PBUF_FLAG_RAM */ /* p->flags == PBUF_FLAG_RAM */
} else { } else {
mem_free(p); mem_free(p);

View File

@ -58,8 +58,5 @@ void *memp_malloc(memp_t type);
void *memp_realloc(memp_t fromtype, memp_t totype, void *mem); void *memp_realloc(memp_t fromtype, memp_t totype, void *mem);
void memp_free(memp_t type, void *mem); void memp_free(memp_t type, void *mem);
void *memp_mallocp(memp_t type);
void memp_freep(memp_t type, void *mem);
#endif /* __LWIP_MEMP_H__ */ #endif /* __LWIP_MEMP_H__ */