aboutsummaryrefslogtreecommitdiff
path: root/test/eh/nc_alloc.cpp
blob: 6234eab0e23e489c1259c67fe5edb6dadf575fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/************************************************************************************************
 NC_ALLOC.CPP

 * Copyright (c) 1997
 * Mark of the Unicorn, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Mark of the Unicorn makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.

************************************************************************************************/

#include "nc_alloc.h"
#include <string>

#if defined (EH_NEW_HEADERS)
#  include <new>
#  include <cassert>
#  include <cstdlib>
#else
#  include <assert.h>
#  include <stdlib.h>
#  include <new.h>
#endif

#if defined (EH_NEW_IOSTREAMS)
#  include <iostream>
#else
#  include <iostream.h>
#endif

long alloc_count = 0;
long object_count = 0;
long TestController::possible_failure_count = 0;
const char* TestController::current_test = "<unknown>";
const char* TestController::current_test_category = "no category";
const char* TestController::current_container = 0;
bool  TestController::nc_verbose = true;
bool  TestController::never_fail = false;
bool  TestController::track_allocations = false;
bool  TestController::leak_detection_enabled = false;
TestController gTestController;

//************************************************************************************************
void TestController::maybe_fail(long) {
  if (never_fail || Failure_threshold() == kNotInExceptionTest)
    return;

  // throw if allocation would satisfy the threshold
  if (possible_failure_count++ >= Failure_threshold()) {
    // what about doing some standard new_handler() behavior here (to test it!) ???

    // reset and simulate an out-of-memory failure
    Failure_threshold() = kNotInExceptionTest;
#ifndef EH_NO_EXCEPTIONS
    throw EH_STD::bad_alloc();
#endif
  }
}

#if defined (EH_HASHED_CONTAINERS_IMPLEMENTED)
#  if defined (__SGI_STL)
#    if defined (EH_NEW_HEADERS)
#      include <hash_set>
#    else
#      include <hash_set.h>
#    endif
#  elif defined (__MSL__)
#    include <hashset.h>
#  else
#    error what do I include to get hash_set?
#  endif
#else
#  if defined (EH_NEW_HEADERS)
#    include <set>
#  else
#    include <set.h>
#  endif
#endif

#if !defined (EH_HASHED_CONTAINERS_IMPLEMENTED)
typedef EH_STD::set<void*, EH_STD::less<void*> > allocation_set;
#else

USING_CSTD_NAME(size_t)

struct hash_void {
  size_t operator()(void* x) const { return (size_t)x; }
};

typedef EH_STD::hash_set<void*, ::hash_void, EH_STD::equal_to<void*> > allocation_set;
#endif

static allocation_set& alloc_set() {
  static allocation_set s;
  return s;
}

// Prevents infinite recursion during allocation
static bool using_alloc_set = false;

#if !defined (NO_FAST_ALLOCATOR)
//
//  FastAllocator -- speeds up construction of TestClass objects when
// TESTCLASS_DEEP_DATA is enabled, and speeds up tracking of allocations
// when the suite is run with the -t option.
//
class FastAllocator {
public:
  //FastAllocator() : mFree(0), mUsed(0) {}
  static void *Allocate(size_t s) {
    void *result = 0;

    if (s <= sizeof(Block)) {
      if (mFree != 0) {
        result = mFree;
        mFree = mFree->next;
      }
      else if (mBlocks != 0 && mUsed < kBlockCount) {
        result =  (void*)&mBlocks[mUsed++];
      }
    }
    return result;
  }

  static bool Free(void* p) {
    Block* b = (Block*)p;
    if (mBlocks == 0 || b < mBlocks || b >= mBlocks + kBlockCount)
      return false;
    b->next = mFree;
    mFree = b;
    return true;
  }

  struct Block;
  friend struct Block;

  enum {
    // Number of fast allocation blocks to create.
    kBlockCount = 1500,

    // You may need to adjust this number for your platform.
    // A good choice will speed tests. A bad choice will still work.
    kMinBlockSize = 48
  };

  struct Block {
    union {
      Block *next;
      double dummy; // fbp - force alignment
      char dummy2[kMinBlockSize];
    };
  };

  static Block* mBlocks;
  static Block *mFree;
  static size_t mUsed;
};

FastAllocator::Block *FastAllocator::mBlocks =
(FastAllocator::Block*)EH_CSTD::calloc( sizeof(FastAllocator::Block), FastAllocator::kBlockCount );
FastAllocator::Block *FastAllocator::mFree;
size_t FastAllocator::mUsed;


