Make sure framebuffers are properly aligned
parent
f0974e5af6
commit
22eba2bed8
8
src/fb.c
8
src/fb.c
|
@ -3,7 +3,10 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
|
||||||
|
#define UDIV_UP(a, b) (((a) + (b) - 1) / (b))
|
||||||
|
#define ALIGN_UP(n, a) (UDIV_UP(n, a) * a)
|
||||||
#define EXPORT __attribute__((visibility("default")))
|
#define EXPORT __attribute__((visibility("default")))
|
||||||
|
|
||||||
EXPORT
|
EXPORT
|
||||||
|
@ -20,11 +23,14 @@ struct nvnc_fb* nvnc_fb_new(uint16_t width, uint16_t height,
|
||||||
fb->fourcc_format = fourcc_format;
|
fb->fourcc_format = fourcc_format;
|
||||||
fb->size = width * height * 4; /* Assume 4 byte format for now */
|
fb->size = width * height * 4; /* Assume 4 byte format for now */
|
||||||
|
|
||||||
|
size_t alignment = MAX(4, sizeof(void*));
|
||||||
|
size_t aligned_size = ALIGN_UP(fb->size, alignment);
|
||||||
|
|
||||||
/* fb could be allocated in single allocation, but I want to reserve
|
/* fb could be allocated in single allocation, but I want to reserve
|
||||||
* the possiblity to create an fb with a pixel buffer passed from the
|
* the possiblity to create an fb with a pixel buffer passed from the
|
||||||
* user.
|
* user.
|
||||||
*/
|
*/
|
||||||
fb->addr = malloc(fb->size);
|
fb->addr = aligned_alloc(alignment, aligned_size);
|
||||||
if (!fb->addr) {
|
if (!fb->addr) {
|
||||||
free(fb);
|
free(fb);
|
||||||
fb = NULL;
|
fb = NULL;
|
||||||
|
|
Loading…
Reference in New Issue