Diff for /libaitio/src/bufio.c between versions 1.3 and 1.10

version 1.3, 2012/03/15 01:52:23 version 1.10, 2016/08/18 09:06:31
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012Copyright 2004 - 2016
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 62  cf_(struct tagBufIO *buf) Line 62  cf_(struct tagBufIO *buf)
         }          }
   
         if (buf->buf_mode == BUFIO_MODE_INFINIT)          if (buf->buf_mode == BUFIO_MODE_INFINIT)
                free(buf->buf_base);                e_free(buf->buf_base);
         else if (buf->buf_unmap)          else if (buf->buf_unmap)
                 buf->buf_unmap(buf);                  buf->buf_unmap(buf);
   
        free(buf);        e_free(buf);
         return 0;          return 0;
 }  }
   
   #ifndef __linux__
   #ifdef __NetBSD__
   static off_t
   sf_lim(struct tagBufIO *buf, off_t pos, int w)
   #else
 static fpos_t  static fpos_t
 sf_lim(struct tagBufIO *buf, fpos_t pos, int w)  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)          if (!buf)
                 goto err;                  goto err;
   
Line 134  wf_lim(struct tagBufIO *buf, const char *dat, int siz) Line 148  wf_lim(struct tagBufIO *buf, const char *dat, int siz)
         return siz;          return siz;
 }  }
   
   #ifndef __linux__
   #ifdef __NetBSD__
   static off_t
   sf_inf(struct tagBufIO *buf, off_t pos, int w)
   #else
 static fpos_t  static fpos_t
 sf_inf(struct tagBufIO *buf, fpos_t pos, int w)  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;          void *b;
   #ifdef __linux__
           off_t pos = (off_t) *ppos;
   #endif
   
         if (!buf)          if (!buf)
                 goto err;                  goto err;
