TAS
TCP Acceleration as an OS Service
utils_circ.h
1 /*
2  * Copyright 2019 University of Washington, Max Planck Institute for
3  * Software Systems, and The University of Texas at Austin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef UTILS_CRIC_H_
26 #define UTILS_CRIC_H_
27 
28 #include <assert.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 
32 /* Calculates one or two (in case of wrap-around) ranges of length `len' at
33  * `pos' in circular buffer of length `b_len' at address `b_base'. */
34 static inline int circ_range(void **b1, size_t *l1, void **b2, void *b_base,
35  size_t b_len, size_t pos, size_t len)
36 {
37  uint8_t *b = b_base;
38  size_t l;
39 
40  if (pos + len <= b_len) {
41  //printf("circ_range case1: len=%zu l1=%zu b_len=%zu pos=%zu\n", len, len, b_len, pos);
42  *b1 = b + pos;
43  *l1 = len;
44  *b2 = NULL;
45  return 0;
46  } else {
47  l = b_len - pos;
48  //printf("circ_range case2: len=%zu l1=%zu b_len=%zu pos=%zu\n", len, l, b_len, pos);
49 
50  *b1 = b + pos;
51  *l1 = l;
52  *b2 = b;
53  return 1;
54  }
55 }
56 
57 /* Increment position `pos' in circular buffer of length `len' by
58  * `off'. */
59 static inline size_t circ_offset(size_t pos, size_t len, size_t off)
60 {
61  pos += off;
62  if (pos >= len) {
63  pos -= len;
64  }
65  return pos;
66 }
67 
68 static inline int circ_in_interval(size_t start, size_t end, size_t len,
69  size_t pos)
70 {
71  if (start < end) {
72  return (pos >= start && pos <= end);
73  } else {
74  return (pos >= start) || (pos <= end);
75  }
76 }
77 
78 static inline void circ_read(void *dst, void *b_base, size_t b_len,
79  size_t pos, size_t len)
80 {
81  uint8_t *d = dst;
82  void *b1, *b2;
83  size_t l;
84 
85  if (circ_range(&b1, &l, &b2, b_base, b_len, pos, len) == 0) {
86  memcpy(d, b1, l);
87  } else {
88  memcpy(d, b1, l);
89  memcpy(d + l, b2, len - l);
90  }
91 }
92 
93 static inline void circ_write(const void *dst, void *b_base, size_t b_len,
94  size_t pos, size_t len)
95 {
96  const uint8_t *d = dst;
97  void *b1, *b2;
98  size_t l;
99 
100  if (circ_range(&b1, &l, &b2, b_base, b_len, pos, len) == 0) {
101  memcpy(b1, d, l);
102  } else {
103  memcpy(b1, d, l);
104  memcpy(b2, d + l, len - l);
105  }
106 }
107 
108 static inline void split_write(const void *src, size_t len, void *buf_1,
109  size_t len_1, void *buf_2, size_t len_2, size_t off)
110 {
111  size_t l;
112 
113  assert(len + off <= len_1 + len_2);
114  if (off + len <= len_1) {
115  /* only in first half */
116  memcpy((uint8_t *) buf_1 + off, src, len);
117  } else if (off >= len_1) {
118  /* only in second half */
119  memcpy((uint8_t *) buf_2 + (off - len_1), src, len);
120  } else {
121  /* spread over both halves */
122  l = len_1 - off;
123  memcpy((uint8_t *) buf_1 + off, src, l);
124  memcpy(buf_2, (const uint8_t *) src + l, len - l);
125  }
126 }
127 
128 static inline void split_read(void *dst, size_t len, const void *buf_1,
129  size_t len_1, const void *buf_2, size_t len_2, size_t off)
130 {
131  size_t l;
132 
133  assert(len + off <= len_1 + len_2);
134  if (off + len <= len_1) {
135  /* only in first half */
136  memcpy(dst, (const uint8_t *) buf_1 + off, len);
137  } else if (off >= len_1) {
138  /* only in second half */
139  memcpy(dst, (const uint8_t *) buf_2 + (off - len_1), len);
140  } else {
141  /* spread over both halves */
142  l = len_1 - off;
143  memcpy(dst, (const uint8_t *) buf_1 + off, l);
144  memcpy((uint8_t *) dst + l, buf_2, len - l);
145  }
146 }
147 
148 #endif /* ndef UTILS_CIRC_H_ */