| 1 |
=== Stones of Nvwa === |
| 2 |
|
| 3 |
The Name |
| 4 |
|
| 5 |
Nvwa ("v" is pronounced like the French "u"), is one of the most |
| 6 |
ancient Chinese goddesses. She was said to have created human beings, |
| 7 |
and, when the evil god Gong-gong crashed himself upon one of the |
| 8 |
pillars that support the sky and made a hole in it, she mended the sky |
| 9 |
with five-coloured stones. |
| 10 |
|
| 11 |
I thought of the name Nvwa by analogy with Loki. Since it is so small |
| 12 |
a project and it contains utilities instead of a complete framework, I |
| 13 |
think "stones" a good metaphor. |
| 14 |
|
| 15 |
|
| 16 |
Code Organization |
| 17 |
|
| 18 |
Nvwa versions prior to 1.0 did not use a namespace. It looked to me |
| 19 |
overkill to use a namespace in this small project. However, having |
| 20 |
had more project experience and seen more good examples, I have |
| 21 |
changed my mind. All nvwa functions and global variables are now |
| 22 |
inside the namespace "nvwa". |
| 23 |
|
| 24 |
This said, I do not want to break backward compatibility abruptly. |
| 25 |
First, people are still able to disable the namespace by defining the |
| 26 |
macro NVWA_USE_NAMESPACE to 0 (not really recommended, though). |
| 27 |
Second, I still use the quotation mark (") to include header files |
| 28 |
internally so that old users do not have to change the include |
| 29 |
directory (again, not recommended and intended to help the transition |
| 30 |
only). Third, I have not changed most of the macro names -- that |
| 31 |
would break existing code and could do more harm than good now. In |
| 32 |
addition, I am not a fan of macro prefixes anyway. |
| 33 |
|
| 34 |
Overall, I am quite happy with the changes: some code is simplified, |
| 35 |
and in many cases I do not have to use the uglified names to avoid |
| 36 |
potential name conflicts. The changes also do not mean anything to |
| 37 |
new users. You just need to follow the modern C++ way. You should |
| 38 |
add the root directory of this project to your include directory |
| 39 |
(there should be a "nvwa" directory inside it with the source files). |
| 40 |
To include the header file, use "#include <nvwa/...>". |
| 41 |
|
| 42 |
|
| 43 |
Contents |
| 44 |
|
| 45 |
A brief introduction follows. Check the Doxygen documentation for |
| 46 |
more (technical) details. |
| 47 |
|
| 48 |
* boolarray.cpp |
| 49 |
* boolarray.h |
| 50 |
|
| 51 |
A replacement of std::vector<bool>. I wrote it before I knew of |
| 52 |
vector<bool>, or I might not have written it at all. However, it is |
| 53 |
faster than many vector<bool> implementations (your mileage may |
| 54 |
vary), and it has members like at, set, reset, flip, and count. I |
| 55 |
personally find "count" very useful. |
| 56 |
|
| 57 |
* c++11.h |
| 58 |
|
| 59 |
Detection macros for certain features of C++11 that might be of |
| 60 |
interest to code/library writers. I have used existing online |
| 61 |
resources, and I have tested them on popular platforms and compilers |
| 62 |
that I have access to (Windows, OS X, and Linux; MSVC, Clang, and |
| 63 |
GCC), but of course not all versions or all combinations. Patches |
| 64 |
are welcome for corrections and amendments. |
| 65 |
|
| 66 |
* class_level_lock.h |
| 67 |
|
| 68 |
The Loki ClassLevelLockable adapted to use the fast_mutex layer. One |
| 69 |
minor divergence from Loki is that the template has an additional |
| 70 |
template parameter _RealLock to boost the performance in non-locking |
| 71 |
scenarios. Unless HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION is |
| 72 |
explicitly defined to 0, this is enabled automatically. Cf. |
| 73 |
object_level_lock.h. |
| 74 |
|
| 75 |
* cont_ptr_utils.h |
| 76 |
|
| 77 |
Utility functors for containers of pointers adapted from Scott Meyers' |
| 78 |
Effective STL. |
| 79 |
|
| 80 |
* debug_new.cpp |
| 81 |
* debug_new.h |
| 82 |
|
| 83 |
A cross-platform, thread-safe memory leak detector. It is a |
| 84 |
light-weight one designed only to catch unmatched pairs of |
| 85 |
new/delete. I know there are already many existing memory leak |
| 86 |
detectors, but as far as I know, free solutions are generally slow, |
| 87 |
memory-consuming, and quite complicated to use. This solution is |
| 88 |
very easy to use, and has very low space/time overheads. Just link |
| 89 |
in debug_new.cpp for leakage report, and include debug_new.h for |
| 90 |
additional file/line information. It will automatically switch on |
| 91 |
multi-threading when the appropriate option of a recognized compiler |
| 92 |
is specified. Check fast_mutex.h for more threading details. |
| 93 |
|
| 94 |
Special support for gcc/binutils has been added to debug_new lately. |
| 95 |
Even if the header file debug_new.h is not included, or |
| 96 |
_DEBUG_NEW_REDEFINE_NEW is defined to 0 when it is included, |
| 97 |
file/line information can be displayed if debugging symbols are |
| 98 |
present in the executable, since debug_new stores the caller |
| 99 |
addresses of memory allocation/deallocation routines and they will be |
| 100 |
converted with addr2line (or atos on a Mac) on the fly. This makes |
| 101 |
memory tracing much easier. |
| 102 |
|
| 103 |
NOTE for Mac OS X users: If you use OS X 10.7 or later, you may need |
| 104 |
to add "-Wl,-no_pie" at the end of the command line. By default OS X |
| 105 |
will now create position-independent executables (PIE), and the OS |
| 106 |
will load a PIE at a random address each time it is executed, making |
| 107 |
it difficult to convert the address to symbols. (I Googled a while |
| 108 |
for a way to get the base address of a process, but could not find |
| 109 |
working code. If you know how to do it right, drop me a message.) |
| 110 |
|
| 111 |
With an idea from Greg Herlihy's post in comp.lang.c++.moderated, the |
| 112 |
implementation was much improved in 2007. The most significant |
| 113 |
result is that placement new can be used with debug_new now! Full |
| 114 |
support for new(std::nothrow) is provided, with its null-returning |
| 115 |
error semantics (by default). Memory corruption will be checked on |
| 116 |
freeing the pointers and checking the leaks, and a new function |
| 117 |
check_mem_corruption is added for your on-demand use in debugging. |
| 118 |
You may also want to define _DEBUG_NEW_TAILCHECK to something like 4 |
| 119 |
for past-end memory corruption check, which is off by default to |
| 120 |
ensure performance is not affected. |
| 121 |
|
| 122 |
An article on its design and implementation is available at |
| 123 |
|
| 124 |
http://nvwa.sourceforge.net/article/debug_new.htm |
| 125 |
|
| 126 |
* fast_mutex.h |
| 127 |
|
| 128 |
The threading transparent layer simulating a non-recursive mutex. It |
| 129 |
supports C++11 mutex, POSIX mutex, and Win32 critical section, as |
| 130 |
well as a no-threads mode. Unlike Loki and some other libraries, |
| 131 |
threading mode is not to be specified in code, but detected from the |
| 132 |
environment. It will automatically switch on multi-threading mode |
| 133 |
(inter-thread exclusion) when the "-MT"/"-MD" option of MSVC, the |
| 134 |
"-mthreads" option of MinGW GCC, or the "-pthread" option of GCC |
| 135 |
under POSIX environments, is used. One advantage of the current |
| 136 |
implementation is that the construction and destruction of a static |
| 137 |
object using a static fast_mutex not yet constructed or already |
| 138 |
destroyed are allowed to work (with lock/unlock operations ignored), |
| 139 |
and there are re-entry checks for lock/unlock operations when the |
| 140 |
preprocessing symbol _DEBUG is defined. |
| 141 |
|
| 142 |
* fc_queue.h |
| 143 |
|
| 144 |
A queue that has a fixed capacity (maximum number of allowed items). |
| 145 |
All memory is pre-allocated when the queue is initialized, so memory |
| 146 |
consumption is well controlled. When the queue is full, inserting a |
| 147 |
new item will discard the oldest item in the queue. Care is taken to |
| 148 |
ensure this class template provides the strong exception safety |
| 149 |
guarantee. |
| 150 |
|
| 151 |
This class template is a good exercise for me to make a STL-type |
| 152 |
container. Depending on your needs, you may find circular_buffer in |
| 153 |
Boost more useful, which provides similar functionalities and a |
| 154 |
richer API. However, the design philosophies behind the two |
| 155 |
implementations are quite different. fc_queue is modelled closely on |
| 156 |
std::queue, and I optimize mainly for a lockless producer-consumer |
| 157 |
pattern -- when there is one producer and one consumer, and the |
| 158 |
producer does not try to queue an element when the queue is already |
| 159 |
full, no lock is needed for the queue operations. |
| 160 |
|
| 161 |
* file_line_reader.cpp |
| 162 |
* file_line_reader.h |
| 163 |
|
| 164 |
This is one of the line reading classes I implemented modelling the |
| 165 |
Python approach. They make reading lines from a file a simple loop. |
| 166 |
This specific class allows reading from a traditional FILE*. |
| 167 |
Cf. istream_line_reader.h and mmap_line_reader.h. |
| 168 |
|
| 169 |
See the following blog for the motivation and example code: |
| 170 |
|
| 171 |
https://yongweiwu.wordpress.com/2016/11/12/performance-of-my-line-readers/ |
| 172 |
|
| 173 |
* fixed_mem_pool.h |
| 174 |
|
| 175 |
A memory pool implementation that requires initialization (allocates |
| 176 |
a fixed-size chunk) prior to its use. It is simple and makes no |
| 177 |
memory fragmentation, but the memory pool size cannot be changed |
| 178 |
after initialization. Macros are provided to easily make a class use |
| 179 |
pooled new/delete. |
| 180 |
|
| 181 |
* functional.h |
| 182 |
|
| 183 |
This is my test bed for functional programming. If you are into |
| 184 |
functional programming, check it out. It includes functional |
| 185 |
programming patterns like: |
| 186 |
|
| 187 |
- map |
| 188 |
- reduce |
| 189 |
- compose |
| 190 |
- fixed-point combinator |
| 191 |
- curry |
| 192 |
- optional |
| 193 |
|
| 194 |
My blogs on functional programming may be helpful: |
| 195 |
|
| 196 |
https://yongweiwu.wordpress.com/2014/12/07/study-notes-functional-programming-with-cplusplus/ |
| 197 |
https://yongweiwu.wordpress.com/2014/12/14/y-combinator-and-cplusplus/ |
| 198 |
https://yongweiwu.wordpress.com/2014/12/29/type-deduction-and-my-reference-mistakes/ |
| 199 |
https://yongweiwu.wordpress.com/2015/01/03/generic-lambdas-and-the-compose-function/ |
| 200 |
|
| 201 |
* istream_line_reader.h |
| 202 |
|
| 203 |
This is one of the line reading classes I implemented modelling the |
| 204 |
Python approach. They make reading lines from a file a simple loop. |
| 205 |
This specific class allows reading from an input stream. |
| 206 |
Cf. file_line_reader.h and mmap_line_reader.h. |
| 207 |
|
| 208 |
See the following blogs for the motivation and example code: |
| 209 |
|
| 210 |
https://yongweiwu.wordpress.com/2016/08/16/python-yield-and-cplusplus-coroutines/ |
| 211 |
https://yongweiwu.wordpress.com/2016/11/12/performance-of-my-line-readers/ |
| 212 |
|
| 213 |
* mem_pool_base.cpp |
| 214 |
* mem_pool_base.h |
| 215 |
|
| 216 |
A class solely to be inherited by memory pool implementations. It is |
| 217 |
used by static_mem_pool and fixed_mem_pool. |
| 218 |
|
| 219 |
* mmap_line_reader.cpp |
| 220 |
* mmap_line_reader.h |
| 221 |
|
| 222 |
This is one of the line reading classes I implemented modelling the |
| 223 |
Python approach. They make reading lines from a file a simple loop. |
| 224 |
This specific class allows reading from an on-disk file via memory- |
| 225 |
mapped file I/O. Cf. istream_line_reader.h and file_line_reader.h. |
| 226 |
|
| 227 |
See the following blog for the motivation and example code: |
| 228 |
|
| 229 |
https://yongweiwu.wordpress.com/2016/11/12/performance-of-my-line-readers/ |
| 230 |
|
| 231 |
* object_level_lock.h |
| 232 |
|
| 233 |
The Loki ObjectLevelLockable adapted to use the fast_mutex layer. |
| 234 |
The member function get_locked_object does not exist in Loki, but is |
| 235 |
also taken from Mr Alexandrescu's article. Cf. class_level_lock.h. |
| 236 |
|
| 237 |
* pctimer.h |
| 238 |
|
| 239 |
A function to get a high-resolution timer for Win32/Cygwin/Unix. It |
| 240 |
is quite useful for measurement and optimization. |
| 241 |
|
| 242 |
* set_assign.h |
| 243 |
|
| 244 |
Utility routines to make up for the fact that STL only has set_union |
| 245 |
(+) and set_difference (-) algorithms but no corresponding += and -= |
| 246 |
operations available. |
| 247 |
|
| 248 |
* static_mem_pool.cpp |
| 249 |
* static_mem_pool.h |
| 250 |
|
| 251 |
A memory pool implementation to pool memory blocks according to |
| 252 |
compile-time block sizes. Macros are provided to easily make a class |
| 253 |
use pooled new/delete. |
| 254 |
|
| 255 |
An article on its design and implementation is available at |
| 256 |
|
| 257 |
http://nvwa.sourceforge.net/article/static_mem_pool.htm |
| 258 |
|
| 259 |
* tree.h |
| 260 |
|
| 261 |
A generic tree class template along with traversal utilities. |
| 262 |
Besides the usual template argument of value type, it has an |
| 263 |
additional argument of storage policy, which can be either unique or |
| 264 |
shared. Traversal utility classed are provided so that traversing a |
| 265 |
tree can be simply done in a range-based for loop. The test code, |
| 266 |
test/test_tree.cpp, shows its basic usage. |
| 267 |
|
| 268 |
* type_traits.h |
| 269 |
|
| 270 |
This file was separated from fc_queue.h. Type traits were not |
| 271 |
standardized until C++11, and, in addition, there were late changes |
| 272 |
in the trait names that did not go into even the 2012 releases of |
| 273 |
MSVC and GCC. So I made a nvwa::is_trivially_destructible that can |
| 274 |
work across main compilers. |
| 275 |
|
| 276 |
In the post-C++11 world, this is no longer useful. Use of this file |
| 277 |
is deprecated. |
| 278 |
|
| 279 |
|
| 280 |
$Id: README,v 1.1 2017/03/29 14:08:55 adah Exp $ |
| 281 |
|
| 282 |
vim:autoindent:expandtab:formatoptions=tcqlm:textwidth=72: |