[Spice-devel] [spice-server PATCH v1 1/12] glz: use PIXEL_PTR to cast PIXEL*

Victor Toso victortoso at redhat.com
Wed Aug 5 05:23:15 PDT 2015


Besides making the code a bit cleaner this would help avoid warnings
about alignment from clang.

./glz_encode_tmpl.c:321:29: error: cast from 'uint8_t *'
(aka 'unsigned char *') to 'rgb16_pixel_t *' (aka 'unsigned short *')
increases required alignment from 1 to 2 [-Werror,-Wcast-align]
 ref_limit = (PIXEL *)(seg->lines_end);
             ^~~~~~~~~~~~~~~~~~~~~~~~~
---
 server/glz_encode_tmpl.c | 48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/server/glz_encode_tmpl.c b/server/glz_encode_tmpl.c
index 1402f95..9101196 100644
--- a/server/glz_encode_tmpl.c
+++ b/server/glz_encode_tmpl.c
@@ -128,14 +128,17 @@
 
 #endif
 
+#define PIXEL_PTR(ptr) \
+    ((PIXEL *)(void *)ptr)
+
 #ifndef LZ_PLT
 #define PIXEL_ID(pix_ptr, seg_ptr) \
-    ((pix_ptr) - ((PIXEL *)(seg_ptr)->lines) + (seg_ptr)->pixels_so_far)
+    ((pix_ptr) - PIXEL_PTR((seg_ptr)->lines) + (seg_ptr)->pixels_so_far)
 #define PIXEL_DIST(src_pix_ptr, src_seg_ptr, ref_pix_ptr, ref_seg_ptr) \
     (PIXEL_ID(src_pix_ptr,src_seg_ptr) - PIXEL_ID(ref_pix_ptr, ref_seg_ptr))
 #else
 #define PIXEL_ID(pix_ptr, seg_ptr, pix_per_byte) \
-    (((pix_ptr) - ((PIXEL *)(seg_ptr)->lines)) * pix_per_byte + (seg_ptr)->pixels_so_far)
+    (((pix_ptr) - PIXEL_PTR((seg_ptr)->lines)) * pix_per_byte + (seg_ptr)->pixels_so_far)
 #define PIXEL_DIST(src_pix_ptr, src_seg_ptr, ref_pix_ptr, ref_seg_ptr, pix_per_byte) \
     ((PIXEL_ID(src_pix_ptr,src_seg_ptr, pix_per_byte) - \
     PIXEL_ID(ref_pix_ptr, ref_seg_ptr, pix_per_byte)) / pix_per_byte)
