1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Hibler and Chris Torek. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)memset.c 8.1 (Berkeley) 06/04/93"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/types.h> 16 17 #include <limits.h> 18 #include <string.h> 19 20 #define wsize sizeof(u_int) 21 #define wmask (wsize - 1) 22 23 #ifdef BZERO 24 #define RETURN return 25 #define VAL 0 26 #define WIDEVAL 0 27 28 void 29 bzero(dst0, length) 30 void *dst0; 31 register size_t length; 32 #else 33 #define RETURN return (dst0) 34 #define VAL c0 35 #define WIDEVAL c 36 37 void * 38 memset(dst0, c0, length) 39 void *dst0; 40 register int c0; 41 register size_t length; 42 #endif 43 { 44 register size_t t; 45 register u_int c; 46 register u_char *dst; 47 48 dst = dst0; 49 /* 50 * If not enough words, just fill bytes. A length >= 2 words 51 * guarantees that at least one of them is `complete' after 52 * any necessary alignment. For instance: 53 * 54 * |-----------|-----------|-----------| 55 * |00|01|02|03|04|05|06|07|08|09|0A|00| 56 * ^---------------------^ 57 * dst dst+length-1 58 * 59 * but we use a minimum of 3 here since the overhead of the code 60 * to do word writes is substantial. 61 */ 62 if (length < 3 * wsize) { 63 while (length != 0) { 64 *dst++ = VAL; 65 --length; 66 } 67 RETURN; 68 } 69 70 #ifndef BZERO 71 if ((c = (u_char)c0) != 0) { /* Fill the word. */ 72 c = (c << 8) | c; /* u_int is 16 bits. */ 73 #if UINT_MAX > 0xffff 74 c = (c << 16) | c; /* u_int is 32 bits. */ 75 #endif 76 #if UINT_MAX > 0xffffffff 77 c = (c << 32) | c; /* u_int is 64 bits. */ 78 #endif 79 } 80 #endif 81 /* Align destination by filling in bytes. */ 82 if ((t = (int)dst & wmask) != 0) { 83 t = wsize - t; 84 length -= t; 85 do { 86 *dst++ = VAL; 87 } while (--t != 0); 88 } 89 90 /* Fill words. Length was >= 2*words so we know t >= 1 here. */ 91 t = length / wsize; 92 do { 93 *(u_int *)dst = WIDEVAL; 94 dst += wsize; 95 } while (--t != 0); 96 97 /* Mop up trailing bytes, if any. */ 98 t = length & wmask; 99 if (t != 0) 100 do { 101 *dst++ = VAL; 102 } while (--t != 0); 103 RETURN; 104 } 105