--- libaitio/src/bufio.c 2012/02/02 21:32:42 1.2 +++ libaitio/src/bufio.c 2013/03/13 14:54:39 1.7 @@ -1,3 +1,48 @@ +/************************************************************************* +* (C) 2011 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: bufio.c,v 1.7 2013/03/13 14:54:39 misho Exp $ +* +************************************************************************** +The ELWIX and AITNET software is distributed under the following +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, 2013 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" @@ -17,16 +62,21 @@ cf_(struct tagBufIO *buf) } if (buf->buf_mode == BUFIO_MODE_INFINIT) - free(buf->buf_base); + e_free(buf->buf_base); else if (buf->buf_unmap) buf->buf_unmap(buf); - free(buf); + e_free(buf); return 0; } +#ifdef __NetBSD__ +static off_t +sf_lim(struct tagBufIO *buf, off_t pos, int w) +#else static fpos_t sf_lim(struct tagBufIO *buf, fpos_t pos, int w) +#endif { if (!buf) goto err; @@ -89,8 +139,13 @@ wf_lim(struct tagBufIO *buf, const char *dat, int siz) return siz; } +#ifdef __NetBSD__ +static off_t +sf_inf(struct tagBufIO *buf, off_t pos, int w) +#else static fpos_t sf_inf(struct tagBufIO *buf, fpos_t pos, int w) +#endif { void *b; @@ -102,7 +157,7 @@ sf_inf(struct tagBufIO *buf, fpos_t pos, int w) if (pos < 0) goto err; if (buf->buf_size < pos) { - b = realloc(buf->buf_base, pos); + b = e_realloc(buf->buf_base, pos); if (!b) { LOGERR; return -1; @@ -118,7 +173,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 = realloc(buf->buf_base, buf->buf_offset + pos); + b = e_realloc(buf->buf_base, buf->buf_offset + pos); if (!b) { LOGERR; return -1; @@ -135,7 +190,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 = realloc(buf->buf_base, buf->buf_size + pos); + b = e_realloc(buf->buf_base, buf->buf_size + pos); if (!b) { LOGERR; return -1; @@ -169,7 +224,7 @@ wf_inf(struct tagBufIO *buf, const char *dat, int siz) } if (buf->buf_offset + siz > buf->buf_size) { - b = realloc(buf->buf_base, buf->buf_offset + siz); + b = e_realloc(buf->buf_base, buf->buf_offset + siz); if (!b) { LOGERR; return -1; @@ -188,7 +243,7 @@ wf_inf(struct tagBufIO *buf, const char *dat, int siz) /* - * io_fmemopen() File buffered stream operations over memory block + * io_fmemopen() - File buffered stream operations over memory block * * @base = Base address of memory block, if =NULL Infinit length(auto-grow) * @basesize = Size of memory block @@ -205,7 +260,7 @@ io_fmemopen(void ** __restrict base, off_t basesize) return NULL; } - buf = malloc(sizeof(struct tagBufIO)); + buf = e_malloc(sizeof(struct tagBufIO)); if (!buf) { LOGERR; return NULL; @@ -213,10 +268,10 @@ io_fmemopen(void ** __restrict base, off_t basesize) memset(buf, 0, sizeof(struct tagBufIO)); if (!*base) { - *base = malloc(basesize); + *base = e_malloc(basesize); if (!*base) { LOGERR; - free(buf); + e_free(buf); return NULL; } else memset(*base, 0, basesize); @@ -228,9 +283,21 @@ io_fmemopen(void ** __restrict base, off_t basesize) buf->buf_base = *base; buf->buf_size = basesize; +#ifdef __NetBSD__ if (buf->buf_mode == BUFIO_MODE_INFINIT) f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, (int (*)(void *, char const *, int)) wf_inf, + (off_t (*)(void *, off_t, int)) sf_inf, + (int (*)(void *)) cf_); + else + f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, + (int (*)(void *, char const *, int)) wf_lim, + (off_t (*)(void *, off_t, int)) sf_lim, + (int (*)(void *)) cf_); +#else + if (buf->buf_mode == BUFIO_MODE_INFINIT) + f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, + (int (*)(void *, char const *, int)) wf_inf, (fpos_t (*)(void *, fpos_t, int)) sf_inf, (int (*)(void *)) cf_); else @@ -238,13 +305,14 @@ io_fmemopen(void ** __restrict base, off_t basesize) (int (*)(void *, char const *, int)) wf_lim, (fpos_t (*)(void *, fpos_t, int)) sf_lim, (int (*)(void *)) cf_); +#endif if (!f) { LOGERR; if (buf->buf_mode == BUFIO_MODE_INFINIT) { - free(*base); + e_free(*base); *base = NULL; } - free(buf); + e_free(buf); return NULL; } @@ -252,7 +320,7 @@ io_fmemopen(void ** __restrict base, off_t basesize) } /* - * io_fmapopen() File buffered stream operations over MMAP block + * io_fmapopen() - File buffered stream operations over MMAP block * * @csFile = Filename for MMAP, if =NULL private MMAP block * @mode = File open mode @@ -305,7 +373,7 @@ io_fmapopen(const char *csFile, int mode, int perm, in } - buf = malloc(sizeof(struct tagBufIO)); + buf = e_malloc(sizeof(struct tagBufIO)); if (!buf) { LOGERR; munmap(base, basesize); @@ -318,13 +386,20 @@ io_fmapopen(const char *csFile, int mode, int perm, in buf->buf_size = basesize; buf->buf_unmap = unmap_cf; +#ifdef __NetBSD__ f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, (int (*)(void *, char const *, int)) wf_lim, + (off_t (*)(void *, off_t, int)) sf_lim, + (int (*)(void *)) cf_); +#else + f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, + (int (*)(void *, char const *, int)) wf_lim, (fpos_t (*)(void *, fpos_t, int)) sf_lim, (int (*)(void *)) cf_); +#endif if (!f) { LOGERR; - free(buf); + e_free(buf); munmap(base, basesize); return NULL; } @@ -333,7 +408,7 @@ io_fmapopen(const char *csFile, int mode, int perm, in } /* - * io_dumbFile() Create empry or dumb file with fixed size + * io_dumbFile() - Create empry or dumb file with fixed size * * @csFile = Filename for create * @mode = File access permissions @@ -366,7 +441,7 @@ err: } /* - * io_fd2buf() Convert open file handle to buffered file I/O + * io_fd2buf() - Convert open file handle to buffered file I/O * * @fd = File handle * @mode = Permissions for new buffered file I/O