1 /* @(#)zerobytes.c 1.2 11/07/30 Copyright Copyright 1987, 1995-2011 J. Schilling */ 2 /* 3 * fill memory with null bytes 4 * 5 * Copyright (c) 1987, 1995-2011 J. Schilling 6 */ 7 /* 8 * The contents of this file are subject to the terms of the 9 * Common Development and Distribution License, Version 1.0 only 10 * (the "License"). You may not use this file except in compliance 11 * with the License. 12 * 13 * See the file CDDL.Schily.txt in this distribution for details. 14 * A copy of the CDDL is also available via the Internet at 15 * http://www.opensource.org/licenses/cddl1.txt 16 * 17 * When distributing Covered Code, include this CDDL HEADER in each 18 * file and include the License file CDDL.Schily.txt from this distribution. 19 */ 20 21 #include <schily/standard.h> 22 #include <schily/align.h> 23 #include <schily/types.h> 24 #include <schily/schily.h> 25 26 #define DO8(a) a; a; a; a; a; a; a; a; 27 28 /* 29 * zero(to, cnt) 30 */ 31 EXPORT char * zerobytes(tov,cnt)32zerobytes(tov, cnt) 33 void *tov; 34 ssize_t cnt; 35 { 36 register char *to = (char *)tov; 37 register ssize_t n; 38 register long lval = 0L; 39 40 /* 41 * If we change cnt to be unsigned, check for == instead of <= 42 */ 43 if ((n = cnt) <= 0) 44 return (to); 45 46 if (n < 8 * sizeof (long)) { /* Simple may be faster... */ 47 do { /* n is always > 0 */ 48 *to++ = '\0'; 49 } while (--n > 0); 50 return (to); 51 } 52 53 /* 54 * Assign byte-wise until properly aligned for a long pointer. 55 */ 56 while (--n >= 0 && !laligned(to)) { 57 *to++ = '\0'; 58 } 59 n++; 60 61 if (n >= (ssize_t)(8 * sizeof (long))) { 62 register ssize_t rem = n % (8 * sizeof (long)); 63 64 n /= (8 * sizeof (long)); 65 { 66 register long *tol = (long *)to; 67 68 do { 69 DO8 (*tol++ = lval); 70 } while (--n > 0); 71 72 to = (char *)tol; 73 } 74 n = rem; 75 76 if (n >= 8) { 77 n -= 8; 78 do { 79 DO8 (*to++ = '\0'); 80 } while ((n -= 8) >= 0); 81 n += 8; 82 } 83 if (n > 0) do { 84 *to++ = '\0'; 85 } while (--n > 0); 86 return (to); 87 } 88 if (n > 0) do { 89 *to++ = '\0'; 90 } while (--n > 0); 91 return (to); 92 } 93