static FastAllocator gFastAllocator;
#endif

inline char* AllocateBlock(size_t s) {
#if !defined (NO_FAST_ALLOCATOR)
  char * const p = (char*)gFastAllocator.Allocate( s );
  if (p != 0)
    return p;
#endif

  return (char*)EH_CSTD::malloc(s);
}

static void* OperatorNew( size_t s ) {
  if (!using_alloc_set) {
    simulate_possible_failure();
    ++alloc_count;
  }

  char *p = AllocateBlock(s);

  if (gTestController.TrackingEnabled() &&
      gTestController.LeakDetectionEnabled() &&
      !using_alloc_set) {
    using_alloc_set = true;
    bool inserted = alloc_set().insert(p).second;
    // Suppress warning about unused variable.
    inserted; 
    EH_ASSERT(inserted);
    using_alloc_set = false;
  }

  return p;
}

void* _STLP_CALL operator new(size_t s)
#ifdef EH_DELETE_HAS_THROW_SPEC
throw(EH_STD::bad_alloc)
#endif
{ return OperatorNew( s ); }

#ifdef EH_USE_NOTHROW
void* _STLP_CALL operator new(size_t size, const EH_STD::nothrow_t&) throw() {
  try {
    return OperatorNew( size );
  }
  catch (...) {
    return 0;
  }
}
#endif

#if 1 /* defined (EH_VECTOR_OPERATOR_NEW) */
void* _STLP_CALL operator new[](size_t size ) throw(EH_STD::bad_alloc) {
  return OperatorNew( size );
}

#  ifdef EH_USE_NOTHROW
void* _STLP_CALL operator new[](size_t size, const EH_STD::nothrow_t&) throw() {
  try {
    return OperatorNew(size);
  }
  catch (...) {
    return 0;
  }
}
#  endif

void _STLP_CALL operator delete[](void* ptr) throw()
{ operator delete( ptr ); }
#endif

#if defined (EH_DELETE_HAS_THROW_SPEC)
void _STLP_CALL operator delete(void* s) throw()
#else
void _STLP_CALL operator delete(void* s)
#endif
{
  if ( s != 0 ) {
    if ( !using_alloc_set ) {
      --alloc_count;

      if ( gTestController.TrackingEnabled() && gTestController.LeakDetectionEnabled() ) {
        using_alloc_set = true;
        allocation_set::iterator p = alloc_set().find( (char*)s );
        EH_ASSERT( p != alloc_set().end() );
        alloc_set().erase( p );
        using_alloc_set = false;
      }
    }
# if ! defined (NO_FAST_ALLOCATOR)
    if ( !gFastAllocator.Free( s ) )
# endif
      EH_CSTD::free(s);
  }
}


/*===================================================================================
  ClearAllocationSet  (private helper)

  EFFECTS:  Empty the set of allocated blocks.
====================================================================================*/
void TestController::ClearAllocationSet() {
  if (!using_alloc_set) {
    using_alloc_set = true;
    alloc_set().clear();
    using_alloc_set = false;
  }
}


bool TestController::ReportLeaked() {
  EndLeakDetection();

  EH_ASSERT( !using_alloc_set || (alloc_count == static_cast<int>(alloc_set().size())) );

  if (alloc_count != 0 || object_count != 0) {
    EH_STD::cerr<<"\nEH TEST FAILURE !\n";
    PrintTestName(true);
    if (alloc_count)
      EH_STD::cerr << "ERROR : " << alloc_count << " outstanding allocations.\n";
    if (object_count)
      EH_STD::cerr << "ERROR : " << object_count << " non-destroyed objects.\n";
    alloc_count = object_count = 0;
    return true;
  }
  return false;
}



/*===================================================================================
  PrintTestName

  EFFECTS: Prints information about the current test. If err is false, ends with
    an ellipsis, because the test is ongoing. If err is true an error is being
    reported, and the output ends with an endl.
====================================================================================*/

void TestController::PrintTestName(bool err) {
  if (current_container)
    EH_STD::cerr<<"["<<current_container<<"] :";
  EH_STD::cerr<<"testing "<<current_test <<" (" << current_test_category <<")";
  if (err)
    EH_STD::cerr<<EH_STD::endl;
  else
    EH_STD::cerr<<" ... ";
}

void TestController::ReportSuccess(int count) {
  if (nc_verbose)
    EH_STD::cerr<<(count+1)<<" try successful"<<EH_STD::endl;
}

long& TestController::Failure_threshold() {
  static long failure_threshold = kNotInExceptionTest;
  return failure_threshold;
}