From 1c86f39d32b95fe9de306d82f02a982bbad778b4 Mon Sep 17 00:00:00 2001 From: Benjamin Kosnik Date: Thu, 12 Feb 2004 01:11:48 +0000 Subject: [PATCH] [multiple changes] 2004-02-11 Stefan Olsson * docs/html/ext/mt_allocator.html: New. 2004-02-11 Benjamin Kosnik * docs/html/20_util/allocator.html: New file, consolidate allocator information here. Revamp. * docs/html/documentation.html: Change links. * docs/html/20_util/howto.html: Same. * docs/html/ext/howto.html: Same. From-SVN: r77687 --- libstdc++-v3/ChangeLog | 12 + libstdc++-v3/docs/html/20_util/allocator.html | 468 ++++++++++++++++++ libstdc++-v3/docs/html/20_util/howto.html | 2 +- libstdc++-v3/docs/html/documentation.html | 9 +- libstdc++-v3/docs/html/ext/howto.html | 244 +-------- libstdc++-v3/docs/html/ext/mt_allocator.html | 414 ++++++++++++++++ 6 files changed, 900 insertions(+), 249 deletions(-) create mode 100644 libstdc++-v3/docs/html/20_util/allocator.html create mode 100644 libstdc++-v3/docs/html/ext/mt_allocator.html diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 5926b918e7c..b74af678a17 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2004-02-11 Stefan Olsson + + * docs/html/ext/mt_allocator.html: New. + +2004-02-11 Benjamin Kosnik + + * docs/html/20_util/allocator.html: New file, consolidate + allocator information here. Revamp. + * docs/html/documentation.html: Change links. + * docs/html/20_util/howto.html: Same. + * docs/html/ext/howto.html: Same. + 2004-02-11 Paolo Carlini PR libstdc++/13731 (first part: write) diff --git a/libstdc++-v3/docs/html/20_util/allocator.html b/libstdc++-v3/docs/html/20_util/allocator.html new file mode 100644 index 00000000000..43aaae7e079 --- /dev/null +++ b/libstdc++-v3/docs/html/20_util/allocator.html @@ -0,0 +1,468 @@ + + + + + + + + + + Allocators and allocation + + + + + +

Allocators and allocation

+ +

+ The latest version of this document is always available at + + http://gcc.gnu.org/onlinedocs/libstdc++/20_util/allocator.html. +

+ +

+ To the libstdc++-v3 homepage. +

+ + +
+

The C++ Standard encapsulates memory management characteristics + for strings, container classes, and parts of iostreams in a + template class called std::allocator. +

+ +

+ Standard requirements +

+

The C++ standard only gives a few directives in this area: +

+
    +
  • When you add elements to a container, and the container must allocate + more memory to hold them, the container makes the request via its + Allocator template parameter. This includes adding + chars to the string class, which acts as a regular STL container + in this respect. +
  • +
  • The default Allocator of every container-of-T is + std::allocator<T>. +
  • +
  • The interface of the allocator<T> class is + extremely simple. It has about 20 public declarations (nested + typedefs, member functions, etc), but the two which concern us most + are: +
    +      T*    allocate   (size_type n, const void* hint = 0);
    +      void  deallocate (T* p, size_type n);
    + (This is a simplification; the real signatures use nested typedefs.) + The "n" arguments in both those functions is a + count of the number of T's to allocate space for, + not their total size. +
  • +
  • "The storage is obtained by calling + ::operator new(size_t), but it is unspecified when or + how often this function is called. The use of hint + is unspecified, but intended as an aid to locality if an + implementation so desires." [20.4.1.1]/6 +
  • +
+ +

Complete details cam be found in the C++ standard, look in + [20.4 Memory]. +

+ +

+ Problems and Possibilities +

+

The easiest way of fulfilling the requirements is to call operator new + each time a container needs memory, and to call operator delete each + time the container releases memory. BUT + this + method is horribly slow. +

+

Or we can keep old memory around, and reuse it in a pool to save time. + The old libstdc++-v2 used a memory pool, and so do we. As of 3.0, + it's + on by default. The pool is shared among all the containers in the + program: when your program's std::vector<int> gets cut in half + and frees a bunch of its storage, that memory can be reused by the + private std::list<WonkyWidget> brought in from a KDE library + that you linked against. And we don't have to call operators new and + delete to pass the memory on, either, which is a speed bonus. + BUT... +

+

What about threads? No problem: in a threadsafe environment, the + memory pool is manipulated atomically, so you can grow a container in + one thread and shrink it in another, etc. BUT what + if threads in libstdc++-v3 aren't set up properly? + That's been answered already. +

+

BUT what if you want to use your own allocator? What + if you plan on using a runtime-loadable version of malloc() which uses + shared telepathic anonymous mmap'd sections serializable over a + network, so that memory requests should go through malloc? + And what if you need to debug it? +

+ +

+ Implementation details of std::allocator +

