@@ -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 }
0 commit comments