Changeset 71169 in webkit


Ignore:
Timestamp:
Nov 2, 2010 2:18:18 PM (13 years ago)
Author:
andersca@apple.com
Message:

Add convenience functions for allocating NPAPI memory
https://bugs.webkit.org/show_bug.cgi?id=48870

Reviewed by Dan Bernstein.

Add npnMemAlloc, npnMemFree and createNPString functions and
npnMemNew, npnMemNewArray function templates.

  • WebProcess/Plugins/Netscape/JSNPObject.cpp:

(WebKit::JSNPObject::getOwnPropertyNames):
Use npnMemFree.

  • WebProcess/Plugins/Netscape/NPJSObject.cpp:

(WebKit::NPJSObject::enumerate):
Use npnMemNewArray.

  • WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp:

(WebKit::NPRuntimeObjectMap::convertJSValueToNPVariant):
Use createNPString.

  • WebProcess/Plugins/Netscape/NPRuntimeUtilities.cpp:

(WebKit::npnMemAlloc):
Call malloc.

(WebKit::npnMemFree):
Call free.

(WebKit::createNPString):
Allocate the characters for a new NPString.

(WebKit::createNPObject):
Call npnMemNew.

(WebKit::deallocateNPObject):
Call npnMemFree.

(WebKit::releaseNPVariantValue):
Call npnMemFree.

  • WebProcess/Plugins/Netscape/NPRuntimeUtilities.h:

(WebKit::npnMemNew):
(WebKit::npnMemNewArray):
Add new functions.

  • WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp:

(WebKit::NPN_MemAlloc):
Call npnMemAlloc.

(WebKit::NPN_MemFree):
Call npnMemFree.

(WebKit::NPN_UTF8FromIdentifier):
Call npnMemNewArray.

(WebKit::copyCString):
Call npnMemNewArray. Correctly initialize the "len" out parameter.

