Staging: android: Redefine macros as static inline functions
cover Staging: android: Redefine macros as static inline functions
Convert macros page_in_range, range_before_page, page_range_{subsumes/subsumed_by/in}_rang into static inline functions as static inline functions are preferred over macros. The change can be done as the arguments at all call sites have the same type.
Bhumika Goyal (2):
Staging: android: ashmem.c: Redefine macros as static inline functions
Staging: android: ashmem.c: Convert macros
page_range_{subsumes/subsumed_by/in}_range to static inline function
drivers/staging/android/ashmem.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/2 Staging: android: ashmem.c: Redefine macros as static inline functions
Convert macros page_in_range and range_before_page into static inline functions as static inline functions are preferred over macros. The change can be done as the arguments at all call sites have the same type. Also, both the macros have same type of arguments and return values.
Done using coccinelle:
@r@
identifier f;
expression e;
@@
#define f(...) e
@r1@
identifier r.f;
identifier range,page;
expression r.e;
@@
- #define f(range,page) e
+ static inline int f(struct ashmem_range *range, size_t page)
+ {
+ return e;
+ }
--- drivers/staging/android/ashmem.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/staging/android/ashmem.c +8 -4
@@ -112,15 +112,19 @@ static struct kmem_cache *ashmem_range_cachep __read_mostly;#define page_range_subsumed_by_range (range, start, end) \ (((range)->pgstart <= (start)) && ((range)->pgend >= (end))) -#define page_in_range (range, page) \ - (((range)->pgstart <= (page)) && ((range)->pgend >= (page))) +static inline int page_in_range (struct ashmem_range *range,size_t page) +{ +return (((range)->pgstart <= (page)) && ((range)->pgend >= (page))); +}#define page_range_in_range (range, start, end) \ (page_in_range(range, start) || page_in_range(range, end) || \ page_range_subsumes_range(range, start, end)) -#define range_before_page (range, page) \ - ((range)->pgend < (page)) +static inline int range_before_page (struct ashmem_range *range,size_t page) +{ +return ((range)->pgend < (page)); +}#define PROT_MASK (PROT_EXEC | PROT_READ | PROT_WRITE)
Signature · 1 line
1.9.1
PATCH 2/2 Staging: android: ashmem.c: Convert macros page_range_{subsumes/subsumed_by/in}_range to static inline function
In reply to Bhumika GoyalConvert macros page_range_{subsumes/subsumed_by/in}_range to static inline function as static inline functions are preferred over macros. The change can be done as the arguments at all call sites have the same type. Also, all three macro have same type of arguments and return values so they can converted using a common semantic patch.
@r@
identifier f;
expression e;
@@
#define f(...) e
@r2@
identifier r.f;
identifier range,start,end;
expression r.e;
@@
- #define f(range,start,end) e
+ static inline int f(struct ashmem_range *range, size_t start, size_t end) +{
+ return e;
+}
--- drivers/staging/android/ashmem.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/staging/android/ashmem.c +16 -7
@@ -106,20 +106,29 @@ static struct kmem_cache *ashmem_range_cachep __read_mostly;#define range_on_lru (range) \ ((range)->purged == ASHMEM_NOT_PURGED) -#define page_range_subsumes_range (range, start, end) \ - (((range)->pgstart >= (start)) && ((range)->pgend <= (end))) +static inline int page_range_subsumes_range (struct ashmem_range *range, +size_t start,size_t end) +{ +return (((range)->pgstart >= (start)) && ((range)->pgend <= (end))); +} -#define page_range_subsumed_by_range (range, start, end) \ - (((range)->pgstart <= (start)) && ((range)->pgend >= (end))) +static inline int page_range_subsumed_by_range (struct ashmem_range *range, +size_t start,size_t end) +{ +return (((range)->pgstart <= (start)) && ((range)->pgend >= (end))); +}static inline int page_in_range (struct ashmem_range *range,size_t page) {return (((range)->pgstart <= (page)) && ((range)->pgend >= (page))); } -#define page_range_in_range (range, start, end) \ - (page_in_range(range, start) || page_in_range(range, end) || \ - page_range_subsumes_range(range, start, end)) +static inline int page_range_in_range (struct ashmem_range *range, +size_t start,size_t end) +{ +return (page_in_range (range, start) ||page_in_range (range, end) || +page_range_subsumes_range (range, start, end)); +}static inline int range_before_page (struct ashmem_range *range,size_t page) {
Signature · 1 line
1.9.1
Re: [Outreachy kernel] [PATCH 2/2] Staging: android: ashmem.c: Convert macros page_range_{subsumes/subsumed_by/in}_range to static inline function
Quoted 11 lines · Bhumika Goyal
Convert macros page_range_{subsumes/subsumed_by/in}_range to static inline function as static inline functions are preferred over macros. The change can be done as the arguments at all call sites have the same type. Also, all three macro have same type of arguments and return values so they can converted using a common semantic patch.
@r@
identifier f;
expression e;
@@
#define f(...) e
I don't see the point of this rule. The described strategy in the challenge problem was to have Coccinelle check the type properties of the actual arguments to the macro, but you have done that part by hand.
julia
Quoted 68 lines
@r2@
identifier r.f;
identifier range,start,end;
expression r.e;
@@
- #definef(range,start,end)e
+ static inline intf(struct ashmem_range *range, size_t start, size_t end)drivers/staging/android/ashmem.c +3
-0+{ +return e; +}Signed-off-by Bhumika Goyal <[email protected]>--- drivers/staging/android/ashmem.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)drivers/staging/android/ashmem.c +16
-7@@ -106,20 +106,29 @@ static struct kmem_cache *ashmem_range_cachep __read_mostly;#define range_on_lru (range) \ ((range)->purged == ASHMEM_NOT_PURGED) -#define page_range_subsumes_range (range, start, end) \ - (((range)->pgstart >= (start)) && ((range)->pgend <= (end))) +static inline int page_range_subsumes_range (struct ashmem_range *range, +size_t start,size_t end) +{ +return (((range)->pgstart >= (start)) && ((range)->pgend <= (end))); +} -#define page_range_subsumed_by_range (range, start, end) \ - (((range)->pgstart <= (start)) && ((range)->pgend >= (end))) +static inline int page_range_subsumed_by_range (struct ashmem_range *range, +size_t start,size_t end) +{ +return (((range)->pgstart <= (start)) && ((range)->pgend >= (end))); +}static inline int page_in_range (struct ashmem_range *range,size_t page) {return (((range)->pgstart <= (page)) && ((range)->pgend >= (page))); } -#define page_range_in_range (range, start, end) \ - (page_in_range(range, start) || page_in_range(range, end) || \ - page_range_subsumes_range(range, start, end)) +static inline int page_range_in_range (struct ashmem_range *range, +size_t start,size_t end) +{ +return (page_in_range (range, start) ||page_in_range (range, end) || +page_range_subsumes_range (range, start, end)); +}static inline int range_before_page (struct ashmem_range *range,size_t page) {Signature · 8 lines
1.9.1 -- You received this message because you are subscribed to the Google Groups "outreachy-kernel" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/8743d5b317759576c30c8ea05a04c537aec7234b.1457703530.git.bhumirks%40gmail.com. For more options, visit https://groups.google.com/d/optout.