memory.cc (232950) | memory.cc (245304) |
---|---|
1/* 2 * Copyright 2010-2011 PathScale, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. --- 22 unchanged lines hidden (view full) --- 31 * to allow them to be replaced by definitions from a STL implementation. 32 * These versions simply wrap malloc() and free(), they do not provide a 33 * C++-specific allocator. 34 */ 35 36#include <stddef.h> 37#include <stdlib.h> 38#include "stdexcept.h" | 1/* 2 * Copyright 2010-2011 PathScale, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. --- 22 unchanged lines hidden (view full) --- 31 * to allow them to be replaced by definitions from a STL implementation. 32 * These versions simply wrap malloc() and free(), they do not provide a 33 * C++-specific allocator. 34 */ 35 36#include <stddef.h> 37#include <stdlib.h> 38#include "stdexcept.h" |
39#include "atomic.h" |
|
39 | 40 |
40#ifndef __has_builtin 41#define __has_builtin(x) 0 42#endif | |
43 | 41 |
44#if !__has_builtin(__sync_swap) 45#define __sync_swap __sync_lock_test_and_set 46#endif 47 | |
48namespace std 49{ 50 struct nothrow_t {}; 51} 52 53 54/// The type of the function called when allocation fails. 55typedef void (*new_handler)(); --- 6 unchanged lines hidden (view full) --- 62namespace std 63{ 64 /** 65 * Sets a function to be called when there is a failure in new. 66 */ 67 __attribute__((weak)) 68 new_handler set_new_handler(new_handler handler) 69 { | 42namespace std 43{ 44 struct nothrow_t {}; 45} 46 47 48/// The type of the function called when allocation fails. 49typedef void (*new_handler)(); --- 6 unchanged lines hidden (view full) --- 56namespace std 57{ 58 /** 59 * Sets a function to be called when there is a failure in new. 60 */ 61 __attribute__((weak)) 62 new_handler set_new_handler(new_handler handler) 63 { |
70 return __sync_swap(&new_handl, handler); | 64 return ATOMIC_SWAP(&new_handl, handler); |
71 } | 65 } |
66 __attribute__((weak)) 67 new_handler get_new_handler(void) 68 { 69 return ATOMIC_LOAD(&new_handl); 70 } |
|
72} 73 74 75__attribute__((weak)) 76void* operator new(size_t size) 77{ | 71} 72 73 74__attribute__((weak)) 75void* operator new(size_t size) 76{ |
77 if (0 == size) 78 { 79 size = 1; 80 } |
|
78 void * mem = malloc(size); 79 while (0 == mem) 80 { | 81 void * mem = malloc(size); 82 while (0 == mem) 83 { |
81 if (0 != new_handl) | 84 new_handler h = std::get_new_handler(); 85 if (0 != h) |
82 { | 86 { |
83 new_handl(); | 87 h(); |
84 } 85 else 86 { 87 throw std::bad_alloc(); 88 } 89 mem = malloc(size); 90 } 91 92 return mem; 93} 94 95__attribute__((weak)) 96void* operator new(size_t size, const std::nothrow_t &) throw() 97{ | 88 } 89 else 90 { 91 throw std::bad_alloc(); 92 } 93 mem = malloc(size); 94 } 95 96 return mem; 97} 98 99__attribute__((weak)) 100void* operator new(size_t size, const std::nothrow_t &) throw() 101{ |
102 if (0 == size) 103 { 104 size = 1; 105 } |
|
98 void *mem = malloc(size); 99 while (0 == mem) 100 { | 106 void *mem = malloc(size); 107 while (0 == mem) 108 { |
101 if (0 != new_handl) | 109 new_handler h = std::get_new_handler(); 110 if (0 != h) |
102 { 103 try 104 { | 111 { 112 try 113 { |
105 new_handl(); | 114 h(); |
106 } 107 catch (...) 108 { 109 // nothrow operator new should return NULL in case of 110 // std::bad_alloc exception in new handler 111 return NULL; 112 } 113 } --- 32 unchanged lines hidden --- | 115 } 116 catch (...) 117 { 118 // nothrow operator new should return NULL in case of 119 // std::bad_alloc exception in new handler 120 return NULL; 121 } 122 } --- 32 unchanged lines hidden --- |