Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

118 line
2.6 KiB

  1. #ifndef OS_H
  2. #define OS_H
  3. #include "core.h"
  4. // ### Memory ###
  5. void *os_alloc(uint64 capacity);
  6. void os_reserve(void *ptr);
  7. void os_decommit(void *ptr);
  8. void os_free(void *ptr, uint64 freeSize);
  9. // ### File IO ###
  10. string os_readEntireFile(Arena *arena, string filename);
  11. bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
  12. bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
  13. // ### Standard IO ###
  14. void os_print(StdStream target, const char *fmt, va_list argList);
  15. void os_println(StdStream target, const char *fmt, va_list argList);
  16. // ### Multithreading ###
  17. typedef struct OS_Thread OS_Thread;
  18. struct OS_Thread {
  19. uint64 id;
  20. };
  21. OS_Thread os_createThread(void *(*entry)(void *ctx), void *ctx);
  22. // ### Network I/O ###
  23. typedef struct Address Address;
  24. struct Address;
  25. typedef struct SocketHandle SocketHandle;
  26. struct SocketHandle;
  27. typedef struct ServerEvents ServerEvents;
  28. struct ServerEvents;
  29. typedef struct Socket Socket;
  30. struct Socket {
  31. const Address *address;
  32. SocketHandle *handle;
  33. bool closed;
  34. };
  35. DefineList(Socket, Socket);
  36. typedef struct Server Server;
  37. struct Server {
  38. Arena *arena;
  39. Address *address;
  40. uint32 port;
  41. SocketHandle *handle;
  42. SocketList clients;
  43. bool listening;
  44. ServerEvents *events;
  45. };
  46. typedef struct ServerInitInfo ServerInitInfo;
  47. struct ServerInitInfo {
  48. int16 port;
  49. int32 concurrentClients;
  50. int64 memory;
  51. int32 maxEvents;
  52. };
  53. typedef struct SocketConnectInfo SocketConnectInfo;
  54. struct SocketConnectInfo {
  55. string address;
  56. uint16 port;
  57. };
  58. // Server/Client interface
  59. Server serverInit(ServerInitInfo info);
  60. void serverListen(Server *s);
  61. Socket *serverAccept(Server *s);
  62. void serverClose(Server *s);
  63. enum ServerEventType {
  64. ServerEventType_AcceptClient,
  65. ServerEventType_ClientMessage,
  66. ServerEventType_None,
  67. ServerEventType_COUNT,
  68. };
  69. typedef struct ServerEvent ServerEvent;
  70. struct ServerEvent {
  71. enum ServerEventType type;
  72. union {
  73. struct {} tAcceptClient;
  74. struct {
  75. int32 clientId;
  76. Socket *client;
  77. } tClientMessage;
  78. };
  79. };
  80. ServerEvent *serverGetNextEvent(Server *s);
  81. // Generic socket interface
  82. Socket socketConnect(Arena *arena, SocketConnectInfo info);
  83. int64 socketRead(Socket *s, byte *dest, uint64 numBytes);
  84. DefineResult(string, String);
  85. StringResult socketReadStr(Arena *arena, Socket *s);
  86. int64 socketWrite(Socket *s, byte *source, uint64 numBytes);
  87. int64 socketWriteStr(Socket *socket, string data);
  88. void socketClose(Socket *s);
  89. #endif