@@ -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+ Therefor 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+ therefor 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 enabeled 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,53 @@ \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 \texttt {mp\_ lock } struct looks as follows and shall be used to protect the
846+ internal structure when using the library in a multi-threaded application.
847+
848+ \index {mp\_ lock}
849+ \begin {alltt }
850+ typedef struct {
851+ int (*lock)(void *ctx);
852+ int (*unlock)(void *ctx);
853+ void *ctx;
854+ } mp_lock;
855+ \end {alltt }
856+
857+ The \texttt {mp\_ lock.lock } resp. \texttt {mp\_ lock.unlock } functions will be called before resp.
858+ after modifying the internal struct.
859+ The \texttt {mp\_ lock.ctx } element will be passed to those functions.
860+
861+ To free the internally allocated memory the following function shall be called.
862+
863+ \index {mp\_ warray\_ free}
864+ \begin {alltt }
865+ int mp_warray_free(void);
866+ \end {alltt }
867+
868+ The memory allocated on the heap will never be auto-free'd as there's no good point in time where
869+ the library knows when it is fine to free those buffers. Therefor one has to free those buffers
870+ themself by calling \texttt {mp\_ warray\_ free() }.
871+
872+
873+ Those two API functions are always available, even if the \texttt {MP\_ SMALL\_ STACK\_ SIZE } option
874+ has been disabled at compile time.
875+ In that case \texttt {mp\_ warray\_ init() } will return \texttt {MP\_ ERR } and \texttt {mp\_ warray\_ free() }
876+ will return $ -1 $ .
877+
878+
816879\chapter {Basic Operations }
817880\section {Copying }
818881
0 commit comments