blob: 9a19e16b4d6afbcb1e8868a8644f31751ffbf4c9 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2** Copyright 2005, Michael Noisternig. All rights reserved.
3** Copyright 2001, Travis Geiselbrecht. All rights reserved.
4** Distributed under the terms of the NewOS License.
5*/
6/*
7 * Copyright (c) 2008 Travis Geiselbrecht
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files
11 * (the "Software"), to deal in the Software without restriction,
12 * including without limitation the rights to use, copy, modify, merge,
13 * publish, distribute, sublicense, and/or sell copies of the Software,
14 * and to permit persons to whom the Software is furnished to do so,
15 * subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 */
28#include <string.h>
29#include <sys/types.h>
30
31void *
32memset(void *s, int c, size_t count)
33{
34 char *xs = (char *) s;
35 size_t len = (-(size_t)s) & (sizeof(size_t)-1);
36 size_t cc = c & 0xff;
37
38 if ( count > len ) {
39 count -= len;
40 cc |= cc << 8;
41 cc |= cc << 16;
42 if (sizeof(size_t) == 8)
43 cc |= (uint64_t)cc << 32; // should be optimized out on 32 bit machines
44
45 // write to non-aligned memory byte-wise
46 for ( ; len > 0; len-- )
47 *xs++ = c;
48
49 // write to aligned memory dword-wise
50 for ( len = count/sizeof(size_t); len > 0; len-- ) {
51 *((size_t *)xs) = (size_t)cc;
52 xs += sizeof(size_t);
53 }
54
55 count &= sizeof(size_t)-1;
56 }
57
58 // write remaining bytes
59 for ( ; count > 0; count-- )
60 *xs++ = c;
61
62 return s;
63}