Ignore:
Timestamp:
Sep 7, 2017, 1:14:30 AM (8 years ago)
Author:
Yusuke Suzuki
Message:

[JSC] Remove "malloc" and "free" from JSC/API
https://bugs.webkit.org/show_bug.cgi?id=176331

Reviewed by Keith Miller.

Remove "malloc" and "free" manual calls in JSC/API.

  • API/JSValue.mm:

(createStructHandlerMap):

  • API/JSWrapperMap.mm:

(parsePropertyAttributes):
(makeSetterName):
(copyPrototypeProperties):
Use RetainPtr<NSString> to keep NSString. We avoid repeated "char*" to "NSString" conversion.

  • API/ObjcRuntimeExtras.h:

(adoptSystem):
Add adoptSystem to automate calling system free().

(protocolImplementsProtocol):
(forEachProtocolImplementingProtocol):
(forEachMethodInClass):
(forEachMethodInProtocol):
(forEachPropertyInProtocol):
(StringRange::StringRange):
(StringRange::operator const char* const):
(StringRange::get const):
Use CString for backend.

(StructBuffer::StructBuffer):
(StructBuffer::~StructBuffer):
(StringRange::~StringRange): Deleted.
Use fastAlignedMalloc/astAlignedFree to get aligned memory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/ObjcRuntimeExtras.h

    r218316 r221723  
    2424 */
    2525
     26#import <memory>
    2627#import <objc/Protocol.h>
    2728#import <objc/runtime.h>
    2829#import <wtf/HashSet.h>
     30#import <wtf/SystemFree.h>
    2931#import <wtf/Vector.h>
     32#import <wtf/text/CString.h>
     33
     34template<typename T, typename U>
     35inline std::unique_ptr<T, WTF::SystemFree<T>> adoptSystem(U value)
     36{
     37    return std::unique_ptr<T, WTF::SystemFree<T>>(value);
     38}
    3039
    3140inline bool protocolImplementsProtocol(Protocol *candidate, Protocol *target)
    3241{
    3342    unsigned protocolProtocolsCount;
    34     Protocol ** protocolProtocols = protocol_copyProtocolList(candidate, &protocolProtocolsCount);
     43    auto protocolProtocols = adoptSystem<Protocol*[]>(protocol_copyProtocolList(candidate, &protocolProtocolsCount));
    3544    for (unsigned i = 0; i < protocolProtocolsCount; ++i) {
    36         if (protocol_isEqual(protocolProtocols[i], target)) {
    37             free(protocolProtocols);
     45        if (protocol_isEqual(protocolProtocols[i], target))
    3846            return true;
    39         }
    40     }
    41     free(protocolProtocols);
     47    }
    4248    return false;
    4349}
     
    4854    ASSERT(target);
    4955
    50     Vector<Protocol *> worklist;
     56    Vector<Protocol*> worklist;
    5157    HashSet<void*> visited;
    5258
    5359    // Initially fill the worklist with the Class's protocols.
    54     unsigned protocolsCount;
    55     Protocol ** protocols = class_copyProtocolList(cls, &protocolsCount);
    56     worklist.append(protocols, protocolsCount);
    57     free(protocols);
     60    {
     61        unsigned protocolsCount;
     62        auto protocols = adoptSystem<Protocol*[]>(class_copyProtocolList(cls, &protocolsCount));
     63        worklist.append(protocols.get(), protocolsCount);
     64    }
    5865
    5966    bool stop = false;
     
    7481
    7582        // Add incorporated protocols to the worklist.
    76         protocols = protocol_copyProtocolList(protocol, &protocolsCount);
    77         worklist.append(protocols, protocolsCount);
    78         free(protocols);
     83        {
     84            unsigned protocolsCount;
     85            auto protocols = adoptSystem<Protocol*[]>(protocol_copyProtocolList(protocol, &protocolsCount));
     86            worklist.append(protocols.get(), protocolsCount);
     87        }
    7988    }
    8089}
     
    8392{
    8493    unsigned count;
    85     Method* methods = class_copyMethodList(cls, &count);
     94    auto methods = adoptSystem<Method[]>(class_copyMethodList(cls, &count));
    8695    for (unsigned i = 0; i < count; ++i)
    8796        callback(methods[i]);
    88     free(methods);
    8997}
    9098
     
    92100{
    93101    unsigned count;
    94     struct objc_method_description* methods = protocol_copyMethodDescriptionList(protocol, isRequiredMethod, isInstanceMethod, &count);
     102    auto methods = adoptSystem<objc_method_description[]>(protocol_copyMethodDescriptionList(protocol, isRequiredMethod, isInstanceMethod, &count));
    95103    for (unsigned i = 0; i < count; ++i)
    96104        callback(methods[i].name, methods[i].types);
    97     free(methods);
    98105}
    99106
     
    101108{
    102109    unsigned count;
    103     objc_property_t* properties = protocol_copyPropertyList(protocol, &count);
     110    auto properties = adoptSystem<objc_property_t[]>(protocol_copyPropertyList(protocol, &count));
    104111    for (unsigned i = 0; i < count; ++i)
    105112        callback(properties[i]);
    106     free(properties);
    107113}
    108114
     
    125131    WTF_MAKE_NONCOPYABLE(StringRange);
    126132public:
    127     StringRange(const char* begin, const char* end) : m_ptr(strndup(begin, end - begin)) { }
    128     ~StringRange() { free(m_ptr); }
    129     operator const char*() const { return m_ptr; }
    130     const char* get() const { return m_ptr; }
     133    StringRange(const char* begin, const char* end)
     134        : m_string(begin, end - begin)
     135    { }
     136    operator const char*() const { return m_string.data(); }
     137    const char* get() const { return m_string.data(); }
    131138
    132139private:
    133     char* m_ptr;
     140    CString m_string;
    134141};
    135142
     
    141148        NSUInteger size, alignment;
    142149        NSGetSizeAndAlignment(encodedType, &size, &alignment);
    143         --alignment;
    144         m_allocation = static_cast<char*>(malloc(size + alignment));
    145         m_buffer = reinterpret_cast<char*>((reinterpret_cast<intptr_t>(m_allocation) + alignment) & ~alignment);
    146     }
    147 
    148     ~StructBuffer() { free(m_allocation); }
     150        m_buffer = fastAlignedMalloc(alignment, size);
     151    }
     152
     153    ~StructBuffer() { fastAlignedFree(m_buffer); }
    149154    operator void*() const { return m_buffer; }
    150155
    151156private:
    152     void* m_allocation;
    153157    void* m_buffer;
    154158};
Note: See TracChangeset for help on using the changeset viewer.