~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/JavaScriptCore/wtf/TCSystemAlloc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
// Author: Sanjay Ghemawat
32
32
 
33
33
#include "config.h"
 
34
#include "TCSystemAlloc.h"
 
35
 
 
36
#include <algorithm>
 
37
#include <fcntl.h>
 
38
#include "Assertions.h"
 
39
#include "TCSpinLock.h"
 
40
#include "UnusedParam.h"
 
41
 
34
42
#if HAVE(STDINT_H)
35
43
#include <stdint.h>
36
44
#elif HAVE(INTTYPES_H)
38
46
#else
39
47
#include <sys/types.h>
40
48
#endif
 
49
 
41
50
#if PLATFORM(WIN_OS)
42
51
#include "windows.h"
43
52
#else
45
54
#include <unistd.h>
46
55
#include <sys/mman.h>
47
56
#endif
48
 
#include <fcntl.h>
49
 
#include "Assertions.h"
50
 
#include "TCSystemAlloc.h"
51
 
#include "TCSpinLock.h"
52
 
#include "UnusedParam.h"
53
57
 
54
58
#ifndef MAP_ANONYMOUS
55
59
#define MAP_ANONYMOUS MAP_ANON
56
60
#endif
57
61
 
 
62
using namespace std;
 
63
 
58
64
// Structure for discovering alignment
59
65
union MemoryAligner {
60
66
  void*  p;
381
387
  return NULL;
382
388
}
383
389
 
384
 
void TCMalloc_SystemRelease(void* start, size_t length)
385
 
{
386
 
  UNUSED_PARAM(start);
387
 
  UNUSED_PARAM(length);
388
 
#if HAVE(MADV_DONTNEED)
 
390
#if HAVE(MADV_FREE_REUSE)
 
391
 
 
392
void TCMalloc_SystemRelease(void* start, size_t length)
 
393
{
 
394
    while (madvise(start, length, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
 
395
}
 
396
 
 
397
#elif HAVE(MADV_FREE) || HAVE(MADV_DONTNEED)
 
398
 
 
399
void TCMalloc_SystemRelease(void* start, size_t length)
 
400
{
 
401
    // MADV_FREE clears the modified bit on pages, which allows
 
402
    // them to be discarded immediately.
 
403
#if HAVE(MADV_FREE)
 
404
    const int advice = MADV_FREE;
 
405
#else
 
406
    const int advice = MADV_DONTNEED;
 
407
#endif
389
408
  if (FLAGS_malloc_devmem_start) {
390
409
    // It's not safe to use MADV_DONTNEED if we've been mapping
391
410
    // /dev/mem for heap memory
412
431
    // Note -- ignoring most return codes, because if this fails it
413
432
    // doesn't matter...
414
433
    while (madvise(reinterpret_cast<char*>(new_start), new_end - new_start,
415
 
                   MADV_DONTNEED) == -1 &&
 
434
                   advice) == -1 &&
416
435
           errno == EAGAIN) {
417
436
      // NOP
418
437
    }
419
 
    return;
420
438
  }
421
 
#endif
422
 
 
423
 
#if HAVE(MMAP)
 
439
}
 
440
 
 
441
#elif HAVE(MMAP)
 
442
 
 
443
void TCMalloc_SystemRelease(void* start, size_t length)
 
444
{
424
445
  void* newAddress = mmap(start, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
425
446
  // If the mmap failed then that's ok, we just won't return the memory to the system.
426
447
  ASSERT_UNUSED(newAddress, newAddress == start || newAddress == reinterpret_cast<void*>(MAP_FAILED));
427
 
  return;
 
448
}
 
449
 
 
450
#elif HAVE(VIRTUALALLOC)
 
451
 
 
452
void TCMalloc_SystemRelease(void* start, size_t length)
 
453
{
 
454
    if (VirtualFree(start, length, MEM_DECOMMIT))
 
455
        return;
 
456
 
 
457
    // The decommit may fail if the memory region consists of allocations
 
458
    // from more than one call to VirtualAlloc.  In this case, fall back to
 
459
    // using VirtualQuery to retrieve the allocation boundaries and decommit
 
460
    // them each individually.
 
461
 
 
462
    char* ptr = static_cast<char*>(start);
 
463
    char* end = ptr + length;
 
464
    MEMORY_BASIC_INFORMATION info;
 
465
    while (ptr < end) {
 
466
        size_t resultSize = VirtualQuery(ptr, &info, sizeof(info));
 
467
        ASSERT_UNUSED(resultSize, resultSize == sizeof(info));
 
468
 
 
469
        size_t decommitSize = min<size_t>(info.RegionSize, end - ptr);
 
470
        BOOL success = VirtualFree(ptr, decommitSize, MEM_DECOMMIT);
 
471
        ASSERT_UNUSED(success, success);
 
472
        ptr += decommitSize;
 
473
    }
 
474
}
 
475
 
 
476
#else
 
477
 
 
478
// Platforms that don't support returning memory use an empty inline version of TCMalloc_SystemRelease
 
479
// declared in TCSystemAlloc.h
 
480
 
428
481
#endif
429
 
}
430
 
 
431
 
#if HAVE(VIRTUALALLOC)
432
 
void TCMalloc_SystemCommit(void* start, size_t length)
433
 
{
434
 
    UNUSED_PARAM(start);
435
 
    UNUSED_PARAM(length);
436
 
}
 
482
 
 
483
#if HAVE(MADV_FREE_REUSE)
 
484
 
 
485
void TCMalloc_SystemCommit(void* start, size_t length)
 
486
{
 
487
    while (madvise(start, length, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { }
 
488
}
 
489
 
 
490
#elif HAVE(VIRTUALALLOC)
 
491
 
 
492
void TCMalloc_SystemCommit(void* start, size_t length)
 
493
{
 
494
    if (VirtualAlloc(start, length, MEM_COMMIT, PAGE_READWRITE) == start)
 
495
        return;
 
496
 
 
497
    // The commit may fail if the memory region consists of allocations
 
498
    // from more than one call to VirtualAlloc.  In this case, fall back to
 
499
    // using VirtualQuery to retrieve the allocation boundaries and commit them
 
500
    // each individually.
 
501
 
 
502
    char* ptr = static_cast<char*>(start);
 
503
    char* end = ptr + length;
 
504
    MEMORY_BASIC_INFORMATION info;
 
505
    while (ptr < end) {
 
506
        size_t resultSize = VirtualQuery(ptr, &info, sizeof(info));
 
507
        ASSERT_UNUSED(resultSize, resultSize == sizeof(info));
 
508
 
 
509
        size_t commitSize = min<size_t>(info.RegionSize, end - ptr);
 
510
        void* newAddress = VirtualAlloc(ptr, commitSize, MEM_COMMIT, PAGE_READWRITE);
 
511
        ASSERT_UNUSED(newAddress, newAddress == ptr);
 
512
        ptr += commitSize;
 
513
    }
 
514
}
 
515
 
 
516
#else
 
517
 
 
518
// Platforms that don't need to explicitly commit memory use an empty inline version of TCMalloc_SystemCommit
 
519
// declared in TCSystemAlloc.h
 
520
 
437
521
#endif