Revision d3a583ca

View differences:

output.c
1
#include <unistd.h>
2
#include <stdlib.h>
3
#include <string.h>
1 4
#include <stdint.h>
2 5

  
3 6
#include <chunk.h>
4 7

  
8
#define SIZE 8
9

  
10
static int next_chunk;
11
static int outfd;
12
static int buff_size = SIZE;
13

  
14
static struct {
15
  void *data;
16
  int size;
17
} buff[SIZE];
18

  
19
void buffer_flush(int id)
20
{
21
  int i = id % buff_size;
22

  
23
  while(buff[i].data) {
24
    write(outfd, buff[i].data, buff[i].size);
25
    free(buff[i].data);
26
    buff[i].data = NULL;
27
    i = (i + 1) % buff_size;
28
    next_chunk++;
29
    if (i == id) {
30
      break;
31
    }
32
  }
33
}
34

  
5 35
void output_deliver(const struct chunk *c)
6 36
{
37
  if (c->id == next_chunk) {
38
    write(outfd, c->data, c->size);
39
    next_chunk++;
40
    buffer_flush(next_chunk);
41
  } else {
42
    buffer_flush(c->id);
43
    buff[c->id % buff_size].data = malloc(c->size);
44
    memcpy(buff[c->id % buff_size].data, c->data, c->size);
45
    buff[c->id % buff_size].size = c->size;
46
  }
7 47
}

Also available in: Unified diff