libio/sebuf.c定義了sebbuf函數(shù):
成都創(chuàng)新互聯(lián)公司是一家專(zhuān)注于成都網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計(jì),麻陽(yáng)網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:麻陽(yáng)等地區(qū)。麻陽(yáng)做網(wǎng)站價(jià)格咨詢(xún):028-86922220
void setbuf (FILE *fp, char *buf)
{
_IO_setbuffer (fp, buf, BUFSIZ);
}
libio/iosetbuffer.c定義了_IO_setbuffer函數(shù):
void _IO_setbuffer (FILE *fp, char *buf, size_t size)
{
CHECK_FILE (fp, );
_IO_acquire_lock (fp);
fp->_flags &= ~_IO_LINE_BUF;
if (!buf)
size = 0;
(void) _IO_SETBUF (fp, buf, size);
if (_IO_vtable_offset (fp) == 0 && fp->_mode == 0 && _IO_CHECK_WIDE (fp))
/* We also have to set the buffer using the wide char function. */
(void) _IO_WSETBUF (fp, buf, size);
_IO_release_lock (fp);
}
除了鎖定FILE指針外,核心代碼就是_IO_SETBUF和用于多字節(jié)環(huán)境的_IO_WSETBUF,最后解析,重點(diǎn)介紹下_IO_SETBUF
libio/libioP.h定義了_IO_SETBUF和_IO_WSETBUF:
typedef FILE* (*_IO_setbuf_t) (FILE *, char *, ssize_t);
#define _IO_SETBUF(FP, BUFFER, LENGTH) JUMP2 (__setbuf, FP, BUFFER, LENGTH)
#define _IO_WSETBUF(FP, BUFFER, LENGTH) WJUMP2 (__setbuf, FP, BUFFER, LENGTH)
#define JUMP2(FUNC, THIS, X1, X2) (_IO_JUMPS_FUNC(THIS)->FUNC) (THIS, X1, X2)
#define WJUMP2(FUNC, THIS, X1, X2) (_IO_WIDE_JUMPS_FUNC(THIS)->FUNC) (THIS, X1, X2)
struct _IO_FILE_plus
{
FILE file;
const struct _IO_jump_t *vtable;
};
1)_IO_JUMPS_FUNC(THIS)是通過(guò)_IO_CAST_FIELD_ACCESS將setbuf函數(shù)的參數(shù)fp轉(zhuǎn)成struct _IO_FILE_plus類(lèi)型,
并返回struct _IO_FILE_plus的vtable變量,通過(guò)vtable調(diào)用__setbuf函數(shù)(JUMP2的參數(shù)),
2)通過(guò)JUMP_INIT (setbuf, _IO_default_setbuf)定義了vtable默認(rèn)的__setbuf函數(shù)
4. libio/genops.c定義了_IO_default_setbuf及其調(diào)用的_IO_setb,_IO_setb設(shè)置了setbuf函數(shù)的參數(shù)fp的_IO_buf_base等buf變量:
FILE * _IO_default_setbuf (FILE *fp, char *p, ssize_t len)
{
if (_IO_SYNC (fp) == EOF)
return NULL;
if (p == NULL || len == 0)
{
fp->_flags |= _IO_UNBUFFERED;
_IO_setb (fp, fp->_shortbuf, fp->_shortbuf+1, 0);
}
else
{
fp->_flags &= ~_IO_UNBUFFERED;
_IO_setb (fp, p, p+len, 0);
}
fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end = 0;
fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_read_end = 0;
return fp;
}
void _IO_setb (FILE *f, char *b, char *eb, int a)
{
if (f->_IO_buf_base && !(f->_flags & _IO_USER_BUF))
free (f->_IO_buf_base);
f->_IO_buf_base = b;
f->_IO_buf_end = eb;
if (a)
f->_flags &= ~_IO_USER_BUF;
else
f->_flags |= _IO_USER_BUF;
}