Skip to content

Use sizeof more pervasively #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2017
Merged

Use sizeof more pervasively #242

merged 3 commits into from
Apr 27, 2017

Conversation

compnerd
Copy link
Member

Change a few places to use sizeof rather than hard coding the size of structures and types based on pointer size.

@compnerd
Copy link
Member Author

CC: @MadCoder

Copy link
Contributor

@MadCoder MadCoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can't use that compiler specific sizeof pointer

#endif
#define _DISPATCH_CONTINUATION_PTRS 8
#if DISPATCH_HW_CONFIG_UP
// UP devices don't contend on continuations so we don't need to force them to
// occupy a whole cacheline (which is intended to avoid contention)
#define DISPATCH_CONTINUATION_SIZE \
(_DISPATCH_CONTINUATION_PTRS * _DISPATCH_SIZEOF_PTR)
(_DISPATCH_CONTINUATION_PTRS * __SIZEOF_POINTER__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__SIZEOF_POINTER__ is not standard afaict which is why dispatch does it this way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's not standard. Isn't clang a requisite though? The other ones seem fine though, so, let me drop this part, and we can address it separately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, is __LP64__ standard? I don't believe that MSVC defines that.

@compnerd
Copy link
Member Author

Dropped the __SIZEOF_POINTER__ changes, which we can discuss separately.

@compnerd
Copy link
Member Author

@MadCoder anything else for this part of the change?

#else
#define SIZEOF_HEADER 8
#endif

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep the SIZEOF_HEADER to a define to the right sizeof()


#define MAPS_TO_FPMAPS_PADDING (PADDING_TO_CONTINUATION_SIZE(SIZEOF_MAPS))

#define BYTES_LEFT_IN_FIRST_PAGE (BYTES_PER_PAGE - \
(SIZEOF_HEADER + HEADER_TO_SUPERMAPS_PADDING + SIZEOF_SUPERMAPS + \
(sizeof(struct dispatch_magazine_header_s) + HEADER_TO_SUPERMAPS_PADDING + SIZEOF_SUPERMAPS + \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and keep SIZEOF_HEADER here which has the side effect of keeping it to 80 columns

@@ -97,25 +97,20 @@
// Use the largest type your platform is comfortable doing atomic ops with.
// TODO: rdar://11477843
typedef unsigned long bitmap_t;
#if defined(__LP64__)
#define BYTES_PER_BITMAP 8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the other guy, makes this sizeof(bitmap_t)


#define BITMAP_C(v) ((bitmap_t)(v))
#define BITMAP_ALL_ONES (~BITMAP_C(0))

// Stop configuring.

#define CONTINUATIONS_PER_BITMAP (BYTES_PER_BITMAP * 8)
#define CONTINUATIONS_PER_BITMAP (sizeof(bitmap_t) * 8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're feeling pedantic, that 8 should be CHAR_BIT

@@ -138,8 +133,8 @@ typedef unsigned long bitmap_t;
// this will round up such that first_bitmap_in_same_page() can mask the address
// of a bitmap_t in the maps to obtain the first bitmap for that same page
#define ROUND_UP_TO_BITMAP_ALIGNMENT(x) \
(((x) + ((BITMAPS_PER_PAGE * BYTES_PER_BITMAP) - 1u)) & \
~((BITMAPS_PER_PAGE * BYTES_PER_BITMAP) - 1u))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@@ -148,7 +143,7 @@ typedef unsigned long bitmap_t;
#define PADDING_TO_CONTINUATION_SIZE(x) (ROUND_UP_TO_CONTINUATION_SIZE(x) - (x))

#define SIZEOF_SUPERMAPS (BYTES_PER_SUPERMAP * SUPERMAPS_PER_MAGAZINE)
#define SIZEOF_MAPS (BYTES_PER_BITMAP * BITMAPS_PER_SUPERMAP * \
#define SIZEOF_MAPS (sizeof(bitmap_t) * BITMAPS_PER_SUPERMAP * \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@@ -171,7 +166,7 @@ typedef unsigned long bitmap_t;
#define REMAINDER_IN_FIRST_PAGE (BYTES_LEFT_IN_FIRST_PAGE - \
(FULL_BITMAPS_IN_FIRST_PAGE * CONSUMED_BYTES_PER_BITMAP) - \
(FULL_BITMAPS_IN_FIRST_PAGE ? 0 : \
ROUND_UP_TO_CONTINUATION_SIZE(BYTES_PER_BITMAP)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@@ -181,7 +176,7 @@ typedef unsigned long bitmap_t;
(REMAINDERED_CONTINUATIONS_IN_FIRST_PAGE == 0 ? 0 : 1))

#define FPMAPS_TO_FPCONTS_PADDING (PADDING_TO_CONTINUATION_SIZE(\
BYTES_PER_BITMAP * BITMAPS_IN_FIRST_PAGE))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

@@ -162,12 +156,12 @@ typedef unsigned long bitmap_t;
// we want to align the maps to a continuation size, but we must also have proper padding
// so that we can perform first_bitmap_in_same_page()
#define SUPERMAPS_TO_MAPS_PADDING (PADDING_TO_BITMAP_ALIGNMENT_AND_CONTINUATION_SIZE( \
SIZEOF_SUPERMAPS + HEADER_TO_SUPERMAPS_PADDING + SIZEOF_HEADER))
SIZEOF_SUPERMAPS + HEADER_TO_SUPERMAPS_PADDING + sizeof(struct dispatch_magazine_header_s)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