Line 147  sf_inf(struct tagBufIO *buf, fpos_t pos, int w) Line 174  sf_inf(struct tagBufIO *buf, fpos_t pos, int w)
                         if (pos < 0)                          if (pos < 0)
                                 goto err;                                  goto err;
                         if (buf->buf_size < pos) {                          if (buf->buf_size < pos) {
                                b = realloc(buf->buf_base, pos);                                b = e_realloc(buf->buf_base, pos);
                                 if (!b) {                                  if (!b) {
                                         LOGERR;                                          LOGERR;
                                         return -1;                                          return -1;
Line 163  sf_inf(struct tagBufIO *buf, fpos_t pos, int w) Line 190  sf_inf(struct tagBufIO *buf, fpos_t pos, int w)
                         if ((buf->buf_offset + pos) < 0)                          if ((buf->buf_offset + pos) < 0)
                                 goto err;                                  goto err;
                         if (buf->buf_size < (buf->buf_offset + pos)) {                          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) {                                  if (!b) {
                                         LOGERR;                                          LOGERR;
                                         return -1;                                          return -1;
Line 180  sf_inf(struct tagBufIO *buf, fpos_t pos, int w) Line 207  sf_inf(struct tagBufIO *buf, fpos_t pos, int w)
                         if ((buf->buf_size + pos) < 0)                          if ((buf->buf_size + pos) < 0)
                                 goto err;                                  goto err;
                         if (buf->buf_size < (buf->buf_size + pos)) {                          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) {                                  if (!b) {
                                         LOGERR;                                          LOGERR;
                                         return -1;                                          return -1;
Line 214  wf_inf(struct tagBufIO *buf, const char *dat, int siz) Line 241  wf_inf(struct tagBufIO *buf, const char *dat, int siz)
         }          }
   
         if (buf->buf_offset + siz > buf->buf_size) {          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) {                  if (!b) {
                         LOGERR;                          LOGERR;
                         return -1;                          return -1;
Line 233  wf_inf(struct tagBufIO *buf, const char *dat, int siz) Line 260  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)   * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
  * @basesize = Size of memory block   * @basesize = Size of memory block
Line 244  io_fmemopen(void ** __restrict base, off_t basesize) Line 271  io_fmemopen(void ** __restrict base, off_t basesize)
 {  {
         FILE *f = NULL;          FILE *f = NULL;
         struct tagBufIO *buf;          struct tagBufIO *buf;
   #ifdef __linux__
           cookie_io_functions_t cookie;
   #endif
   
         if (!base) {          if (!base) {
                 io_SetErr(EINVAL, "Invalid base argument ...");                  io_SetErr(EINVAL, "Invalid base argument ...");
                 return NULL;                  return NULL;
         }          }
   
        buf = malloc(sizeof(struct tagBufIO));        buf = e_malloc(sizeof(struct tagBufIO));
         if (!buf) {          if (!buf) {
                 LOGERR;                  LOGERR;
                 return NULL;                  return NULL;
Line 258  io_fmemopen(void ** __restrict base, off_t basesize) Line 288  io_fmemopen(void ** __restrict base, off_t basesize)
                 memset(buf, 0, sizeof(struct tagBufIO));                  memset(buf, 0, sizeof(struct tagBufIO));
   
         if (!*base) {          if (!*base) {
                *base = malloc(basesize);                *base = e_malloc(basesize);
                 if (!*base) {                  if (!*base) {
                         LOGERR;                          LOGERR;
                        free(buf);                        e_free(buf);
                         return NULL;                          return NULL;
                 } else                  } else
                         memset(*base, 0, basesize);                          memset(*base, 0, basesize);
Line 273  io_fmemopen(void ** __restrict base, off_t basesize) Line 303  io_fmemopen(void ** __restrict base, off_t basesize)
         buf->buf_base = *base;          buf->buf_base = *base;
         buf->buf_size = basesize;          buf->buf_size = basesize;
   
   #ifndef __linux__
   #ifdef __NetBSD__
         if (buf->buf_mode == BUFIO_MODE_INFINIT)          if (buf->buf_mode == BUFIO_MODE_INFINIT)
                 f = funopen(buf, (int (*)(void *, char *, int)) rf_lim,                   f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, 
                                 (int (*)(void *, char const *, int)) wf_inf,                                   (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,                                   (fpos_t (*)(void *, fpos_t, int)) sf_inf, 
                                 (int (*)(void *)) cf_);                                  (int (*)(void *)) cf_);
         else          else
Line 283  io_fmemopen(void ** __restrict base, off_t basesize) Line 326  io_fmemopen(void ** __restrict base, off_t basesize)
                                 (int (*)(void *, char const *, int)) wf_lim,                                   (int (*)(void *, char const *, int)) wf_lim, 
                                 (fpos_t (*)(void *, fpos_t, int)) sf_lim,                                   (fpos_t (*)(void *, fpos_t, int)) sf_lim, 
                                 (int (*)(void *)) cf_);                                  (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) {          if (!f) {
                 LOGERR;                  LOGERR;
                 if (buf->buf_mode == BUFIO_MODE_INFINIT) {                  if (buf->buf_mode == BUFIO_MODE_INFINIT) {
                        free(*base);                        e_free(*base);
                         *base = NULL;                          *base = NULL;
                 }                  }
                free(buf);                e_free(buf);
                 return NULL;                  return NULL;
         }          }
   
Line 297  io_fmemopen(void ** __restrict base, off_t basesize) Line 356  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   * @csFile = Filename for MMAP, if =NULL private MMAP block
  * @mode = File open mode   * @mode = File open mode
Line 315  io_fmapopen(const char *csFile, int mode, int perm, in Line 374  io_fmapopen(const char *csFile, int mode, int perm, in
         void *base;          void *base;
         off_t basesize;          off_t basesize;
         int fd = -1;          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) {          if (csFile) {
                 fd = open(csFile, mode, perm);                  fd = open(csFile, mode, perm);
Line 339  io_fmapopen(const char *csFile, int mode, int perm, in Line 406  io_fmapopen(const char *csFile, int mode, int perm, in
                         close(fd);                          close(fd);
         } else if (offset) {          } else if (offset) {
                 basesize = 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) {                  if (base == MAP_FAILED) {
                         LOGERR;                          LOGERR;
                         return NULL;                          return NULL;
Line 350  io_fmapopen(const char *csFile, int mode, int perm, in Line 417  io_fmapopen(const char *csFile, int mode, int perm, in
         }          }
   
   
        buf = malloc(sizeof(struct tagBufIO));        buf = e_malloc(sizeof(struct tagBufIO));
         if (!buf) {          if (!buf) {
                 LOGERR;                  LOGERR;
                 munmap(base, basesize);                  munmap(base, basesize);
Line 363  io_fmapopen(const char *csFile, int mode, int perm, in Line 430  io_fmapopen(const char *csFile, int mode, int perm, in
         buf->buf_size = basesize;          buf->buf_size = basesize;
         buf->buf_unmap = unmap_cf;          buf->buf_unmap = unmap_cf;
   
   #ifndef __linux__
   #ifdef __NetBSD__
         f = funopen(buf, (int (*)(void *, char *, int)) rf_lim,           f = funopen(buf, (int (*)(void *, char *, int)) rf_lim, 
                         (int (*)(void *, char const *, int)) wf_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,                           (fpos_t (*)(void *, fpos_t, int)) sf_lim, 
                         (int (*)(void *)) cf_);                          (int (*)(void *)) cf_);
   #endif
   #else
           f = fopencookie(buf, "r+", cookie);
   #endif
         if (!f) {          if (!f) {
                 LOGERR;                  LOGERR;
                free(buf);                e_free(buf);
                 munmap(base, basesize);                  munmap(base, basesize);
                 return NULL;                  return NULL;
         }          }
Line 378  io_fmapopen(const char *csFile, int mode, int perm, in Line 456  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   * @csFile = Filename for create
  * @mode = File access permissions   * @mode = File access permissions
Line 411  err: Line 489  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   * @fd = File handle
  * @mode = Permissions for new buffered file I/O   * @mode = Permissions for new buffered file I/O
  * return: NULL error or open buffered file   * return: NULL error or open buffered file
  */   */
inline FILE *FILE *
 io_fd2buf(int fd, const char *mode)  io_fd2buf(int fd, const char *mode)
 {  {
         FILE *f;          FILE *f;

Removed from v.1.3  
changed lines
  Added in v.1.10


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>