Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011, 2012, 2014 Jonas 'Sortie' Termansen.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* string/memcpy.c
|
|
|
|
* Copy memory between non-overlapping regions.
|
|
|
|
*/
|
2012-09-26 15:02:17 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
inline static
|
|
|
|
void* memcpy_slow(void* restrict dst_ptr,
|
|
|
|
const void* restrict src_ptr,
|
|
|
|
size_t size)
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2014-09-25 09:15:36 -04:00
|
|
|
unsigned char* restrict dst = (unsigned char* restrict) dst_ptr;
|
|
|
|
const unsigned char* restrict src = (const unsigned char* restrict) src_ptr;
|
|
|
|
for ( size_t i = 0; i < size; i++ )
|
2012-12-05 11:34:28 -05:00
|
|
|
dst[i] = src[i];
|
2014-09-25 09:15:36 -04:00
|
|
|
return dst_ptr;
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
void* memcpy(void* restrict dst_ptr,
|
|
|
|
const void* restrict src_ptr,
|
|
|
|
size_t size)
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2014-09-25 09:15:36 -04:00
|
|
|
#if 8 < __SIZEOF_LONG__
|
|
|
|
#warning "you should add support for your unexpectedly large unsigned long."
|
|
|
|
return memcpy_slow(dst_ptr, src_ptr, size);
|
|
|
|
#else
|
|
|
|
unsigned long unalign_mask = sizeof(unsigned long) - 1;
|
|
|
|
unsigned long src_unalign = (unsigned long) src_ptr & unalign_mask;
|
|
|
|
unsigned long dst_unalign = (unsigned long) dst_ptr & unalign_mask;
|
|
|
|
if ( src_unalign != dst_unalign )
|
|
|
|
return memcpy_slow(dst_ptr, src_ptr, size);
|
2012-12-05 11:34:28 -05:00
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned long srcval;
|
2014-09-25 09:15:36 -04:00
|
|
|
const unsigned char* restrict src8;
|
2012-12-05 11:34:28 -05:00
|
|
|
const uint16_t* restrict src16;
|
|
|
|
const uint32_t* restrict src32;
|
|
|
|
const uint64_t* restrict src64;
|
|
|
|
const unsigned long* restrict srcul;
|
2016-02-28 06:11:02 -05:00
|
|
|
} su;
|
|
|
|
su.srcval = (unsigned long) src_ptr;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned long dstval;
|
2014-09-25 09:15:36 -04:00
|
|
|
unsigned char* restrict dst8;
|
2012-12-05 11:34:28 -05:00
|
|
|
uint16_t* restrict dst16;
|
|
|
|
uint32_t* restrict dst32;
|
|
|
|
uint64_t* restrict dst64;
|
|
|
|
unsigned long* restrict dstul;
|
2016-02-28 06:11:02 -05:00
|
|
|
} du;
|
|
|
|
du.dstval = (unsigned long) dst_ptr;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( dst_unalign )
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2016-02-28 06:11:02 -05:00
|
|
|
if ( 1 <= size && !(du.dstval & (1-1)) && (du.dstval & (2-1)) )
|
|
|
|
*du.dst8++ = *su.src8++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 1;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2016-02-28 06:11:02 -05:00
|
|
|
if ( 2 <= size && !(du.dstval & (2-1)) && (du.dstval & (4-1)) )
|
|
|
|
*du.dst16++ = *su.src16++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 2;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
|
|
|
#if 8 <= __SIZEOF_LONG__
|
2016-02-28 06:11:02 -05:00
|
|
|
if ( 4 <= size && !(du.dstval & (4-1)) && (du.dstval & (8-1)) )
|
|
|
|
*du.dst32++ = *su.src32++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 4;
|
2012-12-05 11:34:28 -05:00
|
|
|
#endif
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
size_t num_copies = size / sizeof(unsigned long);
|
|
|
|
for ( size_t i = 0; i < num_copies; i++ )
|
2016-02-28 06:11:02 -05:00
|
|
|
*du.dstul++ = *su.srcul++;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= num_copies * sizeof(unsigned long);
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( size )
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2012-12-05 11:34:28 -05:00
|
|
|
#if 8 <= __SIZEOF_LONG__
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 4 <= size )
|
2016-02-28 06:11:02 -05:00
|
|
|
*du.dst32++ = *su.src32++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 4;
|
2012-12-05 11:34:28 -05:00
|
|
|
#endif
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 2 <= size )
|
2016-02-28 06:11:02 -05:00
|
|
|
*du.dst16++ = *su.src16++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 2;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 1 <= size )
|
2016-02-28 06:11:02 -05:00
|
|
|
*du.dst8++ = *su.src8++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 1;
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
return dst_ptr;
|
|
|
|
#endif
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|