Check memory alignment in python ctypes

I am studying the use of ctypes to use C functions that manage SSE data (__m128), which must be aligned at 16 byte boundaries.

I could not find an easy way to control the alignment of memory allocated by ctypes, so right now I am doing ctypes to call the C function, which provides a properly aligned memory buffer.

The problem with this approach is that I have to manually explicitly free this memory to prevent it from leaking.

Is there a way to control the alignment of memory allocated by ctypes? or is there a way to register a cleanup function to free memory allocated by a C function called ctypes (other than the standard python __ del __ operator)?

What is the best way?

+4
source share
2 answers

I spent some time researching, I came up with a function that should allow me to allocate arbitrary aligned memory using ctypes, mainly relying on ctypes to store a reference to an unmanaged memory buffer, an instance starting in the aligned position in the buffer.

Still need to test this in production.

import ctypes def ctypes_alloc_aligned(size, alignment): bufSize = size+(alignment-1) raw_memory = bytearray(bufSize) ctypes_raw_type = (ctypes.c_char * bufSize) ctypes_raw_memory = ctypes_raw_type.from_buffer(raw_memory) raw_address = ctypes.addressof(ctypes_raw_memory) offset = raw_address % alignment offset_to_aligned = (alignment - offset) % alignment ctypes_aligned_type = (ctypes.c_char * (bufSize-offset_to_aligned)) ctypes_aligned_memory = ctypes_aligned_type.from_buffer(raw_memory, offset_to_aligned) return ctypes_aligned_memory 
+2
source

I believe that c_ulonglong (64 bits) should be aligned on 64 bits; this is the beginning. Then doc suggests using _pack_ to control the alignment of structures. These two are not exactly what you want, but by combining them you can select 8-byte aligned structures without holes.

Let a structure with three 8-byte aligned elements .v1 , .v2 , .v1 be .v2 . Use addressof() to see if the structure is 16 bytes addressof() . If so, use .v1 and .v1 for your 128-bit value; if not, use .v1 and .v2 .

+1
source

Source: https://habr.com/ru/post/1388335/


All Articles