From 208dafca3605f8dbcfba9abed6c01b597488cca0 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 5 Feb 2020 09:55:56 +0100 Subject: [PATCH] kernel: add CONST_BAG_HEADER --- src/gasman.c | 4 ++-- src/gasman.h | 26 +++++++++++++++++++------- src/weakptr.c | 4 ++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/gasman.c b/src/gasman.c index 4b31503bf3..36d5fc2ae4 100644 --- a/src/gasman.c +++ b/src/gasman.c @@ -540,7 +540,7 @@ static void CANARY_ALLOW_ACCESS_BAG(Bag bag) Int bagLength = SIZE_BAG(bag); VALGRIND_MAKE_MEM_DEFINED(ptr, bagLength); - BagHeader * header = BAG_HEADER(bag); + const BagHeader * header = CONST_BAG_HEADER(bag); VALGRIND_MAKE_MEM_DEFINED( header, sizeof(*header) - sizeof(header->memory_canary_padding)); } @@ -554,7 +554,7 @@ static void CANARY_FORBID_ACCESS_BAG(Bag bag) Int bagLength = SIZE_BAG(bag); VALGRIND_MAKE_MEM_NOACCESS(ptr, bagLength); - BagHeader * header = BAG_HEADER(bag); + const BagHeader * header = CONST_BAG_HEADER(bag); VALGRIND_MAKE_MEM_NOACCESS( header, sizeof(*header) - sizeof(header->memory_canary_padding)); } diff --git a/src/gasman.h b/src/gasman.h index d5d0052fa0..ee1ab05236 100644 --- a/src/gasman.h +++ b/src/gasman.h @@ -108,13 +108,20 @@ enum { /**************************************************************************** ** *F BAG_HEADER() . . . . . . . . . . . . . . . . . . . . header of a bag +*F CONST_BAG_HEADER() . . . . . . . . . . . . read-only header of a bag ** ** 'BAG_HEADER' returns the header of the bag with the identifier . */ EXPORT_INLINE BagHeader * BAG_HEADER(Bag bag) { GAP_ASSERT(bag); - return (((BagHeader *)*bag) - 1); + return ((*(BagHeader **)bag) - 1); +} + +EXPORT_INLINE const BagHeader * CONST_BAG_HEADER(Bag bag) +{ + GAP_ASSERT(bag); + return ((*(const BagHeader **)bag) - 1); } @@ -136,7 +143,7 @@ EXPORT_INLINE BagHeader * BAG_HEADER(Bag bag) */ EXPORT_INLINE UInt TNUM_BAG(Bag bag) { - return BAG_HEADER(bag)->type; + return CONST_BAG_HEADER(bag)->type; } @@ -168,7 +175,7 @@ EXPORT_INLINE UInt TNUM_BAG(Bag bag) */ EXPORT_INLINE uint8_t TEST_BAG_FLAG(Bag bag, uint8_t flag) { - return BAG_HEADER(bag)->flags & flag; + return CONST_BAG_HEADER(bag)->flags & flag; } EXPORT_INLINE void SET_BAG_FLAG(Bag bag, uint8_t flag) @@ -181,6 +188,7 @@ EXPORT_INLINE void CLEAR_BAG_FLAG(Bag bag, uint8_t flag) BAG_HEADER(bag)->flags &= ~flag; } + /**************************************************************************** ** *F IS_BAG_REF() . . . . . . verify that is a valid bag identifier @@ -209,8 +217,9 @@ EXPORT_INLINE BOOL IS_BAG_REF(Obj bag) ** the size of a bag when it allocates it with 'NewBag' and may later change ** it with 'ResizeBag' (see "NewBag" and "ResizeBag"). */ -EXPORT_INLINE UInt SIZE_BAG(Bag bag) { - return BAG_HEADER(bag)->size; +EXPORT_INLINE UInt SIZE_BAG(Bag bag) +{ + return CONST_BAG_HEADER(bag)->size; } @@ -223,7 +232,8 @@ EXPORT_INLINE UInt SIZE_BAG(Bag bag) { ** atomic operations that require a memory barrier in between dereferencing ** the bag pointer and accessing the contents of the bag. */ -EXPORT_INLINE UInt SIZE_BAG_CONTENTS(const void *ptr) { +EXPORT_INLINE UInt SIZE_BAG_CONTENTS(const void *ptr) +{ return ((const BagHeader *)ptr)[-1].size; } @@ -244,7 +254,9 @@ EXPORT_INLINE UInt SIZE_BAG_CONTENTS(const void *ptr) { /**************************************************************************** ** -*F PTR_BAG() . . . . . . . . . . . . . . . . . . . . pointer to a bag +*F PTR_BAG() . . . . . . . . . . . . . . . . . . . . . pointer to a bag +*F CONST_PTR_BAG() . . . . . . . . . . . . . read-only pointer to a bag +*F SET_PTR_BAG() . . . . . . . . . . . . . . . set the pointer to a bag ** ** 'PTR_BAG' returns the address of the data area of the bag with identifier ** . Using this pointer the application can then read data from the diff --git a/src/weakptr.c b/src/weakptr.c index f86f851f88..92e5832758 100644 --- a/src/weakptr.c +++ b/src/weakptr.c @@ -152,12 +152,12 @@ static inline void SET_ELM_WPOBJ(Obj list, UInt pos, Obj val) } if (!IS_BAG_REF(ptr[pos])) { ptr[pos] = (Bag)jl_gc_new_weakref((jl_value_t *)val); - jl_gc_wb_back(BAG_HEADER(list)); + jl_gc_wb_back(CONST_BAG_HEADER(list)); } else { jl_weakref_t * wref = (jl_weakref_t *)(ptr[pos]); wref->value = (jl_value_t *)val; - jl_gc_wb(wref, BAG_HEADER(val)); + jl_gc_wb(wref, CONST_BAG_HEADER(val)); } #endif }