iosgrand-central-dispatchlibdispatch

what's meaning of this code from the libdispatch (apple-open-source)?


I am having hard time understanding the following code:

struct dispatch_block_private_data_s {
    DISPATCH_BLOCK_PRIVATE_DATA_HEADER();
    static void* operator new(size_t) = delete;
    static void* operator new [] (size_t) = delete;
    explicit inline DISPATCH_ALWAYS_INLINE dispatch_block_private_data_s(
            dispatch_block_flags_t flags, voucher_t voucher,
            pthread_priority_t priority, dispatch_block_t block) noexcept :
            dbpd_magic(), dbpd_flags(flags), dbpd_atomic_flags(),
            dbpd_performed(), dbpd_priority(priority), dbpd_voucher(voucher),
            dbpd_block(block), dbpd_group(), dbpd_queue(), dbpd_thread()
    {
        // stack structure constructor, no releases on destruction
        _dispatch_block_private_data_debug("create, block: %p", dbpd_block);
    }
};

What's the static void* operator new(size_t) = delete; and why inline func in struct? Who can help me learn with these code ? Here is the code address


Solution

  • Note the .cpp extension. This is C++ code.

    1. The operator ... = delete syntax says that this operator should be suppressed, generating a compiler warning if you try to use it.

    2. The inline qualifier is a performance optimization. To quote from The C++ Programming Language:

      The inline specifier is a hint to the compiler that it should attempt to generate code for a call of [the function] inline rather than laying down the code for the function once and then calling through the usual function call mechanism.

      If (a) a function is small; and (b) performance is of paramount concern, you can use inline qualifier so that the compiler will, effectively, just insert the code of the function wherever you use it rather than saving it as a function and calling it you normally would. This saves the modest overhead of calling the function.

    If you need help understanding C++, I’d suggest you check out these resources.