1// Copyright 2016 The Fuchsia Authors 2// Copyright 2001, Travis Geiselbrecht 3// Copyright 2005, Michael Noisternig 4// Copyright (c) 2008 Travis Geiselbrecht 5// 6// Use of this source code is governed by a MIT-style 7// license that can be found in the LICENSE file or at 8// https://opensource.org/licenses/MIT 9 10#include <string.h> 11#include <sys/types.h> 12 13void * 14memset(void *s, int c, size_t count) 15{ 16 char *xs = (char *) s; 17 size_t len = (-(size_t)s) & (sizeof(size_t)-1); 18 size_t cc = c & 0xff; 19 20 if ( count > len ) { 21 count -= len; 22 cc |= cc << 8; 23 cc |= cc << 16; 24 if (sizeof(size_t) == 8) 25 cc |= (uint64_t)cc << 32; // should be optimized out on 32 bit machines 26 27 // write to non-aligned memory byte-wise 28 for ( ; len > 0; len-- ) 29 *xs++ = c; 30 31 // write to aligned memory dword-wise 32 for ( len = count/sizeof(size_t); len > 0; len-- ) { 33 *((size_t *)xs) = (size_t)cc; 34 xs += sizeof(size_t); 35 } 36 37 count &= sizeof(size_t)-1; 38 } 39 40 // write remaining bytes 41 for ( ; count > 0; count-- ) 42 *xs++ = c; 43 44 return s; 45} 46