Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document last changes
[simgrid.git] / src / nws_portability / Forecast / fbuff.c
1 /* $Id$ */
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <math.h>
7
8 #include "fbuff.h"
9
10 fbuff
11 InitFBuff(int size)
12 {
13         fbuff lb;
14
15         lb = (fbuff)malloc(FBUFF_SIZE);
16
17         if(lb == NULL)
18         {
19                 fprintf(stderr,"InitFBuff: couldn't get space for fbuff\n");
20                 fflush(stderr);
21                 return(NULL);
22         }
23
24         /*
25          * count from bottom to top
26          *
27          * head is the next available space
28          * tail is the last valid data item
29          */
30         lb->size = size;
31         lb->head = size - 1;
32         lb->tail = 0;
33
34         lb->vals = (double *)malloc(size*sizeof(double));
35         if(lb->vals == NULL)
36         {
37                 fprintf(stderr,"InitFBuff: couldn't get space for vals\n");
38                 fflush(stderr);
39                 free(lb);
40                 return(NULL);
41         }
42
43         return(lb);
44
45 }
46
47 void
48 FreeFBuff(fbuff fb)
49 {
50         free(fb->vals);
51         free(fb);
52         
53         return;
54 }
55
56 void
57 UpdateFBuff(fbuff fb, double val)
58 {
59         F_HEAD(fb) = val;
60         fb->head = MODMINUS(fb->head,1,fb->size);
61         
62         /*
63          * if we have moved the head over the tail, bump the tail around too
64          */
65         if(fb->head == fb->tail)
66         {
67                 fb->tail = MODMINUS(fb->tail,1,fb->size);
68         }
69         
70         return;
71 }
72