Libc is now written in C++
This commit is contained in:
@@ -54,7 +54,7 @@ LIBK_OBJS=$(FREEOBJS:.o=.libk.o)
|
||||
BINARIES=libk.a
|
||||
|
||||
.PHONY: all clean install install-headers install-libs
|
||||
.SUFFIXES: .o .libk.o .c .S
|
||||
.SUFFIXES: .o .libk.o .cpp .S
|
||||
|
||||
all: $(BINARIES)
|
||||
|
||||
@@ -64,17 +64,17 @@ libc.a: $(OBJS)
|
||||
libk.a: $(LIBK_OBJS)
|
||||
$(AR) rcs $@ $(LIBK_OBJS)
|
||||
|
||||
.c.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
|
||||
.cpp.o:
|
||||
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
.S.o:
|
||||
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||
$(CXX) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
.c.libk.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
.cpp.libk.o:
|
||||
$(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
|
||||
.S.libk.o:
|
||||
$(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
$(CXX) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(BINARIES) *.a
|
||||
|
||||
@@ -4,14 +4,10 @@
|
||||
|
||||
#define EOF (-1)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
__BEGIN_DECLS
|
||||
|
||||
int printf(const char* __restrict, ...);
|
||||
int putchar(int);
|
||||
int puts(const char*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
__END_DECLS
|
||||
@@ -2,13 +2,9 @@
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
__BEGIN_DECLS
|
||||
|
||||
__attribute__((__noreturn__))
|
||||
void abort(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
__END_DECLS
|
||||
@@ -3,9 +3,7 @@
|
||||
#include <stddef.h>
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
__BEGIN_DECLS
|
||||
|
||||
int memcmp(const void*, const void*, size_t);
|
||||
void* memcpy(void* __restrict, const void* __restrict, size_t);
|
||||
@@ -16,6 +14,4 @@ size_t strlen(const char*);
|
||||
char* strcpy(char* __restrict, const char* __restrict);
|
||||
char* strncpy(char* __restrict, const char* __restrict, size_t);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
__END_DECLS
|
||||
@@ -1,3 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#define __banan_libc 1
|
||||
#define __banan_libc 1
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define __BEGIN_DECLS extern "C" {
|
||||
#define __END_DECLS }
|
||||
#else
|
||||
#define __BEGIN_DECLS
|
||||
#define __END_DECLS
|
||||
#endif
|
||||
@@ -1,26 +1,25 @@
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool print(const char* data, size_t len)
|
||||
{
|
||||
const unsigned char* bytes = (const unsigned char*)data;
|
||||
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(data);
|
||||
for(size_t i = 0; i < len; i++)
|
||||
if (putchar(bytes[i]) == EOF)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool print_int(int value, size_t* out_len)
|
||||
static bool print_int(int value, size_t& out_len)
|
||||
{
|
||||
if (value == -2147483648)
|
||||
{
|
||||
if (!print("-2147483648", 11))
|
||||
return false;
|
||||
*out_len = 11;
|
||||
out_len = 11;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -52,7 +51,7 @@ static bool print_int(int value, size_t* out_len)
|
||||
if (!print(ptr, len))
|
||||
return false;
|
||||
|
||||
*out_len = len;
|
||||
out_len = len;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -63,9 +62,9 @@ static char bits_to_hex(unsigned char bits)
|
||||
return bits - 10 + 'a';
|
||||
}
|
||||
|
||||
static bool print_ptr(void* ptr, size_t* out_len)
|
||||
static bool print_ptr(void* ptr, size_t& out_len)
|
||||
{
|
||||
ptrdiff_t addr = (ptrdiff_t)ptr;
|
||||
ptrdiff_t addr = reinterpret_cast<ptrdiff_t>(ptr);
|
||||
|
||||
char buffer[2 + sizeof(ptrdiff_t) * 2];
|
||||
buffer[0] = '0';
|
||||
@@ -83,11 +82,11 @@ static bool print_ptr(void* ptr, size_t* out_len)
|
||||
if (!print(buffer, 2 + bytes * 2))
|
||||
return false;
|
||||
|
||||
*out_len = 2 + bytes * 2;
|
||||
out_len = 2 + bytes * 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
int printf(const char* restrict format, ...)
|
||||
int printf(const char* __restrict format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
@@ -140,7 +139,7 @@ int printf(const char* restrict format, ...)
|
||||
format++;
|
||||
int value = va_arg(args, int);
|
||||
size_t len;
|
||||
if (!print_int(value, &len))
|
||||
if (!print_int(value, len))
|
||||
return -1;
|
||||
written += len;
|
||||
}
|
||||
@@ -149,7 +148,7 @@ int printf(const char* restrict format, ...)
|
||||
format++;
|
||||
void* const ptr = va_arg(args, void*);
|
||||
size_t len;
|
||||
if (!print_ptr(ptr, &len))
|
||||
if (!print_ptr(ptr, len))
|
||||
return -1;
|
||||
written += len;
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#if defined(__is_libk)
|
||||
#include <kernel/tty.h>
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
int putchar(int c)
|
||||
@@ -10,6 +12,7 @@ int putchar(int c)
|
||||
char ch = (char)c;
|
||||
terminal_write(&ch, sizeof(ch));
|
||||
#else
|
||||
abort();
|
||||
#endif
|
||||
return c;
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#include <string.h>
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n)
|
||||
{
|
||||
const unsigned char* a = s1;
|
||||
const unsigned char* b = s2;
|
||||
|
||||
while (n--)
|
||||
{
|
||||
if (*a != *b)
|
||||
return *a - *b;
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
13
libc/string/memcmp.cpp
Normal file
13
libc/string/memcmp.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <string.h>
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n)
|
||||
{
|
||||
const unsigned char* a = static_cast<const unsigned char*>(s1);
|
||||
const unsigned char* b = static_cast<const unsigned char*>(s2);
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
if (a[i] != b[i])
|
||||
return a[i] - b[i];
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#include <string.h>
|
||||
|
||||
void* memcpy(void* restrict destp, const void* restrict srcp, size_t n)
|
||||
{
|
||||
unsigned char* dest = (unsigned char*)destp;
|
||||
const unsigned char* src = (const unsigned char*)srcp;
|
||||
for (size_t i = 0; i < n; i++)
|
||||
dest[i] = src[i];
|
||||
return destp;
|
||||
}
|
||||
10
libc/string/memcpy.cpp
Normal file
10
libc/string/memcpy.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <string.h>
|
||||
|
||||
void* memcpy(void* __restrict dstp, const void* __restrict srcp, size_t n)
|
||||
{
|
||||
unsigned char* dst = static_cast<unsigned char*>(dstp);
|
||||
const unsigned char* src = static_cast<const unsigned char*>(srcp);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
dst[i] = src[i];
|
||||
return dstp;
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
void* memmove(void* destp, const void* srcp, size_t n)
|
||||
{
|
||||
unsigned char* dest = (unsigned char*)destp;
|
||||
const unsigned char* src = (const unsigned char*)srcp;
|
||||
unsigned char* dest = static_cast<unsigned char*>(destp);
|
||||
const unsigned char* src = static_cast<const unsigned char*>(srcp);
|
||||
|
||||
if (dest < src)
|
||||
{
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
void* memset(void* s, int c, size_t n)
|
||||
{
|
||||
unsigned char* p = (unsigned char*)s;
|
||||
unsigned char* p = static_cast<unsigned char*>(s);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
p[i] = c;
|
||||
return s;
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strcpy(char* restrict dest, const char* restrict src)
|
||||
char* strcpy(char* __restrict dest, const char* __restrict src)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; src[i]; i++)
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <string.h>
|
||||
|
||||
char* strncpy(char* restrict dest, const char* restrict src, size_t n)
|
||||
char* strncpy(char* __restrict dest, const char* __restrict src, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; src[i] && i < n; i++)
|
||||
Reference in New Issue
Block a user