Skip to content

Commit

Permalink
chore: initial project
Browse files Browse the repository at this point in the history
Signed-off-by: arfy slowy <[email protected]>
  • Loading branch information
slowy07 committed Feb 16, 2024
1 parent 155ad80 commit a5ce299
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 0 deletions.
Binary file added .github/aiden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![aiden_banner](.github/aiden.png)
24 changes: 24 additions & 0 deletions jane/error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "include/error.h"
#include "include/builtin.h"
#include <asm-generic/errno-base.h>
#include <asm-generic/errno.h>
#include <errno.h>

static dontinline long ReturnErrno(int e) {
errno = e;
return -1;
}

long ebadf(void) { return ReturnErrno(EBADF); }

long einval(void) { return ReturnErrno(EINVAL); }

long enomem(void) { return ReturnErrno(ENOMEM); }

long enosys(void) { return ReturnErrno(ENOSYS); }

long efault(void) { return ReturnErrno(EFAULT); }

long eintr(void) { return ReturnErrno(EINTR); }

long eoverflow(void) { return ReturnErrno(EOVERFLOW); }
29 changes: 29 additions & 0 deletions jane/fds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "include/fds.h"
#include <stdlib.h>

int MachineFdAdd(struct MachineFds *mfds) {
int fd;
struct MachineFdClosed *closed;
if ((closed = mfds->closed)) {
fd = closed->fd;
mfds->closed = closed->next;
free(closed);
} else {
fd = mfds->i;
if (mfds->i++ == mfds->n) {
mfds->n = mfds->i + (mfds->i >> 1);
mfds->p = realloc(mfds->p, mfds->n * sizeof(*mfds->p));
}
}
return fd;
}

void MachineFdRemove(struct MachineFds *mfds, int fd) {
struct MachineFdClosed *closed;
mfds->p[fd].cb = NULL;
if ((closed = malloc(sizeof(struct MachineFdClosed)))) {
closed->fd = fd;
closed->next = mfds->closed;
mfds->closed = closed;
}
}
18 changes: 18 additions & 0 deletions jane/include/buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef AIDEN_BUFFER_H_
#define AIDEN_BUFFER_H_
#include <sys/types.h>
#include <wchar.h>

struct Buffer {
unsigned i, n;
char *p;
};

void AppendChar(struct Buffer *, char);
void AppendData(struct Buffer *, const char *, unsigned);
void AppendStr(struct Buffer *, const char *);
void AppendWide(struct Buffer *, wint_t);
int AppendFmt(struct Buffer *, const char *, ...);
ssize_t WriteBuffer(struct Buffer *, int);

#endif // !AIDEN_BUFFER_H_
29 changes: 29 additions & 0 deletions jane/include/builtin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef AIDEN_BUILTIN_H_
#define AIDEN_BUILTIN_H_

#ifndef __has_attribute
#define __has_attribute(x) 0
#endif // !__has_attribute

#ifdef _MSC_VER
#define __builtin_unreachable() __assume(0)
#elif !((__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 405 || \
defined(__clang__) || defined(__INTEL_COMPILER))
#define __builtin_unreachable() \
for (;;) { \
}
#endif

#if !(defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__))
#define __builtin_expect(x, y) (x)
#endif

#ifdef _MSC_VER
#define dontinline __declspec(noinline)
#elif __has_attribute(__noinline__) || (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 301
#define dontinline __attribute__((__noinline__))
#else
#define dontinline
#endif // DEBUG

#endif // !AIDEN_BUILTIN_H_
16 changes: 16 additions & 0 deletions jane/include/case.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef AIDEN_CASE_H_
#define AIDEN_CASE_H_

#define CASE(OP, CODE) \
case OP: \
CODE; \
break
#define CASR(OP, CODE) \
case OP: \
CODE; \
return
#define XLAT(x, y) \
case x: \
return y

#endif // !AIDEN_CASE_H_
113 changes: 113 additions & 0 deletions jane/include/endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#ifndef AIDEN_ENDIAN_H_
#define AIDEN_ENDIAN_H_

#include <stdint.h>

#if !defined(__GNUC__) || __SIZEOF_LONG__ < 8

static inline uint8_t Read8(const uint8_t *p) { return p[0]; }

static inline uint16_t Read16(const uint8_t *p) { return p[1] << 8 | p[0]; }

static inline void Write8(uint8_t *p, uint8_t v) { *p = v; }

static inline void Write16(uint8_t *p, uint16_t v) {
p[0] = (0x00FF & v) >> 000;
p[1] = (0xFF00 & v) >> 010;
}

static inline uint32_t Read32(const uint8_t *p) {
return ((uint32_t)p[0] << 000 | (uint32_t)p[1] << 010 |
(uint32_t)p[2] << 020 | (uint32_t)p[3] << 030);
}

