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