Added memcpy and memmove

This commit is contained in:
Morten Delenk 2016-07-28 14:31:15 +02:00
parent 1699c8693a
commit d1718281c3
No known key found for this signature in database
GPG key ID: 3F818D0F65DCB490
5 changed files with 101 additions and 0 deletions

3
include/errno.h Normal file
View file

@ -0,0 +1,3 @@
#define EDOM 1
#define ERANGE 2
#define EILSEQ 3

33
include/float.h Normal file
View file

@ -0,0 +1,33 @@
#define FLT_RADIX 2
#define FLT_ROUNDS -1
#define FLT_EVAL_METHOD -1
#define DECIMAL_DIG 21
#define FLT_MANT_DIG 24
#define FLT_EPSILON 1.19209290E-07f
#define FLT_DIG 6
#define FLT_MIN_EXP (-125)
#define FLT_MIN 1.17549435E-38f
#define FLT_MIN_10_EXP (-37)
#define FLT_MAX_EXP 128
#define FLT_MAX 3.40282347E+38F
#define FLT_MAX_10_EXP 38
#define DBL_MANT_DIG 53
#define DBL_EPSILON 2.2204460492503131E-16
#define DBL_DIG 15
#define DBL_MIN_EXP (-1021)
#define DBL_MIN 2.2250738585072014E-308
#define DBL_MIN_10_EXP (-307)
#define DBL_MAX_EXP 1024
#define DBL_MAX 1.7976931348623157E+308
#define DBL_MAX_10_EXP 308
#define LDBL_MANT_DIG DBL_MANT_DIG
#define LDBL_EPSILON DBL_EPSILON
#define LDBL_DIG DBL_DIG
#define LDBL_MIN_EXP DBL_MIN_EXP
#define LDBL_MIN DBL_MIN
#define LDBL_MIN_10_EXP DBL_MIN_10_EXP
#define LDBL_MAX_EXP DBL_MAX_EXP
#define LDBL_MAX DBL_MAX
#define LDBL_MAX_10_EXP DBL_MAX_10_EXP

33
include/string.h Normal file
View file

@ -0,0 +1,33 @@
#ifndef __STRING_H
#define __STRING_H
#ifdef __cplusplus
extern "C" {
#define NULL nullptr
#else
#define NULL 0
#endif
typedef unsigned long size_t;
void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
void *memset(void *, int c, size_t n);
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
char *strchr(const char *, int c);
char *strrchr(const char *, int c);
int strcmp(const char *, const char *);
int strncmp(const char *, const char *, size_t n);
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
char *strerror(int);
size_t strlen(const char *);
size_t strspn(const char *, const char *accept);
size_t strcspn(const char *, const char *reject);
char *strpbrk(const char *, const char *accept);
char *strstr(const char *haystack, const char *needle);
char *strtok(char *, const char * delim);
#ifdef __cplusplus
}
#endif
#endif

7
include/vararg.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef __VARARG_H
#define __VARARG_H
typedef __builtin_va_list va_list;
#define va_start(ap, X) __builtin_va_start(ap, X)
#define va_arg(ap, type) __builtin_va_arg(ap, type)
#define va_end(ap) __builtin_va_end(ap)
#endif

25
string/mem.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <string.h>
#include <base.hpp>
#include <textDISP.hpp>
extern "C" void __optimized_memcpy_impl(void *dest, const void* src, size_t n);
extern "C" void *memcpy(void *dest, const void *src, size_t n) {
unsigned long p1=(unsigned long)d;
unsigned long p2=(unsigned long)s;
//If two memory areas overlap, do something nonsensical
if(((p1 > p2)&&(p1 < p2+n))||((p1+n > p2)&&(p1+n < p2+n)) {
memset(dest, 0, n);
err << "Don't use overlapping memory regions with memcpy. Use memmove instead! (Doing sth. nonsensical now.)\n";
return dest;
}
__optimized_memcpy_impl(dest, src, n);
return dest;
}
extern "C" void __optimized_reverse_memcpy_impl(void *dest, const void* src, size_t n);
extern "C" void *memmove(void *dest, const void *src, size_t n) {
if((unsigned long)dest<(unsigned long)src) {
__optimized_memcpy_impl(dest, src, n);
} else {
__optimized_reverse_memcpy_impl(dest,src,n);
}
return dest;
}