home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _pthread_alloc.c < prev    next >
C/C++ Source or Header  |  2002-02-08  |  8KB  |  253 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1996,1997
  4.  * Silicon Graphics Computer Systems, Inc.
  5.  *
  6.  * Copyright (c) 1997
  7.  * Moscow Center for SPARC Technology
  8.  *
  9.  * Copyright (c) 1999 
  10.  * Boris Fomitchev
  11.  *
  12.  * This material is provided "as is", with absolutely no warranty expressed
  13.  * or implied. Any use is at your own risk.
  14.  *
  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.
  20.  *
  21.  */
  22. #ifndef _STLP_PTHREAD_ALLOC_C
  23. #define _STLP_PTHREAD_ALLOC_C
  24.  
  25. #ifdef __WATCOMC__
  26. #pragma warning 13 9
  27. #pragma warning 367 9
  28. #pragma warning 368 9
  29. #endif
  30.  
  31. #ifndef _STLP_PTHREAD_ALLOC_H
  32. # include <stl/_pthread_alloc.h>
  33. #endif
  34.  
  35. # if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
  36.  
  37. # include <cerrno>
  38.  
  39. _STLP_BEGIN_NAMESPACE
  40.  
  41. template <size_t _Max_size>
  42. void _Pthread_alloc<_Max_size>::_S_destructor(void * __instance)
  43. {
  44.     _M_lock __lock_instance;    // Need to acquire lock here.
  45.     _Pthread_alloc_per_thread_state<_Max_size>* __s =
  46.         (_Pthread_alloc_per_thread_state<_Max_size> *)__instance;
  47.     __s -> __next = _S_free_per_thread_states;
  48.     _S_free_per_thread_states = __s;
  49. }
  50.  
  51. template <size_t _Max_size>
  52. _Pthread_alloc_per_thread_state<_Max_size> *
  53. _Pthread_alloc<_Max_size>::_S_new_per_thread_state()
  54. {    
  55.     /* lock already held here.    */
  56.     if (0 != _S_free_per_thread_states) {
  57.         _Pthread_alloc_per_thread_state<_Max_size> *__result =
  58.                     _S_free_per_thread_states;
  59.         _S_free_per_thread_states = _S_free_per_thread_states -> __next;
  60.         return __result;
  61.     } else {
  62.         return _STLP_NEW _Pthread_alloc_per_thread_state<_Max_size>;
  63.     }
  64. }
  65.  
  66. template <size_t _Max_size>
  67. _Pthread_alloc_per_thread_state<_Max_size> *
  68. _Pthread_alloc<_Max_size>::_S_get_per_thread_state()
  69. {
  70.  
  71.     int __ret_code;
  72.     __state_type* __result;
  73.     
  74.     if (_S_key_initialized && (__result = (__state_type*) pthread_getspecific(_S_key)))
  75.       return __result;
  76.     
  77.     /*REFERENCED*/
  78.     _M_lock __lock_instance;    // Need to acquire lock here.
  79.     if (!_S_key_initialized) {
  80.       if (pthread_key_create(&_S_key, _S_destructor)) {
  81.     __THROW_BAD_ALLOC;  // failed
  82.       }
  83.       _S_key_initialized = true;
  84.     }
  85.  
  86.     __result = _S_new_per_thread_state();
  87.     __ret_code = pthread_setspecific(_S_key, __result);
  88.     if (__ret_code) {
  89.       if (__ret_code == ENOMEM) {
  90.     __THROW_BAD_ALLOC;
  91.       } else {
  92.     // EINVAL
  93.     _STLP_ABORT();
  94.       }
  95.     }
  96.     return __result;
  97. }
  98.  
  99. /* We allocate memory in large chunks in order to avoid fragmenting     */
  100. /* the malloc heap too much.                                            */
  101. /* We assume that size is properly aligned.                             */
  102. template <size_t _Max_size>
  103. char *_Pthread_alloc<_Max_size>
  104. ::_S_chunk_alloc(size_t __p_size, size_t &__nobjs)
  105. {
  106.   {
  107.     char * __result;
  108.     size_t __total_bytes;
  109.     size_t __bytes_left;
  110.     /*REFERENCED*/
  111.     _M_lock __lock_instance;         // Acquire lock for this routine
  112.  
  113.     __total_bytes = __p_size * __nobjs;
  114.     __bytes_left = _S_end_free - _S_start_free;
  115.     if (__bytes_left >= __total_bytes) {
  116.         __result = _S_start_free;
  117.         _S_start_free += __total_bytes;
  118.         return(__result);
  119.     } else if (__bytes_left >= __p_size) {
  120.         __nobjs = __bytes_left/__p_size;
  121.         __total_bytes = __p_size * __nobjs;
  122.         __result = _S_start_free;
  123.         _S_start_free += __total_bytes;
  124.         return(__result);
  125.     } else {
  126.         size_t __bytes_to_get =
  127.         2 * __total_bytes + _S_round_up(_S_heap_size >> 4);
  128.         // Try to make use of the left-over piece.
  129.         if (__bytes_left > 0) {
  130.             _Pthread_alloc_per_thread_state<_Max_size>* __a = 
  131.                 (_Pthread_alloc_per_thread_state<_Max_size>*)
  132.             pthread_getspecific(_S_key);
  133.             __obj * volatile * __my_free_list =
  134.                         __a->__free_list + _S_freelist_index(__bytes_left);
  135.  
  136.             ((__obj *)_S_start_free) -> __free_list_link = *__my_free_list;
  137.             *__my_free_list = (__obj *)_S_start_free;
  138.         }
  139. #       ifdef _SGI_SOURCE
  140.           // Try to get memory that's aligned on something like a
  141.           // cache line boundary, so as to avoid parceling out
  142.           // parts of the same line to different threads and thus
  143.           // possibly different processors.
  144.           {
  145.             const int __cache_line_size = 128;  // probable upper bound
  146.             __bytes_to_get &= ~(__cache_line_size-1);
  147.             _S_start_free = (char *)memalign(__cache_line_size, __bytes_to_get); 
  148.             if (0 == _S_start_free) {
  149.               _S_start_free = (char *)__malloc_alloc<0>::allocate(__bytes_to_get);
  150.             }
  151.           }
  152. #       else  /* !SGI_SOURCE */
  153.           _S_start_free = (char *)__malloc_alloc<0>::allocate(__bytes_to_get);
  154. #       endif
  155.         _S_heap_size += __bytes_to_get;
  156.         _S_end_free = _S_start_free + __bytes_to_get;
  157.     }
  158.   }
  159.   // lock is released here
  160.   return(_S_chunk_alloc(__p_size, __nobjs));
  161. }
  162.  
  163.  
  164. /* Returns an object of size n, and optionally adds to size n free list.*/
  165. /* We assume that n is properly aligned.                                */
  166. /* We hold the allocation lock.                                         */
  167. template <size_t _Max_size>
  168. void *_Pthread_alloc_per_thread_state<_Max_size>
  169. ::_M_refill(size_t __n)
  170. {
  171.     size_t __nobjs = 128;
  172.     char * __chunk =
  173.     _Pthread_alloc<_Max_size>::_S_chunk_alloc(__n, __nobjs);
  174.     __obj * volatile * __my_free_list;
  175.     __obj * __result;
  176.     __obj * __current_obj, * __next_obj;
  177.     int __i;
  178.  
  179.     if (1 == __nobjs)  {
  180.         return(__chunk);
  181.     }
  182.     __my_free_list = __free_list
  183.          + _Pthread_alloc<_Max_size>::_S_freelist_index(__n);
  184.  
  185.     /* Build free list in chunk */
  186.       __result = (__obj *)__chunk;
  187.       *__my_free_list = __next_obj = (__obj *)(__chunk + __n);
  188.       for (__i = 1; ; __i++) {
  189.         __current_obj = __next_obj;
  190.         __next_obj = (__obj *)((char *)__next_obj + __n);
  191.         if (__nobjs - 1 == __i) {
  192.             __current_obj -> __free_list_link = 0;
  193.             break;
  194.         } else {
  195.             __current_obj -> __free_list_link = __next_obj;
  196.         }
  197.       }
  198.     return(__result);
  199. }
  200.  
  201. template <size_t _Max_size>
  202. void *_Pthread_alloc<_Max_size>
  203. ::reallocate(void *__p, size_t __old_sz, size_t __new_sz)
  204. {
  205.     void * __result;
  206.     size_t __copy_sz;
  207.  
  208.     if (__old_sz > _Max_size
  209.     && __new_sz > _Max_size) {
  210.         return(realloc(__p, __new_sz));
  211.     }
  212.     if (_S_round_up(__old_sz) == _S_round_up(__new_sz)) return(__p);
  213.     __result = allocate(__new_sz);
  214.     __copy_sz = __new_sz > __old_sz? __old_sz : __new_sz;
  215.     memcpy(__result, __p, __copy_sz);
  216.     deallocate(__p, __old_sz);
  217.     return(__result);
  218. }
  219.  
  220. #if defined (_STLP_STATIC_TEMPLATE_DATA) && (_STLP_STATIC_TEMPLATE_DATA > 0)
  221.  
  222. template <size_t _Max_size>
  223. _Pthread_alloc_per_thread_state<_Max_size> * _Pthread_alloc<_Max_size>::_S_free_per_thread_states = 0;
  224.  
  225. template <size_t _Max_size>
  226. pthread_key_t _Pthread_alloc<_Max_size>::_S_key =0;
  227.  
  228. template <size_t _Max_size>
  229. bool _Pthread_alloc<_Max_size>::_S_key_initialized = false;
  230.  
  231. template <size_t _Max_size>
  232. _STLP_mutex_base _Pthread_alloc<_Max_size>::_S_chunk_allocator_lock _STLP_MUTEX_INITIALIZER;
  233.  
  234. template <size_t _Max_size>
  235. char *_Pthread_alloc<_Max_size>::_S_start_free = 0;
  236.  
  237. template <size_t _Max_size>
  238. char *_Pthread_alloc<_Max_size>::_S_end_free = 0;
  239.  
  240. template <size_t _Max_size>
  241. size_t _Pthread_alloc<_Max_size>::_S_heap_size = 0;
  242. # endif
  243.  
  244. _STLP_END_NAMESPACE
  245.  
  246. # endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
  247.  
  248. #endif /*  _STLP_PTHREAD_ALLOC_C */
  249.  
  250. // Local Variables:
  251. // mode:C++
  252. // End:
  253.