]> git.saurik.com Git - apple/libdispatch.git/blobdiff - src/allocator_internal.h
libdispatch-913.30.4.tar.gz
[apple/libdispatch.git] / src / allocator_internal.h
index 5f223f65f5697f062422aceecf3edf3ad1a48b9a..abe4a1d438a3bc744a986b1917a9bdeb668ab645 100644 (file)
 #endif
 #endif
 
 #endif
 #endif
 
-#if TARGET_IPHONE_SIMULATOR && IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED < 1090
-#undef DISPATCH_USE_NANOZONE
-#define DISPATCH_USE_NANOZONE 0
-#endif
 #ifndef DISPATCH_USE_NANOZONE
 #ifndef DISPATCH_USE_NANOZONE
-#if TARGET_OS_MAC && defined(__LP64__) && \
-               (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 || \
-               __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000)
+#if TARGET_OS_MAC && defined(__LP64__)
 #define DISPATCH_USE_NANOZONE 1
 #endif
 #endif
 #define DISPATCH_USE_NANOZONE 1
 #endif
 #endif
@@ -74,7 +68,7 @@
 #if DISPATCH_ALLOCATOR
 
 // Configuration here!
 #if DISPATCH_ALLOCATOR
 
 // Configuration here!
-#define NUM_CPU _dispatch_hw_config.cc_max_logical
+#define NUM_CPU dispatch_hw_config(logical_cpus)
 #define MAGAZINES_PER_HEAP (NUM_CPU)
 
 // Do you care about compaction or performance?
 #define MAGAZINES_PER_HEAP (NUM_CPU)
 
 // Do you care about compaction or performance?
 #define PACK_FIRST_PAGE_WITH_CONTINUATIONS 0
 #endif
 
 #define PACK_FIRST_PAGE_WITH_CONTINUATIONS 0
 #endif
 
+#ifndef PAGE_MAX_SIZE
+#define PAGE_MAX_SIZE PAGE_SIZE
+#endif
+#ifndef PAGE_MAX_MASK
+#define PAGE_MAX_MASK PAGE_MASK
+#endif
+#define DISPATCH_ALLOCATOR_PAGE_SIZE PAGE_MAX_SIZE
+#define DISPATCH_ALLOCATOR_PAGE_MASK PAGE_MAX_MASK
+
+
 #if TARGET_OS_EMBEDDED
 #define PAGES_PER_MAGAZINE 64
 #else
 #if TARGET_OS_EMBEDDED
 #define PAGES_PER_MAGAZINE 64
 #else
 #endif
 
 // Use the largest type your platform is comfortable doing atomic ops with.
 #endif
 
 // Use the largest type your platform is comfortable doing atomic ops with.
-#if defined(__x86_64__) // TODO: rdar://11477843
+// TODO: rdar://11477843
 typedef unsigned long bitmap_t;
 typedef unsigned long bitmap_t;
+#if defined(__LP64__)
 #define BYTES_PER_BITMAP 8
 #else
 #define BYTES_PER_BITMAP 8
 #else
-typedef uint32_t bitmap_t;
 #define BYTES_PER_BITMAP 4
 #endif
 
 #define BYTES_PER_BITMAP 4
 #endif
 
@@ -107,7 +111,7 @@ typedef uint32_t bitmap_t;
 #define CONTINUATIONS_PER_BITMAP (BYTES_PER_BITMAP * 8)
 #define BITMAPS_PER_SUPERMAP (BYTES_PER_SUPERMAP * 8)
 
 #define CONTINUATIONS_PER_BITMAP (BYTES_PER_BITMAP * 8)
 #define BITMAPS_PER_SUPERMAP (BYTES_PER_SUPERMAP * 8)
 
-#define BYTES_PER_MAGAZINE (PAGES_PER_MAGAZINE * PAGE_SIZE)
+#define BYTES_PER_MAGAZINE (PAGES_PER_MAGAZINE * DISPATCH_ALLOCATOR_PAGE_SIZE)
 #define CONSUMED_BYTES_PER_BITMAP (BYTES_PER_BITMAP + \
                (DISPATCH_CONTINUATION_SIZE * CONTINUATIONS_PER_BITMAP))
 
 #define CONSUMED_BYTES_PER_BITMAP (BYTES_PER_BITMAP + \
                (DISPATCH_CONTINUATION_SIZE * CONTINUATIONS_PER_BITMAP))
 
@@ -117,7 +121,7 @@ typedef uint32_t bitmap_t;
 
 #define BYTES_PER_HEAP (BYTES_PER_MAGAZINE * MAGAZINES_PER_HEAP)
 
 
 #define BYTES_PER_HEAP (BYTES_PER_MAGAZINE * MAGAZINES_PER_HEAP)
 
