File size: 1,172 Bytes
9422383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef YOLO26N_BUFFER_VALIDATION_H
#define YOLO26N_BUFFER_VALIDATION_H

#include <cstddef>
#include <cstdint>

namespace yolo26 {

// Returns true when the two memory ranges contain at least one common byte.
// Preprocessing and inference use this check to reject input and output buffers
// that overlap: writing output could otherwise destroy input bytes that have
// not been read yet. Empty ranges never overlap, and buffers that meet exactly
// at one range's end are adjacent rather than overlapping.
inline bool BuffersOverlap(const void* first, size_t first_bytes, const void* second,
                           size_t second_bytes) {
  if (first_bytes == 0 || second_bytes == 0) return false;
  const auto first_address = reinterpret_cast<uintptr_t>(first);
  const auto second_address = reinterpret_cast<uintptr_t>(second);
  // Compare the distance between start addresses. Calculating start + size
  // instead could wrap around the largest address representable by uintptr_t.
  if (first_address <= second_address) return second_address - first_address < first_bytes;
  return first_address - second_address < second_bytes;
}

}  // namespace yolo26

#endif