memset.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $OpenBSD: memset.c,v 1.7 2014/06/10 04:16:57 deraadt Exp $ */
  2. /* $NetBSD: memset.c,v 1.6 1998/03/27 05:35:47 cgd Exp $ */
  3. /*-
  4. * Copyright (c) 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Mike Hibler and Chris Torek.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <sys/limits.h>
  36. #include <sys/systm.h>
  37. #include <lib/libkern/libkern.h>
  38. #define wsize sizeof(u_int)
  39. #define wmask (wsize - 1)
  40. #ifdef BZERO
  41. #define RETURN return
  42. #define VAL 0
  43. #define WIDEVAL 0
  44. void
  45. bzero(void *dst0, size_t length)
  46. #else
  47. #define RETURN return (dst0)
  48. #define VAL c0
  49. #define WIDEVAL c
  50. void *
  51. memset(void *dst0, int c0, size_t length)
  52. #endif
  53. {
  54. size_t t;
  55. #ifndef BZERO
  56. u_int c;
  57. #endif
  58. u_char *dst;
  59. dst = dst0;
  60. /*
  61. * If not enough words, just fill bytes. A length >= 2 words
  62. * guarantees that at least one of them is `complete' after
  63. * any necessary alignment. For instance:
  64. *
  65. * |-----------|-----------|-----------|
  66. * |00|01|02|03|04|05|06|07|08|09|0A|00|
  67. * ^---------------------^
  68. * dst dst+length-1
  69. *
  70. * but we use a minimum of 3 here since the overhead of the code
  71. * to do word writes is substantial.
  72. */
  73. if (length < 3 * wsize) {
  74. while (length != 0) {
  75. *dst++ = VAL;
  76. --length;
  77. }
  78. RETURN;
  79. }
  80. #ifndef BZERO
  81. if ((c = (u_char)c0) != 0) { /* Fill the word. */
  82. c = (c << 8) | c; /* u_int is 16 bits. */
  83. #if UINT_MAX > 0xffff
  84. c = (c << 16) | c; /* u_int is 32 bits. */
  85. #endif
  86. #if UINT_MAX > 0xffffffff
  87. c = (c << 32) | c; /* u_int is 64 bits. */
  88. #endif
  89. }
  90. #endif
  91. /* Align destination by filling in bytes. */
  92. if ((t = (u_long)dst & wmask) != 0) {
  93. t = wsize - t;
  94. length -= t;
  95. do {
  96. *dst++ = VAL;
  97. } while (--t != 0);
  98. }
  99. /* Fill words. Length was >= 2*words so we know t >= 1 here. */
  100. t = length / wsize;
  101. do {
  102. *(u_int *)dst = WIDEVAL;
  103. dst += wsize;
  104. } while (--t != 0);
  105. /* Mop up trailing bytes, if any. */
  106. t = length & wmask;
  107. if (t != 0)
  108. do {
  109. *dst++ = VAL;
  110. } while (--t != 0);
  111. RETURN;
  112. }