utils.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. int
  5. namecmp(char *s, char *t)
  6. {
  7. return strncmp(s, t, ANameSize);
  8. }
  9. void
  10. namecp(char *dst, char *src)
  11. {
  12. strncpy(dst, src, ANameSize - 1);
  13. dst[ANameSize - 1] = '\0';
  14. }
  15. int
  16. nameok(char *name)
  17. {
  18. char *t;
  19. int c;
  20. if(name == nil)
  21. return -1;
  22. for(t = name; c = *t; t++)
  23. if(t - name >= ANameSize
  24. || c < ' ' || c >= 0x7f)
  25. return -1;
  26. return 0;
  27. }
  28. int
  29. stru32int(char *s, u32int *r)
  30. {
  31. char *t;
  32. u32int n, nn, m;
  33. int c;
  34. m = TWID32 / 10;
  35. n = 0;
  36. for(t = s; ; t++){
  37. c = *t;
  38. if(c < '0' || c > '9')
  39. break;
  40. if(n > m)
  41. return -1;
  42. nn = n * 10 + c - '0';
  43. if(nn < n)
  44. return -1;
  45. n = nn;
  46. }
  47. *r = n;
  48. return s != t && *t == '\0';
  49. }
  50. int
  51. stru64int(char *s, u64int *r)
  52. {
  53. char *t;
  54. u64int n, nn, m;
  55. int c;
  56. m = TWID64 / 10;
  57. n = 0;
  58. for(t = s; ; t++){
  59. c = *t;
  60. if(c < '0' || c > '9')
  61. break;
  62. if(n > m)
  63. return -1;
  64. nn = n * 10 + c - '0';
  65. if(nn < n)
  66. return -1;
  67. n = nn;
  68. }
  69. *r = n;
  70. return s != t && *t == '\0';
  71. }
  72. int
  73. vttypevalid(int type)
  74. {
  75. return type < VtMaxType;
  76. }
  77. static char*
  78. logit(int severity, char *fmt, va_list args)
  79. {
  80. char *s;
  81. s = vsmprint(fmt, args);
  82. if(s == nil)
  83. return nil;
  84. if(severity != EOk){
  85. if(argv0 == nil)
  86. fprint(2, "%T %s: err %d: %s\n", argv0, severity, s);
  87. else
  88. fprint(2, "%T err %d: %s\n", severity, s);
  89. }
  90. return s;
  91. }
  92. void
  93. seterr(int severity, char *fmt, ...)
  94. {
  95. char *s;
  96. va_list args;
  97. va_start(args, fmt);
  98. s = logit(severity, fmt, args);
  99. va_end(args);
  100. if(s == nil)
  101. werrstr("error setting error");
  102. else{
  103. werrstr("%s", s);
  104. free(s);
  105. }
  106. }
  107. void
  108. logerr(int severity, char *fmt, ...)
  109. {
  110. char *s;
  111. va_list args;
  112. va_start(args, fmt);
  113. s = logit(severity, fmt, args);
  114. va_end(args);
  115. free(s);
  116. }
  117. u32int
  118. now(void)
  119. {
  120. return time(nil);
  121. }
  122. int abortonmem = 1;
  123. void *
  124. emalloc(ulong n)
  125. {
  126. void *p;
  127. p = malloc(n);
  128. if(p == nil){
  129. if(abortonmem)
  130. abort();
  131. sysfatal("out of memory allocating %lud", n);
  132. }
  133. memset(p, 0xa5, n);
  134. setmalloctag(p, getcallerpc(&n));
  135. if(0)print("emalloc %p-%p by %#p\n", p, (char*)p+n, getcallerpc(&n));
  136. return p;
  137. }
  138. void *
  139. ezmalloc(ulong n)
  140. {
  141. void *p;
  142. p = malloc(n);
  143. if(p == nil){
  144. if(abortonmem)
  145. abort();
  146. sysfatal("out of memory allocating %lud", n);
  147. }
  148. memset(p, 0, n);
  149. setmalloctag(p, getcallerpc(&n));
  150. if(0)print("ezmalloc %p-%p by %#p\n", p, (char*)p+n, getcallerpc(&n));
  151. return p;
  152. }
  153. void *
  154. erealloc(void *p, ulong n)
  155. {
  156. p = realloc(p, n);
  157. if(p == nil){
  158. if(abortonmem)
  159. abort();
  160. sysfatal("out of memory allocating %lud", n);
  161. }
  162. setrealloctag(p, getcallerpc(&p));
  163. if(0)print("erealloc %p-%p by %#p\n", p, (char*)p+n, getcallerpc(&p));
  164. return p;
  165. }
  166. char *
  167. estrdup(char *s)
  168. {
  169. char *t;
  170. int n;
  171. n = strlen(s) + 1;
  172. t = emalloc(n);
  173. memmove(t, s, n);
  174. setmalloctag(t, getcallerpc(&s));
  175. if(0)print("estrdup %p-%p by %#p\n", t, (char*)t+n, getcallerpc(&s));
  176. return t;
  177. }
  178. /*
  179. * return floor(log2(v))
  180. */
  181. int
  182. u64log2(u64int v)
  183. {
  184. int i;
  185. for(i = 0; i < 64; i++)
  186. if((v >> i) <= 1)
  187. break;
  188. return i;
  189. }
  190. int
  191. vtproc(void (*fn)(void*), void *arg)
  192. {
  193. proccreate(fn, arg, 256*1024);
  194. return 0;
  195. }
  196. int
  197. ientryfmt(Fmt *fmt)
  198. {
  199. IEntry *ie;
  200. ie = va_arg(fmt->args, IEntry*);
  201. return fmtprint(fmt, "%V %22lld %3d %5d %3d",
  202. ie->score, ie->ia.addr, ie->ia.type, ie->ia.size, ie->ia.blocks);
  203. }
  204. void
  205. ventifmtinstall(void)
  206. {
  207. fmtinstall('F', vtfcallfmt);
  208. fmtinstall('H', encodefmt);
  209. fmtinstall('I', ientryfmt);
  210. fmtinstall('T', vttimefmt);
  211. fmtinstall('V', vtscorefmt);
  212. }
  213. uint
  214. msec(void)
  215. {
  216. return nsec()/1000000;
  217. }
  218. uint
  219. countbits(uint n)
  220. {
  221. n = (n&0x55555555)+((n>>1)&0x55555555);
  222. n = (n&0x33333333)+((n>>2)&0x33333333);
  223. n = (n&0x0F0F0F0F)+((n>>4)&0x0F0F0F0F);
  224. n = (n&0x00FF00FF)+((n>>8)&0x00FF00FF);
  225. n = (n&0x0000FFFF)+((n>>16)&0x0000FFFF);
  226. return n;
  227. }