@@ -211,13 +214,13 @@ static INLINE size_t FNAME(do_match)(SharedDictionary *dict,
     } else { // the ref is at different image - encode offset from the image start
 #ifndef LZ_PLT
         *o_pix_distance = PIXEL_DIST(ref, ref_seg,
-                                     (PIXEL *)(dict->window.segs[ref_seg->image->first_seg].lines),
+                                     PIXEL_PTR(dict->window.segs[ref_seg->image->first_seg].lines),
                                      &dict->window.segs[ref_seg->image->first_seg]
                                      );
 #else
         // in bytes
         *o_pix_distance = PIXEL_DIST(ref, ref_seg,
-                                     (PIXEL *)(dict->window.segs[ref_seg->image->first_seg].lines),
+                                     PIXEL_PTR(dict->window.segs[ref_seg->image->first_seg].lines),
                                      &dict->window.segs[ref_seg->image->first_seg],
                                      pix_per_byte);
 #endif
@@ -269,8 +272,8 @@ static void FNAME(compress_seg)(Encoder *encoder, uint32_t seg_idx, PIXEL *from,
 {
     WindowImageSegment *seg = &encoder->dict->window.segs[seg_idx];
     const PIXEL *ip = from;
-    const PIXEL *ip_bound = (PIXEL *)(seg->lines_end) - BOUND_OFFSET;
-    const PIXEL *ip_limit = (PIXEL *)(seg->lines_end) - LIMIT_OFFSET;
+    const PIXEL *ip_bound = PIXEL_PTR(seg->lines_end) - BOUND_OFFSET;
+    const PIXEL *ip_limit = PIXEL_PTR(seg->lines_end) - LIMIT_OFFSET;
     int hval;
     int copy = copied;
 #ifdef  LZ_PLT
@@ -307,7 +310,7 @@ static void FNAME(compress_seg)(Encoder *encoder, uint32_t seg_idx, PIXEL *from,
 
         /* check for a run */
 
-        if (LZ_EXPECT_CONDITIONAL(ip > (PIXEL *)(seg->lines))) {
+        if (LZ_EXPECT_CONDITIONAL(ip > PIXEL_PTR(seg->lines))) {
             if (SAME_PIXEL(ip[-1], ip[0]) && SAME_PIXEL(ip[0], ip[1]) && SAME_PIXEL(ip[1], ip[2])) {
                 PIXEL x;
                 pix_dist = 1;
@@ -315,7 +318,7 @@ static void FNAME(compress_seg)(Encoder *encoder, uint32_t seg_idx, PIXEL *from,
 
                 ip += 3;
                 ref = anchor + 2;
-                ref_limit = (PIXEL *)(seg->lines_end);
+                ref_limit = PIXEL_PTR(seg->lines_end);
                 len = 3;
 
                 x = *ref;
@@ -348,11 +351,11 @@ static void FNAME(compress_seg)(Encoder *encoder, uint32_t seg_idx, PIXEL *from,
             if (REF_SEG_IS_VALID(encoder->dict, encoder->id,
                                  ref_seg, seg)) {
 #ifdef CHAINED_HASH
-                ref = ((PIXEL *)ref_seg->lines) + encoder->dict->htab[hval][hash_id].ref_pix_idx;
+                ref = PIXEL_PTR(ref_seg->lines) + encoder->dict->htab[hval][hash_id].ref_pix_idx;
 #else
-                ref = ((PIXEL *)ref_seg->lines) + encoder->dict->htab[hval].ref_pix_idx;
+                ref = PIXEL_PTR(ref_seg->lines) + encoder->dict->htab[hval].ref_pix_idx;
 #endif
-                ref_limit = (PIXEL *)ref_seg->lines_end;
+                ref_limit = PIXEL_PTR(ref_seg->lines_end);
 
                 len = FNAME(do_match)(encoder->dict, ref_seg, ref, ref_limit, seg, ip, ip_bound,
 #ifdef  LZ_PLT
@@ -378,7 +381,7 @@ static void FNAME(compress_seg)(Encoder *encoder, uint32_t seg_idx, PIXEL *from,
 #endif
 
         /* update hash table */
-        UPDATE_HASH(encoder->dict, hval, seg_idx, anchor - ((PIXEL *)seg->lines));
+        UPDATE_HASH(encoder->dict, hval, seg_idx, anchor - PIXEL_PTR(seg->lines));
 
         if (!len) {
             goto literal;
@@ -425,7 +428,7 @@ match:        // RLE or dictionary (both are encoded by distance from ref (-1) a
         if (ip > anchor) {
 #endif
             HASH_FUNC(hval, ip);
-            UPDATE_HASH(encoder->dict, hval, seg_idx, ip - ((PIXEL *)seg->lines));
+            UPDATE_HASH(encoder->dict, hval, seg_idx, ip - PIXEL_PTR(seg->lines));
             ip++;
 #if defined(LZ_RGB16) || defined(LZ_RGB24) || defined(LZ_RGB32)
         } else {ip++;
@@ -435,7 +438,7 @@ match:        // RLE or dictionary (both are encoded by distance from ref (-1) a
         if (ip > anchor) {
 #endif
             HASH_FUNC(hval, ip);
-            UPDATE_HASH(encoder->dict, hval, seg_idx, ip - ((PIXEL *)seg->lines));
+            UPDATE_HASH(encoder->dict, hval, seg_idx, ip - PIXEL_PTR(seg->lines));
             ip++;
 #if defined(LZ_RGB24) || defined(LZ_RGB32)
         } else {
@@ -505,16 +508,16 @@ static void FNAME(compress)(Encoder *encoder)
     // fetch the first image segment that is not too small
     while ((seg_id != NULL_IMAGE_SEG_ID) &&
            (dict->window.segs[seg_id].image->id == encoder->cur_image.id) &&
-           ((((PIXEL *)dict->window.segs[seg_id].lines_end) -
-             ((PIXEL *)dict->window.segs[seg_id].lines)) < 4)) {
+           ((PIXEL_PTR(dict->window.segs[seg_id].lines_end) -
+             PIXEL_PTR(dict->window.segs[seg_id].lines)) < 4)) {
         // coping the segment
         if (dict->window.segs[seg_id].lines != dict->window.segs[seg_id].lines_end) {
-            ip = (PIXEL *)dict->window.segs[seg_id].lines;
+            ip = PIXEL_PTR(dict->window.segs[seg_id].lines);
             // Note: we assume MAX_COPY > 3
             encode_copy_count(encoder, (uint8_t)(
-                                  (((PIXEL *)dict->window.segs[seg_id].lines_end) -
-                                   ((PIXEL *)dict->window.segs[seg_id].lines)) - 1));
-            while (ip < (PIXEL *)dict->window.segs[seg_id].lines_end) {
+                                  (PIXEL_PTR(dict->window.segs[seg_id].lines_end) -
+                                   PIXEL_PTR(dict->window.segs[seg_id].lines)) - 1));
+            while (ip < PIXEL_PTR(dict->window.segs[seg_id].lines_end)) {
                 ENCODE_PIXEL(encoder, *ip);
                 ip++;
             }
@@ -527,7 +530,7 @@ static void FNAME(compress)(Encoder *encoder)
         return;
     }
 
-    ip = (PIXEL *)dict->window.segs[seg_id].lines;
+    ip = PIXEL_PTR(dict->window.segs[seg_id].lines);
 
 
     encode_copy_count(encoder, MAX_COPY - 1);
@@ -550,11 +553,12 @@ static void FNAME(compress)(Encoder *encoder)
         seg_id != NULL_IMAGE_SEG_ID && (
         dict->window.segs[seg_id].image->id == encoder->cur_image.id);
         seg_id = dict->window.segs[seg_id].next) {
-        FNAME(compress_seg)(encoder, seg_id, (PIXEL *)dict->window.segs[seg_id].lines, 0);
+        FNAME(compress_seg)(encoder, seg_id, PIXEL_PTR(dict->window.segs[seg_id].lines), 0);
     }
 }
 
 #undef FNAME
+#undef PIXEL_PTR
 #undef PIXEL_ID
 #undef PIXEL_DIST
 #undef PIXEL
-- 
2.4.3



More information about the Spice-devel mailing list