mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
Liba
This commit is contained in:
2
liba/Makefile
Normal file
2
liba/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
SFLAGS += -Iliba/include
|
||||
objs += $(addprefix liba/src/, assert.o memcpy.o memset.o malloc.o)
|
||||
10
liba/README.txt
Normal file
10
liba/README.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
liba is an adhoc libc.
|
||||
|
||||
We need a very small subset of the functionality provided by the standard C
|
||||
library. We could use an available libc implementation, but those are usually
|
||||
way to large and may have problematic licenses.
|
||||
|
||||
It wouldn't even be fair to call liba a libc at all since it doesn't come close
|
||||
to implementing a significant portion of the standard. However, we do need some
|
||||
functionality usually provided by libc, so instead of reinventing the wheel we
|
||||
just implement the same entities (types, functions, etc…).
|
||||
18
liba/include/assert.h
Normal file
18
liba/include/assert.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef LIBA_ASSERT_H
|
||||
#define LIBA_ASSERT_H
|
||||
|
||||
#include "private/macros.h"
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define assert(e) ((void)0)
|
||||
#else
|
||||
#define assert(e) ((void) ((e) ? ((void)0) : __assert(#e, __FILE__, __LINE__)))
|
||||
#endif
|
||||
|
||||
LIBA_BEGIN_DECLS
|
||||
|
||||
void __assert(const char * expression, const char * file, int line);
|
||||
|
||||
LIBA_END_DECLS
|
||||
|
||||
#endif
|
||||
12
liba/include/private/macros.h
Normal file
12
liba/include/private/macros.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef LIBA_MACROS_H
|
||||
#define LIBA_MACROS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define LIBA_BEGIN_DECLS extern "C" {
|
||||
#define LIBA_END_DECLS }
|
||||
#else
|
||||
#define LIBA_BEGIN_DECLS
|
||||
#define LIBA_END_DECLS
|
||||
#endif
|
||||
|
||||
#endif
|
||||
6
liba/include/private/types.h
Normal file
6
liba/include/private/types.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef LIBA_TYPES_H
|
||||
#define LIBA_TYPES_H
|
||||
|
||||
typedef int size_t;
|
||||
|
||||
#endif
|
||||
6
liba/include/stdbool.h
Normal file
6
liba/include/stdbool.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef LIBA_STDBOOL_H
|
||||
#define LIBA_STDBOOL_H
|
||||
|
||||
typedef char bool;
|
||||
|
||||
#endif
|
||||
11
liba/include/stdint.h
Normal file
11
liba/include/stdint.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef LIBA_STDINT_H
|
||||
#define LIBA_STDINT_H
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned int uint16_t;
|
||||
typedef unsigned long uint32_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
|
||||
typedef long long int64_t;
|
||||
|
||||
#endif
|
||||
17
liba/include/stdlib.h
Normal file
17
liba/include/stdlib.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef LIBA_MALLOC_H
|
||||
#define LIBA_MALLOC_H
|
||||
|
||||
#include "private/macros.h"
|
||||
#include "private/types.h"
|
||||
|
||||
LIBA_BEGIN_DECLS
|
||||
|
||||
void free(void *ptr);
|
||||
void * malloc(size_t size);
|
||||
void * realloc(void *ptr, size_t size);
|
||||
|
||||
void abort(void);
|
||||
|
||||
LIBA_END_DECLS
|
||||
|
||||
#endif
|
||||
18
liba/include/string.h
Normal file
18
liba/include/string.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef LIBA_STRING_H
|
||||
#define LIBA_STRING_H
|
||||
|
||||
#include "private/macros.h"
|
||||
#include "private/types.h"
|
||||
|
||||
#define NULL 0
|
||||
|
||||
LIBA_BEGIN_DECLS
|
||||
|
||||
void * memcpy(void * dst, const void * src, size_t n);
|
||||
void * memset(void * b, int c, size_t len);
|
||||
|
||||
size_t strlen(const char * s);
|
||||
|
||||
LIBA_END_DECLS
|
||||
|
||||
#endif
|
||||
4
liba/include/unistd.h
Normal file
4
liba/include/unistd.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef LIBA_UNISTD_H
|
||||
#define LIBA_UNISTD_H
|
||||
|
||||
#endif
|
||||
12
liba/src/assert.c
Normal file
12
liba/src/assert.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define assert(e) ((void)0)
|
||||
#else
|
||||
#define assert(e) ((void) ((e) ? ((void)0) : __assert(#e, __FILE__, __LINE__)))
|
||||
#endif
|
||||
|
||||
void __assert(const char * expression, const char * file, int line) {
|
||||
abort();
|
||||
}
|
||||
20
liba/src/malloc.c
Normal file
20
liba/src/malloc.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void free(void *ptr) {
|
||||
if (ptr != NULL) {
|
||||
memsys5FreeUnsafe(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void * malloc(size_t size) {
|
||||
void * p = NULL;
|
||||
if (size > 0) {
|
||||
p = memsys5MallocUnsafe(memsys5Roundup(size));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void * realloc(void *ptr, size_t size) {
|
||||
return memsys5Realloc(ptr, memsys5Roundup(size));
|
||||
}
|
||||
12
liba/src/memcpy.c
Normal file
12
liba/src/memcpy.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <string.h>
|
||||
|
||||
void * memcpy(void * dst, const void * src, size_t n) {
|
||||
char * destination = (char *)dst;
|
||||
char * source = (char *)src;
|
||||
|
||||
while (n--) {
|
||||
*destination++ = *source++;
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
8
liba/src/memset.c
Normal file
8
liba/src/memset.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <string.h>
|
||||
|
||||
void * memset(void * b, int c, size_t len) {
|
||||
char * destination = (char *)b;
|
||||
while (len--) {
|
||||
*destination++ = (unsigned char)c;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user