]> git.lizzy.rs Git - plan9front.git/blob - sys/src/liboventi/plan9-io.c
libsec: use tsmemcmp() when comparing hashes, use mpfield() for ecc, use mptober...
[plan9front.git] / sys / src / liboventi / plan9-io.c
1 #include <u.h>
2 #include <libc.h>
3 #include <oventi.h>
4
5 enum {
6         IdealAlignment = 32,
7         ChunkSize       = 128*1024,
8 };
9
10
11 void
12 vtMemFree(void *p)
13 {
14         if(p == 0)
15                 return;
16         free(p);
17 }
18
19
20 void *
21 vtMemAlloc(int size)
22 {
23         void *p;
24
25         p = malloc(size);
26         if(p == 0)
27                 vtFatal("vtMemAlloc: out of memory");
28         setmalloctag(p, getcallerpc(&size));
29         return p;
30 }
31
32 void *
33 vtMemAllocZ(int size)
34 {
35         void *p = vtMemAlloc(size);
36         memset(p, 0, size);
37         setmalloctag(p, getcallerpc(&size));
38         return p;
39 }
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
53
54 void *
55 vtMemBrk(int n)
56 {
57         static Lock lk;
58         static uchar *buf;
59         static int nbuf;
60         static int nchunk;
61         int align, pad;
62         void *p;
63
64         if(n >= IdealAlignment)
65                 align = IdealAlignment;
66         else if(n > 8)
67                 align = 8;
68         else    
69                 align = 4;
70
71         lock(&lk);
72         pad = (align - (uintptr)buf) & (align-1);
73         if(n + pad > nbuf) {
74                 buf = vtMemAllocZ(ChunkSize);
75                 setmalloctag(buf, getcallerpc(&n));
76                 nbuf = ChunkSize;
77                 pad = (align - (uintptr)buf) & (align-1);
78                 nchunk++;
79         }
80
81         assert(n + pad <= nbuf);        
82         
83         p = buf + pad;
84         buf += pad + n;
85         nbuf -= pad + n;
86         unlock(&lk);
87
88         return p;
89 }
90
91 void
92 vtThreadSetName(char *name)
93 {
94         int fd;
95         char buf[32];
96
97         sprint(buf, "/proc/%d/args", getpid());
98         if((fd = open(buf, OWRITE)) >= 0){
99                 write(fd, name, strlen(name));
100                 close(fd);
101         }
102 }
103
104 int
105 vtFdRead(int fd, uchar *buf, int n)
106 {
107         n = read(fd, buf, n);
108         if(n < 0) {
109                 vtOSError();
110                 return -1;
111         }
112         if(n == 0) {
113                 vtSetError("unexpected EOF");
114                 return 0;
115         }
116         return n;
117 }
118
119 int
120 vtFdWrite(int fd, uchar *buf, int n)
121 {
122         int nn;
123         
124         nn = write(fd, buf, n);
125         if(nn < 0) {
126                 vtOSError();
127                 return 0;
128         }
129         if(n != nn) {
130                 vtSetError("truncated write");
131                 return 0;
132         }
133         return 1;
134 }
135
136 void
137 vtFdClose(int fd)
138 {
139         close(fd);
140 }
141
142 char *
143 vtOSError(void)
144 {
145         return vtSetError("%r");
146 }