+

The implementation of std::allocator has continued + to evolve through successive releases. Here's a brief history. +

+ +
+ 3.0, 3.1, 3.2, 3.3 +
+

During this period, all allocators were written to the SGI + style, and all STL containers expected this interface. This + interface had a traits class called _Alloc_traits that + attempted to provide more information for compile-time allocation + selection and optimization. This traits class had another allocator + wrapper, __simple_alloc<T,A>, which was a + wrapper around another allocator, A, which itself is an allocator + for instances of T. But wait, there's more: + __allocator<T,A> is another adapter. Many of + the provided allocator classes were SGI style: such classes can be + changed to a conforming interface with this wrapper: + __allocator<T, __alloc> is thus the same as + allocator<T>. +

+ +

The class std::allocator use the typedef + __alloc to select an underlying allocator that + satisfied memory allocation requests. The selection of this + underlying allocator was not user-configurable. +

+ +
+ 3.4 +
+

For this and later releases, the only allocator interface that + is support is the standard C++ interface. As such, all STL + containers have been adjusted, and all external allocators have + been modified to support this change. Because of this, + __simple_alloc, __allocator, __alloc, and + _Alloc_traits have all been removed. +

+ +

The class std::allocator just has typedef, + constructor, and rebind members. It inherits from one of the + high-speed extension allocators, covered below. Thus, all + allocation and deallocation depends on the base class. +

+ +

The base class that std::allocator is derived from + is not user-configurable. +

+ +
+ How the default allocation strategy is selected. +
+

It's difficult to pick an allocation strategy that will provide + maximum utility, without excessively penalizing some behavior. In + fact, it's difficult just deciding which typical actions to measure + for speed. +

+ +

Three synthetic benchmarks have been created that provide data + that is used to compare different C++ allocators. These tests are: +

+ +
    +
  • Insertion. Over multiple iterations, various STL container + objects have elements inserted to some maximum amount. A variety + of allocators are tested. + Test source here. +
  • + +
  • Insertion, clear, and re-insertion in a multi-threaded + environment. Over multiple iterations, several threads are + started that insert elements into a STL container, then assign a + null instance of the same type to clear memory, and then + re-insert the same number of elements. Several STL containers and + multiple allocators are tested. This test shows the ability of + the allocator to reclaim memory on a pre-thread basis, as well as + measuring thread contention for memory resources. + Test source + + here. +
  • + +
  • A threaded producer/consumer model. + Test source + + here. +
  • +
+ +
+ Disabling memory caching. +
+

In use, std::allocator may allocate and deallocate + using implementation-specified strategies and heuristics. Because of + this, every call to an allocator object's allocate + member function may not actually call the global operator new. This + situation is also duplicated for calls to the + deallocate member function. +

+ +

This can be confusing. +

+ +

In particular, this can make debugging memory errors more + difficult, especially when using third party tools like valgrind or + debug versions of new. +

+ +

There are various ways to solve this problem. One would be to + use a custom allocator that just called operators new + and delete directly, for every + allocation. (See include/ext/new_allocator.h, for instance.) + However, that option would involve changing source code to use the a + non-default allocator. Another option is to force the default + allocator to remove caching and pools, and to directly allocate + with every call of allocate and directly deallocate + with every call of deallocate, regardless of + efficiency. As it turns out, this last option is available, + although the exact mechanism has evolved with time. +

+ +

For GCC releases from 2.95 through the 3.1 series, defining + __USE_MALLOC on the gcc command line would change the + default allocation strategy to instead use malloc and + free. See + this note + for details as to why this was something needing improvement. +

+ +

Starting with GCC 3.2, and continued in the 3.3 series, to + globally disable memory caching within the library for the + default allocator, merely set GLIBCPP_FORCE_NEW (at this time, + with any value) in the system's environment before running the + program. If your program crashes with GLIBCPP_FORCE_NEW in the + environment, it likely means that you linked against objects + built against the older library. Code to support this extension + is fully compatible with 3.2 code if GLIBCPP_FORCE_NEW is not in + the environment. +

+ +

As it turns out, the 3.4 code base continues to use this + mechanism, only the environment variable has been changed to + GLIBCXX_FORCE_NEW. +

+ +

+ Other allocators +

+

Several other allocators are provided as part of this + implementation. The location of the extension allocators and their + names have changed, but in all cases, functionality is + equivalent. Starting with gcc-3.4, all extension allocators are + standard style. Before this point, SGI style was the norm. Because of + this, the number of template arguments also changed. Here's a simple + chart to track the changes. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Allocator (3.4)Header (3.4)Allocator (3.[0-3])Header (3.[0-3])
__gnu_cxx::new_allocator<T><ext/new_allocator.h>std::__new_alloc<memory>
__gnu_cxx::malloc_allocator<T><ext/malloc_allocator.h>std::__malloc_alloc_template<int><memory>
__gnu_cxx::debug_allocator<T><ext/debug_allocator.h>std::debug_alloc<T><memory>
__gnu_cxx::__pool_alloc<bool, int><ext/pool_allocator.h>std::__default_alloc_template<bool,int><memory>
__gnu_cxx::__mt_alloc<T><ext/mt_allocator.h>
+ +

