allocators.cpp

来自「使用QT为linux 下的mplayer写的一个新的gui」· C++ 代码 · 共 1,091 行 · 第 1/3 页

CPP
1,091
字号

// Pthread allocators don't appear to the client to have meaningful
// instances.  We do in fact need to associate some state with each
// thread.  That state is represented by _Pthread_alloc_per_thread_state.

struct _Pthread_alloc_per_thread_state {
  typedef _Pthread_alloc_obj __obj;
  enum { _S_NFREELISTS = _MAX_BYTES / _STLP_DATA_ALIGNMENT };

  // Free list link for list of available per thread structures.
  // When one of these becomes available for reuse due to thread
  // termination, any objects in its free list remain associated
  // with it.  The whole structure may then be used by a newly
  // created thread.
  _Pthread_alloc_per_thread_state() : __next(0)
  { memset((void *)__CONST_CAST(_Pthread_alloc_obj**, __free_list), 0, (size_t)_S_NFREELISTS * sizeof(__obj *)); }
  // Returns an object of size __n, and possibly adds to size n free list.
  void *_M_refill(size_t __n);

  _Pthread_alloc_obj* volatile __free_list[_S_NFREELISTS];
  _Pthread_alloc_per_thread_state *__next;
  // this data member is only to be used by per_thread_allocator, which returns memory to the originating thread.
  _STLP_mutex _M_lock;
};

// Pthread-specific allocator.
class _Pthread_alloc_impl {
public: // but only for internal use:
  typedef _Pthread_alloc_per_thread_state __state_type;
  typedef char value_type;

  // Allocates a chunk for nobjs of size size.  nobjs may be reduced
  // if it is inconvenient to allocate the requested number.
  static char *_S_chunk_alloc(size_t __size, size_t &__nobjs, __state_type*);

  enum {_S_ALIGN = _STLP_DATA_ALIGNMENT};

  static size_t _S_round_up(size_t __bytes)
  { return (((__bytes) + (int)_S_ALIGN - 1) & ~((int)_S_ALIGN - 1)); }
  static size_t _S_freelist_index(size_t __bytes)
  { return (((__bytes) + (int)_S_ALIGN - 1) / (int)_S_ALIGN - 1); }

private:
  // Chunk allocation state. And other shared state.
  // Protected by _S_chunk_allocator_lock.
  static _STLP_STATIC_MUTEX _S_chunk_allocator_lock;
  static char *_S_start_free;
  static char *_S_end_free;
  static size_t _S_heap_size;
  static __state_type *_S_free_per_thread_states;
  static pthread_key_t _S_key;
  static bool _S_key_initialized;
  // Pthread key under which per thread state is stored.
  // Allocator instances that are currently unclaimed by any thread.
  static void _S_destructor(void *instance);
  // Function to be called on thread exit to reclaim per thread
  // state.
  static __state_type *_S_new_per_thread_state();
public:
  // Return a recycled or new per thread state.
  static __state_type *_S_get_per_thread_state();
private:
        // ensure that the current thread has an associated
        // per thread state.
  class _M_lock;
  friend class _M_lock;
  class _M_lock {
  public:
    _M_lock () { _S_chunk_allocator_lock._M_acquire_lock(); }
    ~_M_lock () { _S_chunk_allocator_lock._M_release_lock(); }
  };

public:

  /* n must be > 0      */
  static void * allocate(size_t& __n);

  /* p may not be 0 */
  static void deallocate(void *__p, size_t __n);

  // boris : versions for per_thread_allocator
  /* n must be > 0      */
  static void * allocate(size_t& __n, __state_type* __a);

  /* p may not be 0 */
  static void deallocate(void *__p, size_t __n, __state_type* __a);

  static void * reallocate(void *__p, size_t __old_sz, size_t& __new_sz);
};

/* Returns an object of size n, and optionally adds to size n free list.*/
/* We assume that n is properly aligned.                                */
/* We hold the allocation lock.                                         */
void *_Pthread_alloc_per_thread_state::_M_refill(size_t __n) {
  typedef _Pthread_alloc_obj __obj;
  size_t __nobjs = 128;
  char * __chunk = _Pthread_alloc_impl::_S_chunk_alloc(__n, __nobjs, this);
  __obj * volatile * __my_free_list;
  __obj * __result;
  __obj * __current_obj, * __next_obj;
  size_t __i;

  if (1 == __nobjs)  {
    return __chunk;
  }

  __my_free_list = __free_list + _Pthread_alloc_impl::_S_freelist_index(__n);

  /* Build free list in chunk */
  __result = (__obj *)__chunk;
  *__my_free_list = __next_obj = (__obj *)(__chunk + __n);
  for (__i = 1; ; ++__i) {
    __current_obj = __next_obj;
    __next_obj = (__obj *)((char *)__next_obj + __n);
    if (__nobjs - 1 == __i) {
      __current_obj -> __free_list_link = 0;
      break;
    } else {
      __current_obj -> __free_list_link = __next_obj;
    }
  }
  return __result;
}

