bufpool.c revision 43ec799dbc8971afca055dd7f8fd479b7b72f338
/* Copyright (c) 2001, Stanford University
* All rights reserved
*
* See the file LICENSE.txt for information on redistributing this software.
*/
#include "cr_mem.h"
#include "cr_error.h"
#include "cr_bufpool.h"
#include <limits.h>
/*
* New (version 1.4) buffer pool implementation.
*
* Now, each buffer in the pool can be a different size.
* We only return a buffer from crBufferPoolPop() if we have exactly
* the right size buffer.
*
* Note: the old implementation had a 'max buffers' parameter but it
* really wasn't good for anything since we always grew the buffer pool
* if we pushed a new buffer that would cause use to exceed the limit.
* That's gone now.
*
* We're just using a simple linked list here. Since we seldom have
* more than about 10-15 buffers in the pool, that's OK. A binary tree
* would be nicer though.
*
* MCH: BufferPoolPop will now return the smallest buffer in the pool that
* is >= to the size required. This fixes BufferPool overruns with lots
* of MTUs.
*/
#ifndef NULL
#define NULL ((void *) 0)
#endif
typedef struct buffer
{
void *address;
unsigned int size;
} Buffer;
struct CRBufferPool_t
{
unsigned int maxBuffers;
int numBuffers;
};
int
{
if ( pool )
return pool->numBuffers;
return 0;
}
int
{
if ( pool )
return pool->maxBuffers;
return 0;
}
crBufferPoolInit( unsigned int maxBuffers )
{
if (pool) {
pool->numBuffers = 0;
}
return pool;
}
void
{
crFree(b);
}
}
void
{
crFree(b);
}
}
void
{
if (b) {
#ifdef DEBUG
/* check that the buffer to push isn't already in the pool! */
{
const Buffer *b;
}
}
#endif
pool->numBuffers++;
}
}
void *
{
int i;
/* we found an exact size match! */
void *p = b->address;
if (prev) {
}
else {
}
crFree(b);
pool->numBuffers--;
return p;
}
/* We found a buffer that's large enough, but keep looking
* for a smaller one that's large enough.
*/
}
}
prev = b;
}
/* we found the smallest buffer whose size is > bytes */
void *p;
if (prev_smallest) {
b = prev_smallest->next;
}
else {
}
p = b->address;
if (prev_smallest) {
}
else {
}
crFree(b);
pool->numBuffers--;
return p;
}
/* found no buffer large enough */
return NULL;
}