xref: /plan9/sys/src/lib9p/util.c (revision 59cc4ca53493a3c6d2349fe2b7f7c40f7dce7294)
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include <thread.h>
6 #include "9p.h"
7 #include "impl.h"
8 
9 void*
10 emalloc(ulong sz)
11 {
12 	void *v;
13 
14 	if((v = malloc(sz)) == nil) {
15 		fprint(2, "out of memory allocating %lud\n", sz);
16 		exits("mem");
17 	}
18 	memset(v, 0, sz);
19 	return v;
20 }
21 
22 void*
23 erealloc(void *v, ulong sz)
24 {
25 	if((v = realloc(v, sz)) == nil) {
26 		fprint(2, "out of memory allocating %lud\n", sz);
27 		exits("mem");
28 	}
29 	return v;
30 }
31 
32 char*
33 estrdup(char *s)
34 {
35 	char *t;
36 
37 	if((t = strdup(s)) == nil) {
38 		fprint(2, "out of memory in strdup(%.10s)\n", s);
39 		exits("mem");
40 	}
41 	return t;
42 }
43 
44 
45 void
46 readbuf(vlong off, void *dst, long *dlen, void *src, long slen)
47 {
48 	if(off >= slen) {
49 		*dlen = 0;
50 		return;
51 	}
52 	if(off+*dlen > slen)
53 		*dlen = slen-off;
54 	memmove(dst, (char*)src+off, *dlen);
55 }
56 void
57 readstr(vlong off, void *dst, long *dlen, char *src)
58 {
59 	readbuf(off, dst, dlen, src, strlen(src));
60 }
61