]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/ringbuf.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXC_RINGBUF_H
4 #define __LXC_RINGBUF_H
14 * lxc_ringbuf - Implements a simple and efficient memory mapped ringbuffer.
15 * - The "addr" field of struct lxc_ringbuf is considered immutable. Instead the
16 * read and write offsets r_off and w_off are used to calculate the current
17 * read and write addresses. There should never be a need to use any of those
18 * fields directly. Instead use the appropriate helpers below.
19 * - Callers are expected to synchronize read and write accesses to the
23 char *addr
; /* start address of the ringbuffer */
24 uint64_t size
; /* total size of the ringbuffer in bytes */
25 uint64_t r_off
; /* read offset */
26 uint64_t w_off
; /* write offset */
30 * lxc_ringbuf_create - Initialize a new ringbuffer.
32 * @param[in] size Size of the new ringbuffer as a power of 2.
34 __hidden
extern int lxc_ringbuf_create(struct lxc_ringbuf
*buf
, size_t size
);
35 __hidden
extern void lxc_ringbuf_move_read_addr(struct lxc_ringbuf
*buf
, size_t len
);
36 __hidden
extern int lxc_ringbuf_write(struct lxc_ringbuf
*buf
, const char *msg
, size_t len
);
37 __hidden
extern int lxc_ringbuf_read(struct lxc_ringbuf
*buf
, char *out
, size_t *len
);
39 static inline void lxc_ringbuf_release(struct lxc_ringbuf
*buf
)
42 munmap(buf
->addr
, buf
->size
* 2);
45 static inline void lxc_ringbuf_clear(struct lxc_ringbuf
*buf
)
51 static inline uint64_t lxc_ringbuf_used(struct lxc_ringbuf
*buf
)
53 return buf
->w_off
- buf
->r_off
;
56 static inline uint64_t lxc_ringbuf_free(struct lxc_ringbuf
*buf
)
58 return buf
->size
- lxc_ringbuf_used(buf
);
61 static inline char *lxc_ringbuf_get_read_addr(struct lxc_ringbuf
*buf
)
63 return buf
->addr
+ buf
->r_off
;
66 static inline char *lxc_ringbuf_get_write_addr(struct lxc_ringbuf
*buf
)
68 return buf
->addr
+ buf
->w_off
;
71 static inline void lxc_ringbuf_move_write_addr(struct lxc_ringbuf
*buf
, size_t len
)
76 #endif /* __LXC_RINGBUF_H */