-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb.h
61 lines (52 loc) · 1.71 KB
/
rb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2010-2012 Nicholas J. Kain <njkain at gmail dot com>
// SPDX-License-Identifier: MIT
#ifndef NK_RING_BUFFER_H_
#define NK_RING_BUFFER_H_ 1
/*
* Simple ring buffer specialized for gathering entropy. It's not quite
* general since it assumes that the only necessary distinction between
* data within the buffer is filled or unfilled, with no regards to
* actual ordering.
*/
#include <sys/mman.h>
#include "defines.h"
#include "string.h"
typedef struct {
unsigned char buf[RB_SIZE];
unsigned int size; /* max size of the buffer in bytes */
unsigned int bytes; /* current size of the buffer in bytes */
unsigned int index;
unsigned int fill_idx;
} ring_buffer_t;
/* creates a new, empty ring buffer */
static inline void rb_init(ring_buffer_t *rb)
{
rb->size = RB_SIZE;
rb->index = RB_SIZE;
rb->fill_idx = 0;
rb->bytes = 0;
mlock(rb->buf, RB_SIZE);
memset(rb->buf, '\0', RB_SIZE);
}
/* returns number of bytes stored in the ring buffer */
static inline unsigned int rb_num_bytes(ring_buffer_t *rb)
{
if (!rb)
return 0;
return rb->bytes;
}
/* returns 1 if the ring buffer is full or 0 if it is not full */
static inline int rb_is_full(ring_buffer_t *rb)
{
if (!rb || rb->bytes >= rb->size)
return 1;
else
return 0;
}
/* returns 1 if store successful, otherwise 0 (error or not enough room) */
unsigned int rb_store_byte(ring_buffer_t *rb, unsigned char b);
/* returns 1 if store successful, otherwise 0 (error or not enough room) */
unsigned int rb_store_byte_xor(ring_buffer_t *rb, unsigned char b);
/* returns 0 on success, a negative number if not enough bytes or error */
int rb_move(ring_buffer_t *rb, void *buf, unsigned int bytes);
#endif