184d9c625SLionel Sambuc /*-
284d9c625SLionel Sambuc * Copyright (c) 1990, 1993
384d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
484d9c625SLionel Sambuc *
584d9c625SLionel Sambuc * This code is derived from software contributed to Berkeley by
684d9c625SLionel Sambuc * Mike Hibler and Chris Torek.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
984d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1084d9c625SLionel Sambuc * are met:
1184d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1284d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1384d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1484d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1584d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
1684d9c625SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
1784d9c625SLionel Sambuc * must display the following acknowledgement:
1884d9c625SLionel Sambuc * This product includes software developed by the University of
1984d9c625SLionel Sambuc * California, Berkeley and its contributors.
2084d9c625SLionel Sambuc * 4. Neither the name of the University nor the names of its contributors
2184d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
2284d9c625SLionel Sambuc * without specific prior written permission.
2384d9c625SLionel Sambuc *
2484d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2584d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2684d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2784d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2884d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2984d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3084d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3184d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3284d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3384d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3484d9c625SLionel Sambuc * SUCH DAMAGE.
3584d9c625SLionel Sambuc */
3684d9c625SLionel Sambuc
3784d9c625SLionel Sambuc #include "config.h"
3884d9c625SLionel Sambuc
3984d9c625SLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
4084d9c625SLionel Sambuc static const char sccsid[] = "@(#)memset.c 8.1 (Berkeley) 6/4/93";
4184d9c625SLionel Sambuc #endif /* LIBC_SCCS and not lint */
42*0a6a1f1dSLionel Sambuc #else
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: memset.c,v 1.2 2014/01/26 21:43:45 christos Exp $");
44*0a6a1f1dSLionel Sambuc #endif
4584d9c625SLionel Sambuc
4684d9c625SLionel Sambuc #include <sys/types.h>
4784d9c625SLionel Sambuc
4884d9c625SLionel Sambuc #include <limits.h>
4984d9c625SLionel Sambuc #include <string.h>
5084d9c625SLionel Sambuc
5184d9c625SLionel Sambuc /*
5284d9c625SLionel Sambuc * PUBLIC: #ifndef HAVE_MEMSET
5384d9c625SLionel Sambuc * PUBLIC: void *memset __P((void *, int, size_t));
5484d9c625SLionel Sambuc * PUBLIC: #endif
5584d9c625SLionel Sambuc */
5684d9c625SLionel Sambuc #define wsize sizeof(u_int)
5784d9c625SLionel Sambuc #define wmask (wsize - 1)
5884d9c625SLionel Sambuc
5984d9c625SLionel Sambuc #ifdef BZERO
6084d9c625SLionel Sambuc #define RETURN return
6184d9c625SLionel Sambuc #define VAL 0
6284d9c625SLionel Sambuc #define WIDEVAL 0
6384d9c625SLionel Sambuc
6484d9c625SLionel Sambuc void
bzero(dst0,length)6584d9c625SLionel Sambuc bzero(dst0, length)
6684d9c625SLionel Sambuc void *dst0;
6784d9c625SLionel Sambuc register size_t length;
6884d9c625SLionel Sambuc #else
6984d9c625SLionel Sambuc #define RETURN return (dst0)
7084d9c625SLionel Sambuc #define VAL c0
7184d9c625SLionel Sambuc #define WIDEVAL c
7284d9c625SLionel Sambuc
7384d9c625SLionel Sambuc void *
7484d9c625SLionel Sambuc memset(void *dst0, register int c0, register size_t length)
7584d9c625SLionel Sambuc
7684d9c625SLionel Sambuc
7784d9c625SLionel Sambuc
7884d9c625SLionel Sambuc #endif
7984d9c625SLionel Sambuc {
8084d9c625SLionel Sambuc register size_t t;
8184d9c625SLionel Sambuc register u_int c;
8284d9c625SLionel Sambuc register u_char *dst;
8384d9c625SLionel Sambuc
8484d9c625SLionel Sambuc dst = dst0;
8584d9c625SLionel Sambuc /*
8684d9c625SLionel Sambuc * If not enough words, just fill bytes. A length >= 2 words
8784d9c625SLionel Sambuc * guarantees that at least one of them is `complete' after
8884d9c625SLionel Sambuc * any necessary alignment. For instance:
8984d9c625SLionel Sambuc *
9084d9c625SLionel Sambuc * |-----------|-----------|-----------|
9184d9c625SLionel Sambuc * |00|01|02|03|04|05|06|07|08|09|0A|00|
9284d9c625SLionel Sambuc * ^---------------------^
9384d9c625SLionel Sambuc * dst dst+length-1
9484d9c625SLionel Sambuc *
9584d9c625SLionel Sambuc * but we use a minimum of 3 here since the overhead of the code
9684d9c625SLionel Sambuc * to do word writes is substantial.
9784d9c625SLionel Sambuc */
9884d9c625SLionel Sambuc if (length < 3 * wsize) {
9984d9c625SLionel Sambuc while (length != 0) {
10084d9c625SLionel Sambuc *dst++ = VAL;
10184d9c625SLionel Sambuc --length;
10284d9c625SLionel Sambuc }
10384d9c625SLionel Sambuc RETURN;
10484d9c625SLionel Sambuc }
10584d9c625SLionel Sambuc
10684d9c625SLionel Sambuc #ifndef BZERO
10784d9c625SLionel Sambuc if ((c = (u_char)c0) != 0) { /* Fill the word. */
10884d9c625SLionel Sambuc c = (c << 8) | c; /* u_int is 16 bits. */
10984d9c625SLionel Sambuc #if UINT_MAX > 0xffff
11084d9c625SLionel Sambuc c = (c << 16) | c; /* u_int is 32 bits. */
11184d9c625SLionel Sambuc #endif
11284d9c625SLionel Sambuc #if UINT_MAX > 0xffffffff
11384d9c625SLionel Sambuc c = (c << 32) | c; /* u_int is 64 bits. */
11484d9c625SLionel Sambuc #endif
11584d9c625SLionel Sambuc }
11684d9c625SLionel Sambuc #endif
11784d9c625SLionel Sambuc /* Align destination by filling in bytes. */
11884d9c625SLionel Sambuc if ((t = (int)dst & wmask) != 0) {
11984d9c625SLionel Sambuc t = wsize - t;
12084d9c625SLionel Sambuc length -= t;
12184d9c625SLionel Sambuc do {
12284d9c625SLionel Sambuc *dst++ = VAL;
12384d9c625SLionel Sambuc } while (--t != 0);
12484d9c625SLionel Sambuc }
12584d9c625SLionel Sambuc
12684d9c625SLionel Sambuc /* Fill words. Length was >= 2*words so we know t >= 1 here. */
12784d9c625SLionel Sambuc t = length / wsize;
12884d9c625SLionel Sambuc do {
12984d9c625SLionel Sambuc *(u_int *)dst = WIDEVAL;
13084d9c625SLionel Sambuc dst += wsize;
13184d9c625SLionel Sambuc } while (--t != 0);
13284d9c625SLionel Sambuc
13384d9c625SLionel Sambuc /* Mop up trailing bytes, if any. */
13484d9c625SLionel Sambuc t = length & wmask;
13584d9c625SLionel Sambuc if (t != 0)
13684d9c625SLionel Sambuc do {
13784d9c625SLionel Sambuc *dst++ = VAL;
13884d9c625SLionel Sambuc } while (--t != 0);
13984d9c625SLionel Sambuc RETURN;
14084d9c625SLionel Sambuc }
141