mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2024-12-28 05:25:59 +01:00
Avoid additional allocations
This commit is contained in:
parent
40fe9ced1b
commit
904a157f1b
1 changed files with 29 additions and 49 deletions
|
@ -301,14 +301,12 @@ running_under_wsl(void)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct zathura_point_s
|
typedef struct zathura_point_s {
|
||||||
{
|
uintptr_t x;
|
||||||
unsigned int x;
|
uintptr_t y;
|
||||||
unsigned int y;
|
|
||||||
} zathura_point_t;
|
} zathura_point_t;
|
||||||
|
|
||||||
static int
|
static int cmp_point(const void* va, const void* vb) {
|
||||||
cmp_point(const void* va, const void* vb) {
|
|
||||||
const zathura_point_t* a = va;
|
const zathura_point_t* a = va;
|
||||||
const zathura_point_t* b = vb;
|
const zathura_point_t* b = vb;
|
||||||
|
|
||||||
|
@ -323,38 +321,32 @@ cmp_point(const void* va, const void* vb) {
|
||||||
return a->x < b->x ? -1 : 1;
|
return a->x < b->x ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int
|
static uintptr_t ufloor(double f) {
|
||||||
ufloor(double f) {
|
|
||||||
return floor(f);
|
return floor(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int
|
static uintptr_t uceil(double f) {
|
||||||
uceil(double f) {
|
|
||||||
return ceil(f);
|
return ceil(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int cmp_uint(const void* vx, const void* vy) {
|
||||||
cmp_uint(const void* vx, const void* vy) {
|
const uintptr_t x = (uintptr_t)vx;
|
||||||
const unsigned int* x = vx;
|
const uintptr_t y = (uintptr_t)vy;
|
||||||
const unsigned int* y = vy;
|
|
||||||
|
|
||||||
return *x == *y ? 0 : (*x > *y ? 1 : -1);
|
return x == y ? 0 : (x > y ? 1 : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int cmp_rectangle(const void* vr1, const void* vr2) {
|
||||||
cmp_rectangle(const void* vr1, const void* vr2) {
|
|
||||||
const zathura_rectangle_t* r1 = vr1;
|
const zathura_rectangle_t* r1 = vr1;
|
||||||
const zathura_rectangle_t* r2 = vr2;
|
const zathura_rectangle_t* r2 = vr2;
|
||||||
|
|
||||||
return (ufloor(r1->x1) == ufloor(r2->x1) && uceil(r1->x2) == uceil(r2->x2) &&
|
return (ufloor(r1->x1) == ufloor(r2->x1) && uceil(r1->x2) == uceil(r2->x2) && ufloor(r1->y1) == ufloor(r2->y1) &&
|
||||||
ufloor(r1->y1) == ufloor(r2->y1) && uceil(r1->y2) == uceil(r2->y2))
|
uceil(r1->y2) == uceil(r2->y2))
|
||||||
? 0
|
? 0
|
||||||
: -1;
|
: -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool girara_list_append_unique(girara_list_t* l, girara_compare_function_t cmp, void* item) {
|
||||||
static bool
|
|
||||||
girara_list_append_unique(girara_list_t* l, girara_compare_function_t cmp, void* item) {
|
|
||||||
if (girara_list_find(l, cmp, item) != NULL) {
|
if (girara_list_find(l, cmp, item) != NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -363,8 +355,7 @@ girara_list_append_unique(girara_list_t* l, girara_compare_function_t cmp, void*
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void append_unique_point(girara_list_t* list, const uintptr_t x, const uintptr_t y) {
|
||||||
append_unique_point(girara_list_t* list, const unsigned int x, const unsigned int y) {
|
|
||||||
zathura_point_t* p = g_try_malloc(sizeof(zathura_point_t));
|
zathura_point_t* p = g_try_malloc(sizeof(zathura_point_t));
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
return;
|
return;
|
||||||
|
@ -378,8 +369,7 @@ append_unique_point(girara_list_t* list, const unsigned int x, const unsigned in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void rectangle_to_points(void* vrect, void* vlist) {
|
||||||
rectangle_to_points(void* vrect, void* vlist) {
|
|
||||||
const zathura_rectangle_t* rect = vrect;
|
const zathura_rectangle_t* rect = vrect;
|
||||||
girara_list_t* list = vlist;
|
girara_list_t* list = vlist;
|
||||||
|
|
||||||
|
@ -389,25 +379,15 @@ rectangle_to_points(void* vrect, void* vlist) {
|
||||||
append_unique_point(list, uceil(rect->x2), uceil(rect->y2));
|
append_unique_point(list, uceil(rect->x2), uceil(rect->y2));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void append_unique_uint(girara_list_t* list, const uintptr_t v) {
|
||||||
append_unique_uint(girara_list_t* list, const unsigned int v) {
|
girara_list_append_unique(list, cmp_uint, (void*)v);
|
||||||
unsigned int* p = g_try_malloc(sizeof(unsigned int));
|
|
||||||
if (p == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p = v;
|
|
||||||
|
|
||||||
if (girara_list_append_unique(list, cmp_uint, p) == false) {
|
|
||||||
g_free(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform a rectangle into multiple new ones according a grid of points
|
// transform a rectangle into multiple new ones according a grid of points
|
||||||
static void cut_rectangle(const zathura_rectangle_t* rect, girara_list_t* points, girara_list_t* rectangles) {
|
static void cut_rectangle(const zathura_rectangle_t* rect, girara_list_t* points, girara_list_t* rectangles) {
|
||||||
// Lists of ordred relevant points
|
// Lists of ordred relevant points
|
||||||
girara_list_t* xs = girara_sorted_list_new2(cmp_uint, g_free);
|
girara_list_t* xs = girara_sorted_list_new(cmp_uint);
|
||||||
girara_list_t* ys = girara_sorted_list_new2(cmp_uint, g_free);
|
girara_list_t* ys = girara_sorted_list_new(cmp_uint);
|
||||||
|
|
||||||
append_unique_uint(xs, uceil(rect->x2));
|
append_unique_uint(xs, uceil(rect->x2));
|
||||||
append_unique_uint(ys, uceil(rect->y2));
|
append_unique_uint(ys, uceil(rect->y2));
|
||||||
|
@ -424,17 +404,17 @@ static void cut_rectangle(const zathura_rectangle_t* rect, girara_list_t* points
|
||||||
|
|
||||||
double x = ufloor(rect->x1);
|
double x = ufloor(rect->x1);
|
||||||
for (size_t idx = 0; idx != girara_list_size(xs); ++idx) {
|
for (size_t idx = 0; idx != girara_list_size(xs); ++idx) {
|
||||||
const unsigned int* cx = girara_list_nth(xs, idx);
|
const uintptr_t cx = (uintptr_t)girara_list_nth(xs, idx);
|
||||||
double y = ufloor(rect->y1);
|
double y = ufloor(rect->y1);
|
||||||
for (size_t inner_idx = 0; inner_idx != girara_list_size(ys); ++inner_idx) {
|
for (size_t inner_idx = 0; inner_idx != girara_list_size(ys); ++inner_idx) {
|
||||||
const unsigned int* cy = girara_list_nth(ys, inner_idx);
|
const uintptr_t cy = (uintptr_t)girara_list_nth(ys, inner_idx);
|
||||||
zathura_rectangle_t* r = g_try_malloc(sizeof(zathura_rectangle_t));
|
zathura_rectangle_t* r = g_try_malloc(sizeof(zathura_rectangle_t));
|
||||||
|
|
||||||
*r = (zathura_rectangle_t){x, y, *cx, *cy};
|
*r = (zathura_rectangle_t){x, y, cx, cy};
|
||||||
y = *cy;
|
y = cy;
|
||||||
girara_list_append_unique(rectangles, cmp_rectangle, r);
|
girara_list_append_unique(rectangles, cmp_rectangle, r);
|
||||||
}
|
}
|
||||||
x = *cx;
|
x = cx;
|
||||||
}
|
}
|
||||||
|
|
||||||
girara_list_free(xs);
|
girara_list_free(xs);
|
||||||
|
|
Loading…
Reference in a new issue