More details on each of these allocators follows.

+
    +
  • new_allocator +

    Simply wraps ::operator new + and ::operator delete. +

    +
  • +
  • malloc_allocator +

    Simply wraps + malloc and free. There is also a hook + for an out-of-memory handler (for new/delete this is taken care of + elsewhere). +

    +
  • +
  • debug_allocator +

    A wrapper around an + arbitrary allocator A. It passes on slightly increased size + requests to A, and uses the extra memory to store size information. + When a pointer is passed to deallocate(), the stored + size is checked, and assert() is used to guarantee they match. +

    +
  • +
  • __pool_alloc +

    A high-performance, single pool allocator. The reusable + memory is shared among identical instantiations of this type. + It calls through ::operator new to obtain new memory + when its lists run out. If a client container requests a block + larger than a certain threshold size, then the pool is bypassed, + and the allocate/deallocate request is passed to + ::operator new directly.

    + +

    This class take a boolean template parameter, called + thr, and an integer template parameter, called + inst. +

    +

    The inst number is used to track additional memory + pools. The point of the number is to allow multiple + instantiations of the classes without changing the semantics at + all. All three of +

    + +
    +    typedef  __pool_alloc<true,0>    normal;
    +    typedef  __pool_alloc<true,1>    private;
    +    typedef  __pool_alloc<true,42>   also_private;
    +

    behave exactly the same way. However, the memory pool for each type + (and remember that different instantiations result in different types) + remains separate. +

    +

    The library uses 0 in all its instantiations. If you + wish to keep separate free lists for a particular purpose, use a + different number. +

    +

    The thr boolean determines whether the pool should + be manipulated atomically or not. When thr=true, the allocator + is is threadsafe, while thr=false, and is slightly faster but + unsafe for multiple threads. +

    +

    (Note that the GCC thread abstraction layer allows us to provide safe + zero-overhead stubs for the threading routines, if threads were + disabled at configuration time.) +

    + +
  • + +
  • __mt_alloc +

    A high-performance + fixed-size allocator. It has its own documentation, found here. +

    +
  • +
+ + +

+ Using a specific allocator +

+

You can specify different memory management schemes on a + per-container basis, by overriding the default + Allocator template parameter. For example, an easy + (but non-portable) method of specifying that only malloc/free + should be used instead of the default node allocator is: +

+
+    std::list <int, __gnu_cxx::malloc_allocator<int> >  malloc_list;
+ Likewise, a debugging form of whichever allocator is currently in use: +
+    std::deque <int, __gnu_cxx::debug_allocator<std::allocator<int> > >  debug_deque;
+ + +

+ Writing custom allocators +

+

Writing a portable C++ allocator would dictate that the + interface would look much like the one specified for + std::allocator. Additional member functions, but not + subtractions, would be permissible. +

+ +

Probably the best place to start would be to copy one of the + extension allocators already shipped with libstdc++: say, + new_allocator . +

+ + +

+ Bibliography / Further Reading +

+

+ ISO/IEC 14882:1998 Programming languages - C++ [20.4 Memory] +

+ +

+ Austern, Matt, C/C++ Users Journal. + The Standard Librarian: What Are Allocators Good + For? +

+ +

+ Berger, Emery, + The Hoard memory allocator +

+ +

+ Berger, Emery with Ben Zorn & Kathryn McKinley, OOPSLA 2002 + Reconsidering Custom Memory Allocation +

+ +

+ Kreft, Klaus and Angelika Langer, C++ Report, June 1998 + Allocator Types +

+ +

+ Stroustrup, Bjarne, 19.4 Allocators, The C++ Programming + Language, Special Edition, Addison Wesley, Inc. 2000 +

+ +

+ Yen, Felix, Yalloc: A Recycling C++ Allocator +

+ +
+

Return to the top of the page or + to the libstdc++ homepage. +

+ + + + +
+

+See license.html for copying conditions. +Comments and suggestions are welcome, and may be sent to +the libstdc++ mailing list. +

+ + + + diff --git a/libstdc++-v3/docs/html/20_util/howto.html b/libstdc++-v3/docs/html/20_util/howto.html index 13d0c8e0855..9597707a8be 100644 --- a/libstdc++-v3/docs/html/20_util/howto.html +++ b/libstdc++-v3/docs/html/20_util/howto.html @@ -219,7 +219,7 @@

Memory allocators

The available free store ("heap") management classes are - described here. + described here.

