dlbeer / quirc

QR decoder library
Other
882 stars 286 forks source link

quirc image memory page alignment #122

Open graindcafe opened 2 years ago

graindcafe commented 2 years ago

This PR make quirc use mmap instead of calloc to allocate memory of quirc->image. The use case I have for this is to be able to use mmap() on the application side with the flag MAP_FIXED and memory address given by quirc. The image I capture is accessible in a dmabuf, I access it by using mmap() with the dmabuf’s fd. With quirc using calloc(), my call to mmap() fails because of non-aligned memory page. Instead of calloc/free, this PR proposes to use mmap/munmap. I believe this does not alter the behaviour of quirc.

dlbeer commented 2 years ago

This is a good idea. However:

Another alternative may be to continue using malloc, but to over-allocate by PAGE_SIZE, so that you guarantee yourself room to shift to a page-aligned address within the allocated block.

graindcafe commented 2 years ago

Thanks for the review, gonna fix this.

Do you think it's okay to revert to previous calloc/free for windows with some preprocessor checks? Not sure if it's relevant to have page alignment for Windows, but in such a case maybe I should use _aligned_malloc.

dlbeer commented 2 years ago

On Mon, Dec 27, 2021 at 02:50:54AM -0800, Quentin GODRON wrote:

Thanks for the review, gonna fix this.

Do you think it's okay to revert to previous calloc/free for windows with some preprocessor checks?

Have you considered just over-allocating with malloc? That way you don't need to depend on anything platform-specific. You'd probably need to track an extra pointer though, or have a macro to skip from the pointer returned by malloc to the start of the page-aligned image area.

Cheers, Daniel

-- Daniel Beer @.***> http://dlbeer.co.nz/ PGP: BA6E 0B26 1F89 246C E3F3 C910 1E58 C43A 160A 553B