1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 1994-2009  Red Hat, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34 FUNCTION
35 	<<memset>>---set an area of memory
36 
37 INDEX
38 	memset
39 
40 ANSI_SYNOPSIS
41 	#include <string.h>
42 	void *memset(void *<[dst]>, int <[c]>, size_t <[length]>);
43 
44 TRAD_SYNOPSIS
45 	#include <string.h>
46 	void *memset(<[dst]>, <[c]>, <[length]>)
47 	void *<[dst]>;
48 	int <[c]>;
49 	size_t <[length]>;
50 
51 DESCRIPTION
52 	This function converts the argument <[c]> into an unsigned
53 	char and fills the first <[length]> characters of the array
54 	pointed to by <[dst]> to the value.
55 
56 RETURNS
57 	<<memset>> returns the value of <[dst]>.
58 
59 PORTABILITY
60 <<memset>> is ANSI C.
61 
62     <<memset>> requires no supporting OS subroutines.
63 
64 QUICKREF
65 	memset ansi pure
66 */
67 
68 #include "_ansi.h"
69 #include <string.h>
70 
71 #define LBLOCKSIZE (sizeof(long))
72 #define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
73 #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
74 
75 _PTR _DEFUN(memset, (m, c, n), _PTR m _AND int c _AND size_t n)
76 {
77 	char *s = (char *)m;
78 
79 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
80 	int i;
81 	unsigned long buffer;
82 	unsigned long *aligned_addr;
83 	unsigned int d = c & 0xff;	/* To avoid sign extension, copy C to an
84 					   unsigned variable.  */
85 
86 	while (UNALIGNED(s)) {
87 		if (n--)
88 			*s++ = (char)c;
89 		else
90 			return m;
91 	}
92 
93 	if (!TOO_SMALL(n)) {
94 		/*
95 		 * If we get this far, we know that n is large and s is
96 		 * word-aligned.
97 		 */
98 		aligned_addr = (unsigned long *)s;
99 
100 		/* Store D into each char sized location in BUFFER so that
101 		   we can set large blocks quickly.  */
102 		buffer = (d << 8) | d;
103 		buffer |= (buffer << 16);
104 		for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
105 			buffer = (buffer << i) | buffer;
106 
107 		/* Unroll the loop.  */
108 		while (n >= LBLOCKSIZE * 4) {
109 			*aligned_addr++ = buffer;
110 			*aligned_addr++ = buffer;
111 			*aligned_addr++ = buffer;
112 			*aligned_addr++ = buffer;
113 			n -= 4 * LBLOCKSIZE;
114 		}
115 
116 		while (n >= LBLOCKSIZE) {
117 			*aligned_addr++ = buffer;
118 			n -= LBLOCKSIZE;
119 		}
120 		/* Pick up the remainder with a bytewise loop.  */
121 		s = (char *)aligned_addr;
122 	}
123 #endif /* not PREFER_SIZE_OVER_SPEED */
124 
125 	while (n--)
126 		*s++ = (char)c;
127 
128 	return m;
129 }
130