Files
djstdlib/os.h
2025-11-28 15:29:24 +01:00

81 lines
1.9 KiB
C

#ifndef OS_H
#define OS_H
#include "core.h"
// ### Memory ###
void *os_alloc(uint64 capacity);
void os_reserve(void *ptr);
void os_decommit(void *ptr);
void os_free(void *ptr, uint64 freeSize);
// ### File IO ###
string os_readEntireFile(Arena *arena, string filename);
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
// ### Standard IO ###
void os_print(StdStream target, const char *fmt, va_list argList);
void os_println(StdStream target, const char *fmt, va_list argList);
// ### Multithreading ###
typedef struct OS_Thread OS_Thread;
struct OS_Thread {
uint64 id;
};
OS_Thread os_createThread(void *(*entry)(void *ctx), void *ctx);
// ### Network I/O ###
typedef struct Address Address;
struct Address;
typedef struct SocketHandle SocketHandle;
struct SocketHandle;
typedef struct Socket Socket;
struct Socket {
const Address *address;
SocketHandle *handle;
bool closed;
};
DefineList(Socket, Socket);
typedef struct Server Server;
struct Server {
Arena *arena;
Address *address;
uint32 port;
SocketHandle *handle;
SocketList clients;
bool listening;
};
typedef struct ServerInitInfo ServerInitInfo;
struct ServerInitInfo {
uint16 port;
uint32 concurrentClients;
uint64 memory;
};
typedef struct SocketConnectInfo SocketConnectInfo;
struct SocketConnectInfo {
string address;
uint16 port;
};
// Server/Client interface
Server serverInit(ServerInitInfo info);
void serverListen(Server *s);
Socket *serverAccept(Server *s);
void serverClose(Server *s);
// Generic socket interface
Socket socketConnect(Arena *arena, SocketConnectInfo info);
int64 socketRead(Socket *s, byte *dest, uint64 numBytes);
int64 socketWrite(Socket *s, byte *source, uint64 numBytes);
void socketClose(Socket *s);
#endif