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  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file CDDL.Schily.txt from this distribution.
17  */
18 
19 #include <schily/standard.h>
20 #include <schily/align.h>
21 #include <schily/types.h>
22 #include <schily/schily.h>
23 
24 #define	DO8(a)	a; a; a; a; a; a; a; a;
25 
26 /*
27  * zero(to, cnt)
28  */
29 EXPORT char *
zerobytes(tov,cnt)30 zerobytes(tov, cnt)
31 	void	*tov;
32 	ssize_t	cnt;
33 {
34 	register char	*to = (char *)tov;
35 	register ssize_t n;
36 	register long	lval = 0L;
37 
38 	/*
39 	 * If we change cnt to be unsigned, check for == instead of <=
40 	 */
41 	if ((n = cnt) <= 0)
42 		return (to);
43 
44 	if (n < 8 * sizeof (long)) {	/* Simple may be faster... */
45 		do {			/* n is always > 0 */
46 			*to++ = '\0';
47 		} while (--n > 0);
48 		return (to);
49 	}
50 
51 	/*
52 	 * Assign byte-wise until properly aligned for a long pointer.
53 	 */
54 	while (--n >= 0 && !laligned(to)) {
55 		*to++ = '\0';
56 	}
57 	n++;
58 
59 	if (n >= (ssize_t)(8 * sizeof (long))) {
60 		register ssize_t rem = n % (8 * sizeof (long));
61 
62 		n /= (8 * sizeof (long));
63 		{
64 			register long *tol = (long *)to;
65 
66 			do {
67 				DO8 (*tol++ = lval);
68 			} while (--n > 0);
69 
70 			to = (char *)tol;
71 		}
72 		n = rem;
73 
74 		if (n >= 8) {
75 			n -= 8;
76 			do {
77 				DO8 (*to++ = '\0');
78 			} while ((n -= 8) >= 0);
79 			n += 8;
80 		}
81 		if (n > 0) do {
82 			*to++ = '\0';
83 		} while (--n > 0);
84 		return (to);
85 	}
86 	if (n > 0) do {
87 		*to++ = '\0';
88 	} while (--n > 0);
89 	return (to);
90 }
91