Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible
@ 2020-05-12 23:27 Vladislav Shpilevoy
  2020-05-12 23:45 ` Timur Safin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Vladislav Shpilevoy @ 2020-05-12 23:27 UTC (permalink / raw)
  To: tarantool-patches, tsafin, gorcunov

Rlist implemented its own offsetof() as a classic NULL
pointer cast to the target type with taking address of
the member. So it turned into NULL + member offset.

But appeared undefined behaviour clang sanitizer is
not friendly to this hack.

The patch makes rlist define its own offsetof only as
a last resort.

Part of https://github.com/tarantool/tarantool/issues/4609
---
Branch: http://github.com/tarantool/small/tree/gerold103/rlist-offsetof
Issue: https://github.com/tarantool/tarantool/issues/4609

 small/rlist.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/small/rlist.h b/small/rlist.h
index 2b0bcb4..26edd2e 100644
--- a/small/rlist.h
+++ b/small/rlist.h
@@ -40,6 +40,10 @@ extern "C" {
 #define typeof __typeof__
 #endif
 
+#ifndef offsetof
+#define offsetof(type, member) ((size_t) &((type *)0)->member)
+#endif
+
 /**
  * list entry and head structure
  */
@@ -272,8 +276,9 @@ rlist_cut_before(struct rlist *head1, struct rlist *head2, struct rlist *item)
  * return entry by list item
  */
 #define rlist_entry(item, type, member) ({				\
-	const typeof( ((type *)0)->member ) *__mptr = (item);		\
-	(type *)( (char *)__mptr - ((size_t) &((type *)0)->member) ); })
+	const typeof(((type *)0)->member) *__mptr = (item);		\
+	(type *)( (char *)__mptr - offsetof(type,member));		\
+})
 
 /**
  * return first entry
-- 
2.21.1 (Apple Git-122.3)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible
  2020-05-12 23:27 [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible Vladislav Shpilevoy
@ 2020-05-12 23:45 ` Timur Safin
  2020-05-13  7:39 ` Cyrill Gorcunov
  2020-05-13 22:21 ` Vladislav Shpilevoy
  2 siblings, 0 replies; 4+ messages in thread
From: Timur Safin @ 2020-05-12 23:45 UTC (permalink / raw)
  To: 'Vladislav Shpilevoy', tarantool-patches, gorcunov

LGTM

: -----Original Message-----
: From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
: Sent: Wednesday, May 13, 2020 2:27 AM
: To: tarantool-patches@dev.tarantool.org; tsafin@tarantool.org;
: gorcunov@gmail.com
: Subject: [PATCH small 1/1] rlist: use built-in offsetof() when possible
: 
: Rlist implemented its own offsetof() as a classic NULL
: pointer cast to the target type with taking address of
: the member. So it turned into NULL + member offset.
: 
: But appeared undefined behaviour clang sanitizer is
: not friendly to this hack.
: 
: The patch makes rlist define its own offsetof only as
: a last resort.
: 
: Part of https://github.com/tarantool/tarantool/issues/4609
: ---
: Branch: http://github.com/tarantool/small/tree/gerold103/rlist-offsetof
: Issue: https://github.com/tarantool/tarantool/issues/4609
: 
:  small/rlist.h | 9 +++++++--
:  1 file changed, 7 insertions(+), 2 deletions(-)
: 
: diff --git a/small/rlist.h b/small/rlist.h
: index 2b0bcb4..26edd2e 100644
: --- a/small/rlist.h
: +++ b/small/rlist.h
: @@ -40,6 +40,10 @@ extern "C" {
:  #define typeof __typeof__
:  #endif
: 
: +#ifndef offsetof
: +#define offsetof(type, member) ((size_t) &((type *)0)->member)
: +#endif
: +
:  /**
:   * list entry and head structure
:   */
: @@ -272,8 +276,9 @@ rlist_cut_before(struct rlist *head1, struct rlist
: *head2, struct rlist *item)
:   * return entry by list item
:   */
:  #define rlist_entry(item, type, member) ({				\
: -	const typeof( ((type *)0)->member ) *__mptr = (item);		\
: -	(type *)( (char *)__mptr - ((size_t) &((type *)0)->member) ); })
: +	const typeof(((type *)0)->member) *__mptr = (item);		\
: +	(type *)( (char *)__mptr - offsetof(type,member));		\
: +})
: 
:  /**
:   * return first entry
: --
: 2.21.1 (Apple Git-122.3)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible
  2020-05-12 23:27 [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible Vladislav Shpilevoy
  2020-05-12 23:45 ` Timur Safin
@ 2020-05-13  7:39 ` Cyrill Gorcunov
  2020-05-13 22:21 ` Vladislav Shpilevoy
  2 siblings, 0 replies; 4+ messages in thread
From: Cyrill Gorcunov @ 2020-05-13  7:39 UTC (permalink / raw)
  To: Vladislav Shpilevoy; +Cc: tarantool-patches

On Wed, May 13, 2020 at 01:27:25AM +0200, Vladislav Shpilevoy wrote:
> Rlist implemented its own offsetof() as a classic NULL
> pointer cast to the target type with taking address of
> the member. So it turned into NULL + member offset.
> 
> But appeared undefined behaviour clang sanitizer is
> not friendly to this hack.
> 
> The patch makes rlist define its own offsetof only as
> a last resort.
> 
> Part of https://github.com/tarantool/tarantool/issues/4609

I think one day we should gather all such tricks and helpers
into compiler.h. Still while we didn't yet

Reviewed-by: Cyrill Gorcunov <gorcunov@gmail.com>

thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible
  2020-05-12 23:27 [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible Vladislav Shpilevoy
  2020-05-12 23:45 ` Timur Safin
  2020-05-13  7:39 ` Cyrill Gorcunov
@ 2020-05-13 22:21 ` Vladislav Shpilevoy
  2 siblings, 0 replies; 4+ messages in thread
From: Vladislav Shpilevoy @ 2020-05-13 22:21 UTC (permalink / raw)
  To: tarantool-patches, tsafin, gorcunov

Thanks for the review. Pushed to the master in tarantool/small. I didn't
update small version in tarantool/tarantool yet, because more patches will
come to small soon. Better merge them all at once later.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-13 22:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-12 23:27 [Tarantool-patches] [PATCH small 1/1] rlist: use built-in offsetof() when possible Vladislav Shpilevoy
2020-05-12 23:45 ` Timur Safin
2020-05-13  7:39 ` Cyrill Gorcunov
2020-05-13 22:21 ` Vladislav Shpilevoy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox