mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2026-01-02 05:25:19 -08:00
initial implementation of klibc
fix linker error about ctx_switch
This commit is contained in:
14
klibc/include/stdarg.h
Normal file
14
klibc/include/stdarg.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef CLASSICOS_KLIBC_STDARG_H
|
||||
#define CLASSICOS_KLIBC_STDARG_H
|
||||
|
||||
typedef __builtin_va_list va_list;
|
||||
|
||||
#ifndef va_start
|
||||
#define va_start(ap, param) __builtin_va_start(ap, param)
|
||||
#endif
|
||||
|
||||
#define va_end(ap) __builtin_va_end(ap)
|
||||
#define va_arg(ap, type) __builtin_va_arg(ap, type)
|
||||
#define va_copy(dest, src) __builtin_va_copy(dest, src)
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STDARG_H
|
||||
6
klibc/include/stdbool.h
Normal file
6
klibc/include/stdbool.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef CLASSICOS_KLIBC_STDBOOL_H
|
||||
#define CLASSICOS_KLIBC_STDBOOL_H
|
||||
|
||||
typedef enum { false = 0, true = 1 } bool;
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STDBOOL_H
|
||||
10
klibc/include/stddef.h
Normal file
10
klibc/include/stddef.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CLASSICOS_KLIBC_STDDEF_H
|
||||
#define CLASSICOS_KLIBC_STDDEF_H
|
||||
|
||||
typedef __SIZE_TYPE__ size_t;
|
||||
typedef __PTRDIFF_TYPE__ ptrdiff_t;
|
||||
|
||||
#undef NULL
|
||||
#define NULL ((void*)0)
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STDDEF_H
|
||||
16
klibc/include/stdint.h
Normal file
16
klibc/include/stdint.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef CLASSICOS_KLIBC_STDINT_H
|
||||
#define CLASSICOS_KLIBC_STDINT_H
|
||||
|
||||
typedef signed char int8_t;
|
||||
typedef short int int16_t;
|
||||
typedef int int32_t;
|
||||
typedef long long int int64_t;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short int uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long long int uint64_t;
|
||||
|
||||
typedef unsigned int uintptr_t;
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STDINT_H
|
||||
4
klibc/include/stdio.h
Normal file
4
klibc/include/stdio.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef CLASSICOS_KLIBC_STDIO_H
|
||||
#define CLASSICOS_KLIBC_STDIO_H
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STDIO_H
|
||||
4
klibc/include/stdlib.h
Normal file
4
klibc/include/stdlib.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef CLASSICOS_KLIBC_STDLIB_H
|
||||
#define CLASSICOS_KLIBC_STDLIB_H
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STDLIB_H
|
||||
14
klibc/include/string.h
Normal file
14
klibc/include/string.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef CLASSICOS_KLIBC_STRING_H
|
||||
#define CLASSICOS_KLIBC_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
extern int memcmp(const void* s1, const void* s2, size_t n);
|
||||
extern void* memmove(void* dst, const void* src, size_t n);
|
||||
extern void* memcpy(void* dst, const void* src, size_t n);
|
||||
extern void* memset(void* dst, int c, size_t n);
|
||||
|
||||
extern size_t strlen(const char* s);
|
||||
extern int strcmp(const char* s1, const char* s2);
|
||||
|
||||
#endif // CLASSICOS_KLIBC_STRING_H
|
||||
107
klibc/src/string.c
Normal file
107
klibc/src/string.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <string.h>
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n) {
|
||||
const unsigned char* c1 = s1;
|
||||
const unsigned char* c2 = s2;
|
||||
int d = 0;
|
||||
|
||||
while (n--) {
|
||||
d = (int)*c1++ - (int)*c2++;
|
||||
if (d) break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
void* memmove(void* dst, const void* src, size_t n) {
|
||||
const char* p = src;
|
||||
char* q = dst;
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
if (q < p) {
|
||||
__asm__ volatile("cld; rep; movsb" : "+c"(n), "+S"(p), "+D"(q));
|
||||
} else {
|
||||
p += (n - 1);
|
||||
q += (n - 1);
|
||||
__asm__ volatile("std; rep; movsb; cld" : "+c"(n), "+S"(p), "+D"(q));
|
||||
}
|
||||
#else
|
||||
if (q < p) {
|
||||
while (n--) {
|
||||
*q++ = *p++;
|
||||
}
|
||||
} else {
|
||||
p += n;
|
||||
q += n;
|
||||
while (n--) {
|
||||
*--q = *--p;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void* memcpy(void* dst, const void* src, size_t n) {
|
||||
const char* p = src;
|
||||
char* q = dst;
|
||||
#if defined(__i386__)
|
||||
size_t nl = n >> 2;
|
||||
__asm__ volatile("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb"
|
||||
: "+c"(nl), "+S"(p), "+D"(q)
|
||||
: "r"(n & 3));
|
||||
#elif defined(__x86_64__)
|
||||
size_t nq = n >> 3;
|
||||
__asm__ volatile("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb"
|
||||
: "+c"(nq), "+S"(p), "+D"(q)
|
||||
: "r"((uint32_t)(n & 7)));
|
||||
#else
|
||||
while (n--) {
|
||||
*q++ = *p++;
|
||||
}
|
||||
#endif
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void* memset(void* dst, int c, size_t n) {
|
||||
char* q = dst;
|
||||
|
||||
#if defined(__i386__)
|
||||
size_t nl = n >> 2;
|
||||
__asm__ volatile("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
|
||||
: "+c"(nl), "+D"(q)
|
||||
: "a"((unsigned char)c * 0x01010101U), "r"(n & 3));
|
||||
#elif defined(__x86_64__)
|
||||
size_t nq = n >> 3;
|
||||
__asm__ volatile("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
|
||||
: "+c"(nq), "+D"(q)
|
||||
: "a"((unsigned char)c * 0x0101010101010101U),
|
||||
"r"((uint32_t)n & 7));
|
||||
#else
|
||||
while (n--) {
|
||||
*q++ = c;
|
||||
}
|
||||
#endif
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
size_t strlen(const char* s) {
|
||||
const char* ss = s;
|
||||
while (*ss) ss++;
|
||||
return ss - s;
|
||||
}
|
||||
|
||||
int strcmp(const char* s1, const char* s2) {
|
||||
const unsigned char* c1 = (const unsigned char*)s1;
|
||||
const unsigned char* c2 = (const unsigned char*)s2;
|
||||
unsigned char ch;
|
||||
int d = 0;
|
||||
|
||||
while (1) {
|
||||
d = (int)(ch = *c1++) - (int)*c2++;
|
||||
if (d || !ch) break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
Reference in New Issue
Block a user