/home/ntakagi/work/STLport-5.1.5/stlport/stl/_alloc.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1996,1997
00004  * Silicon Graphics Computer Systems, Inc.
00005  *
00006  * Copyright (c) 1997
00007  * Moscow Center for SPARC Technology
00008  *
00009  * Copyright (c) 1999
00010  * Boris Fomitchev
00011  *
00012  * This material is provided "as is", with absolutely no warranty expressed
00013  * or implied. Any use is at your own risk.
00014  *
00015  * Permission to use or copy this software for any purpose is hereby granted
00016  * without fee, provided the above notices are retained on all copies.
00017  * Permission to modify the code and to distribute modified code is granted,
00018  * provided the above notices are retained, and a notice that the code was
00019  * modified is included with the above copyright notice.
00020  *
00021  */
00022 #ifndef _STLP_ALLOC_C
00023 #define _STLP_ALLOC_C
00024 
00025 #ifndef _STLP_INTERNAL_ALLOC_H
00026 #  include <stl/_alloc.h>
00027 #endif
00028 
00029 #if defined (__WATCOMC__)
00030 #  pragma warning 13 9
00031 #  pragma warning 367 9
00032 #  pragma warning 368 9
00033 #endif
00034 
00035 _STLP_BEGIN_NAMESPACE
00036 
00037 template <class _Alloc>
00038 void * _STLP_CALL __debug_alloc<_Alloc>::allocate(size_t __n) {
00039   size_t __total_extra = __extra_before_chunk() + __extra_after_chunk();
00040   size_t __real_n = __n + __total_extra;
00041   if (__real_n < __n) {
00042     //It means that we rolled on size_t, __n must be very large, lets hope
00043     //that allocating it will raised a bad_alloc exception:
00044     __real_n = __n + (__total_extra - __real_n - 1);
00045   }
00046   __alloc_header *__result = (__alloc_header *)__allocator_type::allocate(__real_n);
00047   memset((char*)__result, __shred_byte, __real_n * sizeof(value_type));
00048   __result->__magic = __magic;
00049   __result->__type_size = sizeof(value_type);
00050   __result->_M_size = (_STLP_UINT32_T)__n;
00051   return ((char*)__result) + (long)__extra_before;
00052 }
00053 
00054 template <class _Alloc>
00055 void  _STLP_CALL
00056 __debug_alloc<_Alloc>::deallocate(void *__p, size_t __n) {
00057   __alloc_header * __real_p = (__alloc_header*)((char *)__p -(long)__extra_before);
00058   // check integrity
00059   _STLP_VERBOSE_ASSERT(__real_p->__magic != __deleted_magic, _StlMsg_DBA_DELETED_TWICE)
00060   _STLP_VERBOSE_ASSERT(__real_p->__magic == __magic, _StlMsg_DBA_NEVER_ALLOCATED)
00061   _STLP_VERBOSE_ASSERT(__real_p->__type_size == 1,_StlMsg_DBA_TYPE_MISMATCH)
00062   _STLP_VERBOSE_ASSERT(__real_p->_M_size == __n, _StlMsg_DBA_SIZE_MISMATCH)
00063   // check pads on both sides
00064   unsigned char* __tmp;
00065   for (__tmp = (unsigned char*)(__real_p + 1); __tmp < (unsigned char*)__p; ++__tmp) {
00066     _STLP_VERBOSE_ASSERT(*__tmp == __shred_byte, _StlMsg_DBA_UNDERRUN)
00067   }
00068 
00069   size_t __real_n = __n + __extra_before_chunk() + __extra_after_chunk();
00070 
00071   for (__tmp= ((unsigned char*)__p) + __n * sizeof(value_type);
00072        __tmp < ((unsigned char*)__real_p) + __real_n ; ++__tmp) {
00073     _STLP_VERBOSE_ASSERT(*__tmp == __shred_byte, _StlMsg_DBA_OVERRUN)
00074   }
00075 
00076   // that may be unfortunate, just in case
00077   __real_p->__magic = __deleted_magic;
00078   memset((char*)__p, __shred_byte, __n * sizeof(value_type));
00079   __allocator_type::deallocate(__real_p, __real_n);
00080 }
00081 
00082 _STLP_END_NAMESPACE
00083 
00084 #endif /*  _STLP_ALLOC_C */
00085 
00086 // Local Variables:
00087 // mode:C++
00088 // End:



Generated on Mon Mar 10 15:32:18 2008 by  doxygen 1.5.1