frames.c

来自「掌握如何用C来实现各种算法」· C语言 代码 · 共 97 行

C
97
字号
/*****************************************************************************
*                                                                            *
*  ------------------------------- frames.c -------------------------------  *
*                                                                            *
*****************************************************************************/

#include <stdlib.h>

#include "frames.h"
#include "list.h"

/*****************************************************************************
*                                                                            *
*  ------------------------------ alloc_frame -----------------------------  *
*                                                                            *
*****************************************************************************/

int alloc_frame(List *frames) {

int                frame_number,
                   *data;

if (list_size(frames) == 0)

   /**************************************************************************
   *                                                                         *
   *  Return that there are no frames available.                             *
   *                                                                         *
   **************************************************************************/

   return -1;

else {

   if (list_rem_next(frames, NULL, (void **)&data) != 0)

      /***********************************************************************
      *                                                                      *
      *  Return that a frame could not be retrieved.                         *
      *                                                                      *
      ***********************************************************************/

      return -1;

   else {

      /***********************************************************************
      *                                                                      *
      *  Store the number of the available frame.                            *
      *                                                                      *
      ***********************************************************************/

      frame_number = *data;
      free(data);

   }

}

return frame_number;

}

/*****************************************************************************
*                                                                            *
*  ------------------------------ free_frame ------------------------------  *
*                                                                            *
*****************************************************************************/

int free_frame(List *frames, int frame_number) {

int                *data;

/*****************************************************************************
*                                                                            *
*  Allocate storage for the frame number.                                    *
*                                                                            *
*****************************************************************************/

if ((data = (int *)malloc(sizeof(int))) == NULL)
   return -1;

/*****************************************************************************
*                                                                            *
*  Put the frame back in the list of available frames.                       *
*                                                                            *
*****************************************************************************/

*data = frame_number;

if (list_ins_next(frames, NULL, data) != 0)
   return -1;

return 0;

}

⌨️ 快捷键说明

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