/***
*memmove.c - contains memmove routine
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* memmove() copies a source memory buffer to a destination buffer.
* Overlapping buffers are treated specially, to avoid propogation.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*memmove - Copy source buffer to destination buffer
*
*Purpose:
* memmove() copies a source memory buffer to a destination memory buffer.
* This routine recognize overlapping buffers to avoid propogation.
* For cases where propogation is not a problem, memcpy() can be used.
*
*Entry:
* void *dst = pointer to destination buffer
* const void *src = pointer to source buffer
* size_t count = number of bytes to copy
*
*Exit:
* Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/
void * __cdecl memmove (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
#if defined (_M_IA64) || defined (_M_AMD64)
{
__declspec(dllimport)
void RtlMoveMemory( void *, const void *, size_t count );
RtlMoveMemory( dst, src, count );
}
#else /* defined (_M_IA64) || defined (_M_AMD64) */
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
#endif /* defined (_M_IA64) || defined (_M_AMD64) */
return(ret);
}
上面这是在D:\Program Files\Microsoft Visual Studio 9.0-2008\VC\crt\src里VC++自带的C运行时库函数,也查看了linux下的C运行库函数glibc-2.1.3\sysdeps\generic这个文件夹下,发现里面有好多宏调用。
/***
*memset.c - set a section of memory to all one byte
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* contains the memset() routine
*
*******************************************************************************/
#include <string.h>
#ifdef _MSC_VER
#pragma function(memset)
#endif /* _MSC_VER */
/***
*char *memset(dst, val, count) - sets "count" bytes at "dst" to "val"
*
*Purpose:
* Sets the first "count" bytes of the memory starting
* at "dst" to the character value "val".
*
*Entry:
* void *dst - pointer to memory to fill with val
* int val - value to put in dst bytes
* size_t count - number of bytes of dst to fill
*
*Exit:
* returns dst, with filled bytes
*
*Exceptions:
*
*******************************************************************************/
void * __cdecl memset (
void *dst,
int val,
size_t count
)
{
void *start = dst;
#if defined (_M_IA64) || defined (_M_AMD64)
{
__declspec(dllimport)
void RtlFillMemory( void *, size_t count, char );
RtlFillMemory( dst, count, (char)val );
}
#else /* defined (_M_IA64) || defined (_M_AMD64) */
while (count--) {
*(char *)dst = (char)val;
dst = (char *)dst + 1;
}
#endif /* defined (_M_IA64) || defined (_M_AMD64) */
return(start);
}
这个函数既然只能设置一个字符,为什么参数传递整形呢?/***
*memcmp.c - compare two blocks of memory
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines memcmp() - compare two memory blocks lexically and
* find their order.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
#ifdef _MSC_VER
#pragma function(memcmp)
#endif /* _MSC_VER */
/***
*int memcmp(buf1, buf2, count) - compare memory for lexical order
*
*Purpose:
* Compares count bytes of memory starting at buf1 and buf2
* and find if equal or which one is first in lexical order.
*
*Entry:
* void *buf1, *buf2 - pointers to memory sections to compare
* size_t count - length of sections to compare
*
*Exit:
* returns < 0 if buf1 < buf2
* returns 0 if buf1 == buf2
* returns > 0 if buf1 > buf2
*
*Exceptions:
*
*******************************************************************************/
int __cdecl memcmp (
const void * buf1,
const void * buf2,
size_t count
)
{
if (!count)
return(0);
#if defined (_M_AMD64)
{
__declspec(dllimport)
size_t RtlCompareMemory( const void * src1, const void * src2, size_t length );
size_t length;
if ( ( length = RtlCompareMemory( buf1, buf2, count ) ) == count ) {
return(0);
}
buf1 = (char *)buf1 + length;
buf2 = (char *)buf2 + length;
}
#else /* defined (_M_AMD64) */
while ( --count && *(char *)buf1 == *(char *)buf2 ) {
buf1 = (char *)buf1 + 1;
buf2 = (char *)buf2 + 1;
}
#endif /* defined (_M_AMD64) */
return( *((unsigned char *)buf1) - *((unsigned char *)buf2) );
}
为什么最后一行要转换成unsigned char*呢,因为是按ascii码比较的,否则的话,可能buf1是正数,buf2是负数(例如01111110,和10000001),我是这么想的。/***
*memccpy.c - copy bytes until a character is found
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines _memccpy() - copies bytes until a specifed character
* is found, or a maximum number of characters have been copied.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *_memccpy(dest, src, c, count) - copy bytes until character found
*
*Purpose:
* Copies bytes from src to dest until count bytes have been
* copied, or up to and including the character c, whichever
* comes first.
*
*Entry:
* void *dest - pointer to memory to receive copy
* void *src - source of bytes
* int c - character to stop copy at
* size_t count - max number of bytes to copy
*
*Exit:
* returns pointer to byte immediately after c in dest
* returns NULL if c was never found
*
*Exceptions:
*
*******************************************************************************/
void * __cdecl _memccpy (
void * dest,
const void * src,
int c,
size_t count
)
{
while ( count && (*((char *)(dest = (char *)dest + 1) - 1) =
*((char *)(src = (char *)src + 1) - 1)) != (char)c )
count--;
return(count ? dest : NULL);
}
//////////////////////////////////////////////////////////////////////////
void *memccpy ( void * dest,const void * src,int c,size_t count )
{
while(count&&((*(char*)dest=*(char*)src)!=(char)c))
{
count--;
src = (char*)src + 1;
dest = (char*)dest + 1;
}
return(count?(char*)dest+1:NULL);//返回目的串的中停止字符的后一个位置
}
这个函数有什么用?/***
*memchr.c - search block of memory for a given character
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines memchr() - search memory until a character is
* found or a limit is reached.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *memchr(buf, chr, cnt) - search memory for given character.
*
*Purpose:
* Searches at buf for the given character, stopping when chr is
* first found or cnt bytes have been searched through.
*
*Entry:
* void *buf - memory buffer to be searched
* int chr - character to search for
* size_t cnt - max number of bytes to search
*
*Exit:
* returns pointer to first occurence of chr in buf
* returns NULL if chr not found in the first cnt bytes
*
*Exceptions:
*
*******************************************************************************/
void * __cdecl memchr (
const void * buf,
int chr,
size_t cnt
)
{
while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
buf = (unsigned char *)buf + 1;
cnt--;
}
return(cnt ? (void *)buf : NULL);
}
都用unsigned char*