Location:
trunk/WebKit2
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKit2/ChangeLog

    r71168 r71169  
     12010-11-02  Anders Carlsson  <andersca@apple.com>
     2
     3        Reviewed by Dan Bernstein.
     4
     5        Add convenience functions for allocating NPAPI memory
     6        https://bugs.webkit.org/show_bug.cgi?id=48870
     7
     8        Add npnMemAlloc, npnMemFree and createNPString functions and
     9        npnMemNew, npnMemNewArray function templates.
     10
     11        * WebProcess/Plugins/Netscape/JSNPObject.cpp:
     12        (WebKit::JSNPObject::getOwnPropertyNames):
     13        Use npnMemFree.
     14
     15        * WebProcess/Plugins/Netscape/NPJSObject.cpp:
     16        (WebKit::NPJSObject::enumerate):
     17        Use npnMemNewArray.
     18
     19        * WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp:
     20        (WebKit::NPRuntimeObjectMap::convertJSValueToNPVariant):
     21        Use createNPString.
     22
     23        * WebProcess/Plugins/Netscape/NPRuntimeUtilities.cpp:
     24        (WebKit::npnMemAlloc):
     25        Call malloc.
     26
     27        (WebKit::npnMemFree):
     28        Call free.
     29
     30        (WebKit::createNPString):
     31        Allocate the characters for a new NPString.
     32
     33        (WebKit::createNPObject):
     34        Call npnMemNew.
     35
     36        (WebKit::deallocateNPObject):
     37        Call npnMemFree.
     38
     39        (WebKit::releaseNPVariantValue):
     40        Call npnMemFree.
     41
     42        * WebProcess/Plugins/Netscape/NPRuntimeUtilities.h:
     43        (WebKit::npnMemNew):
     44        (WebKit::npnMemNewArray):
     45        Add new functions.
     46
     47        * WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp:
     48        (WebKit::NPN_MemAlloc):
     49        Call npnMemAlloc.
     50
     51        (WebKit::NPN_MemFree):
     52        Call npnMemFree.
     53
     54        (WebKit::NPN_UTF8FromIdentifier):
     55        Call npnMemNewArray.
     56
     57        (WebKit::copyCString):
     58        Call npnMemNewArray. Correctly initialize the "len" out parameter.
     59
    1602010-11-02  Jessie Berlin  <jberlin@apple.com>
    261
  • trunk/WebKit2/WebProcess/Plugins/Netscape/JSNPObject.cpp

    r70898 r71169  
    356356    }
    357357
    358     // This should use NPN_MemFree, but we know that it uses free under the hood.
    359     free(identifiers);
     358    npnMemFree(identifiers);
    360359}
    361360
  • trunk/WebKit2/WebProcess/Plugins/Netscape/NPJSObject.cpp

    r71054 r71169  
    239239    m_jsObject->getPropertyNames(exec, propertyNames);
    240240
    241     // This should use NPN_MemAlloc, but we know that it uses malloc under the hood.
    242     NPIdentifier* nameIdentifiers = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier) * propertyNames.size()));
     241    NPIdentifier* nameIdentifiers = npnMemNewArray<NPIdentifier>(propertyNames.size());
    243242
    244243    for (size_t i = 0; i < propertyNames.size(); ++i)
  • trunk/WebKit2/WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp

    r70898 r71169  
    166166
    167167    if (value.isString()) {
    168         CString utf8String = value.toString(exec).utf8();
    169 
    170         // This should use NPN_MemAlloc, but we know that it uses malloc under the hood.
    171         char* utf8Characters = static_cast<char*>(malloc(utf8String.length()));
    172         memcpy(utf8Characters, utf8String.data(), utf8String.length());
    173        
    174         STRINGN_TO_NPVARIANT(utf8Characters, utf8String.length(), variant);
     168        NPString npString = createNPString(value.toString(exec).utf8());
     169        STRINGN_TO_NPVARIANT(npString.UTF8Characters, npString.UTF8Length, variant);
    175170        return;
    176171    }
  • trunk/WebKit2/WebProcess/Plugins/Netscape/NPRuntimeUtilities.cpp

    r70898 r71169  
    2626#include "NPRuntimeUtilities.h"
    2727
     28#include <wtf/text/CString.h>
     29
    2830namespace WebKit {
     31
     32void* npnMemAlloc(uint32_t size)
     33{
     34    // We could use fastMalloc here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free,
     35    // so having them be equivalent seems like a good idea.
     36    return malloc(size);
     37}
     38
     39void npnMemFree(void* ptr)
     40{
     41    // We could use fastFree here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free,
     42    // so having them be equivalent seems like a good idea.
     43    free(ptr);
     44}
     45
     46NPString createNPString(const CString& string)
     47{
     48    char* utf8Characters = npnMemNewArray<char>(string.length());
     49    memcpy(utf8Characters, string.data(), string.length());
     50
     51    NPString npString;
     52    npString.UTF8Characters = utf8Characters;
     53    npString.UTF8Length = string.length();
     54
     55    return npString;
     56}
    2957
    3058NPObject* createNPObject(NPP npp, NPClass* npClass)
     
    3563    if (npClass->allocate)
    3664        npObject = npClass->allocate(npp, npClass);
    37     else {
    38         // This should use NPN_MemAlloc, but we know that it uses malloc under the hood.
    39         npObject = static_cast<NPObject*>(malloc(sizeof(NPObject)));
    40     }
     65    else
     66        npObject = npnMemNew<NPObject>();
    4167
    4268    npObject->_class = npClass;
     
    5480    if (npObject->_class->deallocate)
    5581        npObject->_class->deallocate(npObject);
    56     else {
    57         // This should really call NPN_MemFree, but we know that it uses free
    58         // under the hood so it's fine.
    59         free(npObject);
    60     }
     82    else
     83        npnMemFree(npObject);
    6184}
    6285
     
    96119       
    97120    case NPVariantType_String:
    98         free(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters));
     121        npnMemFree(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters));
    99122        variant->value.stringValue.UTF8Characters = 0;
    100123        variant->value.stringValue.UTF8Length = 0;
  • trunk/WebKit2/WebProcess/Plugins/Netscape/NPRuntimeUtilities.h

    r70898 r71169  
    2828
    2929#include <WebCore/npruntime_internal.h>
     30#include <wtf/Forward.h>
    3031
    3132struct NPClass;
     
    3334
    3435namespace WebKit {
     36
     37void* npnMemAlloc(uint32_t);
     38void npnMemFree(void*);
     39
     40template<typename T> T* npnMemNew()
     41{
     42    return static_cast<T*>(npnMemAlloc(sizeof(T)));
     43}
     44
     45template<typename T> T* npnMemNewArray(size_t count)
     46{
     47    return static_cast<T*>(npnMemAlloc(sizeof(T) * count));
     48}
     49
     50NPString createNPString(const CString&);
    3551
    3652NPObject* createNPObject(NPP, NPClass*);
  • trunk/WebKit2/WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp

    r68962 r71169  
    337337static void* NPN_MemAlloc(uint32_t size)
    338338{
    339     // We could use fastMalloc here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free,
    340     // so having them be equivalent seems like a good idea.
    341     return malloc(size);
     339    return npnMemAlloc(size);
    342340}
    343341
    344342static void NPN_MemFree(void* ptr)
    345343{
    346     // We could use fastFree here, but there might be plug-ins that mix NPN_MemAlloc/NPN_MemFree with malloc and free,
    347     // so having them be equivalent seems like a good idea.
    348     free(ptr);
     344    npnMemFree(ptr);
    349345}
    350346
     
    549545
    550546    uint32_t stringLength = strlen(string);
    551     char* utf8String = static_cast<char*>(NPN_MemAlloc(stringLength + 1));
     547    char* utf8String = npnMemNewArray<char>(stringLength + 1);
    552548    memcpy(utf8String, string, stringLength);
    553549    utf8String[stringLength] = '\0';
     
    688684    ASSERT(len);
    689685
    690     *value = static_cast<char*>(NPN_MemAlloc(string.length()));
     686    *value = npnMemNewArray<char>(string.length());
    691687    if (!*value)
    692688        return NPERR_GENERIC_ERROR;
    693689
    694690    memcpy(*value, string.data(), string.length());
     691    *len = string.length();
    695692    return NPERR_NO_ERROR;
    696693}
Note: See TracChangeset for help on using the changeset viewer.