Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88150
  • 博文数量: 60
  • 博客积分: 4002
  • 博客等级: 中校
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 18:11
文章分类

全部博文(60)

文章存档

2011年(60)

我的朋友
mem

分类: C/C++

2011-01-02 12:49:55

    mem 库函数系列用于内存处理。

  1. /**
  2.  * memset - Fill a region of memory with the given value
  3.  * @s: Pointer to the start of the area.
  4.  * @c: The byte to fill the area with
  5.  * @count: The size of the area.
  6.  *
  7.  * Do not use memset() to access IO space, use memset_io() instead.
  8.  */
  9. void *memset(void *s, int c, size_t count)
  10. {
  11.     char *xs = s;

  12.     while (count--)
  13.         *xs++ = c;
  14.     return s;
  15. }

  1. /**
  2.  * memcpy - Copy one area of memory to another
  3.  * @dest: Where to copy to
  4.  * @src: Where to copy from
  5.  * @count: The size of the area.
  6.  *
  7.  * You should not use this function to access IO space, use memcpy_toio()
  8.  * or memcpy_fromio() instead.
  9.  */
  10. void *memcpy(void *dest, const void *src, size_t count)
  11. {
  12.     char *tmp = dest;
  13.     const char *s = src;

  14.     while (count--)
  15.         *tmp++ = *s++;
  16.     return dest;
  17. }

  1. /**
  2.  * memmove - Copy one area of memory to another
  3.  * @dest: Where to copy to
  4.  * @src: Where to copy from
  5.  * @count: The size of the area.
  6.  *
  7.  * Unlike memcpy(), memmove() copes with overlapping areas.
  8.  */
  9. void *memmove(void *dest, const void *src, size_t count)
  10. {
  11.     char *tmp;
  12.     const char *s;

  13.     if (dest <= src) {
  14.         tmp = dest;
  15.         s = src;
  16.         while (count--)
  17.             *tmp++ = *s++;
  18.     } else {
  19.         tmp = dest;
  20.         tmp += count;
  21.         s = src;
  22.         s += count;
  23.         while (count--)
  24.             *--tmp = *--s;
  25.     }
  26.     return dest;
  27. }

  1. /**
  2.  * memcmp - Compare two areas of memory
  3.  * @cs: One area of memory
  4.  * @ct: Another area of memory
  5.  * @count: The size of the area.
  6.  */

  7. int memcmp(const void *cs, const void *ct, size_t count)
  8. {
  9.     const unsigned char *su1, *su2;
  10.     int res = 0;

  11.     for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  12.         if ((res = *su1 - *su2) != 0)
  13.             break;
  14.     return res;
  15. }

  1. /**
  2.  * memscan - Find a character in an area of memory.
  3.  * @addr: The memory area
  4.  * @c: The byte to search for
  5.  * @size: The size of the area.
  6.  *
  7.  * returns the address of the first occurrence of @c, or 1 byte past
  8.  * the area if @c is not found
  9.  */
  10. void *memscan(void *addr, int c, size_t size)
  11. {
  12.     unsigned char *p = addr;

  13.     while (size) {
  14.         if (*p == c)
  15.             return (void *)p;
  16.         p++;
  17.         size--;
  18.     }
  19.       return (void *)p;
  20. }

  1. /**
  2.  * memchr - Find a character in an area of memory.
  3.  * @s: The memory area
  4.  * @c: The byte to search for
  5.  * @n: The size of the area.
  6.  *
  7.  * returns the address of the first occurrence of @c, or %NULL
  8.  * if @c is not found
  9.  */
  10. void *memchr(const void *s, int c, size_t n)
  11. {
  12.     const unsigned char *p = s;
  13.     while (n-- != 0) {
  14.             if ((unsigned char)c == *p++) {
  15.             return (void *)(p - 1);
  16.         }
  17.     }
  18.     return NULL;
  19. }
注:摘自linux内核源码
阅读(369) | 评论(0) | 转发(0) |
0

上一篇:strstr

下一篇:插入排序

给主人留下些什么吧!~~