drgn/generate_readwrite.py
Omar Sandoval acbf14d10c dwarf: rewrite DwarfIndex in pure C
Most of drgn.dwarf is not performance-sensitive, and the part that is
(DwarfIndex) can use some extra tuning which is easier to do in C rather
than Cython.
2018-03-25 02:10:15 -07:00

35 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
SIZES = [8, 16, 32, 64]
def generate_read(name, read_type, ret_type=None):
if ret_type is None:
ret_type = read_type
print(f"""cdef inline int read_{name}(const char *buffer, Py_ssize_t buffer_size, Py_ssize_t *offset, {ret_type} *ret) except -1:
if not in_bounds(buffer_size, offset[0], sizeof({read_type})):
raise EOFError()
ret[0] = (<const {read_type} *>(buffer + offset[0]))[0]
offset[0] += sizeof({read_type})
return 0""")
if __name__ == '__main__':
first = True
for size in SIZES:
if first:
print('# Automatically generated by generate_readwrite.py')
else:
print()
print()
first = False
generate_read(f'u{size}', f'uint{size}_t')
if size < 64:
print()
print()
generate_read(f'u{size}_into_u64', f'uint{size}_t', f'uint64_t')
print()
print()
generate_read(f'u{size}_into_ssize_t', f'uint{size}_t', f'Py_ssize_t')