X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=blobdiff_plain;f=libvisor%2Fsrc%2Fvisor.c;h=af79b4a5b6ad7ba470ebb62614bdd84730fd4643;hp=c6388afee53d3c1343c17ad2da41e3cb14ed446b;hb=4f957b16f77eb7761afd9e0b9064c7f08deb45be;hpb=dc83bdff56d4dd7454559f5e918652f32f7e9d6d diff --git a/libvisor/src/visor.c b/libvisor/src/visor.c index c6388af..af79b4a 100644 --- a/libvisor/src/visor.c +++ b/libvisor/src/visor.c @@ -1,2 +1,72 @@ +#include "vilibc.h" #include "visor.h" +#include "vimpl.h" +#define vi_malloc(s) vi->mm.malloc(s) +#define vi_free(p) vi->mm.free(p) + +#ifdef HAVE_LIBC +static const struct vi_alloc stdalloc = { malloc, free, realloc }; +#endif + +struct visor *vi_create(struct vi_alloc *mm) +{ + struct visor *vi; + +#ifdef HAVE_LIBC + if(!mm) mm = &stdalloc; +#else + if(!mm) return 0; +#endif + + if(!(vi = mm->malloc(sizeof *vi))) { + return 0; + } + memset(vi, 0, sizeof *vi); + vi->mm = *mm; + + return vi; +} + +void vi_destroy(struct visor *vi) +{ + while(vi->buflist) { + vi_delete_buf(vi, vi->buflist); + } + vi_free(vi); +} + +void vi_set_fileops(struct visor *vi, struct vi_fileops *fop) +{ + vi->fop = *fop; +} + +struct vi_buffer *vi_new_buf(struct visor *vi, const char *path) +{ + struct vi_buffer *nb; + + if(!(nb = vi_malloc(sizeof *nb))) { + vi_error(vi, "failed to allocate new buffer\n"); + return 0; + } + memset(nb, 0, sizeof *nb); + + if(path) { + if(vi_buf_read(nb, path) == -1) { + vi_free(nb); + return 0; + } + } + + if(vi->buflist) { + struct vi_buffer *last = vi->buflist->prev; + nb->prev = last; + nb->next = vi->buflist; + last->next = nb; + vi->buflist->prev = nb; + } else { + nb->next = nb->prev = nb; + vi->buflist = nb; + } + return nb; +}