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