blob: 91fa02b8fb280634a8168192e9272ff40186b511 [file] [log] [blame]
Nigel Taofc662072019-02-02 08:01:01 +11001// Copyright 2019 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// ----------------
16
17// This program demonstrates mmap'ing a ring-buffer's N bytes of physical
18// memory three times, to be a contiguous block of 3*N bytes. The three
19// pointers (base + 0*N + i), (base + 1*N + i) and (base + 2*N + i), which are
20// different addresses in virtual memory, all alias the same physical address.
21//
22// Reading or writing a chunk of length M <= N is therefore a simple memcpy,
23// without having to explicitly wrap around the ring-buffer boundaries.
24//
25// This is similar to the technique discussed in
26// https://lo.calho.st/quick-hacks/employing-black-magic-in-the-linux-page-table/
27//
28// This program differs from that web page's discussion by mapping the physical
29// memory three times, not just two. This lets us read or write, implicitly
30// wrapping, both forwards (after the middle mapping's end) and backwards
31// (before the middle mapping's start). That web page only considers forwards
32// reads or writes. Backwards reads are useful when decoding a Lempel-Ziv style
33// compression format, copying from history (recently decoded bytes).
34//
35// Its output should be:
36//
37// middle[-8] == 0x00 == 0x00 == middle[131064]
38// middle[-7] == 0x00 == 0x00 == middle[131065]
39// middle[-6] == 0x00 == 0x00 == middle[131066]
40// middle[-5] == 0x00 == 0x00 == middle[131067]
41// middle[-4] == 0x00 == 0x00 == middle[131068]
42// middle[-3] == 0x00 == 0x00 == middle[131069]
43// middle[-2] == 0x20 == 0x20 == middle[131070]
44// middle[-1] == 0x21 == 0x21 == middle[131071]
45// middle[ 0] == 0x22 == 0x22 == middle[131072]
46// middle[ 1] == 0x23 == 0x23 == middle[131073]
47// middle[ 2] == 0x12 == 0x12 == middle[131074]
48// middle[ 3] == 0x13 == 0x13 == middle[131075]
49// middle[ 4] == 0x30 == 0x30 == middle[131076]
50// middle[ 5] == 0x31 == 0x31 == middle[131077]
51// middle[ 6] == 0x32 == 0x32 == middle[131078]
52// middle[ 7] == 0x17 == 0x17 == middle[131079]
53
54#include <stdint.h>
55#include <stdio.h>
56#include <string.h>
57#include <sys/mman.h>
58#include <unistd.h>
59
60// We should be able to do:
61//
62// #include <sys/memfd.h>
63//
64// but for some reason, this hits "No such file or directory" on Ubuntu 18.04
65// (linux 4.15, glibc 2.27). Instead, we explicitly declare this function
66// signature, copy/pasted from "man memfd_create".
67int memfd_create(const char* name, unsigned int flags);
68
69#define N (128 * 1024)
70
71void* make_ring_buffer() {
72 int page_size = getpagesize();
73 if ((N < page_size) || (page_size <= 0) || ((N % page_size) != 0)) {
74 return NULL;
75 }
76
77 int memfd = memfd_create("ring", 0);
78 if (memfd == -1) {
79 return NULL;
80 }
81 if (ftruncate(memfd, N) == -1) {
82 return NULL;
83 }
84
85 // Have the kernel find a contiguous range of unused address space.
86 void* base = mmap(NULL, 3 * N, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
87 if (base == MAP_FAILED) {
88 return NULL;
89 }
90
91 // Map that "ring" file 3 times, filling that range exactly.
92 for (int i = 0; i < 3; i++) {
93 void* p = mmap(base + (i * N), N, PROT_READ | PROT_WRITE,
94 MAP_FIXED | MAP_SHARED, memfd, 0);
95 if (p == MAP_FAILED) {
96 return NULL;
97 }
98 }
99
100 close(memfd);
101 return base;
102}
103
104int main(int argc, char** argv) {
105 uint8_t* base = make_ring_buffer();
106 if (!base) {
107 fprintf(stderr, "could not make ring buffer\n");
108 return 1;
109 }
110
111 for (int i = 0; i < 8; i++) {
112 base[i] = 0x10 + i;
113 }
114
115 memcpy(base + N - 2, "\x20\x21\x22\x23", 4);
116
117 base[(0 * N) + 4] = 0x30;
118 base[(1 * N) + 5] = 0x31;
119 base[(2 * N) + 6] = 0x32;
120
121 uint8_t* middle = base + N;
122 for (int i = -8; i < 8; i++) {
123 int j = N + i;
124 printf("middle[%2d] == 0x%02X == 0x%02X == middle[%6d]\n", i,
125 middle[i], middle[j], j);
126 }
127
128 return 0;
129}