1199767f8SToomas Soome /*-
2199767f8SToomas Soome * Copyright (c) 1990, 1993
3199767f8SToomas Soome * The Regents of the University of California. All rights reserved.
4199767f8SToomas Soome *
5199767f8SToomas Soome * This code is derived from software contributed to Berkeley by
6199767f8SToomas Soome * Mike Hibler and Chris Torek.
7199767f8SToomas Soome *
8199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
9199767f8SToomas Soome * modification, are permitted provided that the following conditions
10199767f8SToomas Soome * are met:
11199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
12199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
13199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
14199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
15199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
16199767f8SToomas Soome * 3. Neither the name of the University nor the names of its contributors
17199767f8SToomas Soome * may be used to endorse or promote products derived from this software
18199767f8SToomas Soome * without specific prior written permission.
19199767f8SToomas Soome *
20199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30199767f8SToomas Soome * SUCH DAMAGE.
31199767f8SToomas Soome */
32199767f8SToomas Soome
33199767f8SToomas Soome #if defined(LIBC_SCCS) && !defined(lint)
34199767f8SToomas Soome static char sccsid[] = "@(#)memset.c 8.1 (Berkeley) 6/4/93";
35199767f8SToomas Soome #endif /* LIBC_SCCS and not lint */
36199767f8SToomas Soome #include <sys/cdefs.h>
37199767f8SToomas Soome __FBSDID("$FreeBSD$");
38199767f8SToomas Soome
39199767f8SToomas Soome #include <sys/types.h>
40199767f8SToomas Soome
41199767f8SToomas Soome #include <limits.h>
42199767f8SToomas Soome
43199767f8SToomas Soome #define wsize sizeof(u_int)
44199767f8SToomas Soome #define wmask (wsize - 1)
45199767f8SToomas Soome
46199767f8SToomas Soome #ifdef BZERO
47199767f8SToomas Soome #include <strings.h>
48199767f8SToomas Soome
49199767f8SToomas Soome #define RETURN return
50199767f8SToomas Soome #define VAL 0
51199767f8SToomas Soome #define WIDEVAL 0
52199767f8SToomas Soome
53199767f8SToomas Soome void
bzero(void * dst0,size_t length)54199767f8SToomas Soome bzero(void *dst0, size_t length)
55199767f8SToomas Soome #else
56199767f8SToomas Soome #include <string.h>
57199767f8SToomas Soome
58199767f8SToomas Soome #define RETURN return (dst0)
59199767f8SToomas Soome #define VAL c0
60199767f8SToomas Soome #define WIDEVAL c
61199767f8SToomas Soome
62199767f8SToomas Soome void *
63199767f8SToomas Soome memset(void *dst0, int c0, size_t length)
64199767f8SToomas Soome #endif
65199767f8SToomas Soome {
66199767f8SToomas Soome size_t t;
67199767f8SToomas Soome #ifndef BZERO
68199767f8SToomas Soome u_int c;
69199767f8SToomas Soome #endif
70199767f8SToomas Soome u_char *dst;
71199767f8SToomas Soome
72199767f8SToomas Soome dst = dst0;
73199767f8SToomas Soome /*
74199767f8SToomas Soome * If not enough words, just fill bytes. A length >= 2 words
75199767f8SToomas Soome * guarantees that at least one of them is `complete' after
76199767f8SToomas Soome * any necessary alignment. For instance:
77199767f8SToomas Soome *
78199767f8SToomas Soome * |-----------|-----------|-----------|
79199767f8SToomas Soome * |00|01|02|03|04|05|06|07|08|09|0A|00|
80199767f8SToomas Soome * ^---------------------^
81199767f8SToomas Soome * dst dst+length-1
82199767f8SToomas Soome *
83199767f8SToomas Soome * but we use a minimum of 3 here since the overhead of the code
84199767f8SToomas Soome * to do word writes is substantial.
85199767f8SToomas Soome */
86199767f8SToomas Soome if (length < 3 * wsize) {
87199767f8SToomas Soome while (length != 0) {
88199767f8SToomas Soome *dst++ = VAL;
89199767f8SToomas Soome --length;
90199767f8SToomas Soome }
91199767f8SToomas Soome RETURN;
92199767f8SToomas Soome }
93199767f8SToomas Soome
94199767f8SToomas Soome #ifndef BZERO
95199767f8SToomas Soome if ((c = (u_char)c0) != 0) { /* Fill the word. */
96199767f8SToomas Soome c = (c << 8) | c; /* u_int is 16 bits. */
97199767f8SToomas Soome #if UINT_MAX > 0xffff
98199767f8SToomas Soome c = (c << 16) | c; /* u_int is 32 bits. */
99199767f8SToomas Soome #endif
100199767f8SToomas Soome #if UINT_MAX > 0xffffffff
101199767f8SToomas Soome c = (c << 32) | c; /* u_int is 64 bits. */
102199767f8SToomas Soome #endif
103199767f8SToomas Soome }
104199767f8SToomas Soome #endif
105199767f8SToomas Soome /* Align destination by filling in bytes. */
106199767f8SToomas Soome if ((t = (long)dst & wmask) != 0) {
107199767f8SToomas Soome t = wsize - t;
108199767f8SToomas Soome length -= t;
109199767f8SToomas Soome do {
110199767f8SToomas Soome *dst++ = VAL;
111199767f8SToomas Soome } while (--t != 0);
112199767f8SToomas Soome }
113199767f8SToomas Soome
114199767f8SToomas Soome /* Fill words. Length was >= 2*words so we know t >= 1 here. */
115199767f8SToomas Soome t = length / wsize;
116199767f8SToomas Soome do {
117199767f8SToomas Soome *(u_int *)dst = WIDEVAL;
118199767f8SToomas Soome dst += wsize;
119199767f8SToomas Soome } while (--t != 0);
120199767f8SToomas Soome
121199767f8SToomas Soome /* Mop up trailing bytes, if any. */
122199767f8SToomas Soome t = length & wmask;
123199767f8SToomas Soome if (t != 0)
124199767f8SToomas Soome do {
125199767f8SToomas Soome *dst++ = VAL;
126199767f8SToomas Soome } while (--t != 0);
127199767f8SToomas Soome RETURN;
128199767f8SToomas Soome }
129