Return to top of page or to the FAQ. diff --git a/libstdc++-v3/docs/html/documentation.html b/libstdc++-v3/docs/html/documentation.html index ceb60764e10..54bd59e8745 100644 --- a/libstdc++-v3/docs/html/documentation.html +++ b/libstdc++-v3/docs/html/documentation.html @@ -126,8 +126,8 @@

  • Implementation properties
  • Start and Termination
  • Verbose terminate
  • -
  • Dynamic memory management
  • -
  • RTTI, the ABI, and demangling
  • +
  • Dynamic memory management
  • +
  • RTTI, the ABI, and demangling
  • @@ -145,7 +145,7 @@
  • auto_ptr inside container classes
  • Functors
  • Pairs
  • -
  • Memory allocators
  • +
  • Allocators and allocation
  • @@ -226,8 +226,7 @@
    • Ropes and trees and hashes, oh my!
    • Added members and types
    • -
    • Allocators (versions 3.0, 3.1, 3.2, 3.3)
    • -
    • Allocators (version 3.4)
    • +
    • __mt_alloc
    • Compile-time checks
    • LWG Issues
    • Demangling
    • diff --git a/libstdc++-v3/docs/html/ext/howto.html b/libstdc++-v3/docs/html/ext/howto.html index 2ce76ee9db3..3e5c35c476c 100644 --- a/libstdc++-v3/docs/html/ext/howto.html +++ b/libstdc++-v3/docs/html/ext/howto.html @@ -48,8 +48,7 @@
      • Ropes and trees and hashes, oh my!
      • Added members and types
      • -
      • Allocators (versions 3.0, 3.1, 3.2, 3.3)
      • -
      • Allocators (version 3.4)
      • +
      • __mt_alloc
      • Compile-time checks
      • LWG Issues
      • Demangling
      • @@ -140,247 +139,6 @@ to the FAQ.

        -
        -

        Allocators (versions 3.0, 3.1, 3.2, 3.3)

        -

        Thread-safety, space efficiency, high speed, portability... this is a - mess. Where to begin? -

        -

        The Rules

        -

        The C++ standard only gives a few directives in this area: -

        -
          -
        • When you add elements to a container, and the container must allocate - more memory to hold them, the container makes the request via its - Allocator template parameter. This includes adding - char's to the string class, which acts as a regular STL container - in this respect. -
        • -
        • The default Allocator of every container-of-T is - std::allocator<T>. -
        • -
        • The interface of the allocator<T> class is - extremely simple. It has about 20 public declarations (nested - typedefs, member functions, etc), but the two which concern us most - are: -
          -      T*    allocate   (size_type n, const void* hint = 0);
          -      void  deallocate (T* p, size_type n);
          - (This is a simplicifcation; the real signatures use nested typedefs.) - The "n" arguments in both those functions is a - count of the number of T's to allocate space for, - not their total size. -
        • -
        • "The storage is obtained by calling - ::operator new(size_t), but it is unspecified when or - how often this function is called. The use of hint - is unspecified, but intended as an aid to locality if an - implementation so desires." [20.4.1.1]/6 -
        • -
        -

        Problems and Possibilities

        -

        The easiest way of fulfilling the requirements is to call operator new - each time a container needs memory, and to call operator delete each - time the container releases memory. BUT - this - method is horribly slow. -

        -

        Or we can keep old memory around, and reuse it in a pool to save time. - The old libstdc++-v2 used a memory pool, and so do we. As of 3.0, - it's - on by default. The pool is shared among all the containers in the - program: when your program's std::vector<int> gets cut in half - and frees a bunch of its storage, that memory can be reused by the - private std::list<WonkyWidget> brought in from a KDE library - that you linked against. And we don't have to call operators new and - delete to pass the memory on, either, which is a speed bonus. - BUT... -

        -

        What about threads? No problem: in a threadsafe environment, the - memory pool is manipulated atomically, so you can grow a container in - one thread and shrink it in another, etc. BUT what - if threads in libstdc++-v3 aren't set up properly? - That's been answered already. -

        -

        BUT what if you want to use your own allocator? What - if you plan on using a runtime-loadable version of malloc() which uses - shared telepathic anonymous mmap'd sections serializable over a - network, so that memory requests should go through malloc? - And what if you need to debug it? -

        -

        Well then: -

        -

        Available allocators in namespace std

        -

        First I'll describe the situation as it exists for the code which - was released in GCC 3.1 and 3.2. Then I'll describe the differences - for 3.0. The allocator classes also have source documentation, - which is described here (you - will need to retrieve the maintainer-level docs, as almost none of - these entities are in the ISO standard). -

        -

        As a general rule of thumb, users are not allowed to use names which - begin with an underscore. This means that to be portable between - compilers, none of the following may be used in your program directly. - (If you decide to be unportable, then you're free do do what you want, - but it's not our fault if stuff breaks.) They are presented here for - information for maintainers and contributors in addition to users. -

        -

        These classes are always available: -

        -
          -
        • __new_alloc simply wraps ::operator new - and ::operator delete. -
        • -
        • __malloc_alloc_template<int inst> simply wraps - malloc and free. There is also a hook - for an out-of-memory handler (for new/delete this is taken care of - elsewhere). The inst parameter is described below. - This class was called malloc_alloc in earlier versions. -
        • -
        • allocator<T> has already been described; it is - The Standard Allocator for instances of T. It uses the internal - __alloc typedef (see below) to satisy its requests. -
        • -
        • __simple_alloc<T,A> is a wrapper around another - allocator, A, which itself is an allocator for instances of T. - This is primarily used in an internal "allocator traits" - class which helps encapsulate the different styles of allocators. -
        • -
        • __debug_alloc<A> is also a wrapper around an - arbitrary allocator A. It passes on slightly increased size - requests to A, and uses the extra memory to store size information. - When a pointer is passed to deallocate(), the stored - size is checked, and assert() is used to guarantee they match. -
        • -
        • __allocator<T,A> is an adaptor. Many of these - allocator classes have a consistent yet non-standard interface. - Such classes can be changed to a conforming interface with this - wrapper: __allocator<T, __alloc> is thus the - same as allocator<T>. -
        • -
        -

        Normally, - __default_alloc_template<bool thr, int inst> - is also available. This is the high-speed pool, called the default - node allocator. The reusable memory is shared among identical - instantiations of - this type. It calls through __new_alloc to obtain - new memory when its lists run out. If a client container requests a - block larger than a certain threshold size, then the pool is bypassed, - and the allocate/deallocate request is passed to - __new_alloc directly. -

        -

        Its inst parameter is described below. The - thr boolean determines whether the pool should be - manipulated atomically or not. Two typedefs are provided: - __alloc is defined as this node allocator with thr=true, - and therefore is threadsafe, while __single_client_alloc - defines thr=false, and is slightly faster but unsafe for multiple - threads. -

        -

        (Note that the GCC thread abstraction layer allows us to provide safe - zero-overhead stubs for the threading routines, if threads were - disabled at configuration time. In this situation, - __alloc should not be noticably slower than - __single_client_alloc.) -

        -

        [Another threadsafe allocator where each thread keeps its own free - list, so that no locking is needed, might be described here.] -

        -

        A cannon to swat a fly: __USE_MALLOC

        -

        If you've already read this - advice but still think you remember how to use this macro from - SGI STL days. We have removed it in gcc 3.3. See next section - for the new way to get the same effect. -

        -

        Globally disabling memory caching: GLIBCXX_FORCE_NEW

        -

        Starting with gcc 3.3, if you want to globally disable memory - caching within the library for the default allocator (i.e. - the one you get for all library objects when you do not specify - which one to use), merely set GLIBCXX_FORCE_NEW (at this time, - with any value) into your environment before running the - program. You will obtain a similar effect without having to - recompile your entire program and the entire library (the new - operator in gcc is a light wrapper around malloc). If your - program crashes with GLIBCXX_FORCE_NEW in the environment, - it likely means that you linked against objects built against - the older library. Code to support this extension is fully - compatible with 3.2 code if GLIBCXX_FORCE_NEW is not in the - environment. Prior to GCC 3.4, this variable was spelt - GLIBCPP_FORCE_NEW. -

        -

        Writing your own allocators

        -

        Depending on your application (a specific program, a generic library, - etc), allocator classes tend to be one of two styles: "SGI" - or "standard". See the comments in stl_alloc.h for more - information on this crucial difference. -

        -

        At the bottom of that header is a helper type, - _Alloc_traits, and various specializations of it. This - allows the container classes to make possible compile-time - optimizations based on features of the allocator. You should provide - a specialization of this type for your allocator (doing so takes only - two or three statements). -

        -

        Using non-default allocators

        -

        You can specify different memory management schemes on a per-container - basis, by overriding the default Allocator template - parameter. For example, an easy - (but nonportable) - method of specifying that only malloc/free should be used instead of - the default node allocator is: -

        -
        -    std::list <my_type, std::__malloc_alloc_template<0> >  my_malloc_based_list;
        - Likewise, a debugging form of whichever allocator is currently in use: -
        -    std::deque <my_type, std::__debug_alloc<std::__alloc> >  debug_deque;
        -

        inst

        -

        The __malloc_alloc_template and - __default_alloc_template classes take an integer parameter, - called inst here. This number is completely unused. -

        -

        The point of the number is to allow multiple instantiations of the - classes without changing the semantics at all. All three of -

        -
        -    typedef  __default_alloc_template<true,0>    normal;
        -    typedef  __default_alloc_template<true,1>    private;
        -    typedef  __default_alloc_template<true,42>   also_private;
        -

        behave exactly the same way. However, the memory pool for each type - (and remember that different instantiations result in different types) - remains separate. -

        -

        The library uses 0 in all its instantiations. If you - wish to keep separate free lists for a particular purpose, use a - different number. -

        -

        3.0.x

        -

        For 3.0.x, many of the names were incorrectly not prefixed - with underscores. So symbols such as "std::single_client_alloc" - are present. Be very careful to not depend on these names any more - than you would depend on implementation-only names. -

        -

        Certain macros like _NOTHREADS and __STL_THREADS - can affect the 3.0.x allocators. Do not use them. Those macros have - been completely removed for 3.1. -

        -

        Return to top of page or - to the FAQ. -

        - -
        -

        Allocators (version 3.4)

        -

        Changes are coming... -

        -

        If you plan on writing your own allocators, - source documentation is - available. You'll need to get the "maintainers" collection - in order to see the helper classes and extra notes. -

        -

        Return to top of page or - to the FAQ. -

        -

        Compile-time checks

        Currently libstdc++-v3 uses the concept checkers from the Boost diff --git a/libstdc++-v3/docs/html/ext/mt_allocator.html b/libstdc++-v3/docs/html/ext/mt_allocator.html new file mode 100644 index 00000000000..93a5bfb8ee0 --- /dev/null +++ b/libstdc++-v3/docs/html/ext/mt_allocator.html @@ -0,0 +1,414 @@ + + + + + + + + + + A fixed-size, multi-thread optimized allocator + + + + + +

        A fixed-size, multi-thread optimized allocator

        + +

        + The latest version of this document is always available at + + http://gcc.gnu.org/onlinedocs/libstdc++/ext/mt_allocator.html. +

        + +

        + To the libstdc++-v3 homepage. +

        + + +
        +

        + Introduction +

        + +

        The mt allocator [hereinafter referred to simply as "the +allocator"] is a fixed size (power of two) allocator that was +initially developed specifically to suit the needs of multi threaded +applications [hereinafter referred to as an MT application]. Over time +the allocator has evolved and been improved in many ways, one of the +being that it now also does a good job in single threaded applications +[hereinafter referred to as a ST application]. (Note: In this +document, when referring to single threaded applications this also +includes applications that are compiled with gcc without thread +support enabled. This is accomplished using ifdef's on __GTHREADS) +

        + +

        +The aim of this document is to describe - from a application point of +view - the "inner workings" of the allocator. +

        + + +

        + Initialization +

        + +

        +The static variables (pointers to freelists, tuning parameters etc) +are initialized to their default values at file scope, i.e.: +

        + +
        +  template size_t
        +  __mt_alloc<_Tp>::_S_freelist_headroom = 10;
        +
        + +

        +The very first allocate() call will always call the _S_init() function. +In order to make sure that this function is called exactly once we make use +of a __gthread_once (with _S_once_mt and _S_init as arguments) call in MT +applications and check a static bool (_S_initialized) in ST applications. +

        + +

        +The _S_init() function: +- If the GLIBCXX_FORCE_NEW environment variable is set, it sets the bool + _S_force_new to true and then returns. This will cause subsequent calls to + allocate() to return memory directly from a new() call, and deallocate will + only do a delete() call. +

        + +

        +- If the GLIBCXX_FORCE_NEW environment variable is not set, both ST and MT + applications will: + - Calculate the number of bins needed. A bin is a specific power of two size + of bytes. I.e., by default the allocator will deal with requests of up to + 128 bytes (or whatever the value of _S_max_bytes is when _S_init() is + called). This means that there will be bins of the following sizes + (in bytes): 1, 2, 4, 8, 16, 32, 64, 128. + + - Create the _S_binmap array. All requests are rounded up to the next + "large enough" bin. I.e., a request for 29 bytes will cause a block from + the "32 byte bin" to be returned to the application. The purpose of + _S_binmap is to speed up the process of finding out which bin to use. + I.e., the value of _S_binmap[ 29 ] is initialized to 5 (bin 5 = 32 bytes). +

        +

        + - Create the _S_bin array. This array consists of bin_records. There will be + as many bin_records in this array as the number of bins that we calculated + earlier. I.e., if _S_max_bytes = 128 there will be 8 entries. + Each bin_record is then initialized: + - bin_record->first = An array of pointers to block_records. There will be + as many block_records pointers as there are maximum number of threads + (in a ST application there is only 1 thread, in a MT application there + are _S_max_threads). + This holds the pointer to the first free block for each thread in this + bin. I.e., if we would like to know where the first free block of size 32 + for thread number 3 is we would look this up by: _S_bin[ 5 ].first[ 3 ] + - bin_record->last = See above, the only difference being that this points + to the last record on the same freelist. + + The above created block_record pointers members are now initialized to + their initial values. I.e. _S_bin[ n ].first[ n ] = NULL; +

        + +

        +- Additionally a MT application will: + - Create a list of free thread id's. The pointer to the first entry + is stored in _S_thread_freelist_first. The reason for this approach is + that the __gthread_self() call will not return a value that corresponds to + the maximum number of threads allowed but rather a process id number or + something else. So what we do is that we create a list of thread_records. + This list is _S_max_threads long and each entry holds a size_t thread_id + which is initialized to 1, 2, 3, 4, 5 and so on up to _S_max_threads. + Each time a thread calls allocate() or deallocate() we call + _S_get_thread_id() which looks at the value of _S_thread_key which is a + thread local storage pointer. If this is NULL we know that this is a newly + created thread and we pop the first entry from this list and saves the + pointer to this record in the _S_thread_key variable. The next time + we will get the pointer to the thread_record back and we use the + thread_record->thread_id as identification. I.e., the first thread that + calls allocate will get the first record in this list and thus be thread + number 1 and will then find the pointer to its first free 32 byte block + in _S_bin[ 5 ].first[ 1 ] + When we create the _S_thread_key we also define a destructor + (_S_thread_key_destr) which means that when the thread dies, this + thread_record is returned to the front of this list and the thread id + can then be reused if a new thread is created. + This list is protected by a mutex (_S_thread_freelist_mutex) which is only + locked when records are removed/added to the list. +

        +

        + - Initialize the free and used counters of each bin_record: + - bin_record->free = An array of size_t. This keeps track of the number + of blocks on a specific thread's freelist in each bin. I.e., if a thread + has 12 32-byte blocks on it's freelists and allocates one of these, this + counter would be decreased to 11. + + - bin_record->used = An array of size_t. This keeps track of the number + of blocks currently in use of this size by this thread. I.e., if a thread + has made 678 requests (and no deallocations...) of 32-byte blocks this + counter will read 678. + + The above created arrays are now initialized with their initial values. + I.e. _S_bin[ n ].free[ n ] = 0; +

        +

        + - Initialize the mutex of each bin_record: + The bin_record->mutex is used to protect the global freelist. This concept + of a global freelist is explained in more detail in the section + "A multi threaded example", but basically this mutex is locked whenever + a block of memory is retrieved or returned to the global freelist for this + specific bin. This only occurs when a number of blocks are grabbed from the + global list to a thread specific list or when a thread decides to return + some blocks to the global freelist. +

        + +

        + A single threaded example (and a primer for the multi threaded example!) +

        + +

        +Let's start by describing how the data on a freelist is laid out in memory. +This is the first two blocks in freelist for thread id 3 in bin 3 (8 bytes): +

        +
        ++----------------+
        +| next* ---------|--+  (_S_bin[ 3 ].first[ 3 ] points here)
        +|                |  |
        +|                |  |
        +|                |  |
        ++----------------+  |
        +| thread_id = 3  |  |
        +|                |  |
        +|                |  |
        +|                |  |
        ++----------------+  |
        +| DATA           |  |  (A pointer to here is what is returned to the
        +|                |  |   the application when needed)
        +|                |  |
        +|                |  |
        +|                |  |
        +|                |  |
        +|                |  |
        +|                |  |
        ++----------------+  |
        ++----------------+  |
        +| next*          |<-+  (If next == NULL it's the last one on the list and
        +|                |      then the _S_bin[ 3 ].last[ 3 ] pointer points to
        +|                |      here as well)
        +|                |
        ++----------------+
        +| thread_id = 3  |
        +|                |
        +|                |
        +|                |
        ++----------------+
        +| DATA           |
        +|                |
        +|                |
        +|                |
        +|                |
        +|                |
        +|                |
        +|                |
        ++----------------+
        +
        + +

        +With this in mind we simplify things a bit for a while and say that there is +only one thread (a ST application). In this case all operations are made to +what is referred to as the global pool - thread id 0 (No thread may be +assigned this id since they span from 1 to _S_max_threads in a MT application). +

        +

        +When the application requests memory (calling allocate()) we first look at the +requested size and if this is > _S_max_bytes we call new() directly and return. +

        +

        +If the requested size is within limits we start by finding out from which +bin we should serve this request by looking in _S_binmap. +

        +

        +A quick look at _S_bin[ bin ].first[ 0 ] tells us if there are any blocks of +this size on the freelist (0). If this is not NULL - fine, just remove the +block that _S_bin[ bin ].first[ 0 ] points to from the list, +update _S_bin[ bin ].first[ 0 ] and return a pointer to that blocks data. +

        +

        +If the freelist is empty (the pointer is NULL) we must get memory from the +system and build us a freelist within this memory. All requests for new memory +is made in chunks of _S_chunk_size. Knowing the size of a block_record and +the bytes that this bin stores we then calculate how many blocks we can create +within this chunk, build the list, remove the first block, update the pointers +(_S_bin[ bin ].first[ 0 ] and _S_bin[ bin ].last[ 0 ]) and return a pointer +to that blocks data. +

        + +

        +Deallocation is equally simple; the pointer is casted back to a block_record +pointer, lookup which bin to use based on the size, add the block to the end +of the global freelist (with the next pointer set to NULL) and update the +pointers as needed (_S_bin[ bin ].first[ 0 ] and _S_bin[ bin ].last[ 0 ]). +

        + +

        + A multi threaded example +

        + +

        +In the ST example we never used the thread_id variable present in each block. +Let's start by explaining the purpose of this in a MT application. +

        + +

        +The concept of "ownership" was introduced since many MT applications +allocate and deallocate memory to shared containers from different +threads (such as a cache shared amongst all threads). This introduces +a problem if the allocator only returns memory to the current threads +freelist (I.e., there might be one thread doing all the allocation and +thus obtaining ever more memory from the system and another thread +that is getting a longer and longer freelist - this will in the end +consume all available memory). +

        + +

        +Each time a block is moved from the global list (where ownership is +irrelevant), to a threads freelist (or when a new freelist is built +from a chunk directly onto a threads freelist or when a deallocation +occurs on a block which was not allocated by the same thread id as the +one doing the deallocation) the thread id is set to the current one. +

        + +

        +What's the use? Well, when a deallocation occurs we can now look at +the thread id and find out if it was allocated by another thread id +and decrease the used counter of that thread instead, thus keeping the +free and used counters correct. And keeping the free and used counters +corrects is very important since the relationship between these two +variables decides if memory should be returned to the global pool or +not when a deallocation occurs. +

        + +

        +When the application requests memory (calling allocate()) we first +look at the requested size and if this is > _S_max_bytes we call new() +directly and return. +

        + +

        +If the requested size is within limits we start by finding out from which +bin we should serve this request by looking in _S_binmap. +

        + +

        +A call to _S_get_thread_id() returns the thread id for the calling thread +(and if no value has been set in _S_thread_key, a new id is assigned and +returned). +

        + +

        +A quick look at _S_bin[ bin ].first[ thread_id ] tells us if there are +any blocks of this size on the current threads freelist. If this is +not NULL - fine, just remove the block that _S_bin[ bin ].first[ +thread_id ] points to from the list, update _S_bin[ bin ].first[ +thread_id ], update the free and used counters and return a pointer to +that blocks data. +

        + +

        +If the freelist is empty (the pointer is NULL) we start by looking at +the global freelist (0). If there are blocks available on the global +freelist we lock this bins mutex and move up to block_count (the +number of blocks of this bins size that will fit into a _S_chunk_size) +or until end of list - whatever comes first - to the current threads +freelist and at the same time change the thread_id ownership and +update the counters and pointers. When the bins mutex has been +unlocked, we remove the block that _S_bin[ bin ].first[ thread_id ] +points to from the list, update _S_bin[ bin ].first[ thread_id ], +update the free and used counters, and return a pointer to that blocks +data. +

        + +

        +The reason that the number of blocks moved to the current threads +freelist is limited to block_count is to minimize the chance that a +subsequent deallocate() call will return the excess blocks to the +global freelist (based on the _S_freelist_headroom calculation, see +below). +

        + +

        +However if there isn't any memory on the global pool we need to get +memory from the system - this is done in exactly the same way as in a +single threaded application with one major difference; the list built +in the newly allocated memory (of _S_chunk_size size) is added to the +current threads freelist instead of to the global. +

        + +

        +The basic process of a deallocation call is simple: always add the +block to the end of the current threads freelist and update the +counters and pointers (as described earlier with the specific check of +ownership that causes the used counter of the thread that originally +allocated the block to be decreased instead of the current threads +counter). +

        + +

        +And here comes the free and used counters to service. Each time a +deallocation() call is made, the length of the current threads +freelist is compared to the amount memory in use by this thread. +

        + +

        +Let's go back to the example of an application that has one thread +that does all the allocations and one that deallocates. Both these +threads use say 516 32-byte blocks that was allocated during thread +creation for example. Their used counters will both say 516 at this +point. The allocation thread now grabs 1000 32-byte blocks and puts +them in a shared container. The used counter for this thread is now +1516. +

        + +

        +The deallocation thread now deallocates 500 of these blocks. For each +deallocation made the used counter of the allocating thread is +decreased and the freelist of the deallocation thread gets longer and +longer. But the calculation made in deallocate() will limit the length +of the freelist in the deallocation thread to _S_freelist_headroom % +of it's used counter. In this case, when the freelist (given that the +_S_freelist_headroom is at it's default value of 10%) exceeds 52 +(516/10) blocks will be returned to the global pool where the +allocating thread may pick them up and reuse them. +

        + +

        +In order to reduce lock contention (since this requires this bins +mutex to be locked) this operation is also made in chunks of blocks +(just like when chunks of blocks are moved from the global freelist to +a threads freelist mentioned above). The "formula" used can probably +be improved to further reduce the risk of blocks being "bounced back +and forth" between freelists. +

        + +
        +

        Return to the top of the page or + to the libstdc++ homepage. +

        + + + + +
        +

        +See license.html for copying conditions. +Comments and suggestions are welcome, and may be sent to +the libstdc++ mailing list. +

        + + + +