5 #define vi_malloc vi->mm.malloc
6 #define vi_free vi->mm.free
7 #define vi_realloc vi->mm.realloc
9 #define vi_open vi->fop.open
10 #define vi_size vi->fop.size
11 #define vi_close vi->fop.close
12 #define vi_map vi->fop.map
13 #define vi_unmap vi->fop.unmap
14 #define vi_read vi->fop.read
15 #define vi_write vi->fop.write
16 #define vi_seek vi->fop.seek
19 static const struct vi_alloc stdalloc = { malloc, free, realloc };
22 struct visor *vi_create(struct vi_alloc *mm)
27 if(!mm) mm = &stdalloc;
32 if(!(vi = mm->malloc(sizeof *vi))) {
35 memset(vi, 0, sizeof *vi);
41 void vi_destroy(struct visor *vi)
44 vi_delete_buf(vi, vi->buflist);
49 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
54 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
59 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
63 if(!(nb = vi_malloc(sizeof *nb))) {
64 vi_error(vi, "failed to allocate new buffer\n");
67 memset(nb, 0, sizeof *nb);
71 if(vi_buf_read(nb, path) == -1) {
78 struct vi_buffer *last = vi->buflist->prev;
80 nb->next = vi->buflist;
82 vi->buflist->prev = nb;
84 nb->next = nb->prev = nb;
90 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
93 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
98 if(vi->buflist != vb) {
99 vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
106 if(vi->buflist == vb) {
107 vi->buflist = vb->next;
109 vb->prev->next = vb->next;
110 vb->next->prev = vb->prev;
111 vb->next = vb->prev = vb;
115 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
117 if(remove_buf(vi, vb) == -1) {
128 int vi_num_buf(struct visor *vi)
131 struct vi_buffer *vb;
133 if(!vi->buflist) return 0;
136 vb = vi->buflist->next;
137 while(vb != vi->buflist) {
144 struct vi_buffer *vi_getcur_buf(struct visor *vi)
149 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
154 struct vi_buffer *vi_next_buf(struct visor *vi)
156 return vi->buflist ? vi->buflist->next : 0;
159 struct vi_buffer *vi_prev_buf(struct visor *vi)
161 return vi->buflist ? vi->buflist->prev : 0;
164 static int add_span(struct vi_buffer *vb, int src, vi_addr start, unsigned long size)
166 struct visor *vi = vb->vi;
169 if(vb->num_spans >= vb->max_spans) {
170 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
171 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
174 vb->max_spans = newmax;
177 sp = vb->spans + vb->num_spans++;
183 void vi_buf_reset(struct vi_buffer *vb)
185 struct visor *vi = vb->vi;
186 struct vi_buffer *prev, *next;
191 if(vb->file_mapped) vi_unmap(vb->fp);
200 memset(vb, 0, sizeof *vb);
205 int vi_buf_read(struct vi_buffer *vb, const char *path)
207 struct visor *vi = vb->vi;
214 if(!(fp = vi_open(path))) {
218 if(!(vb->path = vi_malloc(plen + 1))) {
219 vi_error(vi, "failed to allocate path name buffer\n");
223 memcpy(vb->path, path, plen + 1);
227 if((fsz = vi_size(fp))) {
228 /* existing file, map it into memory, or failing that read it */
229 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
230 if(!(vb->orig = vi_malloc(fsz))) {
238 if(add_span(vb, SPAN_ORIG, 0, fsz) == -1) {
239 vi_error(vi, "failed to allocate span\n");