memset.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:2k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* Copyright (C) 1991, 1997 Free Software Foundation, Inc.
  2.    This file is part of the GNU C Library.
  3.    The GNU C Library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public License as
  5.    published by the Free Software Foundation; either version 2 of the
  6.    License, or (at your option) any later version.
  7.    The GNU C Library is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10.    Library General Public License for more details.
  11.    You should have received a copy of the GNU Library General Public
  12.    License along with the GNU C Library; see the file COPYING.LIB.  If not,
  13.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14.    Boston, MA 02111-1307, USA.  */
  15. /* Change included declaration file to misc.h.  Include "mem.h" for 
  16.    define of op_t, OPSIZ, and byte.
  17.    mgd@santafe.edu 1998-07-08. */
  18. #include <misc.h>
  19. #include "mem.h"
  20. void *
  21. memset (dstpp, c, len)
  22.      void *dstpp;
  23.      int c;
  24.      size_t len;
  25. {
  26.   long int dstp = (long int) dstpp;
  27.   if (len >= 8)
  28.     {
  29.       size_t xlen;
  30.       op_t cccc;
  31.       cccc = (unsigned char) c;
  32.       cccc |= cccc << 8;
  33.       cccc |= cccc << 16;
  34.       if (OPSIZ > 4)
  35. /* Do the shift in two steps to avoid warning if long has 32 bits.  */
  36. cccc |= (cccc << 16) << 16;
  37.       /* There are at least some bytes to set.
  38.  No need to test for LEN == 0 in this alignment loop.  */
  39.       while (dstp % OPSIZ != 0)
  40. {
  41.   ((byte *) dstp)[0] = c;
  42.   dstp += 1;
  43.   len -= 1;
  44. }
  45.       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
  46.       xlen = len / (OPSIZ * 8);
  47.       while (xlen > 0)
  48. {
  49.   ((op_t *) dstp)[0] = cccc;
  50.   ((op_t *) dstp)[1] = cccc;
  51.   ((op_t *) dstp)[2] = cccc;
  52.   ((op_t *) dstp)[3] = cccc;
  53.   ((op_t *) dstp)[4] = cccc;
  54.   ((op_t *) dstp)[5] = cccc;
  55.   ((op_t *) dstp)[6] = cccc;
  56.   ((op_t *) dstp)[7] = cccc;
  57.   dstp += 8 * OPSIZ;
  58.   xlen -= 1;
  59. }
  60.       len %= OPSIZ * 8;
  61.       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
  62.       xlen = len / OPSIZ;
  63.       while (xlen > 0)
  64. {
  65.   ((op_t *) dstp)[0] = cccc;
  66.   dstp += OPSIZ;
  67.   xlen -= 1;
  68. }
  69.       len %= OPSIZ;
  70.     }
  71.   /* Write the last few bytes.  */
  72.   while (len > 0)
  73.     {
  74.       ((byte *) dstp)[0] = c;
  75.       dstp += 1;
  76.       len -= 1;
  77.     }
  78.   return dstpp;
  79. }