--- libaitio/src/bufio.c 2012/05/23 11:49:35 1.5.6.1 +++ libaitio/src/bufio.c 2016/08/18 09:06:31 1.10 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: bufio.c,v 1.5.6.1 2012/05/23 11:49:35 misho Exp $ +* $Id: bufio.c,v 1.10 2016/08/18 09:06:31 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 +Copyright 2004 - 2016 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -62,14 +62,15 @@ cf_(struct tagBufIO *buf) } if (buf->buf_mode == BUFIO_MODE_INFINIT) - xfree(buf->buf_base); + e_free(buf->buf_base); else if (buf->buf_unmap) buf->buf_unmap(buf); - xfree(buf); + e_free(buf); return 0; } +#ifndef __linux__ #ifdef __NetBSD__ static off_t sf_lim(struct tagBufIO *buf, off_t pos, int w) @@ -77,7 +78,15 @@ sf_lim(struct tagBufIO *buf, off_t pos, int w) static fpos_t sf_lim(struct tagBufIO *buf, fpos_t pos, int w) #endif +#else +static off_t +sf_lim(struct tagBufIO *buf, off64_t *ppos, int w) +#endif { +#ifdef __linux__ + off_t pos = (off_t) *ppos; +#endif + if (!buf) goto err; @@ -139,6 +148,7 @@ wf_lim(struct tagBufIO *buf, const char *dat, int siz) return siz; } +#ifndef __linux__ #ifdef __NetBSD__ static off_t sf_inf(struct tagBufIO *buf, off_t pos, int w) @@ -146,8 +156,15 @@ sf_inf(struct tagBufIO *buf, off_t pos, int w) static fpos_t sf_inf(struct tagBufIO *buf, fpos_t pos, int w) #endif +#else +static off_t +sf_inf(struct tagBufIO *buf, off64_t *ppos, int w) +#endif { void *b; +#ifdef __linux__ + off_t pos = (off_t) *ppos; +#endif if (!buf) goto err; @@ -157,7 +174,7 @@ sf_inf(struct tagBufIO *buf, fpos_t pos, int w) if (pos < 0) goto err; if (buf->buf_size < pos) { - b = xrealloc(buf->buf_base, pos); + b = e_realloc(buf->buf_base, pos); if (!b) { LOGERR; return -1; @@ -173,7 +190,7 @@ sf_inf(struct tagBufIO *buf, fpos_t pos, int w) if ((buf->buf_offset + pos) < 0) goto err; if (buf->buf_size < (buf->buf_offset + pos)) { - b = xrealloc(buf->buf_base, buf->buf_offset + pos); + b = e_realloc(buf->buf_base, buf->buf_offset + pos); if (!b) { LOGERR; return -1; @@ -190,7 +207,7 @@ sf_inf(struct tagBufIO *buf, fpos_t pos, int w) if ((buf->buf_size + pos) < 0) goto err; if (buf->buf_size < (buf->buf_size + pos)) { - b = xrealloc(buf->buf_base, buf->buf_size + pos); + b = e_realloc(buf->buf_base, buf->buf_size + pos); if (!b) { LOGERR; return -1; @@ -224,7 +241,7 @@ wf_inf(struct tagBufIO *buf, const char *dat, int siz) } if (buf->buf_offset + siz > buf->buf_size) { - b = xrealloc(buf->buf_base, buf->buf_offset + siz); + b = e_realloc(buf->buf_base, buf->buf_offset + siz); if (!b) { LOGERR; return -1; @@ -254,13 +271,16 @@ io_fmemopen(void ** __restrict base, off_t basesize) { FILE *f = NULL; struct tagBufIO *buf; +#ifdef __linux__ + cookie_io_functions_t cookie; +#endif if (!base) { io_SetErr(EINVAL, "Invalid base argument ..."); return NULL; } - buf = xmalloc(sizeof(struct tagBufIO)); + buf = e_malloc(sizeof(struct tagBufIO)); if (!buf) { LOGERR; return NULL; @@ -268,10 +288,10 @@ io_fmemopen(void ** __restrict base, off_t basesize) memset(buf, 0, sizeof(struct tagBufIO)); if (!*base) { - *base = xmalloc(basesize); + *base = e_malloc(basesize); if (!*base) { LOGERR; - xfree(buf); + e_free(buf); return NULL; } else memset(*base, 0, basesize); @@ -283,6 +303,7 @@ io_fmemopen(void ** __restrict base, off_t basesize) buf->buf_base = *base; buf->buf_size = basesize; +#ifndef __linux__ #ifdef __NetBSD__ if (buf->buf_mode == BUFIO_MODE_INFINIT) f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, @@ -306,13 +327,28 @@ io_fmemopen(void ** __restrict base, off_t basesize) (fpos_t (*)(void *, fpos_t, int)) sf_lim, (int (*)(void *)) cf_); #endif +#else + if (buf->buf_mode == BUFIO_MODE_INFINIT) { + cookie.read = (cookie_read_function_t*) rf_lim; + cookie.write = (cookie_write_function_t*) wf_inf; + cookie.seek = (cookie_seek_function_t*) sf_inf; + cookie.close = (cookie_close_function_t*) cf_; + } else { + cookie.read = (cookie_read_function_t*) rf_lim; + cookie.write = (cookie_write_function_t*) wf_lim; + cookie.seek = (cookie_seek_function_t*) sf_lim; + cookie.close = (cookie_close_function_t*) cf_; + } + + f = fopencookie(buf, "r+", cookie); +#endif if (!f) { LOGERR; if (buf->buf_mode == BUFIO_MODE_INFINIT) { - xfree(*base); + e_free(*base); *base = NULL; } - xfree(buf); + e_free(buf); return NULL; } @@ -338,6 +374,14 @@ io_fmapopen(const char *csFile, int mode, int perm, in void *base; off_t basesize; int fd = -1; +#ifdef __linux__ + cookie_io_functions_t cookie = { + .read = (cookie_read_function_t*) rf_lim, + .write = (cookie_write_function_t*) wf_lim, + .seek = (cookie_seek_function_t*) sf_lim, + .close = (cookie_close_function_t*) cf_ + }; +#endif if (csFile) { fd = open(csFile, mode, perm); @@ -362,7 +406,7 @@ io_fmapopen(const char *csFile, int mode, int perm, in close(fd); } else if (offset) { basesize = offset; - base = mmap(NULL, basesize, prot, MAP_ANON, -1, 0); + base = mmap(NULL, basesize, prot, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (base == MAP_FAILED) { LOGERR; return NULL; @@ -373,7 +417,7 @@ io_fmapopen(const char *csFile, int mode, int perm, in } - buf = xmalloc(sizeof(struct tagBufIO)); + buf = e_malloc(sizeof(struct tagBufIO)); if (!buf) { LOGERR; munmap(base, basesize); @@ -386,6 +430,7 @@ io_fmapopen(const char *csFile, int mode, int perm, in buf->buf_size = basesize; buf->buf_unmap = unmap_cf; +#ifndef __linux__ #ifdef __NetBSD__ f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, (int (*)(void *, char const *, int)) wf_lim, @@ -397,9 +442,12 @@ io_fmapopen(const char *csFile, int mode, int perm, in (fpos_t (*)(void *, fpos_t, int)) sf_lim, (int (*)(void *)) cf_); #endif +#else + f = fopencookie(buf, "r+", cookie); +#endif if (!f) { LOGERR; - xfree(buf); + e_free(buf); munmap(base, basesize); return NULL; } @@ -447,7 +495,7 @@ err: * @mode = Permissions for new buffered file I/O * return: NULL error or open buffered file */ -inline FILE * +FILE * io_fd2buf(int fd, const char *mode) { FILE *f;