plan9-io.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <oventi.h>
  12. enum {
  13. IdealAlignment = 32,
  14. ChunkSize = 128*1024,
  15. };
  16. void
  17. vtMemFree(void *p)
  18. {
  19. if(p == 0)
  20. return;
  21. free(p);
  22. }
  23. void *
  24. vtMemAlloc(int size)
  25. {
  26. void *p;
  27. p = malloc(size);
  28. if(p == 0)
  29. vtFatal("vtMemAlloc: out of memory");
  30. setmalloctag(p, getcallerpc(&size));
  31. return p;
  32. }
  33. void *
  34. vtMemAllocZ(int size)
  35. {
  36. void *p = vtMemAlloc(size);
  37. memset(p, 0, size);
  38. setmalloctag(p, getcallerpc(&size));
  39. return p;
  40. }
  41. void *
  42. vtMemRealloc(void *p, int size)
  43. {
  44. if(p == nil)
  45. return vtMemAlloc(size);
  46. p = realloc(p, size);
  47. if(p == 0)
  48. vtFatal("vtRealloc: out of memory");
  49. setrealloctag(p, getcallerpc(&size));
  50. return p;
  51. }
  52. void *
  53. vtMemBrk(int n)
  54. {
  55. static Lock lk;
  56. static uint8_t *buf;
  57. static int nbuf;
  58. static int nchunk;
  59. int align, pad;
  60. void *p;
  61. if(n >= IdealAlignment)
  62. align = IdealAlignment;
  63. else if(n > 8)
  64. align = 8;
  65. else
  66. align = 4;
  67. lock(&lk);
  68. pad = (align - (uintptr)buf) & (align-1);
  69. if(n + pad > nbuf) {
  70. buf = vtMemAllocZ(ChunkSize);
  71. setmalloctag(buf, getcallerpc(&n));
  72. nbuf = ChunkSize;
  73. pad = (align - (uintptr)buf) & (align-1);
  74. nchunk++;
  75. }
  76. assert(n + pad <= nbuf);
  77. p = buf + pad;
  78. buf += pad + n;
  79. nbuf -= pad + n;
  80. unlock(&lk);
  81. return p;
  82. }
  83. void
  84. vtThreadSetName(char *name)
  85. {
  86. int fd;
  87. char buf[32];
  88. sprint(buf, "/proc/%d/args", getpid());
  89. if((fd = open(buf, OWRITE)) >= 0){
  90. write(fd, name, strlen(name));
  91. close(fd);
  92. }
  93. }
  94. int
  95. vtFdRead(int fd, uint8_t *buf, int n)
  96. {
  97. n = read(fd, buf, n);
  98. if(n < 0) {
  99. vtOSError();
  100. return -1;
  101. }
  102. if(n == 0) {
  103. vtSetError("unexpected EOF");
  104. return 0;
  105. }
  106. return n;
  107. }
  108. int
  109. vtFdWrite(int fd, uint8_t *buf, int n)
  110. {
  111. int nn;
  112. nn = write(fd, buf, n);
  113. if(nn < 0) {
  114. vtOSError();
  115. return 0;
  116. }
  117. if(n != nn) {
  118. vtSetError("truncated write");
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. void
  124. vtFdClose(int fd)
  125. {
  126. close(fd);
  127. }
  128. char *
  129. vtOSError(void)
  130. {
  131. return vtSetError("%r");
  132. }