Statistics
| Branch: | Revision:

streamers / output.c @ 74ff12bb

History | View | Annotate | Download (2.19 KB)

1 8fed7779 CsabaKiraly
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7 d3a583ca Luca
#include <unistd.h>
8
#include <stdlib.h>
9
#include <string.h>
10 89e893e2 Luca
#include <stdint.h>
11 c792c999 Luca
#include <stdio.h>
12 89e893e2 Luca
13
#include <chunk.h>
14
15 54b631d9 Luca Abeni
#include "out-stream.h"
16 c792c999 Luca
#include "dbg.h"
17
18 d3a583ca Luca
#define SIZE 8
19
20
static int next_chunk;
21
static int buff_size = SIZE;
22
23
static struct {
24
  void *data;
25
  int size;
26 c792c999 Luca
  int id;
27 d3a583ca Luca
} buff[SIZE];
28
29 c792c999 Luca
void buffer_free(int i)
30
{
31 298a8869 Luca Abeni
  dprintf("\t\tFlush Buf %d: %s\n", i, buff[i].data);
32 a6ac9440 Luca Abeni
  chunk_write(buff[i].id, buff[i].data, buff[i].size);
33 c792c999 Luca
  free(buff[i].data);
34
  buff[i].data = NULL;
35 b76a7006 Luca
  dprintf("Next Chunk: %d -> %d\n", next_chunk, buff[i].id + 1);
36 c792c999 Luca
  next_chunk = buff[i].id + 1;
37
}
38
39 d3a583ca Luca
void buffer_flush(int id)
40
{
41
  int i = id % buff_size;
42
43
  while(buff[i].data) {
44 c792c999 Luca
    buffer_free(i);
45 d3a583ca Luca
    i = (i + 1) % buff_size;
46 c792c999 Luca
    if (i == id % buff_size) {
47 d3a583ca Luca
      break;
48
    }
49
  }
50
}
51
52 89e893e2 Luca
void output_deliver(const struct chunk *c)
53
{
54 c792c999 Luca
  dprintf("Chunk %d delivered\n", c->id);
55
  if (c->id < next_chunk) {
56
    return;
57
  }
58
59
  if (c->id >= next_chunk + buff_size) {
60
    int i;
61
62
    /* We might need some space for storing this chunk,
63
     * or the stored chunks are too old
64
     */
65
    for (i = next_chunk; i <= c->id - buff_size; i++) {
66
      if (buff[i % buff_size].data) {
67
        buffer_free(i % buff_size);
68
      } else {
69
        next_chunk++;
70
      }
71
    }
72
    buffer_flush(next_chunk);
73
    dprintf("Next is now %d, chunk is %d\n", next_chunk, c->id);
74
  }
75
76
  dprintf("%d == %d?\n", c->id, next_chunk);
77 d3a583ca Luca
  if (c->id == next_chunk) {
78 298a8869 Luca Abeni
    dprintf("\tOut Chunk[%d] - %d: %s\n", c->id, c->id % buff_size, c->data);
79 54b631d9 Luca Abeni
    chunk_write(c->id, c->data, c->size);
80 d3a583ca Luca
    next_chunk++;
81
    buffer_flush(next_chunk);
82
  } else {
83 c792c999 Luca
    dprintf("Storing %d (in %d)\n", c->id, c->id % buff_size);
84 e8654707 Luca
    if (buff[c->id % buff_size].data) {
85
      if (buff[c->id % buff_size].id == c->id) {
86
        /* Duplicate of a stored chunk */
87
        return;
88
      }
89 c792c999 Luca
      fprintf(stderr, "Crap!\n");
90
      exit(-1);
91
    }
92
    /* We previously flushed, so we know that c->id is free */
93 d3a583ca Luca
    buff[c->id % buff_size].data = malloc(c->size);
94
    memcpy(buff[c->id % buff_size].data, c->data, c->size);
95
    buff[c->id % buff_size].size = c->size;
96 c792c999 Luca
    buff[c->id % buff_size].id = c->id;
97 d3a583ca Luca
  }
98 89e893e2 Luca
}