williamr@4
|
1 |
/*
|
williamr@4
|
2 |
*
|
williamr@4
|
3 |
* Copyright (c) 1996,1997
|
williamr@4
|
4 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@4
|
5 |
*
|
williamr@4
|
6 |
* Copyright (c) 1997
|
williamr@4
|
7 |
* Moscow Center for SPARC Technology
|
williamr@4
|
8 |
*
|
williamr@4
|
9 |
* Copyright (c) 1999
|
williamr@4
|
10 |
* Boris Fomitchev
|
williamr@4
|
11 |
*
|
williamr@4
|
12 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@4
|
13 |
* or implied. Any use is at your own risk.
|
williamr@4
|
14 |
*
|
williamr@4
|
15 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@4
|
16 |
* without fee, provided the above notices are retained on all copies.
|
williamr@4
|
17 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@4
|
18 |
* provided the above notices are retained, and a notice that the code was
|
williamr@4
|
19 |
* modified is included with the above copyright notice.
|
williamr@4
|
20 |
*
|
williamr@4
|
21 |
*/
|
williamr@4
|
22 |
#ifndef _STLP_ALLOC_C
|
williamr@4
|
23 |
#define _STLP_ALLOC_C
|
williamr@4
|
24 |
|
williamr@4
|
25 |
#ifndef _STLP_INTERNAL_ALLOC_H
|
williamr@4
|
26 |
# include <stl/_alloc.h>
|
williamr@4
|
27 |
#endif
|
williamr@4
|
28 |
|
williamr@4
|
29 |
#if defined (__WATCOMC__)
|
williamr@4
|
30 |
# pragma warning 13 9
|
williamr@4
|
31 |
# pragma warning 367 9
|
williamr@4
|
32 |
# pragma warning 368 9
|
williamr@4
|
33 |
#endif
|
williamr@4
|
34 |
|
williamr@4
|
35 |
_STLP_BEGIN_NAMESPACE
|
williamr@4
|
36 |
|
williamr@4
|
37 |
template <class _Alloc>
|
williamr@4
|
38 |
void * _STLP_CALL __debug_alloc<_Alloc>::allocate(size_t __n) {
|
williamr@4
|
39 |
size_t __total_extra = __extra_before_chunk() + __extra_after_chunk();
|
williamr@4
|
40 |
size_t __real_n = __n + __total_extra;
|
williamr@4
|
41 |
if (__real_n < __n) {
|
williamr@4
|
42 |
//It means that we rolled on size_t, __n must be very large, lets hope
|
williamr@4
|
43 |
//that allocating it will raised a bad_alloc exception:
|
williamr@4
|
44 |
__real_n = __n + (__total_extra - __real_n - 1);
|
williamr@4
|
45 |
}
|
williamr@4
|
46 |
__alloc_header *__result = (__alloc_header *)__allocator_type::allocate(__real_n);
|
williamr@4
|
47 |
memset((char*)__result, __shred_byte, __real_n * sizeof(value_type));
|
williamr@4
|
48 |
__result->__magic = __magic;
|
williamr@4
|
49 |
__result->__type_size = sizeof(value_type);
|
williamr@4
|
50 |
__result->_M_size = (_STLP_UINT32_T)__n;
|
williamr@4
|
51 |
return ((char*)__result) + (long)__extra_before;
|
williamr@4
|
52 |
}
|
williamr@4
|
53 |
|
williamr@4
|
54 |
template <class _Alloc>
|
williamr@4
|
55 |
void _STLP_CALL
|
williamr@4
|
56 |
__debug_alloc<_Alloc>::deallocate(void *__p, size_t __n) {
|
williamr@4
|
57 |
__alloc_header * __real_p = (__alloc_header*)((char *)__p -(long)__extra_before);
|
williamr@4
|
58 |
// check integrity
|
williamr@4
|
59 |
_STLP_VERBOSE_ASSERT(__real_p->__magic != __deleted_magic, _StlMsg_DBA_DELETED_TWICE)
|
williamr@4
|
60 |
_STLP_VERBOSE_ASSERT(__real_p->__magic == __magic, _StlMsg_DBA_NEVER_ALLOCATED)
|
williamr@4
|
61 |
_STLP_VERBOSE_ASSERT(__real_p->__type_size == 1,_StlMsg_DBA_TYPE_MISMATCH)
|
williamr@4
|
62 |
_STLP_VERBOSE_ASSERT(__real_p->_M_size == __n, _StlMsg_DBA_SIZE_MISMATCH)
|
williamr@4
|
63 |
// check pads on both sides
|
williamr@4
|
64 |
unsigned char* __tmp;
|
williamr@4
|
65 |
for (__tmp = (unsigned char*)(__real_p + 1); __tmp < (unsigned char*)__p; ++__tmp) {
|
williamr@4
|
66 |
_STLP_VERBOSE_ASSERT(*__tmp == __shred_byte, _StlMsg_DBA_UNDERRUN)
|
williamr@4
|
67 |
}
|
williamr@4
|
68 |
|
williamr@4
|
69 |
size_t __real_n = __n + __extra_before_chunk() + __extra_after_chunk();
|
williamr@4
|
70 |
|
williamr@4
|
71 |
for (__tmp= ((unsigned char*)__p) + __n * sizeof(value_type);
|
williamr@4
|
72 |
__tmp < ((unsigned char*)__real_p) + __real_n ; ++__tmp) {
|
williamr@4
|
73 |
_STLP_VERBOSE_ASSERT(*__tmp == __shred_byte, _StlMsg_DBA_OVERRUN)
|
williamr@4
|
74 |
}
|
williamr@4
|
75 |
|
williamr@4
|
76 |
// that may be unfortunate, just in case
|
williamr@4
|
77 |
__real_p->__magic = __deleted_magic;
|
williamr@4
|
78 |
memset((char*)__p, __shred_byte, __n * sizeof(value_type));
|
williamr@4
|
79 |
__allocator_type::deallocate(__real_p, __real_n);
|
williamr@4
|
80 |
}
|
williamr@4
|
81 |
|
williamr@4
|
82 |
_STLP_END_NAMESPACE
|
williamr@4
|
83 |
|
williamr@4
|
84 |
#endif /* _STLP_ALLOC_C */
|
williamr@4
|
85 |
|
williamr@4
|
86 |
// Local Variables:
|
williamr@4
|
87 |
// mode:C++
|
williamr@4
|
88 |
// End:
|