include/am-allocator-policies.h
author David Anderson <danderson@mozilla.com>
Mon Aug 26 23:18:03 2013 -0700 (2013-08-26)
changeset 11 cfe800908837
parent 4 f3275c49eb7b
permissions -rw-r--r--
Add tests.
danderson@0
     1
// vim: set sts=8 ts=2 sw=2 tw=99 et:
danderson@0
     2
//
danderson@0
     3
// Copyright (C) 2013, David Anderson and AlliedModders LLC
danderson@0
     4
// All rights reserved.
danderson@0
     5
// 
danderson@0
     6
// Redistribution and use in source and binary forms, with or without
danderson@0
     7
// modification, are permitted provided that the following conditions are met:
danderson@0
     8
// 
danderson@0
     9
//  * Redistributions of source code must retain the above copyright notice, this
danderson@0
    10
//    list of conditions and the following disclaimer.
danderson@0
    11
//  * Redistributions in binary form must reproduce the above copyright notice,
danderson@0
    12
//    this list of conditions and the following disclaimer in the documentation
danderson@0
    13
//    and/or other materials provided with the distribution.
danderson@0
    14
//  * Neither the name of AlliedModders LLC nor the names of its contributors
danderson@0
    15
//    may be used to endorse or promote products derived from this software
danderson@0
    16
//    without specific prior written permission.
danderson@0
    17
//
danderson@0
    18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
danderson@0
    19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
danderson@0
    20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
danderson@0
    21
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
danderson@0
    22
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
danderson@0
    23
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
danderson@0
    24
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
danderson@0
    25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
danderson@0
    26
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
danderson@0
    27
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
danderson@0
    28
// POSSIBILITY OF SUCH DAMAGE.
danderson@0
    29
danderson@0
    30
#ifndef _include_amtl_allocatorpolicies_h_
danderson@0
    31
#define _include_amtl_allocatorpolicies_h_
danderson@0
    32
danderson@0
    33
#include <stdio.h>
danderson@0
    34
#include <stdlib.h>
danderson@0
    35
danderson@0
    36
namespace ke {
danderson@0
    37
danderson@0
    38
// The default system allocator policy will crash on out-of-memory.
danderson@0
    39
class SystemAllocatorPolicy
danderson@0
    40
{
dvander@4
    41
  public:
danderson@0
    42
    void reportOutOfMemory() {
danderson@0
    43
      fprintf(stderr, "OUT OF MEMORY\n");
danderson@0
    44
      abort();
danderson@0
    45
    }
danderson@0
    46
    void reportAllocationOverflow() {
danderson@0
    47
      fprintf(stderr, "OUT OF MEMORY\n");
danderson@0
    48
      abort();
danderson@0
    49
    }
danderson@0
    50
danderson@0
    51
  public:
danderson@0
    52
    void free(void *memory) {
danderson@0
    53
      ::free(memory);
danderson@0
    54
    }
danderson@0
    55
    void *malloc(size_t bytes) {
danderson@0
    56
      void *ptr = ::malloc(bytes);
danderson@0
    57
      if (!ptr)
danderson@0
    58
        reportOutOfMemory();
danderson@0
    59
      return ptr;
danderson@0
    60
    }
danderson@0
    61
};
danderson@0
    62
danderson@0
    63
}
danderson@0
    64
danderson@0
    65
#endif // _include_amtl_allocatorpolicies_h_