From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753233AbcHRITe (ORCPT ); Thu, 18 Aug 2016 04:19:34 -0400 Received: from sender153-mail.zoho.com ([74.201.84.153]:21190 "EHLO sender153-mail.zoho.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751041AbcHRITc (ORCPT ); Thu, 18 Aug 2016 04:19:32 -0400 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=zapps768; d=zoho.com; h=from:subject:to:cc:message-id:date:user-agent:mime-version:content-type; b=cusXCcWEjOh+5QQnR8O5MQ+tEsfRaqQ9Y6fEbYRROT5eg8cUOxGSE/JQQ2SFVJX3PMTd1jukUXlN KWcuWyFjnRBJLiWkjgEGK252LfC0Y5hygRJlviFwpxsUVszcE8jx From: zijun_hu Subject: [PATCH 1/1] rbtree: correct mask for extracting node's parent To: Andrew Morton Cc: peterz@infradead.org, dhowells@redhat.com, linux-kernel@vger.kernel.org, zijun_hu@htc.com Message-ID: <57B56F7E.7090608@zoho.com> Date: Thu, 18 Aug 2016 16:19:10 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: zijun_hu for LP64 ABI, struct rb_node aligns at 8 bytes boundary due to sizeof(long) == 8 normally, so 0x07 should be used to extract node's parent rather than 0x03 the mask is corrected based on normal alignment of struct rb_node macros are introduced to replace hard coding numbers too Signed-off-by: zijun_hu --- an alternative to macro RB_PARENT_MASK is shown below via __alignof__ #define RB_PARENT_MASK \ (~((unsigned long)__alignof__(struct rb_node) - 1)) include/linux/rbtree.h | 4 +++- include/linux/rbtree_augmented.h | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h index e585018..f3a8596 100644 --- a/include/linux/rbtree.h +++ b/include/linux/rbtree.h @@ -44,8 +44,10 @@ struct rb_root { struct rb_node *rb_node; }; +#define RB_PARENT_MASK (~((unsigned long)sizeof(long) - 1)) -#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) +#define rb_parent(r) \ + ((struct rb_node *)((r)->__rb_parent_color & RB_PARENT_MASK)) #define RB_ROOT (struct rb_root) { NULL, } #define rb_entry(ptr, type, member) container_of(ptr, type, member) diff --git a/include/linux/rbtree_augmented.h b/include/linux/rbtree_augmented.h index d076183..0a839ea 100644 --- a/include/linux/rbtree_augmented.h +++ b/include/linux/rbtree_augmented.h @@ -96,10 +96,11 @@ rbstatic const struct rb_augment_callbacks rbname = { \ #define RB_RED 0 #define RB_BLACK 1 +#define RB_COLOR_MASK 1UL -#define __rb_parent(pc) ((struct rb_node *)(pc & ~3)) +#define __rb_parent(pc) ((struct rb_node *)(pc & RB_PARENT_MASK)) -#define __rb_color(pc) ((pc) & 1) +#define __rb_color(pc) ((pc) & RB_COLOR_MASK) #define __rb_is_black(pc) __rb_color(pc) #define __rb_is_red(pc) (!__rb_color(pc)) #define rb_color(rb) __rb_color((rb)->__rb_parent_color) -- 1.9.1