3 * Copyright (c) 1996,1997
4 * Silicon Graphics Computer Systems, Inc.
7 * Moscow Center for SPARC Technology
12 * This material is provided "as is", with absolutely no warranty expressed
13 * or implied. Any use is at your own risk.
15 * Permission to use or copy this software for any purpose is hereby granted
16 * without fee, provided the above notices are retained on all copies.
17 * Permission to modify the code and to distribute modified code is granted,
18 * provided the above notices are retained, and a notice that the code was
19 * modified is included with the above copyright notice.
25 #ifndef _STLP_INTERNAL_ALLOC_H
26 # include <stl/_alloc.h>
29 #if defined (__WATCOMC__)
31 # pragma warning 367 9
32 # pragma warning 368 9
37 template <class _Alloc>
38 void * _STLP_CALL __debug_alloc<_Alloc>::allocate(size_t __n) {
39 size_t __total_extra = __extra_before_chunk() + __extra_after_chunk();
40 size_t __real_n = __n + __total_extra;
42 //It means that we rolled on size_t, __n must be very large, lets hope
43 //that allocating it will raised a bad_alloc exception:
44 __real_n = __n + (__total_extra - __real_n - 1);
46 __alloc_header *__result = (__alloc_header *)__allocator_type::allocate(__real_n);
47 memset((char*)__result, __shred_byte, __real_n * sizeof(value_type));
48 __result->__magic = __magic;
49 __result->__type_size = sizeof(value_type);
50 __result->_M_size = (_STLP_UINT32_T)__n;
51 return ((char*)__result) + (long)__extra_before;
54 template <class _Alloc>
56 __debug_alloc<_Alloc>::deallocate(void *__p, size_t __n) {
57 __alloc_header * __real_p = (__alloc_header*)((char *)__p -(long)__extra_before);
59 _STLP_VERBOSE_ASSERT(__real_p->__magic != __deleted_magic, _StlMsg_DBA_DELETED_TWICE)
60 _STLP_VERBOSE_ASSERT(__real_p->__magic == __magic, _StlMsg_DBA_NEVER_ALLOCATED)
61 _STLP_VERBOSE_ASSERT(__real_p->__type_size == 1,_StlMsg_DBA_TYPE_MISMATCH)
62 _STLP_VERBOSE_ASSERT(__real_p->_M_size == __n, _StlMsg_DBA_SIZE_MISMATCH)
63 // check pads on both sides
65 for (__tmp = (unsigned char*)(__real_p + 1); __tmp < (unsigned char*)__p; ++__tmp) {
66 _STLP_VERBOSE_ASSERT(*__tmp == __shred_byte, _StlMsg_DBA_UNDERRUN)
69 size_t __real_n = __n + __extra_before_chunk() + __extra_after_chunk();
71 for (__tmp= ((unsigned char*)__p) + __n * sizeof(value_type);
72 __tmp < ((unsigned char*)__real_p) + __real_n ; ++__tmp) {
73 _STLP_VERBOSE_ASSERT(*__tmp == __shred_byte, _StlMsg_DBA_OVERRUN)
76 // that may be unfortunate, just in case
77 __real_p->__magic = __deleted_magic;
78 memset((char*)__p, __shred_byte, __n * sizeof(value_type));
79 __allocator_type::deallocate(__real_p, __real_n);
84 #endif /* _STLP_ALLOC_C */