⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufpool.c

📁 基于linux环境的ns2多机并行仿真补丁
💻 C
字号:
/*---------------------------------------------------------------------------*//* Buffer pool implementation.                                               *//* Author(s): Richard Fujimoto.                                              *//* $Revision: 1.1 $ $Name: v26apr05 $ $Date: 2003/04/11 20:17:37 $ *//*---------------------------------------------------------------------------*/#include <stdlib.h>#include <string.h>#include <stdio.h>#include "bufpool.h"/* minimum buffer size to hold free list header info */#define MIN_BUFFER_SIZE (sizeof (struct MB_Buffer))/************************************************************************ * * MEMORY BUFFER MANAGER * Create a pool of fixed size buffers. * Interface: * Types: *   MB_BufferPool: pointer to a buffer pool * Procedures: *   MB_BufferPool MB_MakePool (int NBuffers, int BufferSize); *     create a new buffer pool with NBuffers buffers, each with BufferSize *     bytes.  Returns a handle to the buffer pool.  Return NULL if the *     allocation failed. *   ADDR_TYPE MB_GetBuffer(MB_BufferPool Pool) *     Get a buffer from buffer pool Pool.  Return a pointer to the buffer, *     or NULL if the pool is empty. *   void MB_FreeBuffer (MB_BufferPool Pool, ADDR_TYPE Buffer) *     Return buffer pointed to by Buffer to buffer pool Pool * ***********************************************************************/#define IAMABUFFERPOOL 1234567struct MB_Buffer{  struct MB_Buffer *Next;	/* pointer to next free buffer in list */};struct MB_Header {  int32 Signature;		/* flag indicating buffer pool header */  struct MB_Buffer *Free;};/************************************************************************ * * MB_BufferPool MB_MakePool (int NBuffers, int BufferSize) * create a new buffer pool with NBuffers buffers, each with BufferSize * bytes.  Returns a handle to the buffer pool.  Return NULL if the * allocation failed. * ***********************************************************************/MB_BufferPool MB_MakePool (int NBuffers, int BufferSize){  int i;  struct MB_Header *head;  struct MB_Buffer *buf;  /* adjust buffer size so data part an integer # 32 bit words */  if (BufferSize < MIN_BUFFER_SIZE)    BufferSize = MIN_BUFFER_SIZE;  if (BufferSize%4 != 0) BufferSize = (BufferSize+4) - (BufferSize%4);  /* allocate a collection of buffers, string them into a free list */  head = (struct MB_Header *) malloc (sizeof (struct MB_Header));  if (!head)  {    return (NULL);  }  head->Signature = IAMABUFFERPOOL;  /* flag type of memory */  head->Free = NULL;  for (i=0; i<NBuffers; i++)  {    buf = (struct MB_Buffer *) malloc(BufferSize);    if (!buf)    {      /* return memory allocated thus far */      for (buf=head->Free; buf != NULL; buf=buf->Next)        free (buf);      free (head);      return (NULL);    }    buf->Next = head->Free;    head->Free = buf;  }  return (head);}/************************************************************************ * * ADDR_TYPE MB_GetBuffer(MB_BufferPool Pool) * Get a buffer from buffer pool Pool.  Return a pointer to the buffer, * or NULL if the pool is empty. * ***********************************************************************/ADDR_TYPE MB_GetBuffer(MB_BufferPool Pool){  struct MB_Buffer *buf;  if (Pool->Signature != IAMABUFFERPOOL)  {    fprintf (stderr, "MB_GetBuffer: illegal buffer pool pointer specified\n");    exit (1);  }  if (Pool->Free == NULL) return (NULL);  buf = Pool->Free;  Pool->Free = buf->Next;  buf->Next = NULL;	/* clear to prevent misuse */  return ((ADDR_TYPE) buf);}/************************************************************************ * * void MB_FreeBuffer (MB_BufferPool Pool, ADDR_TYPE Buffer) * Return buffer pointed to by Buffer to buffer pool Pool * ***********************************************************************/void MB_FreeBuffer (MB_BufferPool Pool, ADDR_TYPE Buffer){  struct MB_Buffer *buf;  if( !Buffer )      return;  if (!Pool || Pool->Signature != IAMABUFFERPOOL)  {    fprintf (stderr, "MB_FreeBuffer: illegal buffer pool pointer specified\n");    exit (1);  }  buf = (struct MB_Buffer *) Buffer;  buf->Next = Pool->Free;  Pool->Free = buf;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -