Ignore:
Timestamp:
Apr 8, 2009, 6:14:07 PM (16 years ago)
Author:
[email protected]
Message:

2009-04-08 Paul Pedriana <[email protected]>

Reviewed by Darin Adler.

https://bugs.webkit.org/show_bug.cgi?id=20422
Allow custom memory allocation control.

  • wtf/FastAllocBase.h: New added file. Implements allocation base class.
  • wtf/TypeTraits.h: Augments existing type traits support as needed by FastAllocBase.
  • wtf/FastMalloc.h: Changed to support FastMalloc match validation.
  • wtf/FastMalloc.cpp: Changed to support FastMalloc match validation.
  • wtf/Platform.h: Added ENABLE_FAST_MALLOC_MATCH_VALIDATION; defaults to 0.
  • GNUmakefile.am: Updated to include added FastAllocBase.h.
  • JavaScriptCore.xcodeproj/project.pbxproj: Updated to include added FastAllocBase.h.
  • JavaScriptCore.vcproj/WTF/WTF.vcproj: Updated to include added FastAllocBase.h.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/FastMalloc.cpp

    r41660 r42344  
    7979
    8080#include "Assertions.h"
     81#include <limits>
    8182#if ENABLE(JSC_MULTIPLE_THREADS)
    8283#include <pthread.h>
     
    151152
    152153namespace WTF {
     154
     155#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     156
     157namespace Internal {
     158
     159void fastMallocMatchFailed(void*)
     160{
     161    CRASH();
     162}
     163
     164} // namespace Internal
     165
     166#endif
    153167
    154168void* fastZeroedMalloc(size_t n)
     
    184198{
    185199    ASSERT(!isForbidden());
     200
     201#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     202    if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= n)  // If overflow would occur...
     203        return 0;
     204
     205    void* result = malloc(n + sizeof(AllocAlignmentInteger));
     206    if (!result)
     207        return 0;
     208
     209    *static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
     210    result = static_cast<AllocAlignmentInteger*>(result) + 1;
     211
     212    return result;
     213#else
    186214    return malloc(n);
     215#endif
    187216}
    188217
     
    190219{
    191220    ASSERT(!isForbidden());
     221
     222#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     223    void* result = tryFastMalloc(n);
     224#else
    192225    void* result = malloc(n);
     226#endif
     227
    193228    if (!result)
    194229        CRASH();
     
    199234{
    200235    ASSERT(!isForbidden());
     236
     237#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     238    size_t totalBytes = n_elements * element_size;
     239    if (n_elements > 1 && element_size && (totalBytes / element_size) != n_elements || (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= totalBytes))
     240        return 0;
     241
     242    totalBytes += sizeof(AllocAlignmentInteger);
     243    void* result = malloc(totalBytes);
     244    if (!result)
     245        return 0;
     246
     247    memset(result, 0, totalBytes);
     248    *static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
     249    result = static_cast<AllocAlignmentInteger*>(result) + 1;
     250    return result;
     251#else
    201252    return calloc(n_elements, element_size);
     253#endif
    202254}
    203255
     
    205257{
    206258    ASSERT(!isForbidden());
     259
     260#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     261    void* result = tryFastCalloc(n_elements, element_size);
     262#else
    207263    void* result = calloc(n_elements, element_size);
     264#endif
     265
    208266    if (!result)
    209267        CRASH();
     
    214272{
    215273    ASSERT(!isForbidden());
     274
     275#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     276    if (!p)
     277        return;
     278
     279    AllocAlignmentInteger* header = Internal::fastMallocMatchValidationValue(p);
     280    if (*header != Internal::AllocTypeMalloc)
     281        Internal::fastMallocMatchFailed(p);
     282    free(header);
     283#else
    216284    free(p);
     285#endif
    217286}
    218287
     
    220289{
    221290    ASSERT(!isForbidden());
     291
     292#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     293    if (p) {
     294        if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= n)  // If overflow would occur...
     295            return 0;
     296        AllocAlignmentInteger* header = Internal::fastMallocMatchValidationValue(p);
     297        if (*header != Internal::AllocTypeMalloc)
     298            Internal::fastMallocMatchFailed(p);
     299        void* result = realloc(header, n + sizeof(AllocAlignmentInteger));
     300        if (!result)
     301            return 0;
     302
     303        // This should not be needed because the value is already there:
     304        // *static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
     305        result = static_cast<AllocAlignmentInteger*>(result) + 1;
     306        return result;
     307    } else {
     308        return fastMalloc(n);
     309    }
     310#else
    222311    return realloc(p, n);
     312#endif
    223313}
    224314
     
    226316{
    227317    ASSERT(!isForbidden());
     318
     319#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     320    void* result = tryFastRealloc(p, n);
     321#else
    228322    void* result = realloc(p, n);
     323#endif
     324
    229325    if (!result)
    230326        CRASH();
     
    266362#include <algorithm>
    267363#include <errno.h>
     364#include <limits>
    268365#include <new>
    269366#include <pthread.h>
     
    32953392#endif
    32963393void* malloc(size_t size) {
    3297   void* result = do_malloc(size);
     3394#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3395    if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= size)  // If overflow would occur...
     3396        return 0;
     3397    size += sizeof(AllocAlignmentInteger);
     3398    void* result = do_malloc(size);
     3399    if (!result)
     3400        return 0;
     3401
     3402    *static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
     3403    result = static_cast<AllocAlignmentInteger*>(result) + 1;
     3404#else
     3405    void* result = do_malloc(size);
     3406#endif
     3407
    32983408#ifndef WTF_CHANGES
    32993409  MallocHook::InvokeNewHook(result, size);
     
    33093419  MallocHook::InvokeDeleteHook(ptr);
    33103420#endif
    3311   do_free(ptr);
     3421
     3422#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3423    if (!ptr)
     3424        return;
     3425
     3426    AllocAlignmentInteger* header = Internal::fastMallocMatchValidationValue(ptr);
     3427    if (*header != Internal::AllocTypeMalloc)
     3428        Internal::fastMallocMatchFailed(ptr);
     3429    do_free(header);
     3430#else
     3431    do_free(ptr);
     3432#endif
    33123433}
    33133434
     
    33323453#endif
    33333454void* calloc(size_t n, size_t elem_size) {
    3334   const size_t totalBytes = n * elem_size;
     3455  size_t totalBytes = n * elem_size;
    33353456   
    33363457  // Protect against overflow
    33373458  if (n > 1 && elem_size && (totalBytes / elem_size) != n)
    33383459    return 0;
    3339    
    3340   void* result = do_malloc(totalBytes);
    3341   if (result != NULL) {
     3460
     3461#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3462    if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= totalBytes)  // If overflow would occur...
     3463        return 0;
     3464
     3465    totalBytes += sizeof(AllocAlignmentInteger);
     3466    void* result = do_malloc(totalBytes);
     3467    if (!result)
     3468        return 0;
     3469
    33423470    memset(result, 0, totalBytes);
    3343   }
     3471    *static_cast<AllocAlignmentInteger*>(result) = Internal::AllocTypeMalloc;
     3472    result = static_cast<AllocAlignmentInteger*>(result) + 1;
     3473#else
     3474    void* result = do_malloc(totalBytes);
     3475    if (result != NULL) {
     3476        memset(result, 0, totalBytes);
     3477    }
     3478#endif
     3479
    33443480#ifndef WTF_CHANGES
    33453481  MallocHook::InvokeNewHook(result, totalBytes);
     
    33823518void* realloc(void* old_ptr, size_t new_size) {
    33833519  if (old_ptr == NULL) {
     3520#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3521    void* result = malloc(new_size);
     3522#else
    33843523    void* result = do_malloc(new_size);
    33853524#ifndef WTF_CHANGES
    33863525    MallocHook::InvokeNewHook(result, new_size);
    33873526#endif
     3527#endif
    33883528    return result;
    33893529  }
     
    33953535    return NULL;
    33963536  }
     3537
     3538#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3539    if (std::numeric_limits<size_t>::max() - sizeof(AllocAlignmentInteger) <= new_size)  // If overflow would occur...
     3540        return 0;
     3541    new_size += sizeof(AllocAlignmentInteger);
     3542    AllocAlignmentInteger* header = Internal::fastMallocMatchValidationValue(old_ptr);
     3543    if (*header != Internal::AllocTypeMalloc)
     3544        Internal::fastMallocMatchFailed(old_ptr);
     3545    old_ptr = header;
     3546#endif
    33973547
    33983548  // Get the size of the old entry
     
    34323582    // would be small, so don't bother.
    34333583    do_free(old_ptr);
     3584#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3585    new_ptr = static_cast<AllocAlignmentInteger*>(new_ptr) + 1;
     3586#endif
    34343587    return new_ptr;
    34353588  } else {
     3589#if ENABLE(FAST_MALLOC_MATCH_VALIDATION)
     3590    old_ptr = pByte + sizeof(AllocAlignmentInteger);  // Set old_ptr back to the user pointer.
     3591#endif
    34363592    return old_ptr;
    34373593  }
Note: See TracChangeset for help on using the changeset viewer.