Rather than trying to compute the size of the magazine header manually,
use the compiler's builtin `sizeof` operator.
Rather than hardcoding the sizeof(bitmap_t) based on `__LP64__` use the
compiler builtin `sizeof` to compute the size.
@compnerd
Copy link
Member Author

Updated and also changed the 8 to CHAR_BIT.

@MadCoder
Copy link
Contributor

@swift-ci please test

@MadCoder
Copy link
Contributor

@swift-ci please test

@compnerd
Copy link
Member Author

@MadCoder good to go on this?

@MadCoder MadCoder merged commit 14d3d90 into swiftlang:master Apr 27, 2017
@compnerd compnerd deleted the sizeof branch April 28, 2017 22:27
@das
Copy link
Contributor

das commented May 19, 2017

I'm going to revert this, it breaks the build on Darwin with:

In file included from libdispatch/src/allocator.c:22:
libdispatch/src/allocator_internal.h:240:5: error: function-like macro 'sizeof' is not defined
#if SUPERMAPS_TO_MAPS_PADDING > 0
    ^
libdispatch/src/allocator_internal.h:168:3: note: expanded from macro 'SUPERMAPS_TO_MAPS_PADDING'
                SIZEOF_SUPERMAPS + HEADER_TO_SUPERMAPS_PADDING + SIZEOF_HEADER))
                ^
libdispatch/src/allocator_internal.h:159:27: note: expanded from macro 'SIZEOF_SUPERMAPS'
#define SIZEOF_SUPERMAPS (BYTES_PER_SUPERMAP * SUPERMAPS_PER_MAGAZINE)
                          ^
libdispatch/src/allocator_internal.h:125:28: note: expanded from macro 'BYTES_PER_SUPERMAP'
#define BYTES_PER_SUPERMAP sizeof(bitmap_t)
                           ^
libdispatch/src/allocator_internal.h:248:5: error: function-like macro 'sizeof' is not defined
#if MAPS_TO_FPMAPS_PADDING > 0
    ^
libdispatch/src/allocator_internal.h:170:62: note: expanded from macro 'MAPS_TO_FPMAPS_PADDING'
#define MAPS_TO_FPMAPS_PADDING (PADDING_TO_CONTINUATION_SIZE(SIZEOF_MAPS))
                                                             ^
libdispatch/src/allocator_internal.h:160:22: note: expanded from macro 'SIZEOF_MAPS'
#define SIZEOF_MAPS (BYTES_PER_BITMAP * BITMAPS_PER_SUPERMAP * \
                     ^
libdispatch/src/allocator_internal.h:111:26: note: expanded from macro 'BYTES_PER_BITMAP'
#define BYTES_PER_BITMAP sizeof(bitmap_t)
                         ^
libdispatch/src/allocator_internal.h:258:5: error: function-like macro 'sizeof' is not defined
#if FPMAPS_TO_FPCONTS_PADDING > 0
    ^
libdispatch/src/allocator_internal.h:193:3: note: expanded from macro 'FPMAPS_TO_FPCONTS_PADDING'
                BYTES_PER_BITMAP * BITMAPS_IN_FIRST_PAGE))
                ^
libdispatch/src/allocator_internal.h:111:26: note: expanded from macro 'BYTES_PER_BITMAP'
#define BYTES_PER_BITMAP sizeof(bitmap_t)
                         ^
libdispatch/src/allocator_internal.h:278:5: error: function-like macro 'sizeof' is not defined
#if AFTER_CONTS_PADDING > 0
    ^
libdispatch/src/allocator_internal.h:202:33: note: expanded from macro 'AFTER_CONTS_PADDING'
                (DISPATCH_CONTINUATION_SIZE * CONTINUATIONS_PER_MAGAZINE)))
                                              ^
libdispatch/src/allocator_internal.h:140:4: note: expanded from macro 'CONTINUATIONS_PER_MAGAZINE'
                (BITMAPS_PER_MAGAZINE * CONTINUATIONS_PER_BITMAP)
                 ^
libdispatch/src/allocator_internal.h:138:31: note: expanded from macro 'BITMAPS_PER_MAGAZINE'
#define BITMAPS_PER_MAGAZINE (SUPERMAPS_PER_MAGAZINE * BITMAPS_PER_SUPERMAP)
                              ^
libdispatch/src/allocator_internal.h:137:3: note: expanded from macro 'SUPERMAPS_PER_MAGAZINE'
                CONSUMED_BYTES_PER_SUPERMAP)
                ^
libdispatch/src/allocator_internal.h:126:38: note: expanded from macro 'CONSUMED_BYTES_PER_SUPERMAP'
#define CONSUMED_BYTES_PER_SUPERMAP (BYTES_PER_SUPERMAP + \
                                     ^
libdispatch/src/allocator_internal.h:125:28: note: expanded from macro 'BYTES_PER_SUPERMAP'
#define BYTES_PER_SUPERMAP sizeof(bitmap_t)
                           ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants