1 /* @(#)fillbytes.c 1.18 10/08/21 Copyright 1987, 1995-2010 J. Schilling */
2 /*
3 * fill memory with data
4 *
5 * Copyright (c) 1987, 1995-2010 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 #define cval ((char)lval)
27
28 /*
29 * fillbytes(to, cnt, val) is the same as memset(to, val, cnt)
30 */
31 #ifdef PROTOTYPES
32 EXPORT char *
fillbytes(void * tov,ssize_t cnt,char val)33 fillbytes(void *tov, ssize_t cnt, char val)
34 #else
35 EXPORT char *
36 fillbytes(tov, cnt, val)
37 void *tov;
38 ssize_t cnt;
39 char val;
40 #endif
41 {
42 register char *to = (char *)tov;
43 register ssize_t n;
44 register long lval;
45
46 /*
47 * If we change cnt to be unsigned, check for == instead of <=
48 */
49 if ((n = cnt) <= 0)
50 return (to);
51
52 lval = val & 0xFF;
53
54 /*
55 * Assign byte-wise until properly aligned for a long pointer.
56 */
57 while (--n >= 0 && !laligned(to)) {
58 *to++ = cval;
59 }
60 n++;
61
62 if (n >= (ssize_t)(8 * sizeof (long))) {
63 register ssize_t rem = n % (8 * sizeof (long));
64
65 lval |= (lval<<8);
66 lval |= (lval<<16);
67 #if SIZE_LONG > SIZE_INT
68 lval |= (lval<<32);
69 #endif
70
71 n /= (8 * sizeof (long));
72 {
73 register long *tol = (long *)to;
74
75 do {
76 DO8 (*tol++ = lval);
77 } while (--n > 0);
78
79 to = (char *)tol;
80 }
81 n = rem;
82
83 if (n >= 8) {
84 n -= 8;
85 do {
86 DO8 (*to++ = cval);
87 } while ((n -= 8) >= 0);
88 n += 8;
89 }
90 if (n > 0) do {
91 *to++ = cval;
92 } while (--n > 0);
93 return (to);
94 }
95 if (n > 0) do {
96 *to++ = cval;
97 } while (--n > 0);
98 return (to);
99 }
100