📄 bget.cpp
字号:
/* Give user buffer starting at queue links. */
buf = (void *) &(b->ql);
return buf;
}
}
b = b->ql.flink; /* Link to next buffer */
}
#ifdef BECtl
/* No buffer available with requested size free. */
/* Don't give up yet -- look in the reserve supply. */
if (acqfcn != NULL) {
if (size > exp_incr - sizeof(struct bhead)) {
/* Request is too large to fit in a single expansion
block. Try to satisy it by a direct buffer acquisition. */
struct bdhead *bdh;
size += sizeof(struct bdhead) - sizeof(struct bhead);
if ((bdh = BDH((*acqfcn)((bufsize) size))) != NULL) {
/* Mark the buffer special by setting the size field
of its header to zero. */
bdh->bh.bsize = 0;
bdh->bh.prevfree = 0;
bdh->tsize = size;
buf = (void *) (bdh + 1);
return buf;
}
} else {
/* Try to obtain a new expansion block */
void *newpool;
if ((newpool = (*acqfcn)((bufsize) exp_incr)) != NULL) {
bpool(newpool, exp_incr);
buf = bget(requested_size); /* This can't, I say, can't
get into a loop. */
return buf;
}
}
}
/* Still no buffer available */
#endif /* BECtl */
return NULL;
}
/* BGETZ -- Allocate a buffer and clear its contents to zero. We clear
the entire contents of the buffer to zero, not just the
region requested by the caller. */
void *BGet::bgetz(bufsize size)
{
char *buf = (char *) bget(size);
if (buf != NULL) {
struct bhead *b;
bufsize rsize;
b = BH(buf - sizeof(struct bhead));
rsize = -(b->bsize);
if (rsize == 0) {
struct bdhead *bd;
bd = BDH(buf - sizeof(struct bdhead));
rsize = bd->tsize - sizeof(struct bdhead);
} else {
rsize -= sizeof(struct bhead);
}
assert(rsize >= size);
V memset(buf, 0, (MemSize) rsize);
}
return ((void *) buf);
}
/* BGETR -- Reallocate a buffer. This is a minimal implementation,
simply in terms of brel() and bget(). It could be
enhanced to allow the buffer to grow into adjacent free
blocks and to avoid moving data unnecessarily. */
void* BGet::bgetr(void *buf, bufsize size)
{
void *nbuf;
bufsize osize; /* Old size of buffer */
struct bhead *b;
if ((nbuf = bget(size)) == NULL) { /* Acquire new buffer */
return NULL;
}
if (buf == NULL) {
return nbuf;
}
b = BH(((char *) buf) - sizeof(struct bhead));
osize = -b->bsize;
#ifdef BECtl
if (osize == 0) {
/* Buffer acquired directly through acqfcn. */
struct bdhead *bd;
bd = BDH(((char *) buf) - sizeof(struct bdhead));
osize = bd->tsize - sizeof(struct bdhead);
} else
#endif
osize -= sizeof(struct bhead);
assert(osize > 0);
V memcpy((char *) nbuf, (char *) buf, /* Copy the data */
(MemSize) ((size < osize) ? size : osize));
brel(buf);
return nbuf;
}
/* BREL -- Release a buffer. */
void BGet::brel(void *buf)
{
struct bfhead *b, *bn;
b = BFH(((char *) buf) - sizeof(struct bhead));
assert(buf != NULL);
/* Buffer size must be negative, indicating that the buffer is
allocated. */
if (b->bh.bsize >= 0) {
bn = NULL;
}
assert(b->bh.bsize < 0);
/* Back pointer in next buffer must be zero, indicating the
same thing: */
assert(BH((char *) b - b->bh.bsize)->prevfree == 0);
/* If the back link is nonzero, the previous buffer is free. */
if (b->bh.prevfree != 0) {
/* The previous buffer is free. Consolidate this buffer with it
by adding the length of this buffer to the previous free
buffer. Note that we subtract the size in the buffer being
released, since it's negative to indicate that the buffer is
allocated. */
register bufsize size = b->bh.bsize;
/* Make the previous buffer the one we're working on. */
assert(BH((char *) b - b->bh.prevfree)->bsize == b->bh.prevfree);
b = BFH(((char *) b) - b->bh.prevfree);
b->bh.bsize -= size;
} else {
/* The previous buffer isn't allocated. Insert this buffer
on the free list as an isolated free block. */
assert(freelist.ql.blink->ql.flink == &freelist);
assert(freelist.ql.flink->ql.blink == &freelist);
b->ql.flink = &freelist;
b->ql.blink = freelist.ql.blink;
freelist.ql.blink = b;
b->ql.blink->ql.flink = b;
b->bh.bsize = -b->bh.bsize;
}
/* Now we look at the next buffer in memory, located by advancing from
the start of this buffer by its size, to see if that buffer is
free. If it is, we combine this buffer with the next one in
memory, dechaining the second buffer from the free list. */
bn = BFH(((char *) b) + b->bh.bsize);
if (bn->bh.bsize > 0) {
/* The buffer is free. Remove it from the free list and add
its size to that of our buffer. */
assert(BH((char *) bn + bn->bh.bsize)->prevfree == bn->bh.bsize);
assert(bn->ql.blink->ql.flink == bn);
assert(bn->ql.flink->ql.blink == bn);
bn->ql.blink->ql.flink = bn->ql.flink;
bn->ql.flink->ql.blink = bn->ql.blink;
b->bh.bsize += bn->bh.bsize;
/* Finally, advance to the buffer that follows the newly
consolidated free block. We must set its backpointer to the
head of the consolidated free block. We know the next block
must be an allocated block because the process of recombination
guarantees that two free blocks will never be contiguous in
memory. */
bn = BFH(((char *) b) + b->bh.bsize);
}
#ifdef FreeWipe
V memset(((char *) b) + sizeof(struct bfhead), 0x55,
(MemSize) (b->bh.bsize - sizeof(struct bfhead)));
#endif
assert(bn->bh.bsize < 0);
/* The next buffer is allocated. Set the backpointer in it to point
to this buffer; the previous free buffer in memory. */
bn->bh.prevfree = b->bh.bsize;
}
#ifdef BECtl
/* BECTL -- Establish automatic pool expansion control */
void BGet::bectl(void *(*acquire) (bufsize size), bufsize pool_incr)
{
acqfcn = acquire;
exp_incr = pool_incr;
}
#endif
/* BPOOL -- Add a region of memory to the buffer pool. */
void BGet::bpool(void *buf, bufsize len)
{
struct bfhead *b = BFH(buf);
struct bhead *bn;
#ifdef SizeQuant
len &= ~(SizeQuant - 1);
#endif
#ifdef BECtl
if (pool_len == 0) {
pool_len = len;
} else if (len != pool_len) {
pool_len = -1;
}
#endif /* BECtl */
/* Since the block is initially occupied by a single free buffer,
it had better not be (much) larger than the largest buffer
whose size we can store in bhead.bsize. */
assert(len - sizeof(struct bhead) <= -((bufsize) ESent + 1));
/* Clear the backpointer at the start of the block to indicate that
there is no free block prior to this one. That blocks
recombination when the first block in memory is released. */
b->bh.prevfree = 0;
/* Chain the new block to the free list. */
assert(freelist.ql.blink->ql.flink == &freelist);
assert(freelist.ql.flink->ql.blink == &freelist);
b->ql.flink = &freelist;
b->ql.blink = freelist.ql.blink;
freelist.ql.blink = b;
b->ql.blink->ql.flink = b;
/* Create a dummy allocated buffer at the end of the pool. This dummy
buffer is seen when a buffer at the end of the pool is released and
blocks recombination of the last buffer with the dummy buffer at
the end. The length in the dummy buffer is set to the largest
negative number to denote the end of the pool for diagnostic
routines (this specific value is not counted on by the actual
allocation and release functions). */
len -= sizeof(struct bhead);
b->bh.bsize = (bufsize) len;
#ifdef FreeWipe
V memset(((char *) b) + sizeof(struct bfhead), 0x55,
(MemSize) (len - sizeof(struct bfhead)));
#endif
bn = BH(((char *) b) + len);
bn->prevfree = (bufsize) len;
/* Definition of ESent assumes two's complement! */
assert((~0) == -1);
bn->bsize = ESent;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -