Skip to content

Commit c479834

Browse files
nits
1 parent 5d71d60 commit c479834

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

libudpard/udpard.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,21 @@ void udpard_fragment_free_all(udpard_fragment_t* const frag, const udpard_mem_re
149149
}
150150
}
151151

152-
udpard_fragment_t* udpard_fragment_seek(udpard_fragment_t* any_frag, const size_t offset)
152+
udpard_fragment_t* udpard_fragment_seek(udpard_fragment_t* frag, const size_t offset)
153153
{
154-
if (any_frag != NULL) {
155-
while (any_frag->index_offset.up != NULL) { // Only if the given node is not already the root.
156-
any_frag = (udpard_fragment_t*)any_frag->index_offset.up;
154+
if (frag != NULL) {
155+
// Common case: if the offset is already within the provided fragment, return it as-is.
156+
if ((frag->offset <= offset) && ((frag->offset + frag->view.size) > offset)) {
157+
return frag;
158+
}
159+
while (frag->index_offset.up != NULL) { // Only if the given node is not already the root.
160+
frag = (udpard_fragment_t*)frag->index_offset.up;
157161
}
158162
if (offset == 0) { // Common fast path.
159-
return (udpard_fragment_t*)cavl2_min(&any_frag->index_offset);
163+
return (udpard_fragment_t*)cavl2_min(&frag->index_offset);
160164
}
161165
udpard_fragment_t* const f =
162-
(udpard_fragment_t*)cavl2_predecessor(&any_frag->index_offset, &offset, &cavl_compare_fragment_offset);
166+
(udpard_fragment_t*)cavl2_predecessor(&frag->index_offset, &offset, &cavl_compare_fragment_offset);
163167
if ((f != NULL) && ((f->offset + f->view.size) > offset)) {
164168
UDPARD_ASSERT(f->offset <= offset);
165169
return f;
@@ -173,31 +177,31 @@ udpard_fragment_t* udpard_fragment_next(udpard_fragment_t* const frag)
173177
return (frag != NULL) ? ((udpard_fragment_t*)cavl2_next_greater(&frag->index_offset)) : NULL;
174178
}
175179

176-
size_t udpard_fragment_gather(const udpard_fragment_t* any_frag,
177-
const size_t offset,
178-
const size_t size,
179-
void* const destination)
180+
size_t udpard_fragment_gather(const udpard_fragment_t* const frag,
181+
const size_t offset,
182+
const size_t size,
183+
void* const destination)
180184
{
181185
size_t copied = 0;
182-
if ((any_frag != NULL) && (destination != NULL)) {
183-
udpard_fragment_t* frag = udpard_fragment_seek((udpard_fragment_t*)any_frag, offset);
186+
if ((frag != NULL) && (destination != NULL)) {
187+
udpard_fragment_t* f = udpard_fragment_seek((udpard_fragment_t*)frag, offset);
184188
size_t cursor = offset;
185189
byte_t* const out = (byte_t*)destination;
186190
// Copy contiguous fragments starting at the requested offset.
187-
while ((frag != NULL) && (copied < size)) {
188-
UDPARD_ASSERT(frag->offset <= cursor);
189-
UDPARD_ASSERT((frag->offset + frag->view.size) > cursor);
190-
UDPARD_ASSERT(frag->view.data != NULL);
191-
const size_t offset_in_frag = cursor - frag->offset;
192-
const size_t available = frag->view.size - offset_in_frag;
191+
while ((f != NULL) && (copied < size)) {
192+
UDPARD_ASSERT(f->offset <= cursor);
193+
UDPARD_ASSERT((f->offset + f->view.size) > cursor);
194+
UDPARD_ASSERT(f->view.data != NULL);
195+
const size_t offset_in_frag = cursor - f->offset;
196+
const size_t available = f->view.size - offset_in_frag;
193197
const size_t to_copy = smaller(available, size - copied);
194198
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
195-
(void)memcpy(out + copied, ((const byte_t*)frag->view.data) + offset_in_frag, to_copy);
199+
(void)memcpy(out + copied, ((const byte_t*)f->view.data) + offset_in_frag, to_copy);
196200
copied += to_copy;
197201
cursor += to_copy;
198202
if (copied < size) {
199-
frag = udpard_fragment_next(frag);
200-
UDPARD_ASSERT((frag == NULL) || (frag->offset == cursor));
203+
f = udpard_fragment_next(f);
204+
UDPARD_ASSERT((f == NULL) || (f->offset == cursor));
201205
}
202206
}
203207
}

libudpard/udpard.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ typedef struct udpard_fragment_t
210210
void udpard_fragment_free_all(udpard_fragment_t* const frag, const udpard_mem_resource_t fragment_mem_resource);
211211

212212
/// Given any fragment in a transfer, returns the fragment that contains the given payload offset.
213-
/// Returns NULL if the offset points beyond the stored payload, or if any_frag is NULL.
213+
/// Returns NULL if the offset points beyond the stored payload, or if frag is NULL.
214214
/// This is also the idiomatic way to find the head of the fragment list when invoked with offset zero.
215215
/// This function accepts any node in the fragment tree, not necessarily the head or the root, and
216216
/// has a logarithmic complexity in the number of fragments, which makes it very efficient.
217-
udpard_fragment_t* udpard_fragment_seek(udpard_fragment_t* any_frag, const size_t offset);
217+
udpard_fragment_t* udpard_fragment_seek(udpard_fragment_t* frag, const size_t offset);
218218

219219
/// Given any fragment in a transfer, returns the next fragment in strictly ascending order of offsets.
220220
/// The offset of the next fragment always equals the sum of the offset and size of the current fragment.
@@ -227,10 +227,10 @@ udpard_fragment_t* udpard_fragment_next(udpard_fragment_t* const frag);
227227
/// Returns the number of bytes copied into the contiguous destination buffer, which equals `size` unless
228228
/// `offset+size` exceeds the amount of data stored in the fragments.
229229
/// The function has no effect and returns zero if the destination buffer or fragment pointer are NULL.
230-
size_t udpard_fragment_gather(const udpard_fragment_t* any_frag,
231-
const size_t offset,
232-
const size_t size,
233-
void* const destination);
230+
size_t udpard_fragment_gather(const udpard_fragment_t* const frag,
231+
const size_t offset,
232+
const size_t size,
233+
void* const destination);
234234

235235
// =====================================================================================================================
236236
// ================================================= TX PIPELINE =================================================

0 commit comments

Comments
 (0)