void _Pthread_alloc_impl::_S_destructor(void *__instance) {
  _M_lock __lock_instance;  // Need to acquire lock here.
  _Pthread_alloc_per_thread_state* __s = (_Pthread_alloc_per_thread_state*)__instance;
  __s -> __next = _S_free_per_thread_states;
  _S_free_per_thread_states = __s;
}

_Pthread_alloc_per_thread_state* _Pthread_alloc_impl::_S_new_per_thread_state() {
  /* lock already held here.  */
  if (0 != _S_free_per_thread_states) {
    _Pthread_alloc_per_thread_state *__result = _S_free_per_thread_states;
    _S_free_per_thread_states = _S_free_per_thread_states -> __next;
    return __result;
  }
  else {
    return _STLP_NEW _Pthread_alloc_per_thread_state;
  }
}

_Pthread_alloc_per_thread_state* _Pthread_alloc_impl::_S_get_per_thread_state() {
  int __ret_code;
  __state_type* __result;

  if (_S_key_initialized && (__result = (__state_type*) pthread_getspecific(_S_key)))
    return __result;

  /*REFERENCED*/
  _M_lock __lock_instance;  // Need to acquire lock here.
  if (!_S_key_initialized) {
    if (pthread_key_create(&_S_key, _S_destructor)) {
      __THROW_BAD_ALLOC;  // failed
    }
    _S_key_initialized = true;
  }

  __result = _S_new_per_thread_state();
  __ret_code = pthread_setspecific(_S_key, __result);
  if (__ret_code) {
    if (__ret_code == ENOMEM) {
      __THROW_BAD_ALLOC;
    } else {
  // EINVAL
      _STLP_ABORT();
    }
  }
  return __result;
}

/* We allocate memory in large chunks in order to avoid fragmenting     */
/* the malloc heap too much.                                            */
/* We assume that size is properly aligned.                             */
char *_Pthread_alloc_impl::_S_chunk_alloc(size_t __p_size, size_t &__nobjs, _Pthread_alloc_per_thread_state *__a) {
  typedef _Pthread_alloc_obj __obj;
  {
    char * __result;
    size_t __total_bytes;
    size_t __bytes_left;
    /*REFERENCED*/
    _M_lock __lock_instance;         // Acquire lock for this routine

    __total_bytes = __p_size * __nobjs;
    __bytes_left = _S_end_free - _S_start_free;
    if (__bytes_left >= __total_bytes) {
      __result = _S_start_free;
      _S_start_free += __total_bytes;
      return __result;
    } else if (__bytes_left >= __p_size) {
      __nobjs = __bytes_left/__p_size;
      __total_bytes = __p_size * __nobjs;
      __result = _S_start_free;
      _S_start_free += __total_bytes;
      return __result;
    } else {
      size_t __bytes_to_get = 2 * __total_bytes + _S_round_up(_S_heap_size >> 4);
      // Try to make use of the left-over piece.
      if (__bytes_left > 0) {
        __obj * volatile * __my_free_list = __a->__free_list + _S_freelist_index(__bytes_left);
        ((__obj *)_S_start_free) -> __free_list_link = *__my_free_list;
        *__my_free_list = (__obj *)_S_start_free;
      }
#  ifdef _SGI_SOURCE
      // Try to get memory that's aligned on something like a
      // cache line boundary, so as to avoid parceling out
      // parts of the same line to different threads and thus
      // possibly different processors.
      {
        const int __cache_line_size = 128;  // probable upper bound
        __bytes_to_get &= ~(__cache_line_size-1);
        _S_start_free = (char *)memalign(__cache_line_size, __bytes_to_get);
        if (0 == _S_start_free) {
          _S_start_free = (char *)__malloc_alloc::allocate(__bytes_to_get);
        }
      }
#  else  /* !SGI_SOURCE */
      _S_start_free = (char *)__malloc_alloc::allocate(__bytes_to_get);
#  endif
      _S_heap_size += __bytes_to_get;
      _S_end_free = _S_start_free + __bytes_to_get;
    }
  }
  // lock is released here
  return _S_chunk_alloc(__p_size, __nobjs, __a);
}


/* n must be > 0      */
void *_Pthread_alloc_impl::allocate(size_t& __n) {
  typedef _Pthread_alloc_obj __obj;
  __obj * volatile * __my_free_list;
  __obj * __result;
  __state_type* __a;

  if (__n > _MAX_BYTES) {
    return __malloc_alloc::allocate(__n);
  }

  __n = _S_round_up(__n);
  __a = _S_get_per_thread_state();

  __my_free_list = __a->__free_list + _S_freelist_index(__n);
  __result = *__my_free_list;
  if (__result == 0) {
    void *__r = __a->_M_refill(__n);
    return __r;
  }
  *__my_free_list = __result->__free_list_link;
  return __result;
};

