/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Mike Hibler and Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/types.h>3536#include <limits.h>3738#define wsize sizeof(u_long)39#define wmask (wsize - 1)4041#ifdef BZERO42#include <strings.h>4344#undef bzero /* _FORTIFY_SOURCE */4546#define RETURN return47#define VAL 048#define WIDEVAL 04950void51bzero(void *dst0, size_t length)52#else53#include <string.h>5455#undef memset /* _FORTIFY_SOURCE */5657#define RETURN return (dst0)58#define VAL c059#define WIDEVAL c6061void *62memset(void *dst0, int c0, size_t length)63#endif64{65size_t t;66#ifndef BZERO67u_long c;68#endif69u_char *dst;7071dst = dst0;72/*73* If not enough words, just fill bytes. A length >= 2 words74* guarantees that at least one of them is `complete' after75* any necessary alignment. For instance:76*77* |-----------|-----------|-----------|78* |00|01|02|03|04|05|06|07|08|09|0A|00|79* ^---------------------^80* dst dst+length-181*82* but we use a minimum of 3 here since the overhead of the code83* to do word writes is substantial.84*85* TODO: This threshold might not be sensible for 64-bit u_long.86* We should benchmark and revisit this decision.87*/88if (length < 3 * wsize) {89while (length != 0) {90*dst++ = VAL;91--length;92}93RETURN;94}9596#ifndef BZERO97if ((c = (u_char)c0) != 0) { /* Fill the word. */98c = (c << 8) | c; /* u_long is 16 bits. */99#if ULONG_MAX > 0xffff100c = (c << 16) | c; /* u_long is 32 bits. */101#endif102#if ULONG_MAX > 0xffffffff103c = (c << 32) | c; /* u_long is 64 bits. */104#endif105}106#endif107/* Align destination by filling in bytes. */108if ((t = (long)dst & wmask) != 0) {109t = wsize - t;110length -= t;111do {112*dst++ = VAL;113} while (--t != 0);114}115116/* Fill words. Length was >= 2*words so we know t >= 1 here. */117t = length / wsize;118do {119*(u_long *)(void *)dst = WIDEVAL;120dst += wsize;121} while (--t != 0);122123/* Mop up trailing bytes, if any. */124t = length & wmask;125if (t != 0)126do {127*dst++ = VAL;128} while (--t != 0);129RETURN;130}131132133