1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12 
13 /* @(#)fillbytes.c	1.13 03/06/15 Copyright 1987, 1995-2003 J. Schilling */
14 /*
15  *	fill memory with data
16  *
17  *	Copyright (c) 1987, 1995-2003 J. Schilling
18  */
19 /*
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License version 2
22  * as published by the Free Software Foundation.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License along with
30  * this program; see the file COPYING.  If not, write to the Free Software
31  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32  */
33 
34 #include <standard.h>
35 #include <align.h>
36 #include <schily.h>
37 
38 #define	DO8(a)	a; a; a; a; a; a; a; a;
39 
40 #define	cval	((char) lval)
41 
42 #ifdef	PROTOTYPES
43 EXPORT char *
fillbytes(void * tov,int cnt,char val)44 fillbytes(void *tov, int cnt, char val)
45 #else
46 EXPORT char *
47 fillbytes(tov, cnt, val)
48 	void	*tov;
49 	int	cnt;
50 	char	val;
51 #endif
52 {
53 	register char	*to = (char *)tov;
54 	register int	n;
55 	register long	lval;
56 
57 	/*
58 	 * If we change cnt to be unsigned, check for == instead of <=
59 	 */
60 	if ((n = cnt) <= 0)
61 		return (to);
62 
63 	lval = val & 0xFF;
64 
65 	/*
66 	 * Assign byte-wise until properly aligned for a long pointer.
67 	 */
68 	while (--n >= 0 && !laligned(to)) {
69 		*to++ = cval;
70 	}
71 	n++;
72 
73 	if (n >= (int)(8 * sizeof (long))) {
74 		register int rem = n % (8 * sizeof (long));
75 
76 		lval |= (lval<<8);
77 		lval |= (lval<<16);
78 #if SIZE_LONG > SIZE_INT
79 		lval |= (lval<<32);
80 #endif
81 
82 		n /= (8 * sizeof (long));
83 		{
84 			register long *tol = (long *)to;
85 
86 			do {
87 				DO8 (*tol++ = lval);
88 			} while (--n > 0);
89 
90 			to = (char *)tol;
91 		}
92 		n = rem;
93 
94 		if (n >= 8) {
95 			n -= 8;
96 			do {
97 				DO8 (*to++ = cval);
98 			} while ((n -= 8) >= 0);
99 			n += 8;
100 		}
101 		if (n > 0) do {
102 			*to++ = cval;
103 		} while (--n > 0);
104 		return (to);
105 	}
106 	if (n > 0) do {
107 		*to++ = cval;
108 	} while (--n > 0);
109 	return (to);
110 }
111