@@ -352,6 +352,20 @@ \subsubsection{Operand Size Related}
352352 \end {center }
353353\end {small }
354354
355+ \subsection {Small-Stack option }
356+ \label {ch:SMALL_STACK_INTRO }
357+ The library can be compiled with the symbol \texttt {MP\_ SMALL\_ STACK\_ SIZE } defined, which results in
358+ the temporary \texttt {MP\_ WARRAY }-sized stack buffers being put on the heap.
359+ This comes with one problem, namely: formerly promised thread-safety isn't given anymore.
360+ Therefore if the Small-Stack option is enabled while doing multi threading, the provided locking
361+ mechanism shall be used.
362+ For some use cases it can be desired to use the Small-Stack option, but there are no threads and
363+ therefore we provide the possibility to disable locking by defining the symbol \texttt {MP\_ NO\_ LOCKING }.
364+
365+ In case one already knows how many threads must be supported, the symbol \texttt {MP\_ WARRAY\_ NUM } can
366+ be useful. It can be pre-defined at compile time to the number of heap buffers created on automatic
367+ initialisation. C.f. \ref {ch:SMALL_STACK_API } for the dynamic API and further details.
368+
355369\section {Purpose of LibTomMath }
356370Unlike GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath
357371was not written with bleeding edge performance in mind. First and foremost LibTomMath was written
@@ -428,7 +442,9 @@ \chapter{Getting Started with LibTomMath}
428442\section {Building Programs }
429443In order to use LibTomMath you must include `` tommath.h'' and link against the appropriate library
430444file (typically
431- libtommath.a). There is no library initialization required and the entire library is thread safe.
445+ libtommath.a). There is no library initialization required and the entire library is thread safe
446+ if it is used in its default configuration. Locking is recommended if the small-stack option
447+ is enabled and multiple threads are used, c.f. \ref {ch:SMALL_STACK_INTRO } resp. \ref {ch:SMALL_STACK_API }
432448
433449\section {Return Codes }
434450There are five possible return codes a function may return.
@@ -813,6 +829,52 @@ \subsection{Adding additional digits}
813829\end {alltt }
814830\end {small }
815831
832+ \section {Small-Stack option }
833+ \label {ch:SMALL_STACK_API }
834+
835+ In case the \texttt {MP\_ SMALL\_ STACK\_ SIZE } symbol is defined the following functions
836+ can be useful.
837+
838+ To initialize the internal structure the following function shall be called.
839+
840+ \index {mp\_ warray\_ init}
841+ \begin {alltt }
842+ mp_err mp_warray_init(size_t n_alloc, bool preallocate, mp_lock *lock);
843+ \end {alltt }
844+
845+ The flag \texttt {preallocate } controls whether the internal buffers --
846+ \texttt {n\_ alloc } buffers of size \texttt {MP\_ WARRAY } -- will be allocated when
847+ \texttt {mp\_ warray\_ init() } is called, or whether they will be allocated when required.
848+ The \texttt {mp\_ lock } struct looks as follows and shall be used to protect the
849+ internal structure when using the library in a multi-threaded application.
850+
851+ \index {mp\_ lock}
852+ \begin {alltt }
853+ typedef struct {
854+ int (*lock)(void *ctx);
855+ int (*unlock)(void *ctx);
856+ void *ctx;
857+ } mp_lock;
858+ \end {alltt }
859+
860+ The \texttt {mp\_ lock.lock } resp. \texttt {mp\_ lock.unlock } functions will be called before resp.
861+ after modifying the internal struct.
862+ The \texttt {mp\_ lock.ctx } element will be passed to those functions.
863+
864+ To free the internally allocated memory the following function shall be called.
865+
866+ \index {mp\_ warray\_ free}
867+ \begin {alltt }
868+ int mp_warray_free(void);
869+ \end {alltt }
870+
871+
872+ Those two API functions are always available, even if the \texttt {MP\_ SMALL\_ STACK\_ SIZE } option
873+ has been disabled at compile time.
874+ In that case \texttt {mp\_ warray\_ init() } will return \texttt {MP\_ ERR } and \texttt {mp\_ warray\_ free() }
875+ will return $ -1 $ .
876+
877+
816878\chapter {Basic Operations }
817879\section {Copying }
818880
0 commit comments