source: lib/data-struct/simple_circular_buffer.c @ 25c305d

cachetimestampsdevelopetsiliverc-4.0.4ringdecrementfixringperformance
Last change on this file since 25c305d was 25c305d, checked in by Shane Alcock <salcock@…>, 3 years ago

Add DLLEXPORT to SCB data-struct API

This exposes the symbols for the API in libtrace.so, so it
can be used outside of libtrace itself.

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#define _GNU_SOURCE
2
3#include <sys/mman.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <sys/syscall.h>
9#include <sys/socket.h>
10
11#include "simple_circular_buffer.h"
12
13DLLEXPORT void libtrace_scb_init(libtrace_scb_t *buf, uint32_t size,
14                uint16_t id) {
15
16        char anonname[32];
17
18        if (size % getpagesize() != 0) {
19                size = ((size / getpagesize()) + 1) * getpagesize();
20        }
21
22        snprintf(anonname, 32, "lt_scb_%u", id);
23        buf->fd = syscall(__NR_memfd_create, anonname, 0);
24        ftruncate(buf->fd, size);
25
26        buf->address = mmap(NULL, 2 * size, PROT_NONE,
27                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
28        mmap(buf->address, size, PROT_READ | PROT_WRITE,
29                        MAP_SHARED | MAP_FIXED, buf->fd, 0);
30        mmap(buf->address + size, size, PROT_READ | PROT_WRITE,
31                        MAP_SHARED | MAP_FIXED, buf->fd, 0);
32        buf->read_offset = 0;
33        buf->write_offset = 0;
34        buf->count_bytes = size;
35}
36
37DLLEXPORT void libtrace_scb_destroy(libtrace_scb_t *buf) {
38        munmap(buf->address, buf->count_bytes * 2);
39}
40
41DLLEXPORT int libtrace_scb_recv_sock(libtrace_scb_t *buf, int sock,
42                int recvflags) {
43        int space = buf->count_bytes - (buf->write_offset - buf->read_offset);
44        int ret;
45
46        if (space == 0) {
47                return buf->count_bytes;
48        }
49
50        ret = recv(sock, buf->address + buf->write_offset, space, recvflags);
51        if (ret < 0) {
52                return ret;
53        }
54        buf->write_offset += ret;
55        return (buf->write_offset - buf->read_offset);
56}
57
58DLLEXPORT uint8_t *libtrace_scb_get_read(libtrace_scb_t *buf,
59                uint32_t *available) {
60
61        *available = buf->write_offset - buf->read_offset;
62        return buf->address + buf->read_offset;
63}
64
65DLLEXPORT void libtrace_scb_advance_read(libtrace_scb_t *buf,
66                uint32_t forward) {
67
68        buf->read_offset += forward;
69        if (buf->read_offset >= buf->count_bytes) {
70                buf->read_offset -= buf->count_bytes;
71                buf->write_offset -= buf->count_bytes;
72        }
73}
Note: See TracBrowser for help on using the repository browser.