README.md
author David Anderson <dvander@alliedmods.net>
Fri Aug 23 22:36:08 2013 -0700 (2013-08-23)
changeset 1 d5286b4d7c2d
parent 0 ba3d4abaf3f1
child 7 1e8a8b4d209c
permissions -rw-r--r--
Import HashTable.
     1 The AlliedModders Template Library is a small set of C++ headers. It aims to be lightweight,
     2 supporting a minimalistic set of abstractions and data structures useful for high-performance C++
     3 systems code. It is the spiritual succesor to the SourceHook template library.
     4 
     5 # Goals
     6 
     7 * Performance Over Generality. Rather than accomodate every use case, AMTL strives to make
     8   performance explicit, and does not include features which are dangerous for performance, or
     9   features that make malloc() failure detection more difficult.
    10 
    11   For example, while STL provides copy constructors for most containers, AMTL does not, as these
    12   operations are unsafe for fallible malloc(), and can cause large unintentional performance hits.
    13 
    14   Another example is boost::shared\_ptr, which by default is atomic and threadsafe, and
    15   preprocessor macros can be used to eliminate the thread safety option. AMTL opts to use distinct
    16   types for threadsafe and non-threadsafe reference counting, as the cost of atomic refcounting
    17   is higher.
    18 
    19 * Safety. Most code in AlliedModders does not take advantage of RAII, and does not account for
    20   fallible malloc. While in general, infallible malloc is probably okay for game servers, the lack
    21   of RAII leads to very complicated and bloated pointer management. A major goal of AMTL is to
    22   introduce safer pointer management into its codebases, and to allow certain components to safely
    23   handle malloc failures when desired.
    24 
    25 * No linkage. EVerything is contained within headers so that tools don't need to link additional
    26   C++ files. This could be a drawback, for example, it could increase build time or codesize
    27   bloat. We may evaluate this decision in the future, but for now, it fits into our "lightweight"
    28   theme of usability.
    29 
    30   (On some platforms, system libraries may be needed for certain headers.)
    31 
    32 * No RTTI or EH. There are a few reasons for this:
    33   + Both features require linkage to libstdc++. On Linux, this can cause problems with binary
    34     distribution.
    35 
    36   + When using 32-bit Visual C++, try blocks are not zero-cost, as they are implemented using SEH.
    37 
    38 * Fallible malloc. In many cases it's not desirable to terminate the program or throw an
    39   exception when an allocation fails (for example, resizing a large vector). Thus, functions
    40   like ke::Vector::append (analagous to std::vector::push\_back) will usually return bool. Whether
    41   or not these functions return (i.e., abort the program or fail safely) can be specified in an
    42   allocation policy.
    43 
    44 * Room to grow. These templates are intended to be a baseline of features needed for various
    45   AlliedModders projects. They can and will grow as new features are needed, hopefully keeping the
    46   above principles in mind.
    47 
    48 # Overview
    49 
    50 All classes and functions reside in a "ke" namespace, and all macros are prefixed with "KE\_".
    51 
    52 ### Platform Utilities (am-utilities.h)
    53 
    54 This provides some common helper functions pretty much lumped together:
    55 * AutoPtr<T>, RAII for calling delete on a pointer.
    56 * AutoArray<T>, RAII for calling delete[] on a pointer.
    57 * Detectors for add or multiply overflow, useful for computing allocation sizes.
    58 * Fast Log2/Floor2 functions (also known as bitscan or count zeroes).
    59 * Integer alignment (useful for allocators or code generators).
    60 * KE\_DELETE and KE\_OVERRIDE which map to delete and override keywords if C++11 is available.
    61 
    62 ### Thread Utilities (am-thread-utils.h)
    63 
    64 Thread utils provides access to common sychronization primitives: mutexes, condition variables,
    65 and threads. It also provides RAII objects for acquiring and releasing locks. More information is
    66 available at the top of am-thread-utils.h.
    67 
    68 ### Reference Counting (am-refcounting.h)
    69 
    70 Two classes are available to help other classes implement reference counting. They are Refcounted
    71 and RefcountedThreadsafe. Both must be inherited recursively, for example:
    72 
    73     class MyObject : public Refcounted<MyObject>
    74   
    75 The object is instantiated with a reference count of 1. A few RAII objects are provided for
    76 automatically retaining and releasing references:
    77 
    78 * Ref<T>. Adds one reference upon construction and releases one reference on destruction.
    79 * PassRef<T>. Same as Ref<T>, except, when passed into a Ref<T> or into a PassRef<T>, the contained
    80   reference is NULL'd, rather than performing an unnecessary extra AddRef()+Release(). PassRef<T>
    81   is intended only for return values.
    82 * Newborn<T>. If a newly allocated Refcounted object is stored into a Ref<T>, it will have one
    83   extra reference. When allocating new refcounted objects, they should be immediately wrapped in a
    84   Newborn<T> to prevent this.
    85 
    86 RefcountedThreadsafe provides the same accessors as Refcounted. However, the reference counting is
    87 performed using atomic operations, so that two threads racing do not accidentally result in a wrong
    88 reference count (since a read + write is not normally atomic). This does not make the actual object
    89 threadsafe, rather, it makes it safe for multiple threads to mutate the reference count.
    90 
    91 ### Allocator Policies (am-allocator-policies.h)
    92 
    93 ke::HashTable and ke::Vector are both parameterized on an "AllocPolicy". It is accessed whenever a
    94 malloc(), free(), size overflow, or out-of-memory operation occurs. The default functionality of
    95 SystemAllocatorPolicy is to abort the process on any failure, which can be changed by implementing
    96 a new policy. As HashTable and Vector inherit from the AllocPolicy, it is possible to use custom
    97 allocators without accessing static variables.
    98 
    99 ### Vector (am-vector.h)
   100 
   101 ke::Vector is roughly analagous to std::vector. A few method names are changed:
   102 * size() is length()
   103 * push\_back() is append() and returns boolean.
   104 * The underlying buffer of a vector can be "taken", which is sometimes useful for transferring
   105   ownership of data.
   106 
   107 Vector does not have an iterator type. It should be iterated through normal sequential indexing.
   108 
   109 ### HashTable (am-hashtable.h)
   110 
   111 ke::HashTable is an open-addressing, linear-probing hash table. It is a bit more powerful than a
   112 generic key -> value map as it is templated on a "policy", rather than keys and values. A policy
   113 specifies what entries in the table look like, and how they are searched. Policies must have three
   114 members:
   115 
   116 * Payload - the type that will be stored for each entry in the hash table.
   117 * static bool matches(const T &t, Payload &p); - Must return true if |t| is equal to the entry at |p|.
   118 * static uin32\_t hash(const T &t); - Must compute a hash for |t|.
   119 
   120 The typename T in the policy applies for every key type that will be used in the hash table. For
   121 example, a hash table might store string keys, but have faster lookup using std::string versus a
   122 C-string, or even faster lookup for a string type that has its hash precomputed.
   123 
   124 All key types must effectively compute the same hash; i.e., it would not be legal for Payload to
   125 match both |int| and |std::string| but compute different hashes.
   126 
   127 HashTable returns either a Result or Insert object when performing lookups, depending on the type
   128 of lookup. A Result object is either found or not found. An Insert object, however, caches
   129 information about where it found the object, so that it doesn't have to repeat the hashing and
   130 lookup process for the subsequent add() call. Examples on how to use this are in am-hashmap.h, and
   131 the exact rules on how Result and Insert objects can be used are in am-hashtable.h.
   132 
   133 HashTables can be iterated. If the table is mutated during iteration outside of the interface
   134 provided by the iterator, then the iterator may become corrupted. This is acceptable as long as the
   135 iterator is abandoned. However, the table must stay alive for as long as the iterator is active.
   136 
   137 ### HashMap (am-hashmap.h)
   138 
   139 ke::HashMap is a key -> value map built upon HashTable. This saves you from having to implement our
   140 own Policy and provides some more generic accessors. However it is still necessary to implement a
   141 hash function.
   142 
   143 ### InlineList (am-inlinelist.h)
   144 
   145 InlineList is a doubly-linked list that does use any calls to malloc(), with the caveat that list
   146 members can be members in at most one inline list at a time. Objects must recursively inherit from
   147 InlineListNode, which ensures that the object has prev/next pointers. Example:
   148 
   149     class MyObject : public InlineListNode<MyObject>
   150     { };
   151     InlineList<MyObject> list;
   152 
   153 InlineList is ideal for situations where objects have already been allocated and will be a member
   154 of at most one list at any time. If this is the case, the per-node malloc/free of std::list can be
   155 completely avoided.
   156 
   157 An InlineList never claims ownership over its pointers. Its destructor is a no-op and it makes no
   158 attempt to destroy members of its list (though in debug builds it will unlink members in its
   159 destructor for assertion purposes).
   160 
   161 ### LinkedList (am-linkedlist.h)
   162 
   163 LinkedList is a doubly-linked list that requires a malloc() for each entry. It is provided for
   164 backward compatibility, and it should be avoided in general, except in situations where it is
   165 absolutely necessary and allocation performance is not a factor. Otherwise, use InlineList or
   166 Vector.
   167 
   168 ### Strings (am-string.h)
   169 
   170 The String API is still rather in flux. Currently, a simple std::string-like class is provided
   171 called AString (short for ASCII String).
   172 
   173 # Replacing SourceHook Includes
   174 
   175 AMTL is a spiritual successor to the SourceHook template library, used in many AlliedModders
   176 projects. AMTL is intended to deprecate it.
   177 
   178 ### CVector (sh\_vector.h)
   179 
   180 CVector can be replaced with ke::Vector. ke::Vector does not provide an iterator (yet). A few
   181 method names have been changed, as CVector more closely models std::vector, however the classes are
   182 pretty similar. Any missing methods could be added quite easily.
   183 
   184 ### CString (sh\_string.h)
   185 
   186 CString can be replaced with ke::AString, however some methods like trim() are not available yet.
   187 One notable difference is that AString caches the length of the string, whereas CString does not.
   188 That means AString is a larger data structure (by 4 bytes, on 32-bit), but is not prone to O(n^2)
   189 operations like CString. This more closely matches the functionality of std::string.
   190 
   191 ### List (sh\_list.h)
   192 
   193 List can be replaced with ke::LinkedList. A few methods have changed, similar to Vector. For
   194 example, push\_back() becomes append() and push\_front() becomes prepend(), and both return
   195 boolean.
   196 
   197 Consider using ke::InlineList if your list contains already-allocated objects, and those objects
   198 are members of at most one list, LinkedList will call malloc() and free() for each node.
   199 
   200 The implementation of SourceHook::List and ke::LinkedList are basically identical underneath the
   201 hood, however, copy construction and assignment are disallowed in the AMTL variant. This is because
   202 they violate the memory model and also have very poor performance impliciations.
   203 
   204 ### THash<K, V> (sh\_tinyhash.h)
   205 
   206 TinyHash can be replaced with ke::HashMap, with minor changes. Comparators must be implemented as
   207 equality operators on the key type, and the hash function is specified as a template parameter.
   208 
   209 TinyHash is disadvantageous for a number of reasons. It uses separate chaining, and chains are
   210 built on top of SourceHook::List, so it will use more memory on average. Inserts are also more
   211 expensive as a call to malloc() is almost guaranteed. Retrieval is more expensive as well, since it
   212 requires more indirection and has worse cache locality. It also has no ability to resize table
   213 downward when its density is very low.
   214 
   215 ### CStack (sh\_stack.h)
   216 
   217 SourceHook::CStack is a stable-pointer stack. That is, if an element has been pushed, the address
   218 the element is stored at is guaranteed not to change unless the element is removed. This can be an
   219 important requirement in cases that involve re-entrancy. If you are relying on this feature of
   220 CStack, there is no replacement in AMTL. If your CStack entries are LIFO, and tied to local
   221 variables, you might consider simply linking objects together using RAII and avoiding CStack
   222 entirely (though CStack will have better cache performance if you need to walk the list of entries).
   223 
   224 If you don't need stable pointers, CStack can be replaced with ke::Vector.
   225 
   226 # License
   227 
   228 All headers are licensed under a 3-clause BSD license.