Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 2 Wochen
vor 2 Wochen
vor 2 Wochen
vor 2 Tagen
vor 2 Wochen
vor 2 Wochen
vor 2 Wochen
vor 2 Wochen
vor 2 Wochen
vor 2 Tagen
vor 2 Wochen
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef OS_IMPL_LINUX_CPP
  2. #define OS_IMPL_LINUX_CPP
  3. #include "os.h"
  4. #include <sys/mman.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. void *os_alloc(size_t capacity) {
  8. return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  9. }
  10. void os_commit(void *ptr) {
  11. }
  12. void os_decommit(void *ptr) {
  13. }
  14. void os_free(void *ptr, size_t size) {
  15. int err = munmap(ptr, size);
  16. Assert(err != -1);
  17. }
  18. string os_readEntireFile(Arena *arena, string filename) {
  19. Scratch temp = scratchStart(&arena, 1);
  20. FILE *input = fopen(cstring(temp.arena, filename), "r");
  21. string readBuffer;
  22. if (input) {
  23. struct stat st;
  24. stat((char *)filename.str, &st);
  25. size_t fsize = st.st_size;
  26. readBuffer = PushString(arena, fsize);
  27. fread(readBuffer.str, sizeof(byte), readBuffer.length, input);
  28. fclose(input);
  29. } else {
  30. readBuffer = PushString(arena, 0);
  31. }
  32. scratchEnd(temp);
  33. return readBuffer;
  34. }
  35. bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
  36. Scratch temp = scratchStart(&arena, 1);
  37. bool result = false;
  38. FILE *output = fopen(cstring(temp.arena, filename), "w");
  39. if (output) {
  40. fwrite(contents, sizeof(byte), contentsLength, output);
  41. fclose(output);
  42. result = true;
  43. }
  44. scratchEnd(temp);
  45. return result;
  46. }
  47. bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
  48. Scratch temp = scratchStart(&arena, 1);
  49. bool result = false;
  50. FILE *output = fopen(cstring(temp.arena, filename), "a");
  51. if (output) {
  52. fwrite(contents, sizeof(byte), contentsLength, output);
  53. fclose(output);
  54. result = true;
  55. }
  56. scratchEnd(temp);
  57. return result;
  58. }
  59. void os_log(LogTarget target, const char *fmt, va_list argList) {
  60. Scratch temp = scratchStart(0, 0);
  61. string result = strPrintfv(temp.arena, fmt, argList);
  62. // TODO(djledda): finish implementation without cstdlib
  63. switch (target) {
  64. case LogTarget_stdin:
  65. write(0, (const void *)result.str, result.length);
  66. break;
  67. case LogTarget_stderr:
  68. fflush(stderr);
  69. write(2, (const void *)result.str, result.length);
  70. break;
  71. case LogTarget_stdout:
  72. default:
  73. fflush(stdout);
  74. write(1, (const void *)result.str, result.length);
  75. break;
  76. }
  77. scratchEnd(temp);
  78. }
  79. #endif