1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Properly implement memmove(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-10-27 23:07:54 +01:00
parent 2f261b3848
commit 3166413eef

View file

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -23,17 +23,17 @@
*******************************************************************************/ *******************************************************************************/
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
// TODO: This is a hacky implementation! extern "C" void* memmove(void* dest_ptr, const void* src_ptr, size_t n)
extern "C" void* memmove(void* _dest, const void* _src, size_t n)
{ {
uint8_t* dest = (uint8_t*) _dest; uint8_t* dest = (uint8_t*) dest_ptr;
const uint8_t* src = (const uint8_t*) _src; const uint8_t* src = (const uint8_t*) src_ptr;
uint8_t* tmp = (uint8_t*) malloc(n); if ( dest < src )
memcpy(tmp, src, n); for ( size_t i = 0; i < n; i++ )
memcpy(dest, tmp, n); dest[i] = src[i];
free(tmp); else
return _dest; for ( size_t i = n; i != 0; i-- )
dest[i-1] = src[i-1];
return dest_ptr;
} }