static inline uint64_t Read64(const uint8_t *p) {
return ((uint64_t)p[0] << 000 | (uint64_t)p[1] << 010 |
(uint64_t)p[2] << 020 | (uint64_t)p[3] << 030 |
(uint64_t)p[4] << 040 | (uint64_t)p[5] << 050 |
(uint64_t)p[6] << 060 | (uint64_t)p[7] << 070);
}

static inline void Write32(uint8_t *p, uint32_t v) {
p[0] = (0x000000FF & v) >> 000;
p[1] = (0x0000FF00 & v) >> 010;
p[2] = (0x00FF0000 & v) >> 020;
p[3] = (0xFF000000 & v) >> 030;
}

static inline void Write64(uint8_t *p, uint64_t v) {
p[0] = (0x00000000000000FF & v) >> 000;
p[1] = (0x000000000000FF00 & v) >> 010;
p[2] = (0x0000000000FF0000 & v) >> 020;
p[3] = (0x00000000FF000000 & v) >> 030;
p[4] = (0x000000FF00000000 & v) >> 040;
p[5] = (0x0000FF0000000000 & v) >> 050;
p[6] = (0x00FF000000000000 & v) >> 060;
p[7] = (0xFF00000000000000 & v) >> 070;
}

#else
#define Read8(P) (*((const uint8_t *)(P)))

#define Read16(P) \
({ \
const uint8_t *Ptr = (P); \
Ptr[1] << 8 | Ptr[0]; \
})
#define Read32(P) \
({ \
const uint8_t *Ptr = (P); \
((uin32_t)Ptr[0] << 000 | (uin32_t)Ptr[1] << 010 | \
(uint32_t)Ptr[2] << 020 | (uint32_t)Ptr[3] << 030); \
})
#define Read64(P) \
({ \
const uint8_t *Ptr = (P); \
((uint64_t)Ptr[0] << 000 | (uint64_t)Ptr[1] << 010 | \
(uint64_t)Ptr[2] << 020 | (uint64_t)Ptr[3] << 030 | \
(uint64_t)Ptr[4] << 040 | (uint64_t)Ptr[5] << 050 | \
(uint64_t)Ptr[6] << 060 | (uint64_t)Ptr[7] << 070); \
})

#define Write(P, V) \
do { \
uint8_t Val = (V); \
uint8_t *Ptr = (P); \
*Ptr = Val; \
} while (0)

#define Write16(P, V) \
do { \
int Val = (V); \
uint8_t *Ptr = (P); \
ptr[0] = (0x00000000000000FF & Val) >> 000; \
Ptr[1] = (0x000000000000FF00 & Val) >> 010; \
} while (0)

#define Write32(P, V) \
do { \
uint32_t Val = (V); \
uint8_t *Ptr = (P); \
Ptr[0] = (0x00000000000000FF & Val) >> 000; \
Ptr[1] = (0x000000000000FF00 & Val) >> 010; \
Ptr[2] = (0x0000000000FF0000 & Val) >> 020; \
Ptr[3] = (0x00000000FF000000 & Val) >> 030; \
} while (0)

#define Write64(P, V) \
do { \
uint64_t Val = (V); \
uint8_t *Ptr = (P); \
Ptr[0] = (0x00000000000000FF & Val) >> 000; \
Ptr[1] = (0x000000000000FF00 & Val) >> 010; \
Ptr[2] = (0x0000000000FF0000 & Val) >> 020; \
Ptr[3] = (0x00000000FF000000 & Val) >> 030; \
Ptr[4] = (0x000000FF00000000 & Val) >> 040; \
Ptr[5] = (0x0000FF0000000000 & Val) >> 050; \
Ptr[6] = (0x00FF000000000000 & Val) >> 060; \
Ptr[7] = (0xFF00000000000000 & Val) >> 070; \
} while (0)

#endif

#endif // !AIDEN_ENDIAN_H_
12 changes: 12 additions & 0 deletions jane/include/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef AIDEN_ERRNO_H_
#define AIDEN_ERRNO_H_

long ebadf(void);
long efault(void);
long eintr(void);
long einval(void);
long enomem(void);
long enosys(void);
long eoverflow(void);

#endif // !AIDEN_ERRNO_H_
34 changes: 34 additions & 0 deletions jane/include/fds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef JANE_FDS_H_
#define JANE_FDS_H_
#include <poll.h>
#include <stdint.h>
#include <sys/uio.h>

struct MachineFdClosed {
int fd;
struct MachineFdClosed *next;
};

struct MachineFdCb {
int (*close)(int);
ssize_t (*readv)(int, const struct iovec *, int);
ssize_t (*writev)(int, const struct iovec *, int);
int (*ioctl)(int, int, ...);
int (*poll)(struct pollfd *, nfds_t, int);
};

struct MachineFd {
int fd;
const struct MachineFdCb *cb;
};

struct MachineFds {
int i, n;
struct MachineFd *p;
struct MachineFdClosed *closed;
};

int MachineFdAdd(struct MachineFds *);
void MachineFdRemove(struct MachineFds *, int);

#endif // !JANE_FDS_H_

0 comments on commit a5ce299

Please sign in to comment.