From 255d5a748f2d8a77fc317c65685fb67a0f8ec8c5 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Thu, 10 May 2007 05:20:05 +0000 Subject: [PATCH] Included patch #5920: Create define to override C-library memcpy. 2 Defines are created: MEMCPY() for normal memcpy, SMEMCPY() for situations where some compilers might inline the copy and save a function call. Also replaced all calls to memcpy() with calls to (S)MEMCPY(). --- CHANGELOG | 5 +++++ src/api/api_lib.c | 2 +- src/api/sockets.c | 8 ++++---- src/core/ipv4/icmp.c | 4 ++-- src/core/ipv4/ip_frag.c | 8 ++++---- src/core/ipv6/icmp6.c | 4 ++-- src/core/ipv6/ip6_addr.c | 2 +- src/core/pbuf.c | 2 +- src/core/tcp.c | 2 +- src/core/tcp_out.c | 4 ++-- src/include/lwip/opt.h | 12 ++++++++++++ src/netif/etharp.c | 4 ++-- src/netif/loopif.c | 2 +- src/netif/ppp/md5.c | 2 +- src/netif/ppp/ppp.c | 16 ++++++++-------- src/netif/ppp/ppp.h | 2 +- src/netif/ppp/randm.c | 2 +- src/netif/ppp/vj.c | 4 ++-- 18 files changed, 51 insertions(+), 34 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 091e8e44..433bc73b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -22,6 +22,11 @@ HISTORY * [Enter new changes just after this line - do not remove this line] ++ New features: + 2007-05-08 Simon Goldschmidt + * opt.h, *.c/*.h: Included patch #5920: Create define to override C-library + memcpy. 2 Defines are created: MEMCPY() for normal memcpy, SMEMCPY() for + situations where some compilers might inline the copy and save a function + call. Also replaced all calls to memcpy() with calls to (S)MEMCPY(). 2007-05-08 Simon Goldschmidt * mem.h: If MEM_LIBC_MALLOC==1, allow the defines (e.g. mem_malloc() -> malloc()) diff --git a/src/api/api_lib.c b/src/api/api_lib.c index d5478108..7916ae84 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -171,7 +171,7 @@ netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset) if (buf_copy_len > len) buf_copy_len = len; /* copy the necessary parts of the buffer */ - memcpy(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len); + MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len); left += buf_copy_len; len -= buf_copy_len; offset = 0; diff --git a/src/api/sockets.c b/src/api/sockets.c index 6ba7840b..f0671531 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -191,7 +191,7 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) if (*addrlen > sizeof(sin)) *addrlen = sizeof(sin); - memcpy(addr, &sin, *addrlen); + SMEMCPY(addr, &sin, *addrlen); newsock = alloc_socket(newconn); if (newsock == -1) { @@ -412,7 +412,7 @@ lwip_recvfrom(int s, void *mem, int len, unsigned int flags, if (*fromlen > sizeof(sin)) *fromlen = sizeof(sin); - memcpy(from, &sin, *fromlen); + SMEMCPY(from, &sin, *fromlen); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s)); ip_addr_debug_print(SOCKETS_DEBUG, addr); @@ -890,7 +890,7 @@ int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen) if (*namelen > sizeof(sin)) *namelen = sizeof(sin); - memcpy(name, &sin, *namelen); + SMEMCPY(name, &sin, *namelen); sock_set_errno(sock, 0); return 0; } @@ -922,7 +922,7 @@ int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen) if (*namelen > sizeof(sin)) *namelen = sizeof(sin); - memcpy(name, &sin, *namelen); + SMEMCPY(name, &sin, *namelen); sock_set_errno(sock, 0); return 0; } diff --git a/src/core/ipv4/icmp.c b/src/core/ipv4/icmp.c index 76d25dab..349d5efa 100644 --- a/src/core/ipv4/icmp.c +++ b/src/core/ipv4/icmp.c @@ -141,7 +141,7 @@ icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t) ICMPH_TYPE_SET(idur, ICMP_DUR); ICMPH_CODE_SET(idur, t); - memcpy((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8); + SMEMCPY((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8); /* calculate checksum */ idur->chksum = 0; @@ -179,7 +179,7 @@ icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t) ICMPH_CODE_SET(tehdr, t); /* copy fields from original packet */ - memcpy((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8); + SMEMCPY((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8); /* calculate checksum */ tehdr->chksum = 0; diff --git a/src/core/ipv4/ip_frag.c b/src/core/ipv4/ip_frag.c index 659bc8cc..632d02e4 100644 --- a/src/core/ipv4/ip_frag.c +++ b/src/core/ipv4/ip_frag.c @@ -74,7 +74,7 @@ copy_from_pbuf(struct pbuf *p, u16_t * offset, p->len -= *offset; while (len) { l = len < p->len ? len : p->len; - memcpy(buffer, p->payload, l); + MEMCPY(buffer, p->payload, l); buffer += l; len -= l; if (len) @@ -141,7 +141,7 @@ ip_reass(struct pbuf *p) buffer. The timer is updated with the maximum age. */ if (ip_reasstmr == 0) { LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: new packet\n")); - memcpy(iphdr, fraghdr, IP_HLEN); + SMEMCPY(iphdr, fraghdr, IP_HLEN); ip_reasstmr = IP_REASS_MAXAGE; ip_reassflags = 0; /* Clear the bitmap. */ @@ -277,7 +277,7 @@ ip_reass(struct pbuf *p) ("ip_reass: memcpy from %p (%"S16_F") to %p, %"S16_F" bytes\n", (void *)&ip_reassbuf[i], i, q->payload, q->len > ip_reasslen - i ? ip_reasslen - i : q->len)); - memcpy(q->payload, &ip_reassbuf[i], + MEMCPY(q->payload, &ip_reassbuf[i], q->len > ip_reasslen - i ? ip_reasslen - i : q->len); i += q->len; } @@ -333,7 +333,7 @@ ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest) /* Copy the IP header in it */ iphdr = rambuf->payload; - memcpy(iphdr, p->payload, IP_HLEN); + SMEMCPY(iphdr, p->payload, IP_HLEN); /* Save original offset */ tmp = ntohs(IPH_OFFSET(iphdr)); diff --git a/src/core/ipv6/icmp6.c b/src/core/ipv6/icmp6.c index 10b6903c..f80ca990 100644 --- a/src/core/ipv6/icmp6.c +++ b/src/core/ipv6/icmp6.c @@ -130,7 +130,7 @@ icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t) idur->type = (u8_t)ICMP6_DUR; idur->icode = (u8_t)t; - memcpy((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8); + SMEMCPY((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8); /* calculate checksum */ idur->chksum = 0; @@ -162,7 +162,7 @@ icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t) tehdr->icode = (u8_t)t; /* copy fields from original packet */ - memcpy((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8); + SMEMCPY((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8); /* calculate checksum */ tehdr->chksum = 0; diff --git a/src/core/ipv6/ip6_addr.c b/src/core/ipv6/ip6_addr.c index 6f87ca37..f35b40ab 100644 --- a/src/core/ipv6/ip6_addr.c +++ b/src/core/ipv6/ip6_addr.c @@ -58,7 +58,7 @@ ip_addr_cmp(struct ip_addr *addr1, struct ip_addr *addr2) void ip_addr_set(struct ip_addr *dest, struct ip_addr *src) { - memcpy(dest, src, sizeof(struct ip_addr)); + SMEMCPY(dest, src, sizeof(struct ip_addr)); /* dest->addr[0] = src->addr[0]; dest->addr[1] = src->addr[1]; dest->addr[2] = src->addr[2]; diff --git a/src/core/pbuf.c b/src/core/pbuf.c index f68fd2f8..331b391e 100644 --- a/src/core/pbuf.c +++ b/src/core/pbuf.c @@ -808,7 +808,7 @@ pbuf_copy(struct pbuf *p_to, struct pbuf *p_from) /* current p_from does not fit into current p_to */ len = p_to->len - offset_to; } - memcpy((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len); + MEMCPY((u8_t*)p_to->payload + offset_to, (u8_t*)p_from->payload + offset_from, len); #ifdef LWIP_DEBUG copied += len; #endif diff --git a/src/core/tcp.c b/src/core/tcp.c index c4bb9bb9..6e2cd5ba 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -747,7 +747,7 @@ tcp_seg_copy(struct tcp_seg *seg) if (cseg == NULL) { return NULL; } - memcpy((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); + SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg)); pbuf_ref(cseg->p); return cseg; } diff --git a/src/core/tcp_out.c b/src/core/tcp_out.c index a8be31fd..1e0915e1 100644 --- a/src/core/tcp_out.c +++ b/src/core/tcp_out.c @@ -213,7 +213,7 @@ tcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len, } ++queuelen; if (arg != NULL) { - memcpy(seg->p->payload, ptr, seglen); + MEMCPY(seg->p->payload, ptr, seglen); } seg->dataptr = seg->p->payload; } @@ -280,7 +280,7 @@ tcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len, /* Copy options into data portion of segment. Options can thus only be sent in non data carrying segments such as SYN|ACK. */ - memcpy(seg->dataptr, optdata, optlen); + SMEMCPY(seg->dataptr, optdata, optlen); } LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_enqueue: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n", ntohl(seg->tcphdr->seqno), diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index debfe79c..5e3fae14 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -53,6 +53,18 @@ #define NO_SYS 0 #endif +/* override this if you have a faster implementation at hand than the one + included in your C library */ +#ifndef MEMCPY +#define MEMCPY(dst,src,len) memcpy(dst,src,len) +#endif + +/* override this with care: some compilers (e.g. gcc) can inline a call to + memcpy() if the length is known at compile time and is small */ +#ifndef SMEMCPY +#define SMEMCPY(dst,src,len) memcpy(dst,src,len) +#endif + /* ---------- Memory options ---------- */ #ifndef MEM_LIBC_MALLOC #define MEM_LIBC_MALLOC 0 diff --git a/src/netif/etharp.c b/src/netif/etharp.c index 1980faea..a04afc53 100644 --- a/src/netif/etharp.c +++ b/src/netif/etharp.c @@ -576,8 +576,8 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without * structure packing (not using structure copy which breaks strict-aliasing rules). */ - memcpy(&sipaddr, &hdr->sipaddr, sizeof(sipaddr)); - memcpy(&dipaddr, &hdr->dipaddr, sizeof(dipaddr)); + SMEMCPY(&sipaddr, &hdr->sipaddr, sizeof(sipaddr)); + SMEMCPY(&dipaddr, &hdr->dipaddr, sizeof(dipaddr)); /* this interface is not configured? */ if (netif->ip_addr.addr == 0) { diff --git a/src/netif/loopif.c b/src/netif/loopif.c index 01edd147..97278142 100644 --- a/src/netif/loopif.c +++ b/src/netif/loopif.c @@ -114,7 +114,7 @@ loopif_output(struct netif *netif, struct pbuf *p, */ ptr = r->payload; for(q = p; q != NULL; q = q->next) { - memcpy(ptr, q->payload, q->len); + MEMCPY(ptr, q->payload, q->len); ptr += q->len; } diff --git a/src/netif/ppp/md5.c b/src/netif/ppp/md5.c index e077cdea..ef775936 100644 --- a/src/netif/ppp/md5.c +++ b/src/netif/ppp/md5.c @@ -199,7 +199,7 @@ void MD5Final (unsigned char hash[], MD5_CTX *mdContext) mdContext->digest[ii+3] = (unsigned char)((mdContext->buf[i] >> 24) & 0xFF); } - memcpy(hash, mdContext->digest, 16); + SMEMCPY(hash, mdContext->digest, 16); } /* Basic MD5 step. Transforms buf based on in. diff --git a/src/netif/ppp/ppp.c b/src/netif/ppp/ppp.c index 41b63ee1..3bf7cc03 100644 --- a/src/netif/ppp/ppp.c +++ b/src/netif/ppp/ppp.c @@ -840,7 +840,7 @@ void ppp_send_config( */ void ppp_set_xaccm(int unit, ext_accm *accm) { - memcpy(pppControl[unit].outACCM, accm, sizeof(ext_accm)); + SMEMCPY(pppControl[unit].outACCM, accm, sizeof(ext_accm)); PPPDEBUG((LOG_INFO, "ppp_set_xaccm[%d]: outACCM=%X %X %X %X\n", unit, pppControl[unit].outACCM[0], @@ -1075,11 +1075,11 @@ int sifaddr( st = 0; PPPDEBUG((LOG_WARNING, "sifup[%d]: bad parms\n", pd)); } else { - memcpy(&pc->addrs.our_ipaddr, &o, sizeof(o)); - memcpy(&pc->addrs.his_ipaddr, &h, sizeof(h)); - memcpy(&pc->addrs.netmask, &m, sizeof(m)); - memcpy(&pc->addrs.dns1, &ns1, sizeof(ns1)); - memcpy(&pc->addrs.dns2, &ns2, sizeof(ns2)); + SMEMCPY(&pc->addrs.our_ipaddr, &o, sizeof(o)); + SMEMCPY(&pc->addrs.his_ipaddr, &h, sizeof(h)); + SMEMCPY(&pc->addrs.netmask, &m, sizeof(m)); + SMEMCPY(&pc->addrs.dns1, &ns1, sizeof(ns1)); + SMEMCPY(&pc->addrs.dns2, &ns2, sizeof(ns2)); } return st; } @@ -1269,7 +1269,7 @@ static struct pbuf *pppSingleBuf(struct pbuf *p) } for(b = p, pl = q->payload; b != NULL; b = b->next) { - memcpy(pl, b->payload, b->len); + MEMCPY(pl, b->payload, b->len); pl += b->len; } @@ -1394,7 +1394,7 @@ static void pppInput(void *arg) } #if BYTE_ORDER == LITTLE_ENDIAN protocol = htons(protocol); - memcpy(nb->payload, &protocol, sizeof(protocol)); + SMEMCPY(nb->payload, &protocol, sizeof(protocol)); #endif lcp_sprotrej(pd, nb->payload, nb->len); } diff --git a/src/netif/ppp/ppp.h b/src/netif/ppp/ppp.h index 1e196d50..b0963f25 100644 --- a/src/netif/ppp/ppp.h +++ b/src/netif/ppp/ppp.h @@ -206,7 +206,7 @@ enum NPmode { #define DECPTR(n, cp) ((cp) -= (n)) #define BCMP(s0, s1, l) memcmp((u_char *)(s0), (u_char *)(s1), (l)) -#define BCOPY(s, d, l) memcpy((d), (s), (l)) +#define BCOPY(s, d, l) MEMCPY((d), (s), (l)) #define BZERO(s, n) memset(s, 0, n) #if PPP_DEBUG #define PRINTMSG(m, l) { m[l] = '\0'; ppp_trace(LOG_INFO, "Remote message: %s\n", m); } diff --git a/src/netif/ppp/randm.c b/src/netif/ppp/randm.c index 05eeb441..ee3a837d 100644 --- a/src/netif/ppp/randm.c +++ b/src/netif/ppp/randm.c @@ -127,7 +127,7 @@ void avGenRand(char *buf, u32_t bufLen) MD5Update(&md5, (u_char *)&randCount, sizeof(randCount)); MD5Final(tmp, &md5); randCount++; - memcpy(buf, tmp, n); + MEMCPY(buf, tmp, n); buf += n; bufLen -= n; } diff --git a/src/netif/ppp/vj.c b/src/netif/ppp/vj.c index a59c9620..25dfd8e1 100644 --- a/src/netif/ppp/vj.c +++ b/src/netif/ppp/vj.c @@ -605,7 +605,7 @@ int vj_uncompress_tcp( bufptr = n0->payload; for(q = np; q != NULL; q = q->next) { - memcpy(q->payload, bufptr, q->len); + MEMCPY(q->payload, bufptr, q->len); bufptr += q->len; } @@ -631,7 +631,7 @@ int vj_uncompress_tcp( n0 = np; } LWIP_ASSERT("n0->len >= cs->cs_hlen", n0->len >= cs->cs_hlen); - memcpy(n0->payload, &cs->cs_ip, cs->cs_hlen); + MEMCPY(n0->payload, &cs->cs_ip, cs->cs_hlen); *nb = n0;