Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "implement.h"
32
 
33
 
34
/*
35
    DEFAULT NEW HANDLER
36
 
37
    The default new handler throws a bad_alloc exception.
38
*/
39
 
40
static void default_new_handler ()
41
{
42
    throw bad_alloc () ;
43
}
44
 
45
 
46
/*
47
    CURRENT NEW HANDLER
48
 
49
    This value gives the current new handler function.
50
*/
51
 
52
static new_handler crt_new_handler = default_new_handler ;
53
 
54
 
55
/*
56
    SET CURRENT NEW HANDLER
57
 
58
    This routine sets the current new handler function to fn, returning
59
    the previous value.
60
*/
61
 
62
new_handler std::set_new_handler ( new_handler fn ) throw ()
63
{
64
    new_handler prev = crt_new_handler ;
65
    crt_new_handler = fn ;
66
    return ( prev ) ;
67
}
68
 
69
 
70
/*
71
    STANDARD MEMORY ALLOCATION FUNCTION
72
 
73
    This routine allocates sz bytes of memory, throwing an exception if
74
    this is not possible.
75
*/
76
 
77
void *__TCPPLUS_new ( size_t sz ) throw ( bad_alloc )
78
{
79
    void *p ;
80
    if ( sz == 0 ) sz = 1 ;
81
    while ( p = malloc ( sz ), p == NULL ) {
82
	new_handler fn = crt_new_handler ;
83
	if ( fn == NULL ) throw bad_alloc () ;
84
	fn () ;
85
    }
86
    return ( p ) ;
87
}
88
 
89
 
90
/*
91
    NO-EXCEPTION MEMORY ALLOCATION FUNCTION
92
 
93
    This routine allocates sz bytes of memory, returning a null pointer if
94
    this is not possible.
95
*/
96
 
97
void *__TCPPLUS_new_nothrow ( size_t sz ) throw ()
98
{
99
    void *p ;
100
    if ( sz == 0 ) sz = 1 ;
101
    while ( p = malloc ( sz ), p == NULL ) {
102
	try {
103
	    new_handler fn = crt_new_handler ;
104
	    if ( fn == NULL ) return ( NULL ) ;
105
	    fn () ;
106
	}
107
	catch ( const bad_alloc & ) {
108
	    // Return null if handler throws an exception
109
	    return ( NULL ) ;
110
	}
111
    }
112
    return ( p ) ;
113
}
114
 
115
 
116
/*
117
    STANDARD MEMORY DEALLOCATION FUNCTION
118
 
119
    This routine deallocates the memory given by p.
120
*/
121
 
122
void __TCPPLUS_delete ( void *p ) throw ()
123
{
124
    if ( p ) free ( p ) ;
125
    return ;
126
}
127
 
128
 
129
/*
130
    THE STANDARD NOTHROW OBJECT
131
 
132
    This object is used in the no-exception memory allocation routines.
133
*/
134
 
135
const nothrow_t std::nothrow = {} ;