Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit d30b576

Browse files
committed
Added base version files
1 parent 0fce67b commit d30b576

File tree

9 files changed

+89
-0
lines changed

9 files changed

+89
-0
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
BINS =
2+
TEST_BINS = test
3+
CLIBC_INSTALL_PATH = /usr/include
4+
5+
all: $(TEST_BINS)
6+
7+
%: %.c; clang -o $@ -g -fsanitize=address -I. $<
8+
9+
install:
10+
cp -r clibc/ $(CLIBC_INSTALL_PATH)

clibc/io.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CCIO_H_
2+
#define CCIO_H_
3+
#include <stdarg.h>
4+
#include "clibc/std.h"
5+
// TODO: add modern C wrapper around these
6+
extern int fprintf (FILE* __restrict __stream, str __restrict __format, ...);
7+
8+
extern int printf (const char *__restrict __format, ...);
9+
10+
extern int sprintf (char *__restrict __s,
11+
const char *__restrict __format, ...);
12+
13+
#define io_write(x, ...) _Generic((x), \
14+
char*: printf, \
15+
str: printf, \
16+
FILE*: fprintf \
17+
)(x, __VA_ARGS__);
18+
19+
#endif /* CCIO_H_ */

clibc/mem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#if !defined(CCMEM_H_) || !defined(JEMALLOC_H_)
2+
#define CCMEM_H_
3+
#include <jemalloc/jemalloc.h>
4+
5+
#endif /* CCMEM_H_ */

clibc/std.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef CCSTD_H_
2+
#define CCSTD_H_
3+
#define dynarray(T) T*
4+
#define arr_size(A) (sizeof(A) / sizeof(A[0]))
5+
6+
typedef const char* str;
7+
8+
typedef struct _iobuf {
9+
char* _ptr;
10+
int _cnt;
11+
char* _base;
12+
int _flag;
13+
int _file;
14+
int _charbuf;
15+
int _bufsiz;
16+
char* _tmpfname;
17+
} FILE;
18+
19+
typedef unsigned long size_t;
20+
21+
extern FILE* stdout;
22+
extern FILE* stdin;
23+
extern FILE* stderr;
24+
#endif /* CCSTD_H_ */

clibc/string.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#ifndef CCSTRING_H_
2+
#define CCSTRING_H_
3+
// TODO: better alternatives to str routines
4+
5+
#endif /* CCSTRING_H_

install.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
echo "[+] installing jemalloc..."
2+
cd ./jemalloc
3+
./autogen.sh
4+
make
5+
make install
6+
7+
echo "[+] installing clibc..."
8+
cd ..
9+
sudo make install

jemalloc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 1f688490e176aafbc3e3529d3025df7fcbce725b

test

1.44 MB
Binary file not shown.

test.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <clibc/std.h>
2+
#include <clibc/io.h>
3+
#include <clibc/mem.h>
4+
5+
int main() {
6+
// jemalloc test
7+
str* string = (str*)malloc(sizeof(str)); /* jemalloc allocator */
8+
*string = "string in dynamic memory";
9+
io_write("%s\n", *string);
10+
11+
free(string);
12+
13+
// write test
14+
io_write(stdout, "write to file !\n"); /* write to file */
15+
io_write("%d %d\n%s\n", 23, 44, "string !"); /* formated write */
16+
}

0 commit comments

Comments
 (0)