| 1 |
|
| 2 |
This chapter describes Easel from a developer's perspective. It shows |
| 3 |
how a module's source code is organized, written, tested, and |
| 4 |
documented. It should help you with implementing new Easel code, and |
| 5 |
also with understanding the structure of existing Easel code. |
| 6 |
|
| 7 |
We expect Easel to constantly evolve, both in code and in style. |
| 8 |
Talking about our code style does not mean we enforce foolish |
| 9 |
consistency. Rather, the goal is aspirational; one way we try to |
| 10 |
manage the complexity of our growing codebase is to continuously |
| 11 |
cajole Easel code toward a clean and consistent presentation. We try |
| 12 |
to organize code modules in similar ways, use certain naming |
| 13 |
conventions, and channel similar functions towards common |
| 14 |
\esldef{interfaces} that provide common calling conventions and |
| 15 |
behaviors. |
| 16 |
|
| 17 |
But because it evolves, not all Easel code obeys the code style |
| 18 |
described in this chapter. Easel code style is like a local building |
| 19 |
ordinance. Any new construction should comply. Older construction is |
| 20 |
grandfathered in and does not have to immediately conform to the |
| 21 |
current rules. When it comes time to renovate, it's also time to bring |
| 22 |
the old work up to the current standards. |
| 23 |
|
| 24 |
For a concrete example we will focus primarily on one Easel module, |
| 25 |
the \eslmod{buffer} module. We'll take a bottom up approach, starting |
| 26 |
from the overall organization of the module and working down into |
| 27 |
details. If you're a starting developer, you might have preferred a |
| 28 |
bottom-up description; you might just want to know how to write or |
| 29 |
improve a single Easel function, for example. In that case, skim |
| 30 |
ahead. |
| 31 |
|
| 32 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 33 |
% Table: Easel naming conventions |
| 34 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 |
\begin{table} |
| 36 |
\begin{minipage}{\textwidth} |
| 37 |
\begin{tabular}{l>{\raggedright}p{3.5in}l} |
| 38 |
\textbf{What} & \textbf{Explanation} & \textbf{Example} \\ \hline |
| 39 |
Easel module |
| 40 |
& |
| 41 |
Module names should be 10 characters or less.\footnote{sqc assumes |
| 42 |
this in output formatting, for example.} |
| 43 |
Many modules are organized around a single Easel object |
| 44 |
that they implement. The name of the module matches the |
| 45 |
name of the object. For example, \ccode{esl\_buffer.c} implements \ccode{ESL\_BUFFER}. |
| 46 |
& \eslmod{buffer} \\ \\ |
| 47 |
|
| 48 |
tag name |
| 49 |
& Names in the module are constructed either using the module's full |
| 50 |
name or sometimes with a shorter abbreviation, usually 3 |
| 51 |
characters (sometimes 2 or 4). |
| 52 |
& \ccode{buf} \\ \\ |
| 53 |
|
| 54 |
source file |
| 55 |
& Each module has one source file, named \ccode{esl\_}\itcode{modulename}\ccode{.c}. |
| 56 |
& \ccode{esl\_buffer.c} \\ \\ |
| 57 |
|
| 58 |
header file |
| 59 |
& Each module has one header file, named \ccode{esl\_}\itcode{modulename}\ccode{.h}. |
| 60 |
& \ccode{esl\_buffer.h} \\ \\ |
| 61 |
|
| 62 |
documentation |
| 63 |
& Each module has one documentation chapter, named \ccode{esl\_}\itcode{modulename}\ccode{.tex}. |
| 64 |
& \ccode{esl\_buffer.tex} \\ \\ |
| 65 |
|
| 66 |
Easel object |
| 67 |
& Easel ``objects'' are typedef'ed C structures (usually) or |
| 68 |
types (rarely\footnote{\ccode{ESL\_DSQ} is a \ccode{uint8\_t}, for example.}). |
| 69 |
& \ccode{ESL\_BUFFER} \\ \\ |
| 70 |
|
| 71 |
external function |
| 72 |
& All exposed functions have tripartite names \ccode{esl\_}\itcode{module}\ccode{\_specificname}(). |
| 73 |
The specific part of function names often adhere to a standardized API |
| 74 |
``interface'' nomenclature. (All \ccode{\_Open()} functions must follow the same standardized |
| 75 |
behavior guidelines, for example.) Functions in the base \ccode{easel.c} module |
| 76 |
have a bipartite name, omitting the module name. The specific |
| 77 |
name part generally uses mixed case capitalization. |
| 78 |
& \ccode{esl\_buffer\_OpenFile()} \\ \\ |
| 79 |
|
| 80 |
static function |
| 81 |
& Internal functions (static within a module file) drop the |
| 82 |
\ccode{esl\_} prefix, and are |
| 83 |
named \itcode{modulename}\ccode{\_function}. |
| 84 |
& \ccode{buffer\_refill()} \\ \\ |
| 85 |
|
| 86 |
macro |
| 87 |
& Macros follow the same naming convention as external functions, |
| 88 |
except they are all upper case. |
| 89 |
& \ccode{ESL\_ALLOC()} \\ \\ |
| 90 |
|
| 91 |
defined constant |
| 92 |
& Defined constants in Easel modules are named |
| 93 |
\ccode{esl}\itcode{MODULENAME}\ccode{\_FOO}. Constants defined |
| 94 |
in the base \ccode{easel.h} module are named just |
| 95 |
\ccode{eslFOO}. |
| 96 |
& \ccode{eslBUFFER\_SLURPSIZE}\\ \\ |
| 97 |
|
| 98 |
return codes |
| 99 |
& Return codes are constants defined in \ccode{easel.h}, so |
| 100 |
they obey the rules of other defined constants in the base module (\ccode{eslOK}, |
| 101 |
\ccode{eslFAIL}). Additionally, error codes start with |
| 102 |
\ccode{E}, as in \ccode{eslE}\itcode{ERRTYPE}. |
| 103 |
& \ccode{eslENOTFOUND} \\ \\ |
| 104 |
|
| 105 |
config constant |
| 106 |
& Constants that don't start with \ccode{esl} are almost always |
| 107 |
configuration (compile-time) constants determined by the autoconf |
| 108 |
\ccode{./configure} script and defined in \ccode{esl\_config.h}. |
| 109 |
& \ccode{HAVE\_STDINT\_H} \\ \\ |
| 110 |
\end{tabular} |
| 111 |
\end{minipage} |
| 112 |
\caption{\textbf{Easel naming conventions.} } |
| 113 |
\end{table} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 |
\section{An Easel module} |
| 119 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 120 |
|
| 121 |
Each module consists of three files: a .c C code file, a .h header |
| 122 |
file, and a .tex documentation file. These filenames are constructed |
| 123 |
from the module name. For example, the \eslmod{buffer} module is |
| 124 |
implemented in \ccode{esl\_buffer.c}, \ccode{esl\_buffer.h}, and |
| 125 |
\ccode{esl\_buffer.tex}. |
| 126 |
|
| 127 |
%%%%%%%%%%%%%%%% |
| 128 |
\subsection{The .c file} |
| 129 |
%%%%%%%%%%%%%%%% |
| 130 |
|
| 131 |
Easel \ccode{.c} files are larger than most coding styles would |
| 132 |
advocate. Easel module code is designed to be \emph{read}, to be |
| 133 |
\emph{self-documenting}, to contain its own \emph{testing methods}, |
| 134 |
and to provide useful \emph{working examples}. Thus the size of the |
| 135 |
files is a little deceptive, compared to C code that's solely |
| 136 |
implementating some functions. In general, only about a a quarter of |
| 137 |
an Easel module's \ccode{.c} file is the actual module implementation. |
| 138 |
Typically, around half of an Easel \ccode{.c} file is documentation, |
| 139 |
and much of this gets automatically parsed into the PDF userguide. The |
| 140 |
rest consists of drivers for unit testing and examples. |
| 141 |
|
| 142 |
Module files are organized into a somewhat stereotypical set of |
| 143 |
sections, to facilitate navigating the code, as follows. |
| 144 |
|
| 145 |
The \ccode{.c} file starts with a comment that contains the {\bfseries |
| 146 |
table of contents}. The table of contents helps us navigate a long |
| 147 |
Easel source file. This initial comment also includes a short |
| 148 |
description of the module's purpose. It may also contain miscellaneous |
| 149 |
notes. |
| 150 |
|
| 151 |
For example, from the \eslmod{buffer} module: |
| 152 |
|
| 153 |
\input{cexcerpts/header_example} |
| 154 |
|
| 155 |
None of this is parsed automatically. Its structure is just |
| 156 |
convention. |
| 157 |
|
| 158 |
The short description lines in the table of contents match section |
| 159 |
headings in comments later in the file. A search forward with the text |
| 160 |
of a heading will move you to that section of the code. |
| 161 |
|
| 162 |
Next come the {\bfseries includes} and any {\bf definitions}. Of the |
| 163 |
include files, the \ccode{esl\_config.h} header must always be |
| 164 |
included first. It contains platform-independent configuration code |
| 165 |
that may affect even the standard library header files. Standard |
| 166 |
headers like \ccode{stdio.h} come next, then Easel's main header |
| 167 |
\ccode{easel.h}; then headers of any other Easel modules this module |
| 168 |
depends on, then the module's own header. For example, the |
| 169 |
\ccode{\#include}'s in the \eslmod{buffer} module look like: |
| 170 |
|
| 171 |
\input{cexcerpts/include_example} |
| 172 |
|
| 173 |
Next come the {\bfseries private function declarations}. We declare |
| 174 |
all private functions at the top of the file, where they can be seen |
| 175 |
easily by a developer who's casually reading the source. Their |
| 176 |
definitions are buried deeper, in one or more sections following the |
| 177 |
implementation of the exposed API. |
| 178 |
|
| 179 |
\input{cexcerpts/statics_example} |
| 180 |
|
| 181 |
The rest of the file is the {\bfseries code}. It is split into |
| 182 |
sections. Each section is numbered and given one-line titles that |
| 183 |
appear in the table of contents. Each section starts with a section |
| 184 |
header, a comment block in front of each code section in the |
| 185 |
\ccode{.c} file. These section headers match comments in front of |
| 186 |
that section's declarations in the \ccode{.h} file. Because of the |
| 187 |
numbering and titling, a particular section of code can be located by |
| 188 |
searching on the number or title. A common section structure includes |
| 189 |
the following, in this order: |
| 190 |
|
| 191 |
|
| 192 |
\begin{description} |
| 193 |
\item[\textbf{The \ccode{FOOBAR} object.}] |
| 194 |
The first section of the file provides the API for creating and |
| 195 |
destroying the object that this module implements. |
| 196 |
|
| 197 |
\item[\textbf{The rest of the API.}] |
| 198 |
Everything else that is part of the API for this module. |
| 199 |
This might be split across multiple sections. |
| 200 |
|
| 201 |
\item[\textbf{Debugging/dev code.}] |
| 202 |
Most objects can be validated or dumped to an output stream |
| 203 |
for inspection. |
| 204 |
|
| 205 |
\item[\textbf{Private functions.}] |
| 206 |
Easel isn't rigorous about where private (non-exposed) functions go, |
| 207 |
but they often go in a separate section in about the middle of the |
| 208 |
\ccode{.c} file, after the API and before the drivers. |
| 209 |
|
| 210 |
\item[\textbf{Optional drivers}] Stats, benchmark, and regression |
| 211 |
drivers, if any. |
| 212 |
|
| 213 |
\item [\textbf{Unit tests.}] |
| 214 |
The unit tests are internal controls that test that the module's API |
| 215 |
works as advertised. |
| 216 |
|
| 217 |
\item [\textbf{Test driver.}] |
| 218 |
All modules have an automated test driver is a \ccode{main()} that |
| 219 |
runs the unit tests. |
| 220 |
|
| 221 |
\item [\textbf{Examples.}] |
| 222 |
All modules have at least one \ccode{main()} showing an example of |
| 223 |
how to use the main features of the module. |
| 224 |
|
| 225 |
\end{description} |
| 226 |
|
| 227 |
%%%%%%%%%%%%%%%% |
| 228 |
\subsection{The .h file} |
| 229 |
%%%%%%%%%%%%%%%% |
| 230 |
|
| 231 |
|
| 232 |
%%%%%%%%%%%%%%%% |
| 233 |
\subsection{Special syntax in Easel C comments} |
| 234 |
%%%%%%%%%%%%%%%% |
| 235 |
|
| 236 |
Easel comments sometimes include special syntax recognized by tools other |
| 237 |
than the compiler. Here are some quick explanations of the special |
| 238 |
stuff a developer needs to be aware of. |
| 239 |
|
| 240 |
\begin{table} |
| 241 |
\begin{tabular}{l>{\raggedright}p{3.5in}l} |
| 242 |
\textbf{Special syntax} & \textbf{Description} & \textbf{Parsed by}\\ \hline |
| 243 |
|
| 244 |
\ccode{/* Function: }\itcode{funcname} |
| 245 |
& Function documentation that gets converted to \LaTeX\ and included |
| 246 |
in Easel's PDF documentation. |
| 247 |
& \emcode{autodoc} \\ \\ |
| 248 |
|
| 249 |
\ccode{ *\# }\itcode{x.\ secheading} |
| 250 |
& Section heading corresponding to section number x in a \ccode{.c} |
| 251 |
file's table of contents. This is automatically extracted as part |
| 252 |
of creating a summary table in the PDF documentation. |
| 253 |
& \emcode{autodoc -t} \\ \\ |
| 254 |
|
| 255 |
\ccode{/*::cexcerpt::} ... |
| 256 |
& Comments that marking beginning/end of code that is extracted |
| 257 |
verbatim into the documentation. |
| 258 |
& \emcode{cexcerpt} \\ \\ |
| 259 |
|
| 260 |
\hline |
| 261 |
\end{tabular} |
| 262 |
\caption{{\bfseries Summary of special syntax in Easel C comments.}} |
| 263 |
\end{table} |
| 264 |
|
| 265 |
%%%% |
| 266 |
\subsubsection{function documentation} |
| 267 |
%%%% |
| 268 |
|
| 269 |
Any comment that starts with |
| 270 |
\begin{cchunk} |
| 271 |
/* Function: ... |
| 272 |
\end{cchunk} |
| 273 |
will be recognized and parsed by our \prog{autodoc} program, |
| 274 |
which assumes it is looking at a structured function documentation |
| 275 |
header. |
| 276 |
|
| 277 |
See section XX for details on how these headers work. |
| 278 |
|
| 279 |
We want all external functions in the Easel API to be documented |
| 280 |
automatically by \prog{autodoc}. We don't want internal functions tp |
| 281 |
appear in the documentation, but we do want them documented in the |
| 282 |
code. To keep \prog{autodoc} from recognizing the function header of |
| 283 |
an internal (static) function, we just leave off the \ccode{Function:} |
| 284 |
tag in the comment block. |
| 285 |
|
| 286 |
%%%% |
| 287 |
\subsubsection{section headings} |
| 288 |
%%%% |
| 289 |
|
| 290 |
The automatically generated \LaTeX\ code for a module's documentation |
| 291 |
includes a table summarizing the functions in the exposed API. This |
| 292 |
table is constructed automatically from the source code by |
| 293 |
\prog{autodoc -t}. The list of functions in this table is extracted |
| 294 |
from the function documentation (above). The table is broken into |
| 295 |
sections, just as the module code is, using section headings. The |
| 296 |
comment block marking the start of a section heading for exposed API |
| 297 |
code has an extra \ccode{\#}: |
| 298 |
|
| 299 |
\begin{cchunk} |
| 300 |
/***************************************************************** |
| 301 |
*# 1. ESL_BUFFER object: opening/closing. |
| 302 |
*****************************************************************/ |
| 303 |
\end{cchunk} |
| 304 |
|
| 305 |
Section headings for internal functions omit the \ccode{\#}, and |
| 306 |
\prog{autodoc} ignores them: |
| 307 |
|
| 308 |
\begin{cchunk} |
| 309 |
/***************************************************************** |
| 310 |
* 10. Unit tests |
| 311 |
*****************************************************************/ |
| 312 |
\end{cchunk} |
| 313 |
|
| 314 |
%%%% |
| 315 |
\subsubsection{excerpting} |
| 316 |
%%%% |
| 317 |
|
| 318 |
This book includes many examples of C code extracted verbatim from |
| 319 |
Easel source. These {\bfseries excerpts} are marked with specially |
| 320 |
formatted comments in the C file: |
| 321 |
|
| 322 |
\begin{cchunk} |
| 323 |
/*::cexcerpt::my_example::begin::*/ |
| 324 |
while (esl_sq_Read(sqfp, sq) == eslOK) |
| 325 |
{ n++; } |
| 326 |
/*::cexcerpt::my_example::end::*/ |
| 327 |
\end{cchunk} |
| 328 |
|
| 329 |
When we build the Easel documentation from its source, our |
| 330 |
\prog{cexcerpt} program extracts all marked excerpts from \ccode{.c} |
| 331 |
and \ccode{.h} files, and places them in individual files in a |
| 332 |
temporary \ccode{cexcerpts/} directory, from where they are included |
| 333 |
in the main \LaTeX documentation. |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
%%%%%%%%%%%%%%%% |
| 338 |
\subsection{Driver programs} |
| 339 |
%%%%%%%%%%%%%%%% |
| 340 |
|
| 341 |
An unusual (innovative?) thing about Easel modules is how we embed |
| 342 |
{\bfseries driver programs} directly in the module's \ccode{.c} |
| 343 |
file. Driver programs include our unit tests, benchmarks, and working |
| 344 |
examples. These small programs are enclosed in standardized |
| 345 |
\ccode{\#ifdef}'s that enable them to be conditionally compiled. |
| 346 |
|
| 347 |
None of these programs are installed by \ccode{make install}. Test |
| 348 |
drivers are compiled as part of \ccode{make check}. A \ccode{make |
| 349 |
dev} compiles all driver programs. |
| 350 |
|
| 351 |
There are six main types of drivers used in Easel: |
| 352 |
|
| 353 |
\begin{description} |
| 354 |
|
| 355 |
\item[\textbf{Unit test driver(s).}] (Mandatory.) Each module has one (and only one) |
| 356 |
\ccode{main()} that runs the unit tests and any other automated for |
| 357 |
the module. The test driver is compiled and run by the testsuite in |
| 358 |
\ccode{testsuite/testsuite.sqc} when one does a \ccode{make check} |
| 359 |
on the package. It is also run by several of the automated tools |
| 360 |
used in development, including the coverage (\ccode{gcov}) and |
| 361 |
memory (\ccode{valgrind}) tests. A test driver takes no arguments |
| 362 |
(it must generate any input files it needs). If it succeeds, it |
| 363 |
returns 0, with no output. If it fails, it returns nonzero and calls |
| 364 |
\ccode{esl\_fatal()} to issue a short error message on |
| 365 |
\ccode{stdout}. Our test harness, \emcode{sqc}, depends on these |
| 366 |
output and exit status conventions. Optionally, it may use a flag |
| 367 |
to show more useful output when it's run more interactively. |
| 368 |
(usually a \ccode{-v}, for verbose). |
| 369 |
The test driver is enclosed by |
| 370 |
\ccode{\#ifdef esl}\itcode{MODULE}\ccode{\_TESTDRIVE} for |
| 371 |
conditional compilation. |
| 372 |
|
| 373 |
\item[\textbf{Regression/comparison test(s).}] (Optional.) These tests |
| 374 |
link to one or more libraries that provide identical comparable |
| 375 |
functionality, such as previous versions of Easel, the old |
| 376 |
\prog{SQUID} library, \prog{LAPACK} or the GNU Scientific Library. |
| 377 |
They test that Easel's functionality performs at least as it used |
| 378 |
to, or as well as the 'competition'. These tests are run on demand, |
| 379 |
and not included in automated testing, because the other libraries |
| 380 |
may only be present on a subset of our development machines. They |
| 381 |
are enclosed by \ccode{\#ifdef |
| 382 |
esl}\itcode{MODULE}\ccode{\_REGRESSION} for conditional |
| 383 |
compilation. |
| 384 |
|
| 385 |
\item[\textbf{Benchmark(s).}] (Optional.) These tests run a |
| 386 |
standardized performance benchmark and collect time and/or memory |
| 387 |
statistics. They may generate output suitable for graphing. They are |
| 388 |
run on demand, not by automated tools. They typically use |
| 389 |
\eslmod{stopwatch} for timing. They are enclosed by |
| 390 |
\ccode{\#ifdef esl}\itcode{MODULE}\ccode{\_BENCHMARK} for |
| 391 |
conditional compilation. |
| 392 |
|
| 393 |
\item[\textbf{Statistics generator(s).}] (Optional.) These tests collect |
| 394 |
statistics used to characterize the module's scientific performance, |
| 395 |
such as its accuracy at some task. They may generate graphing |
| 396 |
output. They are run on demand, not by automated tools. They are |
| 397 |
enclosed by \ccode{\#ifdef esl}\itcode{MODULE}\ccode{\_STATS} |
| 398 |
for conditional compilation. |
| 399 |
|
| 400 |
\item[\textbf{Experiment(s).}] (Optional.) These are other reproducible |
| 401 |
experiments we've done on the module code, essentially the same as |
| 402 |
statistics generators. They are |
| 403 |
enclosed by \ccode{\#ifdef esl}\itcode{MODULE}\ccode{\_EXPERIMENT} |
| 404 |
for conditional compilation. |
| 405 |
|
| 406 |
\item[\textbf{Example(s).}] (Mandatory). Every module has at least one example |
| 407 |
\ccode{main()} that provides a ``hello world'' level example of |
| 408 |
using the module's API. Examples are enclosed in \ccode{cexcerpt} |
| 409 |
tags for extraction and verbatim inclusion in the documentation. |
| 410 |
They are enclosed by \ccode{\#ifdef esl}\itcode{MODULE}\ccode{\_EXAMPLE} |
| 411 |
for conditional compilation. |
| 412 |
\end{description} |
| 413 |
|
| 414 |
All modules have at least one test driver and one example. Other tests |
| 415 |
and examples are optional. When there is more than one \ccode{main()} |
| 416 |
of a given type, the additional tags are numbered starting from 2: for |
| 417 |
example, a module with three example \ccode{main()'s} would have three |
| 418 |
tags for conditional compilation, \ccode{eslFOO\_EXAMPLE}, |
| 419 |
\ccode{eslFOO\_EXAMPLE2}, and \ccode{eslFOO\_EXAMPLE3}. |
| 420 |
|
| 421 |
The format of the conditional compilation tags for all the drivers |
| 422 |
(including test and example drivers) must be obeyed. Some test scripts |
| 423 |
are scanning the .c files and identifying these tags |
| 424 |
automatically. For instance, the driver compilation test identifies any |
| 425 |
tag named |
| 426 |
\ccode{esl}\itcode{MODULENAME}\ccode{\_\{TESTDRIVE,EXAMPLE,REGRESSION,BENCHMARK,STATS\}*} |
| 427 |
and attempt to compile the code with that tag defined. |
| 428 |
|
| 429 |
Which driver is compiled (if any) is controlled by conditional |
| 430 |
compilation of the module's \ccode{.c} file with the appropriate |
| 431 |
tag. For example, to compile and run the \eslmod{sqio} test driver as |
| 432 |
a standalone module: |
| 433 |
|
| 434 |
\begin{cchunk} |
| 435 |
% gcc -g -Wall -I. -o esl_sqio_utest -DeslSQIO_TESTDRIVE esl_sqio.c easel.c -lm |
| 436 |
% ./esl_sqio_utest |
| 437 |
\end{cchunk} |
| 438 |
|
| 439 |
or to compile and run it in full library configuration: |
| 440 |
|
| 441 |
\begin{cchunk} |
| 442 |
% gcc -g -Wall -I. -L. -o esl_sqio_utest -DeslSQIO_TESTDRIVE esl_sqio.c -leasel -lm |
| 443 |
% ./esl_sqio_utest |
| 444 |
\end{cchunk} |
| 445 |
|
| 446 |
|
| 447 |
\begin{table} |
| 448 |
\begin{tabular}{llll} |
| 449 |
\textbf{Driver type} & \textbf{Compilation flag} & \textbf{Driver program name} & \textbf{Notes}\\ \hline |
| 450 |
Unit test & \ccode{esl}\itcode{MODULE}\ccode{\_TESTDRIVE} & \ccode{esl\_}\itcode{module}\ccode{\_utest} & output and exit status standardized for \emcode{sqc}\\ |
| 451 |
Regression test & \ccode{esl}\itcode{MODULE}\ccode{\_REGRESSION} & \ccode{esl\_}\itcode{module}\ccode{\_regression} & may require other libraries installed\\ |
| 452 |
Benchmark & \ccode{esl}\itcode{MODULE}\ccode{\_BENCHMARK} & \ccode{esl\_}\itcode{module}\ccode{\_benchmark} & \\ |
| 453 |
Statistics collection & \ccode{esl}\itcode{MODULE}\ccode{\_STATS} & \ccode{esl\_}\itcode{module}\ccode{\_stats} & \\ |
| 454 |
Experiment & \ccode{esl}\itcode{MODULE}\ccode{\_EXPERIMENT} & \ccode{esl\_}\itcode{module}\ccode{\_experiment} & \\ |
| 455 |
Example & \ccode{esl}\itcode{MODULE}\ccode{\_EXAMPLE} & \ccode{esl\_}\itcode{module}\ccode{\_example} & \\ |
| 456 |
\end{tabular} |
| 457 |
\caption{{\bfseries Summary of types of driver programs in Easel.}} |
| 458 |
\end{table} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 469 |
\section{Writing an Easel function} |
| 470 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 471 |
|
| 472 |
|
| 473 |
Documentation of functions, particularly in the structured comment |
| 474 |
header that's parsed by the \emcode{autodoc} program, is described in |
| 475 |
a different section of its own. |
| 476 |
|
| 477 |
%%%% |
| 478 |
\subsubsection{conventions for function names} |
| 479 |
%%%% |
| 480 |
|
| 481 |
Function names are tripartite, constructed as |
| 482 |
\ccode{esl\_}\itcode{moduletag\_funcname}. |
| 483 |
|
| 484 |
The \itcode{moduletag} should generally be the module's full name; |
| 485 |
sometimes (historically) it is an abbreviated tag name for the module |
| 486 |
(such as \ccode{abc} for the \eslmod{alphabet} module); on occasion, |
| 487 |
it is the name of an Easel object or datatype that has not yet budded |
| 488 |
off into its own module. Long versus short \itcode{moduletag}'s are |
| 489 |
sometimes used to indicate functions that operate directly on objects |
| 490 |
via common interfaces, versus other functions in the exposed API. The |
| 491 |
long form may indicate functions that obey a common interface, such as |
| 492 |
\ccode{esl\_alphabet\_Create()}.\footnote{This is a clumsy C version |
| 493 |
of what C++ would do with namespaces, object methods, and |
| 494 |
constructors/destructors.} Miscellaneous exposed functions in the API |
| 495 |
of a module may be named by the three-letter short tag, such as |
| 496 |
\ccode{esl\_abc\_Digitize()}. |
| 497 |
|
| 498 |
The function's \ccode{\{funcname\}} can be anything. Some names |
| 499 |
are standard and indicate the use of a common {\bfseries interface}. |
| 500 |
This part of the name is usually in mixed-case capitalization. |
| 501 |
|
| 502 |
Only exposed (\ccode{extern}) functions must follow these rules. In |
| 503 |
general, private (\ccode{static}) functions can have any |
| 504 |
name. However, it's common in Easel for private functions to obey the |
| 505 |
same naming conventions except without the \ccode{esl\_} prefix. |
| 506 |
|
| 507 |
Sometimes essentially the same function must be provided for different |
| 508 |
data types. In these cases one-letter prefixes are used to indicate |
| 509 |
datatype: |
| 510 |
|
| 511 |
\begin{tabular}{ll} |
| 512 |
\ccode{C} & \ccode{char} type, or a standard C string \\ |
| 513 |
\ccode{X} & \ccode{ESL\_DSQ} type, or an Easel digitized sequence\\ |
| 514 |
\ccode{I} & \ccode{int} type \\ |
| 515 |
\ccode{F} & \ccode{float} type \\ |
| 516 |
\ccode{D} & \ccode{double} type \\ |
| 517 |
\end{tabular} |
| 518 |
|
| 519 |
For example, \eslmod{vectorops} uses this convention heavily; |
| 520 |
\ccode{esl\_vec\_FNorm()} normalizes a vector of floats and |
| 521 |
\ccode{esl\_vec\_DNorm()} normalizes a vector of doubles. A second |
| 522 |
example is in \eslmod{randomseq}, which provides routines for shuffling |
| 523 |
either text strings or digitized sequences, such as |
| 524 |
\ccode{esl\_rsq\_CShuffle()} and \ccode{esl\_rsq\_XShuffle()}. |
| 525 |
|
| 526 |
%%%% |
| 527 |
\subsubsection{conventions for argument names} |
| 528 |
%%%% |
| 529 |
|
| 530 |
When using pointers in C, it can be hard to tell which arguments are |
| 531 |
for input data (which are provided by the caller and will not be |
| 532 |
modified), output data (which are created and returned by the |
| 533 |
function), and modified data (which are both input and output). |
| 534 |
|
| 535 |
For output consisting of pointers to nonscalar types such as objects |
| 536 |
or arrays, it also can be hard to distinguish when the caller is |
| 537 |
supposed to provide pre-allocated storage for the result, versus the |
| 538 |
storage being newly allocated by the function.\footnote{A common |
| 539 |
strategy in C library design is to strive for \emph{no} allocation in |
| 540 |
the library, so the caller is always responsible for explicit |
| 541 |
alloc/free pairs. I feel this puts a tedious burden of allocation code |
| 542 |
on an application.} |
| 543 |
|
| 544 |
When functions return more than one kind of result, it is convenient |
| 545 |
to make all the individual results optional, so the caller doesn't |
| 546 |
have to deal with managing storage for results it isn't interested in. |
| 547 |
In Easel, an optional result pointer is passed as \ccode{NULL} to |
| 548 |
indicate a possible result is not wanted (and is not allocated, if |
| 549 |
returning that result required new allocation). |
| 550 |
|
| 551 |
Easel uses a prefix convention on pointer argument names to indicate |
| 552 |
these situations: |
| 553 |
|
| 554 |
\begin{table}[h] |
| 555 |
\begin{center} |
| 556 |
{\small |
| 557 |
\begin{tabular}{cp{2.5in}p{3in}} |
| 558 |
\textbf{prefix} & \textbf{argument type} & \textbf{allocation (if any):}\\ |
| 559 |
none & If qualified as \ccode{const}, a pointer |
| 560 |
to input data, not modified by the call. |
| 561 |
If unqualified, a pointer to data modified |
| 562 |
by the call (it's both input and output). & by caller\\ |
| 563 |
\ccode{ret\_} & Pointer to result. & in the function \\ |
| 564 |
\ccode{opt\_} & Pointer to optional result. |
| 565 |
If non-\ccode{NULL}, result is obtained. & in the function \\ |
| 566 |
\end{tabular} |
| 567 |
} |
| 568 |
\end{center} |
| 569 |
\end{table} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
%%%% |
| 574 |
\subsubsection{Return status} |
| 575 |
%%%% |
| 576 |
|
| 577 |
%%%% |
| 578 |
\subsubsection{conventions for exception handling} |
| 579 |
%%%% |
| 580 |
|
| 581 |
Easel functions {\bfseries should never exit except through an Easel |
| 582 |
return code or through the Easel exception handler}. When you write |
| 583 |
Easel code you must {\bfseries always} deal with the case when the |
| 584 |
caller has registered a nonfatal exception handler, causing thrown |
| 585 |
exceptions to return a nonzero code rather than exiting. The Easel |
| 586 |
library is designed to be used in programs that can't just suddenly |
| 587 |
crash out with an error message (such as a graphical user interface |
| 588 |
environment), and programs that have specialized error handlers |
| 589 |
because they don't even have access to a \ccode{stderr} stream on a |
| 590 |
terminal (such as a UNIX daemon). |
| 591 |
|
| 592 |
This means that Easel functions must clean up their memory and set |
| 593 |
appropriate return status and return arguments, even in the case of |
| 594 |
thrown exceptions. |
| 595 |
|
| 596 |
|
| 597 |
%%%% |
| 598 |
\subsubsection{Easel's idiomatic function structure} |
| 599 |
%%%% |
| 600 |
|
| 601 |
To deal with the above strictures of return status, returned |
| 602 |
arguments, and exception handling and cleanup, most Easel functions |
| 603 |
follow an idiomatic structure. The following snippet illustrates the |
| 604 |
key ideas: |
| 605 |
|
| 606 |
\begin{cchunk} |
| 607 |
1 int |
| 608 |
2 esl_example_Hello(char *opt_hello, char *opt_len) |
| 609 |
3 { |
| 610 |
4 char *msg = NULL; |
| 611 |
5 int n; |
| 612 |
6 int status; |
| 613 |
|
| 614 |
7 if ( (status = esl_strdup("hello world!\n", -1, &msg)) != eslOK) goto ERROR; |
| 615 |
8 n = strlen(msg); |
| 616 |
|
| 617 |
9 if (opt_hello) *opt_hello = msg; else free(msg); |
| 618 |
10 if (opt_len) *opt_len = n; |
| 619 |
11 return eslOK; |
| 620 |
|
| 621 |
12 ERROR: |
| 622 |
13 if (msg) free(msg); |
| 623 |
14 if (opt_hello) *opt_hello = NULL; |
| 624 |
15 if (opt_n) *opt_n = 0; |
| 625 |
16 return status; |
| 626 |
17 } |
| 627 |
\end{cchunk} |
| 628 |
|
| 629 |
The stuff to notice here: |
| 630 |
|
| 631 |
\begin{itemize} |
| 632 |
\item[line 2:] The \ccode{opt\_hello} and \ccode{opt\_len} arguments |
| 633 |
are optional. The caller might want only one of them (or neither, |
| 634 |
but that would be weird). We're expecting calls like |
| 635 |
\ccode{esl\_example\_Hello(\&hello, \&n)}, |
| 636 |
\ccode{esl\_example\_Hello(\&hello, NULL)}, or |
| 637 |
\ccode{esl\_example\_Hello(NULL, \&n)}. |
| 638 |
|
| 639 |
\item[line 4:] Anything we allocate, we initialize its pointer to \ccode{NULL}. |
| 640 |
Now, if an exception occurs and we have to break out of the function early, |
| 641 |
we can tell whether the allocation has already happened (and hence we need |
| 642 |
to clean up its memory), if the pointer has become non-\ccode{NULL}. |
| 643 |
|
| 644 |
\item[line 6:] Most functions have an explicit \ccode{status} variable. |
| 645 |
Standard error-handling macros (\ccode{ESL\_XEXCEPTION()} for example) expect it to be present, |
| 646 |
as do standard allocation macros (\ccode{ESL\_ALLOC()} for example). |
| 647 |
If we have to handle an exception, we're going to make sure the status |
| 648 |
is set how we want it, then jump to a cleanup block. |
| 649 |
|
| 650 |
\item[line 7:] When any Easel function calls another Easel function, |
| 651 |
it must check the return status for both normal errors and thrown |
| 652 |
exceptions. If an exception has already been thrown by a callee, |
| 653 |
usually the caller just relays the exception status up the call |
| 654 |
stack. The idiom is to set the return \ccode{status} and go |
| 655 |
immediately to the error cleanup block, \ccode{ERROR:}. We use a |
| 656 |
\ccode{goto} for this, Dijkstra notwithstanding. |
| 657 |
|
| 658 |
\item[lines 9,10:] When we set optional arguments for a normal return, |
| 659 |
we first check whether a valid return pointer was provided. If the |
| 660 |
optional pointer is \ccode{NULL} the caller doesn't want the result, |
| 661 |
and we clean up any memory we need to (line 9). |
| 662 |
|
| 663 |
\item[line 13:] In the error cleanup block, we first free any memory |
| 664 |
that got allocated before the failure point. The idiom of |
| 665 |
immediately initializing all allocated pointers to \ccode{NULL} |
| 666 |
enables us to tell which things have been allocated or not. |
| 667 |
|
| 668 |
\item[line 14:] When we return from a function with an unsuccessful |
| 669 |
status, we also make sure that any returned arguments are in |
| 670 |
a documented ground state, usually \ccode{NULL}'s and \ccode{0}'s. |
| 671 |
\end{itemize} |
| 672 |
|
| 673 |
%%%% |
| 674 |
\subsubsection{reentrancy: plan for threads} |
| 675 |
%%%% |
| 676 |
|
| 677 |
Easel code must expect to be called in multithreaded applications. All |
| 678 |
functions must be reentrant. There should be no use of global or |
| 679 |
static variables. |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 686 |
\section{Standard Easel function interfaces} |
| 687 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 688 |
|
| 689 |
Some function names are shared and have common behaviors across |
| 690 |
modules, like \ccode{\_Get*()} and \ccode{\_Set*()} functions. These |
| 691 |
special names are called \esldef{common interfaces}. |
| 692 |
|
| 693 |
\begin{table} |
| 694 |
\begin{minipage}{\textwidth} |
| 695 |
\begin{tabular}{l>{\raggedright}p{3.0in}ll} |
| 696 |
\textbf{Function name} & \textbf{Description} & \textbf{Returns} & \textbf{Example} \\ \hline |
| 697 |
\multicolumn{4}{c}{\bfseries Creating and destroying new objects}\\ |
| 698 |
\ccode{\_Create} |
| 699 |
& Create a new object. |
| 700 |
& \ccode{ESL\_}\itcode{FOO}\ccode{ *} |
| 701 |
& \ccode{esl\_alphabet\_Create()} \\ |
| 702 |
|
| 703 |
\ccode{\_Destroy} |
| 704 |
& Free an object. |
| 705 |
& \ccode{void} |
| 706 |
& \ccode{esl\_alphabet\_Destroy()} \\ |
| 707 |
|
| 708 |
\ccode{\_Clone} |
| 709 |
& Duplicate an object, by creating and allocating a new one. |
| 710 |
& \ccode{ESL\_}\itcode{FOO}\ccode{ *} |
| 711 |
& \ccode{esl\_msa\_Clone()} \\ |
| 712 |
|
| 713 |
\ccode{\_Shadow} |
| 714 |
& Partially duplicate an object, creating a dependent shadow. |
| 715 |
& \ccode{ESL\_}\itcode{FOO}\ccode{ *} |
| 716 |
& \ccode{p7\_oprofile\_Shadow()} \\ |
| 717 |
|
| 718 |
\ccode{\_Copy} |
| 719 |
& Make a copy of an object, using an existing allocated object for space. |
| 720 |
& [standard] |
| 721 |
& \ccode{esl\_msa\_Copy()} \\ |
| 722 |
|
| 723 |
\multicolumn{4}{c}{\bfseries Opening and closing input sources}\\ |
| 724 |
\ccode{\_Open} |
| 725 |
& Open an input source, associating it with an Easel object. |
| 726 |
& [standard] |
| 727 |
& \ccode{esl\_buffer\_Open()} \\ |
| 728 |
|
| 729 |
\ccode{\_Close} |
| 730 |
& Close an Easel object corresponding to an input source. |
| 731 |
& [standard] |
| 732 |
& \ccode{esl\_buffer\_Close()} \\ |
| 733 |
|
| 734 |
\multicolumn{4}{c}{\bfseries Managing memory allocation}\\ |
| 735 |
|
| 736 |
\ccode{\_Grow} |
| 737 |
& Expand the allocation in an existing object, typically by doubling. |
| 738 |
& [standard] |
| 739 |
& \ccode{esl\_tree\_Grow()} \\ |
| 740 |
|
| 741 |
\ccode{\_GrowTo} |
| 742 |
& Reallocate object (if needed) for some new data size. |
| 743 |
& [standard] |
| 744 |
& \ccode{esl\_sq\_GrowTo()} \\ |
| 745 |
|
| 746 |
\ccode{\_Reuse} |
| 747 |
& Recycle an object, reinitializing it while reusing as much of its existing |
| 748 |
allocation(s) as possible. |
| 749 |
& [standard] |
| 750 |
& \ccode{esl\_keyhash\_Reuse()} \\ |
| 751 |
|
| 752 |
\ccode{size\_t \_Sizeof} |
| 753 |
& Return the allocation size of an object |
| 754 |
& size, in bytes |
| 755 |
& - \\ |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
\multicolumn{4}{c}{\bfseries Accessing information in objects}\\ |
| 760 |
|
| 761 |
\ccode{\_Is} |
| 762 |
& Return \ccode{TRUE} or \ccode{FALSE} for some query of the |
| 763 |
internal state of an object. |
| 764 |
& \ccode{TRUE | FALSE} |
| 765 |
& \ccode{esl\_opt\_IsOn()} \\ |
| 766 |
|
| 767 |
\ccode{\_Get} |
| 768 |
& Return a value for some query of the internal state of an object. |
| 769 |
& value |
| 770 |
& \ccode{esl\_buffer\_Get()} \\ |
| 771 |
|
| 772 |
\ccode{\_Read} |
| 773 |
& Get a value in the object and return it in a location provided (and possibly allocated) by the caller. |
| 774 |
& [standard] |
| 775 |
& \ccode{esl\_buffer\_Read()} \\ |
| 776 |
|
| 777 |
\ccode{\_Fetch} |
| 778 |
& Get a value in the object and return it in newly allocated space; |
| 779 |
the caller becomes responsible for the newly allocated space. |
| 780 |
& [standard] |
| 781 |
& \ccode{esl\_buffer\_FetchLine()} \\ |
| 782 |
|
| 783 |
\ccode{\_Set} |
| 784 |
& Set a value in the object. |
| 785 |
& [standard] |
| 786 |
& \ccode{esl\_buffer\_Set()} \\ |
| 787 |
|
| 788 |
\ccode{\_Format} |
| 789 |
& Set a string in the object using \ccode{sprintf()}-like |
| 790 |
semantics. |
| 791 |
& [standard] |
| 792 |
& \ccode{esl\_msa\_FormatName()} \\ |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
\multicolumn{4}{c}{\bfseries Debugging}\\ |
| 797 |
\ccode{\_Validate} |
| 798 |
& Run validation tests on the internal state of an object. |
| 799 |
& [standard] |
| 800 |
& \ccode{esl\_tree\_Validate()} \\ |
| 801 |
|
| 802 |
\ccode{\_Compare} |
| 803 |
& Compare two objects to each other for equality (or close enough). |
| 804 |
& [standard] |
| 805 |
& \ccode{esl\_msa\_Compare()} \\ |
| 806 |
|
| 807 |
\ccode{\_Dump} |
| 808 |
& Dump a verbose, possibly ugly, but developer-readable output |
| 809 |
of the internal state of an object. |
| 810 |
& [standard] |
| 811 |
& \ccode{esl\_keyhash\_Dump()} \\ |
| 812 |
|
| 813 |
\ccode{\_TestSample} |
| 814 |
& Sample a mostly syntactically correct object for test purposes |
| 815 |
& [standard] |
| 816 |
& \ccode{p7\_tophits\_TestSample()} \\ |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
\multicolumn{4}{c}{\bfseries Miscellaneous}\\ |
| 821 |
|
| 822 |
\ccode{\_Write} |
| 823 |
& Write something from an object to an output stream. |
| 824 |
& [standard] |
| 825 |
& \ccode{esl\_msa\_Write()} \\ |
| 826 |
|
| 827 |
\ccode{\_Encode} |
| 828 |
& Convert a user-readable string (such as ``fasta'') to an |
| 829 |
internal Easel code (such as \ccode{eslSQFILE\_FASTA}). |
| 830 |
& [standard] |
| 831 |
& \ccode{esl\_msa\_EncodeFormat()} \\ |
| 832 |
|
| 833 |
\ccode{\_Decode} |
| 834 |
& Convert an internal Easel code (such as \ccode{eslSQFILE\_FASTA}) |
| 835 |
to a user-readable string (such as ``fasta''). |
| 836 |
& [standard] |
| 837 |
& \ccode{esl\_msa\_DecodeFormat()} \\ |
| 838 |
\end{tabular} |
| 839 |
\end{minipage} |
| 840 |
\caption{\textbf{Standard function ``interfaces''.} } |
| 841 |
\end{table} |
| 842 |
|
| 843 |
|
| 844 |
%%%%%%%%%%%%%%%% |
| 845 |
\subsection{Creating and destroying new objects} |
| 846 |
%%%%%%%%%%%%%%%% |
| 847 |
|
| 848 |
Most Easel objects are allocated and free'd by |
| 849 |
\ccode{\_Create()/\_Destroy()} interface. Creating an object often |
| 850 |
just means allocating space for it, so that some other routine can |
| 851 |
fill data into it. It does not necessarily mean that the object |
| 852 |
contains valid data. |
| 853 |
|
| 854 |
\begin{sreapi} |
| 855 |
|
| 856 |
|
| 857 |
\hypertarget{ifc:Create} |
| 858 |
{\item[\_Create(n)]} |
| 859 |
|
| 860 |
A \ccode{\_Create()} interface takes any necessary initialization or |
| 861 |
size information as arguments (there often aren't any), and it returns a |
| 862 |
pointer to the newly allocated object. If an (optional) number of |
| 863 |
elements \ccode{n} is provided, this specifies the number of elements |
| 864 |
that the object is going to contain (for a fixed-size object) or the |
| 865 |
initial allocation size (for a resizable object). In the event of an |
| 866 |
allocation failure, a \ccode{\_Create} procedure throws \ccode{NULL}. |
| 867 |
|
| 868 |
(If any error other than an allocation failure can happen, you should |
| 869 |
use \ccode{\_Build()} instead. A caller is allowed to assume that a |
| 870 |
\ccode{NULL} return from \ccode{\_Create()} is equivalent to |
| 871 |
\ccode{eslEMEM}.) |
| 872 |
|
| 873 |
The internals of some resizeable objects have an \ccode{nredline} |
| 874 |
parameter that controls an additional memory management rule. These |
| 875 |
objects are allowed to grow to arbitrary size (either by doubling with |
| 876 |
\ccode{\_Grow} or by a specific allocation with \ccode{\_Reinit} or |
| 877 |
\ccode{\_GrowTo}) -- but when the object is reused for new data, they |
| 878 |
can be reallocated \emph{downward}, back to the redline |
| 879 |
limit. Specifically, if the allocation size exceeds \ccode{nredline}, |
| 880 |
a \ccode{\_Reuse()} or \ccode{\_Reinit()} call will shrink the |
| 881 |
allocation back to the \ccode{nredline} limit. The idea is for a |
| 882 |
frequently-reused object to be able to briefly handle a rare |
| 883 |
exceptionally large problem, while not permanently committing the |
| 884 |
resizeable object to an extreme allocation size. |
| 885 |
|
| 886 |
At least one module (\ccode{esl\_tree}) allows for creating either a |
| 887 |
fixed-size or a resizeable object; in this case, there is a |
| 888 |
\ccode{\_CreateGrowable()} call for the resizeable version. |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
\hypertarget{ifc:Build} |
| 894 |
{\item[\_Build()]} |
| 895 |
|
| 896 |
A \ccode{\_Build()} interface is the same as \ccode{\_Create()}, but |
| 897 |
instead of returning a pointer to the new object, we return an Easel |
| 898 |
error code, and the new object is returned through a \ccode{*ret\_obj} |
| 899 |
argument. |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
\hypertarget{ifc:Destroy} |
| 906 |
{\item[\_Destroy(obj)]} |
| 907 |
A \ccode{\_Destroy()} interface takes an object pointer as an |
| 908 |
argument, and frees all the memory associated with it. A |
| 909 |
\ccode{\_Destroy} procedure returns \ccode{void} (there is no useful |
| 910 |
information to return about a failure; the only calls are to |
| 911 |
\ccode{free()} and if that fails, we're in trouble). |
| 912 |
\end{sreapi} |
| 913 |
|
| 914 |
For example: |
| 915 |
\begin{cchunk} |
| 916 |
ESL_SQ *sq; |
| 917 |
sq = esl_sq_Create(); |
| 918 |
esl_sq_Destroy(sq); |
| 919 |
\end{cchunk} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
%%%%%%%%%%%%%%%% |
| 925 |
\subsubsection{opening and closing input streams} |
| 926 |
%%%%%%%%%%%%%%%% |
| 927 |
|
| 928 |
Some objects (such as \ccode{ESL\_SQFILE} and \ccode{ESL\_MSAFILE}) |
| 929 |
correspond to open input streams -- usually an open file, but possibly |
| 930 |
reading from a pipe. Such objects are \ccode{\_Open()}'ed and |
| 931 |
\ccode{\_Close()'d}, not created and destroyed. |
| 932 |
|
| 933 |
Input stream objects have to be capable of handling normal failures, |
| 934 |
because of bad user input. Input stream objects contain an |
| 935 |
\ccode{errbuf[eslERRBUFSIZE]} field to capture informative parse error |
| 936 |
messages. |
| 937 |
|
| 938 |
\begin{sreapi} |
| 939 |
\hypertarget{ifc:Open} |
| 940 |
{\item[\_Open(file, formatcode, \&ret\_obj)]} |
| 941 |
|
| 942 |
Opens the \ccode{file}, which is in a format indicated by |
| 943 |
\ccode{formatcode} for reading; return the open input object in |
| 944 |
\ccode{ret\_obj}. A \ccode{formatcode} of 0 typically means unknown, |
| 945 |
in which case the \ccode{\_Open()} procedure attempts to autodetect |
| 946 |
the format. If the \ccode{file} is \ccode{"-"}, the object is |
| 947 |
configured to read from the \ccode{stdin} stream instead of opening a |
| 948 |
file. If the \ccode{file} ends in a \ccode{.gz} suffix, the object is |
| 949 |
configured to read from a pipe from \ccode{gzip -dc}. Returns |
| 950 |
\ccode{eslENOTFOUND} if \ccode{file} cannot be opened, and |
| 951 |
\ccode{eslEFORMAT} if autodetection is attempted but the format cannot |
| 952 |
be determined. |
| 953 |
|
| 954 |
Newer \ccode{\_Open} procedures return a standard Easel error code, |
| 955 |
and on a normal error they also return the allocated object, using the |
| 956 |
object's error message buffer to report the reason for the failed |
| 957 |
open. |
| 958 |
|
| 959 |
\hypertarget{ifc:Close} |
| 960 |
{\item[\_Close(obj)]} |
| 961 |
|
| 962 |
Closes the input stream \ccode{obj}. Should return a standard Easel |
| 963 |
error code. There are cases where an error in an input stream is only |
| 964 |
detected at closing time (inputs using \ccode{popen()}/\ccode{pclose()} |
| 965 |
are an example). |
| 966 |
\end{sreapi} |
| 967 |
|
| 968 |
For example: |
| 969 |
\begin{cchunk} |
| 970 |
char *seqfile = "foo.fa"; |
| 971 |
ESL_SQFILE *sqfp; |
| 972 |
|
| 973 |
esl_sqio_Open(seqfile, eslSQFILE_FASTA, NULL, &sqfp); |
| 974 |
esl_sqio_Close(sqfp); |
| 975 |
\end{cchunk} |
| 976 |
|
| 977 |
|
| 978 |
%%%% |
| 979 |
\subsubsection{making copies of objects} |
| 980 |
%%%% |
| 981 |
|
| 982 |
\begin{sreapi} |
| 983 |
|
| 984 |
\hypertarget{ifc:Clone} |
| 985 |
{\item[\_Clone(obj)]} |
| 986 |
|
| 987 |
Creates and returns a pointer to a duplicate of \ccode{obj}. |
| 988 |
Equivalent to (and is a shortcut for, and is generally implemented as) |
| 989 |
\ccode{dest = \_Create(); \_Copy(src, dest)}. Caller is responsible |
| 990 |
for free'ing the duplicate object, just as if it had been |
| 991 |
\ccode{\_Create}'d. Throws \ccode{NULL} if allocation fails. |
| 992 |
|
| 993 |
|
| 994 |
\hypertarget{ifc:Copy} |
| 995 |
{\item[\_Copy(src, dest)]} |
| 996 |
|
| 997 |
Copies \ccode{src} object into \ccode{dest}, where the caller has |
| 998 |
already created an appropriately allocated and empty \ccode{dest} |
| 999 |
object (or buffer, or whatever). Returns \ccode{eslOK} on success; |
| 1000 |
throws \ccode{eslEINCOMPAT} if the objects are not compatible (for |
| 1001 |
example, two matrices that are not the same size). |
| 1002 |
|
| 1003 |
Note that the order of the arguments is always \ccode{src} |
| 1004 |
$\rightarrow$ \ccode{dest} (unlike the C library's \ccode{strcpy()} |
| 1005 |
convention, which is the opposite order). |
| 1006 |
|
| 1007 |
|
| 1008 |
\hypertarget{ifc:Shadow} |
| 1009 |
{\item[\_Shadow(obj)]} |
| 1010 |
|
| 1011 |
Creates and returns a pointer to a partial, dependent copy of |
| 1012 |
\ccode{obj}. Shadow creation arises in multithreading, when threads |
| 1013 |
can share some but not all internal object data. A shadow keeps |
| 1014 |
constant data as pointers to the original object. The object needs to |
| 1015 |
know whether it is a shadow or not, so that <\_Destroy()> works |
| 1016 |
properly on both the original and its shadows. |
| 1017 |
|
| 1018 |
\end{sreapi} |
| 1019 |
|
| 1020 |
%%%%%%%%%%%%%%%% |
| 1021 |
\subsection{Managing memory allocation} |
| 1022 |
%%%%%%%%%%%%%%%% |
| 1023 |
|
| 1024 |
%%%% |
| 1025 |
\subsubsection{resizable objects} |
| 1026 |
%%%% |
| 1027 |
|
| 1028 |
Some objects need to be reallocated and expanded during their use. |
| 1029 |
These objects are called \esldef{resizable}. |
| 1030 |
|
| 1031 |
In some cases, the whole purpose of the object is to have elements |
| 1032 |
added to it, such as \ccode{ESL\_STACK} (pushdown stacks) and |
| 1033 |
\ccode{ESL\_HISTOGRAM} (histograms). In these cases, the normal |
| 1034 |
\ccode{\_Create()} interface performs an initial allocation, and the |
| 1035 |
object keeps track of both its current contents size (often |
| 1036 |
\ccode{obj->N}) and the current allocation size (often |
| 1037 |
\ccode{obj->nalloc}). |
| 1038 |
|
| 1039 |
In at least one case, an object might be either growable or not, |
| 1040 |
depending on how it's being used. This happens, for instance, when we |
| 1041 |
have routines for parsing input data to create a new object, and we |
| 1042 |
need to dynamically reallocate as we go because the input doesn't tell |
| 1043 |
us the total size when we start. For instance, with \ccode{ESL\_TREE} |
| 1044 |
(phylogenetic trees), sometimes we know exactly the size of the tree |
| 1045 |
we need to create (because we're making a tree ourselves), and |
| 1046 |
sometimes we need to create a resizable object (because we're reading a |
| 1047 |
tree from a file). In these cases, the normal \ccode{\_Create()} |
| 1048 |
interface creates a static, nongrowable object of known size, and a |
| 1049 |
\ccode{\_CreateGrowable()} interface specifies an initial allocation |
| 1050 |
for a resizable object. |
| 1051 |
|
| 1052 |
Easel usually handles its own reallocation of resizable objects. For |
| 1053 |
instance, many resizable objects have an interface called something |
| 1054 |
like \ccode{\_Add()} or \ccode{\_Push()} for storing the next element |
| 1055 |
in the object, and this interface will deal with increasing allocation |
| 1056 |
size as needed. In a few cases, a public \ccode{\_Grow()} interface |
| 1057 |
is provided for reallocating an object to a larger size, in cases |
| 1058 |
where a caller might need to grow the object itself. \ccode{\_Grow()} |
| 1059 |
only increases an allocation when it is necessary, and it makes that |
| 1060 |
check immediately and efficiently, so that a caller can call |
| 1061 |
\ccode{\_Grow()} before every attempt to add a new element without |
| 1062 |
worrying about efficiency. An example of where a public |
| 1063 |
\ccode{\_Grow()} interface is generally provided is when an object |
| 1064 |
might be input from different file formats, and an application may |
| 1065 |
need to create its own parser. Although creating an input parser |
| 1066 |
requires familiarity with the Easel object's internal data structures, |
| 1067 |
at least the \ccode{\_Grow()} interface frees the caller from having |
| 1068 |
to understand its memory management. |
| 1069 |
|
| 1070 |
Resizable objects necessarily waste some memory, because they are |
| 1071 |
overallocated in order to reduce the number of calls to |
| 1072 |
\ccode{malloc()}. The wastage is bounded (to a maximum of two-fold, |
| 1073 |
for the default doubling strategies, once an object has exceeded its |
| 1074 |
initial allocation size) but nonetheless may not always be tolerable. |
| 1075 |
|
| 1076 |
In summary: |
| 1077 |
|
| 1078 |
\begin{sreapi} |
| 1079 |
\hypertarget{ifc:Grow} |
| 1080 |
{\item[\_Grow(obj)]} |
| 1081 |
|
| 1082 |
A \ccode{\_Grow()} function checks to see if \ccode{obj} can hold |
| 1083 |
another element. If not, it increases the allocation, according to |
| 1084 |
internally stored rules on reallocation strategy (usually, by |
| 1085 |
doubling). |
| 1086 |
\end{sreapi} |
| 1087 |
|
| 1088 |
\begin{sreapi} |
| 1089 |
\hypertarget{ifc:GrowTo} |
| 1090 |
{\item[\_GrowTo(obj, n)]} |
| 1091 |
|
| 1092 |
A \ccode{\_GrowTo()} function checks to see \ccode{obj} is large |
| 1093 |
enough to hold \ccode{n} elements. If not, it reallocates to at least |
| 1094 |
that size. |
| 1095 |
\end{sreapi} |
| 1096 |
|
| 1097 |
%%%% |
| 1098 |
\subsubsection{reusable objects} |
| 1099 |
%%%% |
| 1100 |
|
| 1101 |
Memory allocation is computationally expensive. An application needs |
| 1102 |
to minimize \ccode{malloc()/free()} calls in performance-critical |
| 1103 |
regions. In loops where one \ccode{\_Destroy()}'s an old object only |
| 1104 |
to \ccode{\_Create()} the next one, such as a sequential input loop |
| 1105 |
that processes objects from a file one at a time, one generally wants |
| 1106 |
to \ccode{\_Reuse()} the same object instead: |
| 1107 |
|
| 1108 |
\begin{sreapi} |
| 1109 |
\hypertarget{ifc:Reuse} |
| 1110 |
{\item[\_Reuse(obj)]} |
| 1111 |
|
| 1112 |
A \ccode{\_Reuse()} interface takes an existing object and |
| 1113 |
reinitializes it as a new object, while reusing as much memory as |
| 1114 |
possible. Any state information that was specific to the problem the |
| 1115 |
object was just used for is reinitialized. Any allocations and state |
| 1116 |
information specific to those allocations are preserved (to the extent |
| 1117 |
possible). A \ccode{\_Reuse()} call should exactly replace (and be |
| 1118 |
equivalent to) a \ccode{\_Destroy()/\_Create()} pair. If the object is |
| 1119 |
growable, it typically would keep the last allocation size, and it |
| 1120 |
must keep at least the same allocation size that a default |
| 1121 |
\ccode{\_Create()} call would give. |
| 1122 |
|
| 1123 |
If the object is arbitrarily resizeable and it has a \ccode{nredline} |
| 1124 |
control on its memory, the allocation is shrunk back to |
| 1125 |
\ccode{nredline} (which must be at least the default initial |
| 1126 |
allocation). |
| 1127 |
|
| 1128 |
\end{sreapi} |
| 1129 |
|
| 1130 |
For example: |
| 1131 |
|
| 1132 |
\begin{cchunk} |
| 1133 |
ESL_SQFILE *sqfp; |
| 1134 |
ESL_SQ *sq; |
| 1135 |
|
| 1136 |
esl_sqfile_Open(\"foo.fa\", eslSQFILE_FASTA, NULL, &sqfp); |
| 1137 |
sq = esl_sq_Create(); |
| 1138 |
while (esl_sqio_Read(sqfp, sq) == eslOK) |
| 1139 |
{ |
| 1140 |
/* do stuff with this sq */ |
| 1141 |
esl_sq_Reuse(sq); |
| 1142 |
} |
| 1143 |
esl_sq_Destroy(sq); |
| 1144 |
\end{cchunk} |
| 1145 |
|
| 1146 |
%%%% |
| 1147 |
\subsubsection{other} |
| 1148 |
%%%% |
| 1149 |
\begin{sreapi} |
| 1150 |
\hypertarget{ifc:Sizeof} |
| 1151 |
{\item[size\_t \_Sizeof(obj)]} |
| 1152 |
|
| 1153 |
Returns the total size of an object and its allocations, in bytes. |
| 1154 |
\end{sreapi} |
| 1155 |
|
| 1156 |
|
| 1157 |
%%%%%%%%%%%%%%%% |
| 1158 |
\subsection{Accessing information in objects} |
| 1159 |
%%%%%%%%%%%%%%%% |
| 1160 |
|
| 1161 |
\begin{sreapi} |
| 1162 |
|
| 1163 |
\hypertarget{ifc:Is} |
| 1164 |
{\item[\_Is*(obj)]} |
| 1165 |
|
| 1166 |
Performs some specific test of the internal state of an |
| 1167 |
object, and returns \ccode{TRUE} or \ccode{FALSE}. |
| 1168 |
|
| 1169 |
\hypertarget{ifc:Get} |
| 1170 |
{\item[value = \_Get*(obj, ...)]} |
| 1171 |
|
| 1172 |
Retrieves some specified data from \ccode{obj} and returns it |
| 1173 |
directly. Because no error code can be returned, a \ccode{\_Get} |
| 1174 |
call must be a simple access call within the object, guaranteed to |
| 1175 |
succeed. \ccode{\_Get()} methods may often be implemented as macros. |
| 1176 |
(\ccode{\_Read} or \ccode{\_Fetch} interfaces are for more complex |
| 1177 |
access methods that might fail, and require an error code return.) |
| 1178 |
|
| 1179 |
\hypertarget{ifc:Read} |
| 1180 |
{\item[\_Read*(obj, ..., \&ret\_value)]} |
| 1181 |
|
| 1182 |
Retrieves some specified data from \ccode{obj} and puts it in |
| 1183 |
\ccode{ret\_value}, where caller has provided (and already allocated, |
| 1184 |
if needed) the space for \ccode{ret\_value}. |
| 1185 |
|
| 1186 |
\hypertarget{ifc:Fetch} |
| 1187 |
{\item[\_Fetch*(obj, ..., \&ret\_value)]} |
| 1188 |
|
| 1189 |
Retrieves some specified data from \ccode{obj} and puts it in |
| 1190 |
\ccode{ret\_value}, where space for the returned value is allocated by |
| 1191 |
the function. Caller becomes responsible for free'ing that space. |
| 1192 |
|
| 1193 |
\hypertarget{ifc:Set} |
| 1194 |
{\item[\_Set*(obj, value)]} |
| 1195 |
|
| 1196 |
Sets some value(s) in \ccode{obj} to \ccode{value}. If a value was |
| 1197 |
already set, it is replaced with the new one. If any memory needs to |
| 1198 |
be reallocated or free'd, this is done. \ccode{\_Set} functions have |
| 1199 |
some appropriate longer name, like \ccode{\_SetZero()} (set something |
| 1200 |
in an object to zero(s)), or \ccode{esl\_dmatrix\_SetIdentity()} (set |
| 1201 |
a dmatrix to an identity matrix). |
| 1202 |
|
| 1203 |
\hypertarget{ifc:Format} |
| 1204 |
{\item[\_Format*(obj, fmtstring, ...)]} |
| 1205 |
|
| 1206 |
Like \ccode{\_Set}, but with \ccode{sprintf()}-style semantics. Sets |
| 1207 |
some string value in \ccode{obj} according to the |
| 1208 |
\ccode{sprintf()}-style \ccode{fmtstring} and any subsequence |
| 1209 |
\ccode{sprintf()}-style arguments. If a value was already set, it is |
| 1210 |
replaced with the new one. If any memory needs to be reallocated or |
| 1211 |
free'd, this is done. \ccode{\_Format} functions have some |
| 1212 |
appropriate longer name, like |
| 1213 |
\ccode{esl\_msa\_FormatSeqDescription()}. |
| 1214 |
|
| 1215 |
Because \ccode{fmtstring} is a \ccode{printf()}-style format string, |
| 1216 |
it must not contain '\%' characters. \ccode{\_Format*} functions |
| 1217 |
should only be used with format strings set by a program; they should |
| 1218 |
not be used to copy user input that might contain '\%' characters. |
| 1219 |
\end{sreapi} |
| 1220 |
|
| 1221 |
|
| 1222 |
%%%%%%%%%%%%%%%% |
| 1223 |
\subsection{Debugging, testing, development} |
| 1224 |
%%%%%%%%%%%%%%%% |
| 1225 |
|
| 1226 |
\begin{sreapi} |
| 1227 |
\hypertarget{ifc:Validate} |
| 1228 |
{\item[\_Validate*(obj, errbuf...)]} |
| 1229 |
|
| 1230 |
Checks that the internals of \ccode{obj} are all right. Returns |
| 1231 |
\ccode{eslOK} if they are, and returns \ccode{eslFAIL} if they |
| 1232 |
aren't. Additionally, if the caller provides a non-\ccode{NULL} |
| 1233 |
message buffer \ccode{errbuf}, on failure, an informative message |
| 1234 |
describing the reason for the failure is formatted and left in |
| 1235 |
\ccode{errbuf}. If the caller provides this message buffer, it must |
| 1236 |
allocate it for at least \ccode{eslERRBUFSIZE} characters. |
| 1237 |
|
| 1238 |
Failures in \ccode{\_Validate()} routines are handled by |
| 1239 |
\ccode{ESL\_FAIL()} (or \ccode{ESL\_XFAIL()}, if the validation |
| 1240 |
routine needs to do any memory cleanup). Validation failures are |
| 1241 |
classified as normal (returned) errors so that \ccode{\_Validate()} |
| 1242 |
routines can be used in production code -- for example, to validate |
| 1243 |
user input. |
| 1244 |
|
| 1245 |
At the same time, because the \ccode{ESL\_FAIL()} and |
| 1246 |
\ccode{ESL\_XFAIL()} macros call the stub \ccode{esl\_fail()}, you can |
| 1247 |
set a debugging breakpoint on \ccode{esl\_fail} to get a |
| 1248 |
\ccode{\_Validate()} routine fail immediately at whatever test |
| 1249 |
failed. |
| 1250 |
|
| 1251 |
The \ccode{errbuf} message therefore can be coarse-grained |
| 1252 |
(``validation of object X failed'') or fine-grained (``in object X, |
| 1253 |
data element Y fails test Z''). A validation of user input (which we |
| 1254 |
expect to fail often) should be fine-grained, to return maximally |
| 1255 |
useful information about what the user did wrong. A validation of |
| 1256 |
internal data can be very coarse-grained, knowing that a developer can |
| 1257 |
simply set a breakpoint in \ccode{esl\_fail()} to get at exactly where |
| 1258 |
a validation failed. |
| 1259 |
|
| 1260 |
A \ccode{\_Validate()} function is not intended to test all possible |
| 1261 |
invalid states of an object, even if that were feasible. Rather, the |
| 1262 |
goal is to automatically catch future problems we've already seen in |
| 1263 |
past debugging and testing. So a \ccode{\_Validate()} function is a |
| 1264 |
place to systematically organize a set of checks that essentially |
| 1265 |
amount to regression tests against past debugging/testing efforts. |
| 1266 |
|
| 1267 |
\hypertarget{ifc:Compare} |
| 1268 |
{\item[\_Compare*(obj1, obj2...)]} |
| 1269 |
|
| 1270 |
Compares \ccode{obj1} to \ccode{obj2}. Returns \ccode{eslOK} if the |
| 1271 |
contents are judged to be identical, and \ccode{eslFAIL} if they |
| 1272 |
differ. When the comparison involves floating point scalar |
| 1273 |
comparisons, a fractional tolerance argument \ccode{tol} is also |
| 1274 |
passed. |
| 1275 |
|
| 1276 |
Failures in \ccode{\_Compare()} functions are handled by |
| 1277 |
\ccode{ESL\_FAIL()} (or \ccode{ESL\_XFAIL()}, if the validation |
| 1278 |
routine needs to do any memory cleanup), because they may be used in a |
| 1279 |
context where a ``failure'' is expected; for example, when using |
| 1280 |
\ccode{esl\_dmatrix\_Compare()} as a test for successful convergence |
| 1281 |
of a matrix algebra routine. |
| 1282 |
|
| 1283 |
However, the main use of \ccode{\_Compare()} functions is in unit |
| 1284 |
tests. During debugging and development, we want to see exactly where |
| 1285 |
a comparison failed, and we don't want to have to write a bunch |
| 1286 |
laboriously informative error messages to get that information. |
| 1287 |
Instead we can exploit the fact that the \ccode{ESL\_FAIL()} and |
| 1288 |
\ccode{ESL\_XFAIL()} macros call the stub \ccode{esl\_fail()}; you can |
| 1289 |
set a debugging breakpoint in \ccode{esl\_fail()} to stop execution in |
| 1290 |
the failure macros. |
| 1291 |
|
| 1292 |
\hypertarget{ifc:Dump} |
| 1293 |
{\item[\_Dump*(FILE *fp, obj...)]} |
| 1294 |
|
| 1295 |
Prints the internals of an object in human-readable, easily parsable |
| 1296 |
tabular ASCII form. Useful during debugging and development to view |
| 1297 |
the entire object at a glance. Returns \ccode{eslOK} on success. |
| 1298 |
Unlike a more robust \ccode{\_Write()} call, \ccode{\_Dump()} call may |
| 1299 |
assume that all its writes will succeed, and does not need to check |
| 1300 |
return status of \ccode{fprintf()} or other system calls, because it |
| 1301 |
is not intended for production use. |
| 1302 |
|
| 1303 |
|
| 1304 |
\hypertarget{ifc:TestSample} |
| 1305 |
{\item[\_TestSample(ESL\_RANDOMNESS *rng, ..., OBJTYPE **ret\_obj)]} |
| 1306 |
|
| 1307 |
Create an object filled with randomly sampled values for all data |
| 1308 |
elements. The aim is to exercise valid values and ranges, and |
| 1309 |
presence/absence of optional information and allocations, but not to |
| 1310 |
obsess about internal semantic consistency. For example, we use |
| 1311 |
\ccode{\_TestSample()} calls in testing MPI send/receive |
| 1312 |
communications routines, where we don't care so much about the meaning |
| 1313 |
of the object's contents, as we do about faithful transmission of any |
| 1314 |
object with valid contents. |
| 1315 |
|
| 1316 |
A \ccode{\_TestSample()} call produces an object that is sufficiently |
| 1317 |
valid for other debugging tools, including \ccode{\_Dump()}, |
| 1318 |
\ccode{\_Compare()}, and \ccode{\_Validate()}. However, because |
| 1319 |
elements may be randomly sampled independently, in ways that don't |
| 1320 |
respect interdependencies, the object may contain data inconsistencies |
| 1321 |
that make the object invalid for other purposes. Contrast |
| 1322 |
\ccode{\_Sample()} routines, which generate fully valid objects for |
| 1323 |
all purposes, but which may not exercise the object's fields as |
| 1324 |
thoroughly. |
| 1325 |
|
| 1326 |
\end{sreapi} |
| 1327 |
|
| 1328 |
%%%%%%%%%%%%%%%% |
| 1329 |
\subsection{Miscellaneous other interfaces} |
| 1330 |
%%%%%%%%%%%%%%%% |
| 1331 |
|
| 1332 |
\begin{sreapi} |
| 1333 |
\hypertarget{ifc:Write} |
| 1334 |
{\item[\_Write(fp, obj)]} |
| 1335 |
Writes something from an object to an output stream \ccode{fp}. Used |
| 1336 |
for exporting and saving files in official data exchange formats. |
| 1337 |
\ccode{\_Write()} functions must be robust to system write errors, |
| 1338 |
such as filling or unexpectedly disconnecting a disk. They must check |
| 1339 |
return status of all system calls, and throw an \ccode{eslEWRITE} |
| 1340 |
error on any failures. |
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
\hypertarget{ifc:Encode} |
| 1346 |
{\item[code = \_Encode*(char *s)]} |
| 1347 |
|
| 1348 |
Given a string \ccode{<s>}, match it case-insensitively against a list |
| 1349 |
of possible string values and convert this visible representation to |
| 1350 |
its internal \ccode{\#define} or \ccode{enum} code. For example, |
| 1351 |
\ccode{esl\_sqio\_EncodeFormat("fasta")} returns |
| 1352 |
\ccode{eslSQFILE\_FASTA}. If the string is not recognized, returns a |
| 1353 |
code signifying ``unknown''. This needs to be a normal return (not a |
| 1354 |
thrown error) because the string might come from user input, and might |
| 1355 |
be invalid. |
| 1356 |
|
| 1357 |
|
| 1358 |
\hypertarget{ifc:Decode} |
| 1359 |
{\item[char *s = \_Decode*(int code)]} |
| 1360 |
|
| 1361 |
Given an internal code (an \ccode{enum} or \ccode{\#define} constant), |
| 1362 |
return a pointer to an informative string value, for diagnostics and |
| 1363 |
other output. The string is static. If the code is not recognized, |
| 1364 |
throws an \ccode{eslEINVAL} exception and returns \ccode{NULL}. |
| 1365 |
|
| 1366 |
\end{sreapi} |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1374 |
\section{Writing unit tests} |
| 1375 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1376 |
|
| 1377 |
An Easel test driver runs a set of individual unit tests one after |
| 1378 |
another. Sometimes there is one unit test assigned to each exposed |
| 1379 |
function in the API. Sometimes, it makes sense to test several exposed |
| 1380 |
functions in a single unit test function. |
| 1381 |
|
| 1382 |
A unit test for \ccode{esl\_foo\_Baz()} is named \ccode{static void |
| 1383 |
utest\_Baz()}. |
| 1384 |
|
| 1385 |
Upon success, unit tests return void. |
| 1386 |
|
| 1387 |
Upon any failure, a unit test calls \ccode{esl\_fatal()} with an error |
| 1388 |
message, and terminates. It should not use any other error-catching |
| 1389 |
mechanism. It aids debugging if the test program terminates |
| 1390 |
immediately, using a single function that we can easily breakpoint at |
| 1391 |
(\ccode{break esl\_fatal} in GDB). It must not use \ccode{abort()}, |
| 1392 |
for example, because this will screw up the output of scripts running |
| 1393 |
automated tests in \ccode{make check} and \ccode{make dcheck}, such as |
| 1394 |
\emcode{sqc}. \emcode{sqc} traps \ccode{stderr} from |
| 1395 |
\ccode{esl\_fatal()} correctly. A unit test must not use |
| 1396 |
\ccode{exit(1)} either, because that leaves no error message, so |
| 1397 |
someone running a test program on the command line can't easily tell |
| 1398 |
that it failed. |
| 1399 |
|
| 1400 |
Unit tests should attempt to deliberately generate exceptions and |
| 1401 |
failures, and test that the appropriate error code is returned. Unit |
| 1402 |
tests must temporarily register a nonfatal error handler when testing |
| 1403 |
exceptions. |
| 1404 |
|
| 1405 |
Every function, procedure, and macro in the exposed API shall be |
| 1406 |
tested by one or more unit tests. The unit tests aim for complete code |
| 1407 |
coverage. This is measured by code coverage tests using \ccode{gcov}. |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
%%%%%%%%%%%%%%%% |
| 1412 |
\subsection{Dealing with expected stochastic failures in unit tests} |
| 1413 |
%%%%%%%%%%%%%%%% |
| 1414 |
|
| 1415 |
Many unit tests are based on statistical samples and/or random number |
| 1416 |
generation. For example, we test a maximum likelihood parameter |
| 1417 |
fitting routine by fitting to samples generated with known parameters, |
| 1418 |
and testing that the estimated parameters are close enough to the true |
| 1419 |
parameters. The trouble is defining ``close enough''. There may be a |
| 1420 |
small but finite probability that such a test will fail. I call these |
| 1421 |
``stochastic failures''. We don't want tests to fail due to expected |
| 1422 |
statistical deviations, but neither do we want to set p-values so |
| 1423 |
loose that a flaw escapes notice. |
| 1424 |
|
| 1425 |
Current Easel strategy is to have such unit tests reinitialize the RNG |
| 1426 |
to a predetermined fixed seed known to work. Optionally, the test can |
| 1427 |
be made to use the RNG without reinitialization (therefore allowing |
| 1428 |
stochastic failures to occur), with a \ccode{-x} option to the test |
| 1429 |
driver. |
| 1430 |
% example: esl_mixdchlet |
| 1431 |
|
| 1432 |
In the test driver, these unit tests need to be run last; unit tests |
| 1433 |
that don't have a stochastic failure mode are run first. This is so |
| 1434 |
the \ccode{-s <seed>} option for setting the RNG seed takes effect |
| 1435 |
properly. (Otherwise, having a unit test reset the RNG seed would |
| 1436 |
override the \ccode{-s <seed>} setting.} |
| 1437 |
|
| 1438 |
Otherwise the default for \ccode{<seed>} should be 0, so all other |
| 1439 |
tests are randomized from run to run. |
| 1440 |
|
| 1441 |
In some older Easel code, fixed RNG seeds are used for tests that can |
| 1442 |
stochastically fail. The newer approach is preferable because it gives |
| 1443 |
more fine-grained control - only some utests need to deal with |
| 1444 |
stochastic failure, not all of them. |
| 1445 |
|
| 1446 |
%%%%%%%%%%%%%%%% |
| 1447 |
\subsection{Using temporary files in unit tests} |
| 1448 |
%%%%%%%%%%%%%%%% |
| 1449 |
|
| 1450 |
If a unit test or testdriver needs to create a named temporary file |
| 1451 |
(to test i/o), the tmpfile is created with |
| 1452 |
\ccode{esl\_tmpfile\_named()}: |
| 1453 |
|
| 1454 |
\begin{cchunk} |
| 1455 |
char tmpfile[16] = "esltmpXXXXXX"; |
| 1456 |
FILE *fp; |
| 1457 |
|
| 1458 |
if (esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal("failed to create tmpfile"); |
| 1459 |
write_stuff_to(fp); |
| 1460 |
fclose(fp); |
| 1461 |
|
| 1462 |
if ((fp = fopen(tmpfile)) == NULL) esl_fatal("failed to open tmpfile"); |
| 1463 |
read_stuff_from(fp); |
| 1464 |
fclose(fp); |
| 1465 |
|
| 1466 |
remove(tmpfile); |
| 1467 |
\end{cchunk} |
| 1468 |
|
| 1469 |
Thus tmp files created by Easel's test suite have a common naming |
| 1470 |
convention, and are put in the current working directory. On a test |
| 1471 |
failure, the tmp file remains, to assist debugging; on a test success, |
| 1472 |
the tmp file is removed. The \ccode{make clean} targets in Makefiles |
| 1473 |
are looking to remove files matching the target \ccode{esltmp??????}. |
| 1474 |
|
| 1475 |
It is important to declare it as \ccode{char tmpfile[16]} rather than |
| 1476 |
\ccode{char *tmpfile}. Compilers are allowed to treat the string in a |
| 1477 |
\ccode{char *foo = "bar"} initialization as a read-only constant. |
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1484 |
\section{Easel development environment; using development tools} |
| 1485 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1486 |
|
| 1487 |
Easel is developed primarily on GNU/Linux and Mac OS/X systems with |
| 1488 |
the following tools installed: |
| 1489 |
|
| 1490 |
\begin{tabular}{ll} |
| 1491 |
{\bfseries Tool} & {\bfseries Use} \\ |
| 1492 |
\emcode{emacs} & editor \\ |
| 1493 |
\emcode{gcc} & GNU compiler \\ |
| 1494 |
\emcode{icc} & Intel compiler \\ |
| 1495 |
\emcode{gdb} & debugger\\ |
| 1496 |
\emcode{autoconf} & platform-independent configuration manager, Makefile generator\\ |
| 1497 |
\emcode{make} & build/compilation management\\ |
| 1498 |
\emcode{valgrind} & memory bounds and leak checking\\ |
| 1499 |
\emcode{gcov} & code coverage analysis\\ |
| 1500 |
\emcode{gprof} & profiling and optimization (GNU)\\ |
| 1501 |
\emcode{shark} & profiling and optimization (Mac OS/X)\\ |
| 1502 |
\LaTeX & documentation typesetting\\ |
| 1503 |
Subversion & revision control\\ |
| 1504 |
Bourne shell (\ccode{/bin/sh}) & scripting\\ |
| 1505 |
Perl & scripting\\ |
| 1506 |
\end{tabular} |
| 1507 |
|
| 1508 |
Most of these are standard and well-known. The following sections |
| 1509 |
describe some Easel work patterns with some of the less commonly used |
| 1510 |
tools. |
| 1511 |
|
| 1512 |
%%%%%%%%%%%%%%%% |
| 1513 |
\subsection{Using valgrind to find memory leaks and more} |
| 1514 |
%%%%%%%%%%%%%%%% |
| 1515 |
|
| 1516 |
We use \emcode{valgrind} to check for memory leaks and other problems, |
| 1517 |
especially on the unit tests: |
| 1518 |
|
| 1519 |
\begin{cchunk} |
| 1520 |
% valgrind ./esl_buffer_utest |
| 1521 |
\end{cchunk} |
| 1522 |
|
| 1523 |
The \ccode{valgrind\_report.pl} script in \ccode{testsuite} automates |
| 1524 |
valgrind testing for all Easel modules. To run it: |
| 1525 |
|
| 1526 |
\begin{cchunk} |
| 1527 |
% cd testsuite |
| 1528 |
% ./valgrind_report.pl > valgrind.report |
| 1529 |
\end{cchunk} |
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
%%%%%%%%%%%%%%%% |
| 1535 |
\subsection{Using gcov to measure unit test code coverage} |
| 1536 |
%%%%%%%%%%%%%%%% |
| 1537 |
|
| 1538 |
We use \emcode{gcov} to measure code coverage of our unit |
| 1539 |
testing. \emcode{gcov} works best with unoptimized code. The code |
| 1540 |
must be compiled with \emcode{gcc} and it needs to be compiled with |
| 1541 |
\ccode{-fprofile-arcs -ftest-coverage}. The configure script knows |
| 1542 |
about this: give it the \ccode{--enable-gcov} option. An example: |
| 1543 |
|
| 1544 |
\begin{cchunk} |
| 1545 |
% make distclean |
| 1546 |
% ./configure --enable-gcov |
| 1547 |
% make esl_buffer_utest |
| 1548 |
% ./esl_buffer_utest |
| 1549 |
% gcov esl_buffer.c |
| 1550 |
File 'esl_buffer.c' |
| 1551 |
Lines executed:73.85% of 589 |
| 1552 |
esl_buffer.c:creating 'esl_buffer.c.gcov' |
| 1553 |
% emacs esl_buffer.c.gcov |
| 1554 |
\end{cchunk} |
| 1555 |
|
| 1556 |
The file \ccode{esl\_buffer.c.gcov} contains an annotated source listing |
| 1557 |
of the \ccode{.c} file, showing which lines were and weren't covered |
| 1558 |
by the test suite. |
| 1559 |
|
| 1560 |
The \ccode{coverage\_report.pl} script in \ccode{testsuite} automates coverage |
| 1561 |
testing for all Easel modules. To run it: |
| 1562 |
|
| 1563 |
\begin{cchunk} |
| 1564 |
% cd testsuite |
| 1565 |
% coverage_report.pl > coverage.report |
| 1566 |
\end{cchunk} |
| 1567 |
|
| 1568 |
|
| 1569 |
%%%%%%%%%%%%%%%% |
| 1570 |
\subsection{Using gprof for performance profiling} |
| 1571 |
%%%%%%%%%%%%%%%% |
| 1572 |
|
| 1573 |
On a Linux machine (gprof does not work on Mac OS/X, apparently): |
| 1574 |
|
| 1575 |
\begin{cchunk} |
| 1576 |
% make distclean |
| 1577 |
% ./configure --enable-gprof |
| 1578 |
% make |
| 1579 |
\end{cchunk} |
| 1580 |
|
| 1581 |
Run any program you want to profile, then: |
| 1582 |
|
| 1583 |
\begin{cchunk} |
| 1584 |
% gprof -l <progname> |
| 1585 |
\end{cchunk} |
| 1586 |
|
| 1587 |
%%%%%%%%%%%%%%%% |
| 1588 |
\subsection{Using the clang static analyzer, checker} |
| 1589 |
%%%%%%%%%%%%%%%% |
| 1590 |
|
| 1591 |
The clang static analyzer for Mac OS/X is at |
| 1592 |
\url{http://clang-analyzer.llvm.org/}. I install it by moving its |
| 1593 |
entire distro directory (checker-276, for example) to |
| 1594 |
\ccode{/usr/local}, and symlinking to \ccode{checker}. |
| 1595 |
My \ccode{bashrc} has: |
| 1596 |
|
| 1597 |
\begin{cchunk} |
| 1598 |
test -d /usr/local/checker && PATH=${PATH}:/usr/local/checker |
| 1599 |
\end{cchunk} |
| 1600 |
|
| 1601 |
and that puts \prog{scan-build} in my \ccode{PATH}. |
| 1602 |
|
| 1603 |
To use it: |
| 1604 |
|
| 1605 |
\begin{cchunk} |
| 1606 |
% scan-build ./configure --enable-debugging |
| 1607 |
% scan-build make |
| 1608 |
\end{cchunk} |
| 1609 |
|
| 1610 |
It'll give you a scan-view command line, including the name of its |
| 1611 |
output html file, so you can then visualize and interact with the |
| 1612 |
results: |
| 1613 |
|
| 1614 |
\begin{cchunk} |
| 1615 |
% scan-view /var/folders/blah/baz/foo |
| 1616 |
\end{cchunk} |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1624 |
\section{Documentation} |
| 1625 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1626 |
|
| 1627 |
%%%%%%%%%%%%%%%% |
| 1628 |
\subsection{Structured function headers read by autodoc} |
| 1629 |
%%%%%%%%%%%%%%%% |
| 1630 |
The documentation for Easel's functions is embedded in the source code |
| 1631 |
itself, rather than being in separate files. A homegrown documentation |
| 1632 |
extraction tool (\prog{autodoc}) is used to process the source files |
| 1633 |
and extract and format the documentation. |
| 1634 |
|
| 1635 |
An important part of the documentation is the documentation for |
| 1636 |
individual functions. Each Easel function is preceded by |
| 1637 |
documentation in the form of a structured comment header that is |
| 1638 |
parsed by \prog{autodoc}. For example: |
| 1639 |
|
| 1640 |
\input{cexcerpts/function_comment_example} |
| 1641 |
|
| 1642 |
\prog{autodoc} can do one of three things with the text that follows |
| 1643 |
these tags: it can ignore it, use it verbatim, or process |
| 1644 |
it. \esldef{Ignored} text is documentation that resides only in the |
| 1645 |
source code, like the incept date and the notebook |
| 1646 |
crossreferences.\footnote{Eventually, we will probably process the |
| 1647 |
\ccode{Args:} part of the header, but for now it is ignored.} |
| 1648 |
\esldef{Verbatim} text is picked up by \prog{autodoc} and formatted as |
| 1649 |
\verb+\ccode{}+ in the \LaTeX\ documentation. \esldef{Processed} text |
| 1650 |
is interpeted as \LaTeX\ code, with a special addition that angle |
| 1651 |
brackets are used to enclose C code words, such as the argument names. |
| 1652 |
\prog{autodoc} recognizes the angle brackets and formats the enclosed |
| 1653 |
text as \verb+\ccode{}+. Unprotected underscore characters are |
| 1654 |
allowed inside these angle brackets; \prog{autodoc} protects them |
| 1655 |
appropriately when it generates the \LaTeX. Citations, such as |
| 1656 |
\verb+\citep{MolerVanLoan03}+, are formatted for the \LaTeX\ |
| 1657 |
\verb+natbib+ package. |
| 1658 |
|
| 1659 |
The various fields are: |
| 1660 |
|
| 1661 |
\begin{sreitems}{\textbf{Function:}} |
| 1662 |
\item[\textbf{Function:}] |
| 1663 |
The name of the function. \prog{autodoc} uses this line to |
| 1664 |
determine that it's supposed to generate a documentation entry here. |
| 1665 |
\prog{autodoc} checks that it matches the name of the immediately |
| 1666 |
following C function. One line; verbatim; required. |
| 1667 |
|
| 1668 |
\item[\textbf{Synopsis:}] |
| 1669 |
A short one-line summary of the function. \ccode{autodoc -t} uses this |
| 1670 |
line to generate the API summary tables that appear in this guide. |
| 1671 |
One line; processed; not required for \prog{autodoc} itself, but |
| 1672 |
required by \ccode{autodoc -t}. |
| 1673 |
|
| 1674 |
\item[\textbf{Incept:}] Records the author/date of first |
| 1675 |
draft. \prog{autodoc} doesn't use this line. Used to help track |
| 1676 |
development history. The definition of ``incept'' is often fuzzy, |
| 1677 |
because Easel is a palimpsest of rewritten code. This line often |
| 1678 |
also includes a location, such as \ccode{[AA 673 over Greenland]}, |
| 1679 |
for no reason other than to remember how many weird places I've |
| 1680 |
managed to get work done in.. |
| 1681 |
|
| 1682 |
\item[\textbf{Purpose:}] The main body. \prog{autodoc} processes this |
| 1683 |
to produce the \TeX documentation. It explains the purpose of the |
| 1684 |
function, then precisely defines what the caller must provide in |
| 1685 |
each input argument, and what the caller will get back in each |
| 1686 |
output argument. It should be written and referenced as if it will |
| 1687 |
appear in the user guide (because it will). Multiline; processed by |
| 1688 |
\prog{autodoc}; required. |
| 1689 |
|
| 1690 |
\item[\textbf{Args:}] A tabular-ish summary of each argument. Not |
| 1691 |
picked up by \prog{autodoc}, at least not at present. The |
| 1692 |
\ccode{Purpose:} section instead documents each option in free text. |
| 1693 |
Multiline and tabular-ish; ignored by \prog{autodoc}; optional. |
| 1694 |
|
| 1695 |
\item[\textbf{Returns:}] The possible return values from the function, |
| 1696 |
starting with what happens on successful completion (usually, return |
| 1697 |
of an \ccode{eslOK} code). Also indicates codes for unsuccessful |
| 1698 |
calls that are normal (returned) errors. If there are output |
| 1699 |
argument pointers, documents what they will contain upon successful |
| 1700 |
and unsuccessful return, and whether any of the output involved |
| 1701 |
allocating memory that the caller must free. |
| 1702 |
|
| 1703 |
\item[\textbf{Throws:}] The possible exceptions thrown by the |
| 1704 |
function, listing what a program that's handling its own exceptions |
| 1705 |
will have to deal with. (Programs should never assume that this list |
| 1706 |
is complete.) Programs that are letting Easel handle exceptions do |
| 1707 |
not have to worry about any of the thrown codes. The state of |
| 1708 |
output argument pointers is documented -- generally, all output is |
| 1709 |
set to \ccode{NULL} or \ccode{0} values when exceptions happen. |
| 1710 |
After a thrown exception, there is never any memory allocation in |
| 1711 |
output pointers that the caller must free. |
| 1712 |
|
| 1713 |
\item[\textbf{Xref:}] Crossreferences to notebooks (paper or |
| 1714 |
electronic) and to literature, to help track the history of the |
| 1715 |
function's development and rationale.\footnote{A typical reference |
| 1716 |
to one of SRE's notebooks is \ccode{STL10/143}, indicating St. Louis |
| 1717 |
notebook 10, page 143.} Personal developer notebooks are of course |
| 1718 |
not immediately available to all developers (especially bound paper |
| 1719 |
ones) but still, these crossreferences can be traced if necessary. |
| 1720 |
\end{sreitems} |
| 1721 |
|
| 1722 |
\subsection{cexcerpt - extracting C source snippets} |
| 1723 |
|
| 1724 |
The \prog{cexcerpt} program extracts snippets of C code verbatim from |
| 1725 |
Easel's C source files. |
| 1726 |
|
| 1727 |
The \ccode{documentation/Makefile} runs \prog{cexcerpt} on every |
| 1728 |
module .c and .h file. The extracted cexcerpts are placed in .tex |
| 1729 |
files in the temporary \ccode{cexcerpts/} subdirectory. |
| 1730 |
|
| 1731 |
Usage: \ccode{cexcerpt <file.c> <dir>}. Processes C source file |
| 1732 |
\ccode{file.c}; extracts all tagged excerpts, and puts them in a file |
| 1733 |
in directory \ccode{<dir>}. |
| 1734 |
|
| 1735 |
An excerpt is marked with special comments in the C file: |
| 1736 |
\begin{cchunk} |
| 1737 |
/*::cexcerpt::my_example::begin::*/ |
| 1738 |
while (esl_sq_Read(sqfp, sq) == eslOK) |
| 1739 |
{ n++; } |
| 1740 |
/*::cexcerpt::my_example::end::*/ |
| 1741 |
\end{cchunk} |
| 1742 |
|
| 1743 |
The cexcerpt marker's format is \ccode{::cexcerpt::<tag>::begin::} (or |
| 1744 |
end). A comment containing a cexcerpt marker must be the first text on |
| 1745 |
the source line. A cexcerpt comment may be followed on the line by |
| 1746 |
whitespace or a second comment. |
| 1747 |
|
| 1748 |
The \ccode{<tag>} is used to construct the file name, as |
| 1749 |
\ccode{<tag>.tex}. In the example, the tag \ccode{my\_example} creates |
| 1750 |
a file \ccode{my\_example.tex} in \ccode{<dir>}. |
| 1751 |
|
| 1752 |
All the text between the cexcerpt markers is put in the file. In |
| 1753 |
addition, this text is wrapped in a \ccode{cchunk} environment. This |
| 1754 |
file can then be included in a \LaTeX\ file. |
| 1755 |
|
| 1756 |
For best results, the C source should be free of TAB characters. |
| 1757 |
"M-x untabify" on the region to clean them out. |
| 1758 |
|
| 1759 |
Cexcerpts can't overlap or nest in any way in the C file. Only one tag |
| 1760 |
can be active at a time. |
| 1761 |
|
| 1762 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1763 |
\section{The .tex file} |
| 1764 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1770 |
\section{Portability notes} |
| 1771 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1772 |
|
| 1773 |
Easel is intended to be widely portable. We adhere to the ANSI C99 |
| 1774 |
standard. Any dependency on higher-level functionality (including |
| 1775 |
POSIX, X/Open, or system-specific stuff) is optional, and Easel is |
| 1776 |
capable of working around its absence at compile-time. |
| 1777 |
|
| 1778 |
Although we do not currently include Windows machines in our |
| 1779 |
development environment, we are planning for the day when we do. Easel |
| 1780 |
should not include any required UNIX-specific code that wouldn't port |
| 1781 |
to Windows.\footnote{Though it probably does, which we'll discover |
| 1782 |
when we first try to compile for Windows.} |
| 1783 |
|
| 1784 |
|
| 1785 |
% xref J7/83. |
| 1786 |
\paragraph{Why not define \ccode{\_POSIX\_C\_SOURCE}?} You might think |
| 1787 |
it would be a good idea to define \ccode{\_POSIX\_C\_SOURCE} to |
| 1788 |
\ccode{200112L} or some such, to try to enforce the portability of our |
| 1789 |
POSIX-dependent code. This doesn't work; don't do it. According to |
| 1790 |
the standards, if you define \ccode{\_POSIX\_C\_SOURCE}, the host must |
| 1791 |
\emph{disable} anything that's \emph{not} in the POSIX |
| 1792 |
standard. However, Easel \emph{is} allowed to optionally use |
| 1793 |
system-dependent non-POSIX code. A good example is |
| 1794 |
\ccode{esl\_threads.c::esl\_threads\_CPUCount()}. There is no |
| 1795 |
POSIX-compliant way to check for the number of available processors on |
| 1796 |
a system.\footnote{Apparently the POSIX threads standards committee |
| 1797 |
intends it that way; see |
| 1798 |
\url{http://ansi.c.sources.free.fr/threads/butenhof.txt}.} |
| 1799 |
Easel's implementation tries to find one of several system-specific |
| 1800 |
alternatives, including the non-POSIX function \ccode{sysctl{}}. |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|