LCOV - code coverage report
Current view: directory - js/src/ds - LifoAlloc.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 65 56 86.2 %
Date: 2012-06-02 Functions: 6 6 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  * vim: set ts=8 sw=4 et tw=99 ft=cpp:
       3                 :  *
       4                 :  * ***** BEGIN LICENSE BLOCK *****
       5                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       6                 :  *
       7                 :  * The contents of this file are subject to the Mozilla Public License Version
       8                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       9                 :  * the License. You may obtain a copy of the License at
      10                 :  * http://www.mozilla.org/MPL/
      11                 :  *
      12                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      13                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      14                 :  * for the specific language governing rights and limitations under the
      15                 :  * License.
      16                 :  *
      17                 :  * The Original Code is Mozilla SpiderMonkey JavaScript code.
      18                 :  *
      19                 :  * The Initial Developer of the Original Code is
      20                 :  * the Mozilla Foundation.
      21                 :  * Portions created by the Initial Developer are Copyright (C) 2011
      22                 :  * the Initial Developer. All Rights Reserved.
      23                 :  *
      24                 :  * Contributor(s):
      25                 :  *  Chris Leary <cdleary@mozilla.com>
      26                 :  *
      27                 :  * Alternatively, the contents of this file may be used under the terms of
      28                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      29                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      31                 :  * of those above. If you wish to allow use of your version of this file only
      32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      33                 :  * use your version of this file under the terms of the MPL, indicate your
      34                 :  * decision by deleting the provisions above and replace them with the notice
      35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      36                 :  * the provisions above, a recipient may use your version of this file under
      37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      38                 :  *
      39                 :  * ***** END LICENSE BLOCK ***** */
      40                 : 
      41                 : #include "LifoAlloc.h"
      42                 : 
      43                 : #include <new>
      44                 : 
      45                 : using namespace js;
      46                 : 
      47                 : namespace js {
      48                 : namespace detail {
      49                 : 
      50                 : BumpChunk *
      51          382654 : BumpChunk::new_(size_t chunkSize)
      52                 : {
      53          382654 :     JS_ASSERT(RoundUpPow2(chunkSize) == chunkSize);
      54          382654 :     void *mem = js_malloc(chunkSize);
      55          382654 :     if (!mem)
      56               0 :         return NULL;
      57          382654 :     BumpChunk *result = new (mem) BumpChunk(chunkSize - sizeof(BumpChunk));
      58                 : 
      59                 :     /* 
      60                 :      * We assume that the alignment of sAlign is less than that of
      61                 :      * the underlying memory allocator -- creating a new BumpChunk should
      62                 :      * always satisfy the sAlign alignment constraint.
      63                 :      */
      64          382654 :     JS_ASSERT(AlignPtr(result->bump) == result->bump);
      65          382654 :     return result;
      66                 : }
      67                 : 
      68                 : void
      69          382654 : BumpChunk::delete_(BumpChunk *chunk)
      70                 : {
      71                 : #ifdef DEBUG
      72          382654 :         memset(chunk, 0xcd, sizeof(*chunk) + chunk->bumpSpaceSize);
      73                 : #endif
      74          382654 :         js_free(chunk);
      75          382654 : }
      76                 : 
      77                 : bool
      78       216579057 : BumpChunk::canAlloc(size_t n)
      79                 : {
      80       216579057 :     char *aligned = AlignPtr(bump);
      81       216579057 :     char *bumped = aligned + n;
      82       216579057 :     return bumped <= limit && bumped > headerBase();
      83                 : }
      84                 : 
      85                 : } /* namespace detail */
      86                 : } /* namespace js */
      87                 : 
      88                 : void
      89          227278 : LifoAlloc::freeAll()
      90                 : {
      91          837210 :     while (first) {
      92          382654 :         BumpChunk *victim = first;
      93          382654 :         first = first->next();
      94          382654 :         BumpChunk::delete_(victim);
      95                 :     }
      96          227278 :     first = latest = NULL;
      97          227278 : }
      98                 : 
      99                 : void
     100           52591 : LifoAlloc::freeUnused()
     101                 : {
     102                 :     /* Don't free anything if we have outstanding marks. */
     103           52591 :     if (markCount || !first)
     104           28683 :         return; 
     105                 : 
     106           23908 :     JS_ASSERT(first && latest);
     107                 : 
     108                 :     /* Rewind through any unused chunks. */
     109           23908 :     if (!latest->used()) {
     110           23899 :         BumpChunk *lastUsed = NULL;
     111           23899 :         for (BumpChunk *it = first; it != latest; it = it->next()) {
     112               0 :             if (it->used())
     113               0 :                 lastUsed = it;
     114                 :         }
     115           23899 :         if (!lastUsed) {
     116           23899 :             freeAll();
     117           23899 :             return;
     118                 :         }
     119               0 :         latest = lastUsed;
     120                 :     }
     121                 : 
     122                 :     /* Free all chunks after |latest|. */
     123               9 :     BumpChunk *it = latest->next();
     124              18 :     while (it) {
     125               0 :         BumpChunk *victim = it;
     126               0 :         it = it->next();
     127               0 :         BumpChunk::delete_(victim);
     128                 :     }
     129                 : 
     130               9 :     latest->setNext(NULL);
     131                 : }
     132                 : 
     133                 : LifoAlloc::BumpChunk *
     134          814641 : LifoAlloc::getOrCreateChunk(size_t n)
     135                 : {
     136          814641 :     if (first) {
     137                 :         /* Look for existing, unused BumpChunks to satisfy the request. */
     138         1490458 :         while (latest->next()) {
     139          431987 :             latest = latest->next();
     140          431987 :             latest->resetBump(); /* This was an unused BumpChunk on the chain. */
     141          431987 :             if (latest->canAlloc(n))
     142          431987 :                 return latest;
     143                 :         }
     144                 :     }
     145                 : 
     146          382654 :     size_t defaultChunkFreeSpace = defaultChunkSize_ - sizeof(BumpChunk);
     147                 :     size_t chunkSize;
     148          382654 :     if (n > defaultChunkFreeSpace) {
     149             557 :         size_t allocSizeWithHeader = n + sizeof(BumpChunk);
     150                 : 
     151                 :         /* Guard for overflow. */
     152             557 :         if (allocSizeWithHeader < n ||
     153                 :             (allocSizeWithHeader & (size_t(1) << (tl::BitSize<size_t>::result - 1)))) {
     154               0 :             return NULL;
     155                 :         }
     156                 : 
     157             557 :         chunkSize = RoundUpPow2(allocSizeWithHeader);
     158                 :     } else {
     159          382097 :         chunkSize = defaultChunkSize_;
     160                 :     }
     161                 : 
     162                 :     /* If we get here, we couldn't find an existing BumpChunk to fill the request. */
     163          382654 :     BumpChunk *newChunk = BumpChunk::new_(chunkSize);
     164          382654 :     if (!newChunk)
     165               0 :         return NULL;
     166          382654 :     if (!first) {
     167           69412 :         latest = first = newChunk;
     168                 :     } else {
     169          313242 :         JS_ASSERT(latest && !latest->next());
     170          313242 :         latest->setNext(newChunk);
     171          313242 :         latest = newChunk;
     172                 :     }
     173          382654 :     return newChunk;
     174                 : }

Generated by: LCOV version 1.7