backend: dummy: add more sanity checks

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2019-09-23 20:02:30 +01:00
parent a0bb392d8c
commit 73ea5d2bfb
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
1 changed files with 27 additions and 29 deletions

View File

@ -16,7 +16,7 @@
struct dummy_image { struct dummy_image {
xcb_pixmap_t pixmap; xcb_pixmap_t pixmap;
bool transparent; bool transparent;
int refcount; int *refcount;
UT_hash_handle hh; UT_hash_handle hh;
}; };
@ -39,22 +39,27 @@ void dummy_deinit(struct backend_base *data) {
HASH_ITER2(dummy->images, img) { HASH_ITER2(dummy->images, img) {
log_warn("Backend image for pixmap %#010x is not freed", img->pixmap); log_warn("Backend image for pixmap %#010x is not freed", img->pixmap);
HASH_DEL(dummy->images, img); HASH_DEL(dummy->images, img);
free(img->refcount);
free(img); free(img);
} }
free(dummy); free(dummy);
} }
void dummy_compose(struct backend_base *base, void *image, int dst_x attr_unused, static void dummy_check_image(struct backend_base *base, const struct dummy_image *img) {
int dst_y attr_unused, const region_t *reg_paint attr_unused,
const region_t *reg_visible attr_unused) {
auto dummy = (struct dummy_data *)base; auto dummy = (struct dummy_data *)base;
auto img = (struct dummy_image *)image;
struct dummy_image *tmp = NULL; struct dummy_image *tmp = NULL;
HASH_FIND_INT(dummy->images, &img->pixmap, tmp); HASH_FIND_INT(dummy->images, &img->pixmap, tmp);
if (!tmp) { if (!tmp) {
log_warn("Composing with an invalid (possibly freed) image"); log_warn("Using an invalid (possibly freed) image");
assert(false);
} }
assert(*tmp->refcount > 0);
}
void dummy_compose(struct backend_base *base, void *image, int dst_x attr_unused,
int dst_y attr_unused, const region_t *reg_paint attr_unused,
const region_t *reg_visible attr_unused) {
dummy_check_image(base, image);
} }
void dummy_fill(struct backend_base *backend_data attr_unused, struct color c attr_unused, void dummy_fill(struct backend_base *backend_data attr_unused, struct color c attr_unused,
@ -73,14 +78,15 @@ void *dummy_bind_pixmap(struct backend_base *base, xcb_pixmap_t pixmap,
struct dummy_image *img = NULL; struct dummy_image *img = NULL;
HASH_FIND_INT(dummy->images, &pixmap, img); HASH_FIND_INT(dummy->images, &pixmap, img);
if (img) { if (img) {
img->refcount++; (*img->refcount)++;
return img; return img;
} }
img = ccalloc(1, struct dummy_image); img = ccalloc(1, struct dummy_image);
img->pixmap = pixmap; img->pixmap = pixmap;
img->transparent = fmt.alpha_size != 0; img->transparent = fmt.alpha_size != 0;
img->refcount = 1; img->refcount = ccalloc(1, int);
*img->refcount = 1;
HASH_ADD_INT(dummy->images, pixmap, img); HASH_ADD_INT(dummy->images, pixmap, img);
return (void *)img; return (void *)img;
@ -89,22 +95,18 @@ void *dummy_bind_pixmap(struct backend_base *base, xcb_pixmap_t pixmap,
void dummy_release_image(backend_t *base, void *image) { void dummy_release_image(backend_t *base, void *image) {
auto dummy = (struct dummy_data *)base; auto dummy = (struct dummy_data *)base;
auto img = (struct dummy_image *)image; auto img = (struct dummy_image *)image;
assert(img->refcount > 0); assert(*img->refcount > 0);
img->refcount--; (*img->refcount)--;
if (img->refcount == 0) { if (*img->refcount == 0) {
HASH_DEL(dummy->images, img); HASH_DEL(dummy->images, img);
free(img->refcount);
free(img); free(img);
} }
} }
bool dummy_is_image_transparent(struct backend_base *base, void *image) { bool dummy_is_image_transparent(struct backend_base *base, void *image) {
auto dummy = (struct dummy_data *)base;
auto img = (struct dummy_image *)image; auto img = (struct dummy_image *)image;
struct dummy_image *tmp = NULL; dummy_check_image(base, img);
HASH_FIND_INT(dummy->images, &img->pixmap, tmp);
if (!tmp) {
log_warn("Using an invalid (possibly freed) image");
}
return img->transparent; return img->transparent;
} }
@ -112,23 +114,19 @@ int dummy_buffer_age(struct backend_base *base attr_unused) {
return 2; return 2;
} }
bool dummy_image_op(struct backend_base *base attr_unused, enum image_operations op attr_unused, bool dummy_image_op(struct backend_base *base, enum image_operations op attr_unused,
void *image attr_unused, const region_t *reg_op attr_unused, void *image, const region_t *reg_op attr_unused,
const region_t *reg_visible attr_unused, void *args attr_unused) { const region_t *reg_visible attr_unused, void *args attr_unused) {
dummy_check_image(base, image);
return true; return true;
} }
void *dummy_image_copy(struct backend_base *base, const void *image, void *dummy_image_copy(struct backend_base *base, const void *image,
const region_t *reg_visible attr_unused) { const region_t *reg_visible attr_unused) {
auto dummy = (struct dummy_data *)base; auto img = (const struct dummy_image *)image;
auto img = (struct dummy_image *)image; dummy_check_image(base, img);
struct dummy_image *tmp = NULL; (*img->refcount)++;
HASH_FIND_INT(dummy->images, &img->pixmap, tmp); return (void *)img;
if (!tmp) {
log_warn("Using an invalid (possibly freed) image");
}
img->refcount++;
return img;
} }
void *dummy_create_blur_context(struct backend_base *base attr_unused, void *dummy_create_blur_context(struct backend_base *base attr_unused,