Changeset 42344 in webkit for trunk/JavaScriptCore/wtf/FastMalloc.cpp
- Timestamp:
- Apr 8, 2009, 6:14:07 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/FastMalloc.cpp
r41660 r42344 79 79 80 80 #include "Assertions.h" 81 #include <limits> 81 82 #if ENABLE(JSC_MULTIPLE_THREADS) 82 83 #include <pthread.h> … … 151 152 152 153 namespace WTF { 154 155 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 156 157 namespace Internal { 158 159 void fastMallocMatchFailed(void*) 160 { 161 CRASH(); 162 } 163 164 } // namespace Internal 165 166 #endif 153 167 154 168 void* fastZeroedMalloc(size_t n) … … 184 198 { 185 199 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 186 214 return malloc(n); 215 #endif 187 216 } 188 217 … … 190 219 { 191 220 ASSERT(!isForbidden()); 221 222 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 223 void* result = tryFastMalloc(n); 224 #else 192 225 void* result = malloc(n); 226 #endif 227 193 228 if (!result) 194 229 CRASH(); … … 199 234 { 200 235 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 201 252 return calloc(n_elements, element_size); 253 #endif 202 254 } 203 255 … … 205 257 { 206 258 ASSERT(!isForbidden()); 259 260 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 261 void* result = tryFastCalloc(n_elements, element_size); 262 #else 207 263 void* result = calloc(n_elements, element_size); 264 #endif 265 208 266 if (!result) 209 267 CRASH(); … … 214 272 { 215 273 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 216 284 free(p); 285 #endif 217 286 } 218 287 … … 220 289 { 221 290 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 222 311 return realloc(p, n); 312 #endif 223 313 } 224 314 … … 226 316 { 227 317 ASSERT(!isForbidden()); 318 319 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 320 void* result = tryFastRealloc(p, n); 321 #else 228 322 void* result = realloc(p, n); 323 #endif 324 229 325 if (!result) 230 326 CRASH(); … … 266 362 #include <algorithm> 267 363 #include <errno.h> 364 #include <limits> 268 365 #include <new> 269 366 #include <pthread.h> … … 3295 3392 #endif 3296 3393 void* 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 3298 3408 #ifndef WTF_CHANGES 3299 3409 MallocHook::InvokeNewHook(result, size); … … 3309 3419 MallocHook::InvokeDeleteHook(ptr); 3310 3420 #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 3312 3433 } 3313 3434 … … 3332 3453 #endif 3333 3454 void* calloc(size_t n, size_t elem_size) { 3334 constsize_t totalBytes = n * elem_size;3455 size_t totalBytes = n * elem_size; 3335 3456 3336 3457 // Protect against overflow 3337 3458 if (n > 1 && elem_size && (totalBytes / elem_size) != n) 3338 3459 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 3342 3470 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 3344 3480 #ifndef WTF_CHANGES 3345 3481 MallocHook::InvokeNewHook(result, totalBytes); … … 3382 3518 void* realloc(void* old_ptr, size_t new_size) { 3383 3519 if (old_ptr == NULL) { 3520 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 3521 void* result = malloc(new_size); 3522 #else 3384 3523 void* result = do_malloc(new_size); 3385 3524 #ifndef WTF_CHANGES 3386 3525 MallocHook::InvokeNewHook(result, new_size); 3387 3526 #endif 3527 #endif 3388 3528 return result; 3389 3529 } … … 3395 3535 return NULL; 3396 3536 } 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 3397 3547 3398 3548 // Get the size of the old entry … … 3432 3582 // would be small, so don't bother. 3433 3583 do_free(old_ptr); 3584 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 3585 new_ptr = static_cast<AllocAlignmentInteger*>(new_ptr) + 1; 3586 #endif 3434 3587 return new_ptr; 3435 3588 } else { 3589 #if ENABLE(FAST_MALLOC_MATCH_VALIDATION) 3590 old_ptr = pByte + sizeof(AllocAlignmentInteger); // Set old_ptr back to the user pointer. 3591 #endif 3436 3592 return old_ptr; 3437 3593 }
Note:
See TracChangeset
for help on using the changeset viewer.