mman.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef __SYS_MMAN_H
  2. #define __SYS_MMAN_H
  3. #include "asmc_types.h"
  4. #include "errno.h"
  5. #include "stdio.h"
  6. #include "unistd.h"
  7. #define PROT_NONE 0
  8. #define PROT_EXEC (1 << 0)
  9. #define PROT_READ (1 << 1)
  10. #define PROT_WRITE (1 << 2)
  11. #define MAP_FIXED (1 << 0)
  12. #define MAP_PRIVATE (1 << 1)
  13. #define MAP_SHARED (1 << 2)
  14. #define MAP_FILE (1 << 3)
  15. #define MAP_FAILED NULL
  16. int mprotect(void *addr, size_t len, int prot) {
  17. if (prot == PROT_READ | PROT_WRITE | PROT_EXEC) {
  18. // There is no protection, so all memory is automatically rwx
  19. return 0;
  20. } else {
  21. errno = ENOTIMPL;
  22. return -1;
  23. }
  24. }
  25. void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) {
  26. // Ignore MAP_FILE
  27. flags &= ~MAP_FILE;
  28. if (addr != 0 || prot != PROT_READ || flags != MAP_PRIVATE) {
  29. errno = ENOTIMPL;
  30. return MAP_FAILED;
  31. }
  32. void *ret = malloc(len);
  33. if (len == 0) {
  34. return ret;
  35. }
  36. if (ret == NULL) {
  37. errno = ENOMEM;
  38. return MAP_FAILED;
  39. }
  40. char *pos = (char*) ret;
  41. off_t cur_pos = lseek(fildes, 0, SEEK_CUR);
  42. lseek(fildes, off, SEEK_SET);
  43. do {
  44. ssize_t res = read(fildes, pos, 1);
  45. if (!res) {
  46. *pos = '\0';
  47. }
  48. pos++;
  49. } while (--len);
  50. lseek(fildes, cur_pos, SEEK_SET);
  51. return ret;
  52. }
  53. int munmap(void *addr, size_t len) {
  54. free(addr);
  55. return 0;
  56. }
  57. #endif