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