Permalink
Browse files

Don't use buffer structs

  • Loading branch information...
mturk committed Apr 19, 2012
1 parent eefcec3 commit 110b4f93ce04aef22c4095cf3da46b8bc439b20e
@@ -299,24 +299,27 @@ struct file_stream
#else
int _file; /**< cached os file_descriptor */
#endif
- iiobuf _bf; /**< the buffer (at least 1 byte, if !NULL) */
- int _lbfsize; /**< 0 or -_bf._size, for inline putc */
- /* separate buffer for long sequences of ungetc() */
- iiobuf _ub; /**< ungetc buffer */
byte *_up; /**< saved _p when _p is doing ungetc data */
+ byte *_bf_base; /**< the buffer (at least 1 byte, if !NULL) */
+ /* separate buffer for long sequences of ungetc() */
+ byte *_ub_base; /**< ungetc buffer */
+ /* separate buffer for fgetln() when line crosses buffer boundary */
+ byte *_lb_base; /**< buffer for fgetln() */
+ int _lbfsize; /**< 0 or -_bf._size, for inline putc */
+ int _bf_size;
+ int _ub_size;
+ int _lb_size;
+
/* tricks to meet minimum requirements even when malloc() fails */
byte _ubuf[3]; /**< guarantee an ungetc() buffer */
byte _nbuf[1]; /**< guarantee a getc() buffer */
- /* separate buffer for fgetln() when line crosses buffer boundary */
- iiobuf _lb; /**< buffer for fgetln() */
/* Unix stdio files get aligned to block boundaries on fseek() */
int _blksize; /**< stat.st_blksize (may be != _bf._size) */
int _timeout; /**< timeout in milliseconds */
int _type; /**< cached file type */
int _orientation; /**< orientation for fwide() */
acu_off_t _offset; /**< current lseek offset */
mutex *_mutex; /**< used for MT-safety */
- mbstate_t _mbstate; /**< multibyte conversion state */
/* Public low-level api */
/**
@@ -51,12 +51,6 @@ struct uiovec {
int uio_resid;
};
-struct iiobuf
-{
- byte *_base;
- int _size;
-};
-
class descriptor : public refcountable
{
public:
@@ -133,58 +133,48 @@ file_stream *
file_stream_cache::acquire(bool mt_)
{
file_stream *fp = 0;
+ mutex *mp = 0;
scoped_thread_lock __g(_C_mutex);
if (mt_) {
if (!ACU_RING_EMPTY(&_F_cache.free_streams, file_stream, _link)) {
fp = ACU_RING_FIRST(&_F_cache.free_streams);
ACU_RING_REMOVE(fp, _link);
+ mp = fp->_mutex;
_F_free--;
}
}
else {
if (!ACU_RING_EMPTY(&_F_cache.dead_streams, file_stream, _link)) {
fp = ACU_RING_FIRST(&_F_cache.dead_streams);
ACU_RING_REMOVE(fp, _link);
+ mp = fp->_mutex;
_F_free--;
}
}
if (fp == 0) {
- fp = reinterpret_cast<file_stream *>(::calloc(1, sizeof(file_stream)));
+ fp = reinterpret_cast<file_stream *>(::malloc(sizeof(file_stream)));
if (fp == 0)
return 0;
- ACU_RING_ELEM_INIT(fp, _link);
}
- if (mt_ && fp->_mutex == 0) {
+ if (mt_ && mp == 0) {
try {
- fp->_mutex = new thread_mutex();
+ mp = new thread_mutex();
} catch (...) {
memory::free(fp);
ACU_SET_OS_ERROR(ACU_ENOMEM);
return 0;
}
}
+ ::memset(fp, 0, sizeofsizeof(file_stream));
#if defined(_WINDOWS)
fp->_ow.fp = fp;
fp->_fh = INVALID_HANDLE_VALUE;
#else
fp->_file = -1;
#endif
- fp->_p = 0; /* no current pointer */
- fp->_w = 0; /* nothing to read or write */
- fp->_r = 0;
- fp->_bf._base = 0; /* no buffer */
- fp->_bf._size = 0;
- fp->_lbfsize = 0; /* not line buffered */
- fp->_ub._base = 0; /* no ungetc buffer */
- fp->_ub._size = 0;
- fp->_lb._base = 0; /* no line buffer */
- fp->_lb._size = 0;
- fp->_blksize = 0;
- fp->_type = 0;
- fp->_offset = 0;
- fp->_timeout = 0;
-
+ fp->_mutex = mp;
+ ACU_RING_ELEM_INIT(fp, _link);
ACU_RING_INSERT_TAIL(&_F_cache.used_streams, fp, file_stream, _link);
return fp;
}
@@ -198,9 +188,9 @@ file_stream_cache::push_back(file_stream *fp)
SAFE_CLOSE_HANDLE(fp->_ow.overlap.hEvent);
#endif
/* Free line and ungetc buffers */
- if (fp->_ub._base != fp->_ubuf)
- memory::free(fp->_ub._base);
- memory::free(fp->_lb._base);
+ if (fp->_ub_base != fp->_ubuf)
+ memory::free(fp->_ub_base);
+ memory::free(fp->_lb_base);
scoped_thread_lock __g(_C_mutex);
ACU_RING_REMOVE(fp, _link);
@@ -21,22 +21,22 @@
* Test whether the given stdio file has an active ungetc buffer;
* release such a buffer, without restoring ordinary unread data.
*/
-#define HASUB(fp) ((fp)->_ub._base != 0)
+#define HASUB(fp) ((fp)->_ub_base != 0)
#define FREEUB(fp) \
{ \
- if ((fp)->_ub._base != (fp)->_ubuf) \
- ::free((fp)->_ub._base); \
- (fp)->_ub._base = 0; \
+ if ((fp)->_ub_base != (fp)->_ubuf) \
+ ::free((fp)->_ub_base); \
+ (fp)->_ub_base = 0; \
}
/*
* test for an fgetln() buffer.
*/
-#define HASLB(fp) ((fp)->_lb._base != 0)
+#define HASLB(fp) ((fp)->_lb_base != 0)
#define FREELB(fp) \
{ \
- ::free((fp)->_lb._base); \
- (fp)->_lb._base = 0; \
+ ::free((fp)->_lb_base); \
+ (fp)->_lb_base = 0; \
}
/*
@@ -104,15 +104,15 @@ __swsetup(file_stream *fp)
FREEUB(fp);
fp->_flags &= ~(__A_SRD|__A_SEOF);
fp->_r = 0;
- fp->_p = fp->_bf._base;
+ fp->_p = fp->_bf_base;
}
fp->_flags |= __A_SWR;
}
/*
* Make a buffer if necessary, then set _w.
*/
- if (fp->_bf._base == 0)
+ if (fp->_bf_base == 0)
__smakebuf(fp);
if (fp->_flags & __A_SLBF) {
/*
@@ -121,13 +121,13 @@ __swsetup(file_stream *fp)
* to 0 whenever we turn off __SWR.
*/
fp->_w = 0;
- fp->_lbfsize = -fp->_bf._size;
+ fp->_lbfsize = -fp->_bf_size;
}
else {
if (fp->_flags & __A_SNBF)
fp->_w = 0;
else
- fp->_w = fp->_bf._size;
+ fp->_w = fp->_bf_size;
}
return 0;
}
@@ -140,7 +140,7 @@ static inline bool
__prepwrite(file_stream *fp)
{
if (((fp->_flags & __A_SWR) == 0 ||
- (fp->_bf._base == 0 &&
+ (fp->_bf_base == 0 &&
(fp->_flags & __A_SSTR) == 0)) && __swsetup(fp) != 0)
return true;
else
@@ -194,7 +194,7 @@ __srsetup(file_stream *fp)
}
}
- if (fp->_bf._base == 0)
+ if (fp->_bf_base == 0)
__smakebuf(fp);
/*
@@ -215,7 +215,7 @@ __srsetup(file_stream *fp)
if ((fp->_flags & (__A_SLBF|__A_SWR)) == (__A_SLBF|__A_SWR))
__sflush(fp);
}
- fp->_p = fp->_bf._base;
+ fp->_p = fp->_bf_base;
return 0;
}
@@ -268,12 +268,12 @@ __slbexpand(file_stream *fp, int newsize)
void *p;
newsize++;
- if (fp->_lb._size >= newsize)
+ if (fp->_lb_size >= newsize)
return 0;
- if ((p = ::realloc(fp->_lb._base, newsize)) == 0)
+ if ((p = ::realloc(fp->_lb_base, newsize)) == 0)
return -1;
- fp->_lb._base = reinterpret_cast<byte *>(p);
- fp->_lb._size = newsize;
+ fp->_lb_base = reinterpret_cast<byte *>(p);
+ fp->_lb_size = newsize;
return 0;
}
Oops, something went wrong.

0 comments on commit 110b4f9

Please sign in to comment.