Skip to content

Commit

Permalink
Apply coding style
Browse files Browse the repository at this point in the history
Format code with "make fmt" (indent must be installed).
  • Loading branch information
rom1v committed Jul 31, 2017
1 parent 77aef64 commit 347e00b
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 267 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ delay: $(OBJ)

$(OBJ): dtbuf.h time_ms.h

fmt:
indent --linux-style \
--indent-level4 \
--no-tabs \
--format-all-comments \
--braces-on-if-line \
--space-after-cast \
*.c *.h
# fix indent breaking pointer alignment on some function signatures
find -name '*.[ch]' -exec sed -i '/^[a-z].*(.*)$$/s/ \* / */' {} ';'

clean:
rm -f $(OBJ) $(OUT)

Expand Down
178 changes: 92 additions & 86 deletions dtbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,115 @@
#define DTBUF_CHUNK_PAYLOAD_SIZE 4000

struct header {
time_ms timestamp;
chunk_length data_length;
time_ms timestamp;
chunk_length data_length;
};

#define DTBUF_CHUNK_SIZE (sizeof (struct header) + DTBUF_CHUNK_PAYLOAD_SIZE)

int dtbuf_init(struct dtbuf *dtbuf, size_t capacity) {
// to avoid splitting a chunk on the circular buffer boundaries, add
// (DTBUF_CHUNK_SIZE-1) bytes at the end: a chunk starting at (capacity-1)
// will still fit
dtbuf->real_capacity = capacity + DTBUF_CHUNK_SIZE - 1;
if (!(dtbuf->data = malloc(dtbuf->real_capacity))) {
return 1;
}
dtbuf->capacity = capacity;
dtbuf->head = 0;
dtbuf->tail = 0;
return 0;
int dtbuf_init(struct dtbuf *dtbuf, size_t capacity)
{
// to avoid splitting a chunk on the circular buffer boundaries, add
// (DTBUF_CHUNK_SIZE-1) bytes at the end: a chunk starting at (capacity-1)
// will still fit
dtbuf->real_capacity = capacity + DTBUF_CHUNK_SIZE - 1;
if (!(dtbuf->data = malloc(dtbuf->real_capacity))) {
return 1;
}
dtbuf->capacity = capacity;
dtbuf->head = 0;
dtbuf->tail = 0;
return 0;
}

void dtbuf_free(struct dtbuf *dtbuf) {
free(dtbuf->data);
void dtbuf_free(struct dtbuf *dtbuf)
{
free(dtbuf->data);
}

int dtbuf_is_empty(struct dtbuf *dtbuf) {
return dtbuf->head == dtbuf->tail;
int dtbuf_is_empty(struct dtbuf *dtbuf)
{
return dtbuf->head == dtbuf->tail;
}

int dtbuf_is_full(struct dtbuf *dtbuf) {
// When dtbuf->head >= dtbuf->capacity, it "cycles" (reset to 0) if and
// only if there is enough space at the start for a full chunk.
// Thus, if dtbuf->head has not cycled while it is after capacity, then the
// buffer is full.
// Else, if head >= tail, there is always enough space (by design).
// Else (if head < tail), there is enough space only if dtbuf->tail is far
// enough (ie we can put a full chunk at the start).
return dtbuf->head >= dtbuf->capacity || (dtbuf->head < dtbuf->tail
&& dtbuf->tail - dtbuf->head <=
DTBUF_CHUNK_SIZE);
int dtbuf_is_full(struct dtbuf *dtbuf)
{
// When dtbuf->head >= dtbuf->capacity, it "cycles" (reset to 0) if and
// only if there is enough space at the start for a full chunk.
// Thus, if dtbuf->head has not cycled while it is after capacity, then the
// buffer is full.
// Else, if head >= tail, there is always enough space (by design).
// Else (if head < tail), there is enough space only if dtbuf->tail is far
// enough (ie we can put a full chunk at the start).
return dtbuf->head >= dtbuf->capacity || (dtbuf->head < dtbuf->tail
&& dtbuf->tail - dtbuf->head <=
DTBUF_CHUNK_SIZE);
}

time_ms dtbuf_next_timestamp(struct dtbuf *dtbuf) {
struct header *header = (struct header *) &dtbuf->data[dtbuf->tail];
return header->timestamp;
time_ms dtbuf_next_timestamp(struct dtbuf *dtbuf)
{
struct header *header = (struct header *) &dtbuf->data[dtbuf->tail];
return header->timestamp;
}

ssize_t dtbuf_write_chunk(struct dtbuf *dtbuf, int fd_in, time_ms timestamp) {
ssize_t r;
struct header header;
// directly write to dtbuf, at the right index
int payload_index = dtbuf->head + sizeof(struct header);
if ((r =
read(fd_in, &dtbuf->data[payload_index],
DTBUF_CHUNK_PAYLOAD_SIZE)) > 0) {
// write headers
header.timestamp = timestamp;
header.data_length = (chunk_length) r;
memcpy(&dtbuf->data[dtbuf->head], &header, sizeof(header));
dtbuf->head = payload_index + r;
if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= DTBUF_CHUNK_SIZE) {
// not enough space at the end of the buffer, cycle if there is enough
// at the start
dtbuf->head = 0;
ssize_t dtbuf_write_chunk(struct dtbuf *dtbuf, int fd_in, time_ms timestamp)
{
ssize_t r;
struct header header;
// directly write to dtbuf, at the right index
int payload_index = dtbuf->head + sizeof(struct header);
if ((r = read(fd_in, &dtbuf->data[payload_index],
DTBUF_CHUNK_PAYLOAD_SIZE)) > 0) {
// write headers
header.timestamp = timestamp;
header.data_length = (chunk_length) r;
memcpy(&dtbuf->data[dtbuf->head], &header, sizeof(header));
dtbuf->head = payload_index + r;
if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= DTBUF_CHUNK_SIZE) {
// not enough space at the end of the buffer, cycle if there is
// enough at the start
dtbuf->head = 0;
}
} else if (r == -1) {
perror("read()");
}
} else if (r == -1) {
perror("read()");
}
return r;
return r;
}

ssize_t dtbuf_read_chunk(struct dtbuf * dtbuf, int fd_out) {
ssize_t w;
struct header *pheader = (struct header *) &dtbuf->data[dtbuf->tail];
struct header header;
chunk_length length = pheader->data_length;
// directly read from dtbuf, at the right index
int payload_index = dtbuf->tail + sizeof(struct header);
if ((w = write(fd_out, &dtbuf->data[payload_index], length)) > 0) {
if (w == length) {
// we succeed to write all the data
dtbuf->tail = payload_index + w;
if (dtbuf->tail >= dtbuf->capacity) {
// the next chunk cannot be after capacity
dtbuf->tail = 0;
if (dtbuf->head >= dtbuf->capacity) {
// can happen if capacity < DTBUF_CHUNK_SIZE
dtbuf->head = 0;
ssize_t dtbuf_read_chunk(struct dtbuf *dtbuf, int fd_out)
{
ssize_t w;
struct header *pheader = (struct header *) &dtbuf->data[dtbuf->tail];
struct header header;
chunk_length length = pheader->data_length;
// directly read from dtbuf, at the right index
int payload_index = dtbuf->tail + sizeof(struct header);
if ((w = write(fd_out, &dtbuf->data[payload_index], length)) > 0) {
if (w == length) {
// we succeed to write all the data
dtbuf->tail = payload_index + w;
if (dtbuf->tail >= dtbuf->capacity) {
// the next chunk cannot be after capacity
dtbuf->tail = 0;
if (dtbuf->head >= dtbuf->capacity) {
// can happen if capacity < DTBUF_CHUNK_SIZE
dtbuf->head = 0;
}
}
} else {
dtbuf->tail += w;
// set the timestamp for writing at the new tail position
header.timestamp = pheader->timestamp;
// set the remaining length
header.data_length = length - w;
memcpy(&dtbuf->data[dtbuf->tail], &header, sizeof(header));
}
}
} else {
dtbuf->tail += w;
// set the timestamp for writing at the new tail position
header.timestamp = pheader->timestamp;
// set the remaining length
header.data_length = length - w;
memcpy(&dtbuf->data[dtbuf->tail], &header, sizeof(header));
}
if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= DTBUF_CHUNK_SIZE) {
// there is enough space at the start now, head can cycle
dtbuf->head = 0;
if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= DTBUF_CHUNK_SIZE) {
// there is enough space at the start now, head can cycle
dtbuf->head = 0;
}
} else if (w == -1) {
perror("write()");
}
} else if (w == -1) {
perror("write()");
}
return w;
return w;
}
10 changes: 5 additions & 5 deletions dtbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
typedef uint16_t chunk_length;

struct dtbuf {
char *data;
size_t capacity; // expected capacity
size_t real_capacity; // capacity + DTBUF_CHUNK_SIZE - 1
int head; // index of the next chunk
int tail; // index of the oldest chunk in memory
char *data;
size_t capacity; // expected capacity
size_t real_capacity; // capacity + DTBUF_CHUNK_SIZE - 1
int head; // index of the next chunk
int tail; // index of the oldest chunk in memory
};

// init a dtbuf structure with an expected capacity
Expand Down
Loading

0 comments on commit 347e00b

Please # to comment.