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