Blob


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