LCOV - code coverage report
Current view: directory - js/src - jsalloc.h (source / functions) Found Hit Coverage
Test: app.info Lines: 20 17 85.0 %
Date: 2012-06-02 Functions: 9 8 88.9 %

       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 1.9 code, released
      18                 :  * July 16, 2009.
      19                 :  *
      20                 :  * The Initial Developer of the Original Code is
      21                 :  *   the Mozilla Corporation.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #ifndef jsalloc_h_
      40                 : #define jsalloc_h_
      41                 : 
      42                 : #include "jspubtd.h"
      43                 : #include "jsutil.h"
      44                 : 
      45                 : namespace js {
      46                 : 
      47                 : /*
      48                 :  * Allocation policies.  These model the concept:
      49                 :  *  - public copy constructor, assignment, destructor
      50                 :  *  - void *malloc_(size_t)
      51                 :  *      Responsible for OOM reporting on NULL return value.
      52                 :  *  - void *realloc_(size_t)
      53                 :  *      Responsible for OOM reporting on NULL return value.
      54                 :  *      The *used* bytes of the previous buffer is passed in
      55                 :  *      (rather than the old allocation size), in addition to
      56                 :  *      the *new* allocation size requested.
      57                 :  *  - void free_(void *)
      58                 :  *  - reportAllocOverflow()
      59                 :  *      Called on overflow before the container returns NULL.
      60                 :  */
      61                 : 
      62                 : /* Policy for using system memory functions and doing no error reporting. */
      63                 : class SystemAllocPolicy
      64                 : {
      65                 :   public:
      66         5942754 :     void *malloc_(size_t bytes) { return js_malloc(bytes); }
      67          811866 :     void *realloc_(void *p, size_t oldBytes, size_t bytes) { return js_realloc(p, bytes); }
      68         5942754 :     void free_(void *p) { js_free(p); }
      69               0 :     void reportAllocOverflow() const {}
      70                 : };
      71                 : 
      72                 : /*
      73                 :  * Allocation policy that calls the system memory functions and reports errors
      74                 :  * to the context. Since the JSContext given on construction is stored for
      75                 :  * the lifetime of the container, this policy may only be used for containers
      76                 :  * whose lifetime is a shorter than the given JSContext.
      77                 :  *
      78                 :  * FIXME bug 647103 - rewrite this in terms of temporary allocation functions,
      79                 :  * not the system ones.
      80                 :  */
      81                 : class TempAllocPolicy
      82                 : {
      83                 :     JSContext *const cx;
      84                 : 
      85                 :     /*
      86                 :      * Non-inline helper to call JSRuntime::onOutOfMemory with minimal
      87                 :      * code bloat.
      88                 :      */
      89                 :     JS_FRIEND_API(void *) onOutOfMemory(void *p, size_t nbytes);
      90                 : 
      91                 :   public:
      92        11570059 :     TempAllocPolicy(JSContext *cx) : cx(cx) {}
      93                 : 
      94           45212 :     JSContext *context() const {
      95           45212 :         return cx;
      96                 :     }
      97                 : 
      98         4776138 :     void *malloc_(size_t bytes) {
      99         4776138 :         void *p = js_malloc(bytes);
     100         4776138 :         if (JS_UNLIKELY(!p))
     101               0 :             p = onOutOfMemory(NULL, bytes);
     102         4776138 :         return p;
     103                 :     }
     104                 : 
     105          271921 :     void *realloc_(void *p, size_t oldBytes, size_t bytes) {
     106          271921 :         void *p2 = js_realloc(p, bytes);
     107          271921 :         if (JS_UNLIKELY(!p2))
     108               0 :             p2 = onOutOfMemory(p2, bytes);
     109          271921 :         return p2;
     110                 :     }
     111                 : 
     112         4776048 :     void free_(void *p) {
     113         4776048 :         js_free(p);
     114         4776048 :     }
     115                 : 
     116                 :     JS_FRIEND_API(void) reportAllocOverflow() const;
     117                 : };
     118                 : 
     119                 : } /* namespace js */
     120                 : 
     121                 : #endif /* jsalloc_h_ */

Generated by: LCOV version 1.7