/* p may not be 0 */
void _Pthread_alloc_impl::deallocate(void *__p, size_t __n) {
  typedef _Pthread_alloc_obj __obj;
  __obj *__q = (__obj *)__p;
  __obj * volatile * __my_free_list;
  __state_type* __a;

  if (__n > _MAX_BYTES) {
      __malloc_alloc::deallocate(__p, __n);
      return;
  }

  __a = _S_get_per_thread_state();

  __my_free_list = __a->__free_list + _S_freelist_index(__n);
  __q -> __free_list_link = *__my_free_list;
  *__my_free_list = __q;
}

// boris : versions for per_thread_allocator
/* n must be > 0      */
void *_Pthread_alloc_impl::allocate(size_t& __n, __state_type* __a) {
  typedef _Pthread_alloc_obj __obj;
  __obj * volatile * __my_free_list;
  __obj * __result;

  if (__n > _MAX_BYTES) {
    return __malloc_alloc::allocate(__n);
  }
  __n = _S_round_up(__n);

  // boris : here, we have to lock per thread state, as we may be getting memory from
  // different thread pool.
  _STLP_auto_lock __lock(__a->_M_lock);

  __my_free_list = __a->__free_list + _S_freelist_index(__n);
  __result = *__my_free_list;
  if (__result == 0) {
    void *__r = __a->_M_refill(__n);
    return __r;
  }
  *__my_free_list = __result->__free_list_link;
  return __result;
};

/* p may not be 0 */
void _Pthread_alloc_impl::deallocate(void *__p, size_t __n, __state_type* __a) {
  typedef _Pthread_alloc_obj __obj;
  __obj *__q = (__obj *)__p;
  __obj * volatile * __my_free_list;

  if (__n > _MAX_BYTES) {
    __malloc_alloc::deallocate(__p, __n);
    return;
  }

  // boris : here, we have to lock per thread state, as we may be returning memory from
  // different thread.
  _STLP_auto_lock __lock(__a->_M_lock);

  __my_free_list = __a->__free_list + _S_freelist_index(__n);
  __q -> __free_list_link = *__my_free_list;
  *__my_free_list = __q;
}

void *_Pthread_alloc_impl::reallocate(void *__p, size_t __old_sz, size_t& __new_sz) {
  void * __result;
  size_t __copy_sz;

  if (__old_sz > _MAX_BYTES && __new_sz > _MAX_BYTES) {
    return realloc(__p, __new_sz);
  }

  if (_S_round_up(__old_sz) == _S_round_up(__new_sz)) return __p;
  __result = allocate(__new_sz);
  __copy_sz = __new_sz > __old_sz? __old_sz : __new_sz;
  memcpy(__result, __p, __copy_sz);
  deallocate(__p, __old_sz);
  return __result;
}

_Pthread_alloc_per_thread_state* _Pthread_alloc_impl::_S_free_per_thread_states = 0;
pthread_key_t _Pthread_alloc_impl::_S_key = 0;
_STLP_STATIC_MUTEX _Pthread_alloc_impl::_S_chunk_allocator_lock _STLP_MUTEX_INITIALIZER;
bool _Pthread_alloc_impl::_S_key_initialized = false;
char *_Pthread_alloc_impl::_S_start_free = 0;
char *_Pthread_alloc_impl::_S_end_free = 0;
size_t _Pthread_alloc_impl::_S_heap_size = 0;

void * _STLP_CALL _Pthread_alloc::allocate(size_t& __n)
{ return _Pthread_alloc_impl::allocate(__n); }
void _STLP_CALL _Pthread_alloc::deallocate(void *__p, size_t __n)
{ _Pthread_alloc_impl::deallocate(__p, __n); }
void * _STLP_CALL _Pthread_alloc::allocate(size_t& __n, __state_type* __a)
{ return _Pthread_alloc_impl::allocate(__n, __a); }
void _STLP_CALL _Pthread_alloc::deallocate(void *__p, size_t __n, __state_type* __a)
{ _Pthread_alloc_impl::deallocate(__p, __n, __a); }
void * _STLP_CALL _Pthread_alloc::reallocate(void *__p, size_t __old_sz, size_t& __new_sz)
{ return _Pthread_alloc_impl::reallocate(__p, __old_sz, __new_sz); }
_Pthread_alloc_per_thread_state* _STLP_CALL _Pthread_alloc::_S_get_per_thread_state()
{ return _Pthread_alloc_impl::_S_get_per_thread_state(); }

_STLP_MOVE_TO_STD_NAMESPACE

#endif

_STLP_END_NAMESPACE

#undef _S_FREELIST_INDEX

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?