1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * 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 5.8 (Berkeley) 05/12/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 void 25 bzero(dst0, length) 26 void *dst0; 27 register size_t length; 28 #else 29 void * 30 memset(dst0, c0, length) 31 void *dst0; 32 register int c0; 33 register size_t length; 34 #endif 35 { 36 register size_t t; 37 register u_int c; 38 register u_char *dst; 39 40 dst = dst0; 41 /* 42 * If not enough words, just fill bytes. A length >= 2 words 43 * guarantees that at least one of them is `complete' after 44 * any necessary alignment. For instance: 45 * 46 * |-----------|-----------|-----------| 47 * |00|01|02|03|04|05|06|07|08|09|0A|00| 48 * ^---------------------^ 49 * dst dst+length-1 50 * 51 * but we use a minimum of 3 here since the overhead of the code 52 * to do word writes is substantial. 53 */ 54 if (length < 3 * wsize) { 55 while (length != 0) { 56 #ifdef BZERO 57 *dst++ = 0; 58 #else 59 *dst++ = c0; 60 #endif 61 --length; 62 } 63 #ifdef BZERO 64 return; 65 #else 66 return (dst0); 67 #endif 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 #ifdef BZERO 87 *dst++ = 0; 88 #else 89 *dst++ = c0; 90 #endif 91 } while (--t != 0); 92 } 93 94 /* Fill words. Length was >= 2*words so we know t >= 1 here. */ 95 t = length / wsize; 96 do { 97 #ifdef BZERO 98 *(u_int *)dst = 0; 99 #else 100 *(u_int *)dst = c; 101 #endif 102 dst += wsize; 103 } while (--t != 0); 104 105 /* Mop up trailing bytes, if any. */ 106 t = length & wmask; 107 if (t != 0) 108 do { 109 #ifdef BZERO 110 *dst++ = 0; 111 #else 112 *dst++ = c0; 113 #endif 114 } while (--t != 0); 115 #ifdef BZERO 116 return; 117 #else 118 return (dst0); 119 #endif 120 } 121