diff --git a/src/api/api_lib.c b/src/api/api_lib.c index 9ce9ec25..80b2d088 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -44,7 +44,7 @@ netbuf *netbuf_new(void) { struct netbuf *buf; - buf = memp_mallocp(MEMP_NETBUF); + buf = memp_malloc(MEMP_NETBUF); if (buf != NULL) { buf->p = NULL; buf->ptr = NULL; @@ -62,7 +62,7 @@ netbuf_delete(struct netbuf *buf) pbuf_free(buf->p); 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); head->ptr = head->p; - memp_freep(MEMP_NETBUF, tail); + memp_free(MEMP_NETBUF, tail); } /*-----------------------------------------------------------------------------------*/ u16_t @@ -198,7 +198,7 @@ netconn *netconn_new(enum netconn_type t) { struct netconn *conn; - conn = memp_mallocp(MEMP_NETCONN); + conn = memp_malloc(MEMP_NETCONN); if (conn == NULL) { return NULL; } @@ -206,7 +206,7 @@ netconn *netconn_new(enum netconn_type t) conn->pcb.tcp = NULL; if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) { - memp_freep(MEMP_NETCONN, conn); + memp_free(MEMP_NETCONN, conn); return NULL; } conn->recvmbox = SYS_MBOX_NULL; @@ -243,7 +243,7 @@ netconn_delete(struct netconn *conn) return ERR_OK; } - if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { + if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) { return ERR_MEM; } @@ -251,7 +251,7 @@ netconn_delete(struct netconn *conn) msg->msg.conn = conn; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); /* Drain the recvmbox. */ 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); } msg->type = API_MSG_BIND; @@ -362,7 +362,7 @@ netconn_bind(struct netconn *conn, struct ip_addr *addr, msg->msg.msg.bc.port = port; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); 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; } msg->type = API_MSG_CONNECT; @@ -393,7 +393,7 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr, msg->msg.msg.bc.port = port; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); return conn->err; } @@ -406,14 +406,14 @@ netconn_disconnect(struct netconn *conn) return ERR_VAL; } - if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { + if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) { return ERR_MEM; } msg->type = API_MSG_DISCONNECT; msg->msg.conn = conn; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); 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); } msg->type = API_MSG_LISTEN; msg->msg.conn = conn; api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); 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) { 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 data by setting conn->recvmbox to SYS_MBOX_NULL. */ if (p == NULL) { - memp_freep(MEMP_NETBUF, buf); + memp_free(MEMP_NETBUF, buf); sys_mbox_free(conn->recvmbox); conn->recvmbox = SYS_MBOX_NULL; return NULL; @@ -526,7 +526,7 @@ netconn_recv(struct netconn *conn) buf->fromaddr = NULL; /* 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; return buf; } @@ -540,7 +540,7 @@ netconn_recv(struct netconn *conn) api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); } else { sys_mbox_fetch(conn->recvmbox, (void **)&buf); conn->recv_avail -= buf->p->tot_len; @@ -571,7 +571,7 @@ netconn_send(struct netconn *conn, struct netbuf *buf) return conn->err; } - if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { + if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) { return (conn->err = ERR_MEM); } @@ -582,7 +582,7 @@ netconn_send(struct netconn *conn, struct netbuf *buf) api_msg_post(msg); sys_mbox_fetch(conn->mbox, NULL); - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); 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); } msg->type = API_MSG_WRITE; @@ -652,7 +652,7 @@ netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy) } } ret: - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); conn->state = NETCONN_NONE; if (conn->sem != SYS_SEM_NULL) { sys_sem_free(conn->sem); @@ -670,7 +670,7 @@ netconn_close(struct netconn *conn) if (conn == NULL) { return ERR_VAL; } - if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) { + if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) { return (conn->err = ERR_MEM); } @@ -686,7 +686,7 @@ netconn_close(struct netconn *conn) goto again; } conn->state = NETCONN_NONE; - memp_freep(MEMP_API_MSG, msg); + memp_free(MEMP_API_MSG, msg); return conn->err; } /*-----------------------------------------------------------------------------------*/ diff --git a/src/api/api_msg.c b/src/api/api_msg.c index 7dba5e17..de03dca9 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -52,7 +52,7 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, return; } if (conn->recvmbox != SYS_MBOX_NULL) { - buf = memp_mallocp(MEMP_NETBUF); + buf = memp_malloc(MEMP_NETBUF); if (buf == NULL) { pbuf_free(p); return; @@ -193,7 +193,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err) #endif /* API_MSG_DEBUG */ conn = (struct netconn *)arg; mbox = conn->acceptmbox; - newconn = memp_mallocp(MEMP_NETCONN); + newconn = memp_malloc(MEMP_NETCONN); if (newconn == NULL) { return ERR_MEM; } diff --git a/src/api/tcpip.c b/src/api/tcpip.c index bcb114d6..32cdcdf5 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -110,7 +110,7 @@ tcpip_thread(void *arg) default: 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; - msg = memp_mallocp(MEMP_TCPIP_MSG); + msg = memp_malloc(MEMP_TCPIP_MSG); if (msg == NULL) { pbuf_free(p); return ERR_MEM; @@ -137,7 +137,7 @@ tcpip_callback(void (*f)(void *ctx), void *ctx) { struct tcpip_msg *msg; - msg = memp_mallocp(MEMP_TCPIP_MSG); + msg = memp_malloc(MEMP_TCPIP_MSG); if (msg == NULL) { return ERR_MEM; } @@ -153,7 +153,7 @@ void tcpip_apimsg(struct api_msg *apimsg) { struct tcpip_msg *msg; - msg = memp_mallocp(MEMP_TCPIP_MSG); + msg = memp_malloc(MEMP_TCPIP_MSG); if (msg == NULL) { memp_free(MEMP_API_MSG, apimsg); return; diff --git a/src/core/memp.c b/src/core/memp.c index 17b4d3af..26ae7e00 100644 --- a/src/core/memp.c +++ b/src/core/memp.c @@ -182,9 +182,18 @@ memp_malloc(memp_t type) { struct memp *memp; void *mem; +#ifdef SYS_LIGHTWEIGHT_PROT + SYS_ARCH_DECL_PROTECT(old_level); +#endif 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]; if (memp != NULL) { @@ -196,6 +205,11 @@ memp_malloc(memp_t type) lwip_stats.memp[type].max = lwip_stats.memp[type].used; } #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", ((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 ++lwip_stats.memp[type].err; #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 SYS_ARCH_UNPROTECT(old_level); #else /* SYS_LIGHTWEIGHT_PROT */ sys_sem_signal(mutex); #endif /* SYS_LIGHTWEIGHT_PROT */ - return mem; + return NULL; + } } /*-----------------------------------------------------------------------------------*/ void memp_free(memp_t type, void *mem) { struct memp *memp; +#ifdef SYS_LIGHTWEIGHT_PROT + SYS_ARCH_DECL_PROTECT(old_level); +#endif /* SYS_LIGHTWEIGHT_PROT */ if (mem == NULL) { return; } 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 lwip_stats.memp[type].used--; #endif /* MEMP_STATS */ @@ -252,26 +259,10 @@ memp_free(memp_t type, void *mem) 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 SYS_ARCH_UNPROTECT(old_level); #else /* SYS_LIGHTWEIGHT_PROT */ sys_sem_signal(mutex); #endif /* SYS_LIGHTWEIGHT_PROT */ - } /*-----------------------------------------------------------------------------------*/ diff --git a/src/core/pbuf.c b/src/core/pbuf.c index febfa788..05c6f560 100644 --- a/src/core/pbuf.c +++ b/src/core/pbuf.c @@ -323,7 +323,7 @@ pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag) /* pbuf references existing (externally allocated) RAM payload? */ case PBUF_REF: /* only allocate memory for the pbuf structure */ - p = memp_mallocp(MEMP_PBUF); + p = memp_malloc(MEMP_PBUF); 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")); return NULL; @@ -576,7 +576,7 @@ pbuf_free(struct pbuf *p) PBUF_POOL_FREE(p); /* a ROM or RAM referencing pbuf */ } 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 */ } else { mem_free(p); diff --git a/src/include/lwip/memp.h b/src/include/lwip/memp.h index 742ec3ac..2b58f0a1 100644 --- a/src/include/lwip/memp.h +++ b/src/include/lwip/memp.h @@ -58,8 +58,5 @@ void *memp_malloc(memp_t type); void *memp_realloc(memp_t fromtype, memp_t totype, 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__ */