-#define BYTES_PER_PAGE PAGE_SIZE
+#define BYTES_PER_PAGE DISPATCH_ALLOCATOR_PAGE_SIZE
 #define CONTINUATIONS_PER_PAGE (BYTES_PER_PAGE / DISPATCH_CONTINUATION_SIZE)
 #define BITMAPS_PER_PAGE (CONTINUATIONS_PER_PAGE / CONTINUATIONS_PER_BITMAP)
 
 #define CONTINUATIONS_PER_PAGE (BYTES_PER_PAGE / DISPATCH_CONTINUATION_SIZE)
 #define BITMAPS_PER_PAGE (CONTINUATIONS_PER_PAGE / CONTINUATIONS_PER_BITMAP)
 
@@ -131,6 +135,16 @@ typedef uint32_t bitmap_t;
 #define HEAP_MASK (~(uintptr_t)(BYTES_PER_HEAP - 1))
 #define MAGAZINE_MASK (~(uintptr_t)(BYTES_PER_MAGAZINE - 1))
 
 #define HEAP_MASK (~(uintptr_t)(BYTES_PER_HEAP - 1))
 #define MAGAZINE_MASK (~(uintptr_t)(BYTES_PER_MAGAZINE - 1))
 
+// 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))
+// Since these are both powers of two, we end up with not only the max alignment,
+// but happily the least common multiple, which will be the greater of the two.
+#define ROUND_UP_TO_BITMAP_ALIGNMENT_AND_CONTINUATION_SIZE(x) (ROUND_UP_TO_CONTINUATION_SIZE(ROUND_UP_TO_BITMAP_ALIGNMENT(x)))
+#define PADDING_TO_BITMAP_ALIGNMENT_AND_CONTINUATION_SIZE(x) (ROUND_UP_TO_BITMAP_ALIGNMENT_AND_CONTINUATION_SIZE(x) - (x))
+
 #define PADDING_TO_CONTINUATION_SIZE(x) (ROUND_UP_TO_CONTINUATION_SIZE(x) - (x))
 
 #if defined(__LP64__)
 #define PADDING_TO_CONTINUATION_SIZE(x) (ROUND_UP_TO_CONTINUATION_SIZE(x) - (x))
 
 #if defined(__LP64__)
@@ -145,8 +159,11 @@ typedef uint32_t bitmap_t;
 
 // header is expected to end on supermap's required alignment
 #define HEADER_TO_SUPERMAPS_PADDING 0
 
 // header is expected to end on supermap's required alignment
 #define HEADER_TO_SUPERMAPS_PADDING 0
-#define SUPERMAPS_TO_MAPS_PADDING (PADDING_TO_CONTINUATION_SIZE( \
+// 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_HEADER))
+
 #define MAPS_TO_FPMAPS_PADDING (PADDING_TO_CONTINUATION_SIZE(SIZEOF_MAPS))
 
 #define BYTES_LEFT_IN_FIRST_PAGE (BYTES_PER_PAGE - \
 #define MAPS_TO_FPMAPS_PADDING (PADDING_TO_CONTINUATION_SIZE(SIZEOF_MAPS))
 
 #define BYTES_LEFT_IN_FIRST_PAGE (BYTES_PER_PAGE - \
@@ -159,7 +176,8 @@ typedef uint32_t bitmap_t;
                (BYTES_LEFT_IN_FIRST_PAGE / CONSUMED_BYTES_PER_BITMAP)
 #define REMAINDER_IN_FIRST_PAGE (BYTES_LEFT_IN_FIRST_PAGE - \
                (FULL_BITMAPS_IN_FIRST_PAGE * CONSUMED_BYTES_PER_BITMAP) - \
                (BYTES_LEFT_IN_FIRST_PAGE / CONSUMED_BYTES_PER_BITMAP)
 #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)))
+               (FULL_BITMAPS_IN_FIRST_PAGE ? 0 : \
+               ROUND_UP_TO_CONTINUATION_SIZE(BYTES_PER_BITMAP)))
 
 #define REMAINDERED_CONTINUATIONS_IN_FIRST_PAGE \
                (REMAINDER_IN_FIRST_PAGE / DISPATCH_CONTINUATION_SIZE)
 
 #define REMAINDERED_CONTINUATIONS_IN_FIRST_PAGE \
                (REMAINDER_IN_FIRST_PAGE / DISPATCH_CONTINUATION_SIZE)
@@ -194,7 +212,7 @@ struct dispatch_magazine_header_s {
        // Link to the next heap in the chain. Only used in magazine 0's header
        dispatch_heap_t dh_next;
 
        // Link to the next heap in the chain. Only used in magazine 0's header
        dispatch_heap_t dh_next;
 
-       // Points to the first bitmap in the page where this CPU succesfully
+       // Points to the first bitmap in the page where this CPU successfully
        // allocated a continuation last time. Only used in the first heap.
        bitmap_t *last_found_page;
 };
        // allocated a continuation last time. Only used in the first heap.
        bitmap_t *last_found_page;
 };