Skip to content
Permalink
Browse files

Adds esl_arr2, esl_arr3, deprecating esl_Free2D(), esl_Free3D().

Adds esl_free(), which allows ptr to be NULL. This is actually
unnecessary. C99 free() accepts NULL as a no-op.

Adds esl_msa_Sizeof(), which I intend to use to pull some alirecsize
capability into esl-alistat. Also, utest_Sizeof() unit test for it.

[SRE:H4/146]
  • Loading branch information...
cryptogenomicon committed Feb 2, 2018
1 parent 2dffa62 commit f8e929cf3d3aa940f7d74d7392b11935ea01430c
Showing with 434 additions and 160 deletions.
  1. +5 −1 Makefile.in
  2. +20 −0 easel.c
  3. +1 −0 easel.h
  4. +57 −0 esl_arr2.c
  5. +9 −0 esl_arr2.h
  6. +69 −0 esl_arr3.c
  7. +9 −0 esl_arr3.h
  8. +3 −2 esl_distance.c
  9. +7 −4 esl_keyhash.c
  10. +107 −18 esl_msa.c
  11. +3 −3 esl_msa.h
  12. +21 −19 esl_msafile2.c
  13. +18 −17 esl_randomseq.c
  14. +5 −4 esl_ssi.c
  15. +13 −11 esl_tree.c
  16. +6 −5 miniapps/esl-alimanip.c
  17. +17 −15 miniapps/esl-alimask.c
  18. +18 −14 miniapps/esl-alistat.c
  19. +46 −47 miniapps/esl-ssdraw.c
@@ -96,7 +96,8 @@ endif
HDRS = easel.h\
esl_alloc.h\
esl_alphabet.h\
esl_vmx.h\
esl_arr2.h\
esl_arr3.h\
esl_avx.h\
esl_avx512.h\
esl_buffer.h\
@@ -162,13 +163,16 @@ HDRS = easel.h\
esl_threads.h\
esl_tree.h\
esl_vectorops.h\
esl_vmx.h\
esl_weibull.h\
esl_workqueue.h\
esl_wuss.h

OBJS = easel.o\
esl_alloc.o\
esl_alphabet.o\
esl_arr2.o\
esl_arr3.o\
esl_buffer.o\
esl_cluster.o\
esl_composition.o\
20 easel.c
@@ -327,6 +327,22 @@ esl_fatal(const char *format, ...)
* 2. Memory allocation/deallocation conventions.
*****************************************************************/

/* Function: esl_free()
* Synopsis: free(), while allowing ptr to be NULL.
* Incept: SRE, Fri Nov 3 17:12:01 2017
*
* Purpose: Easel uses a convention of initializing ptrs to be NULL
* before allocating them. When cleaning up after errors, a
* routine can check for non-NULL ptrs to know what to
* free(). Easel code is slightly cleaner if we have a
* free() that no-ops on NULL ptrs.
*/
void
esl_free(void *p)
{
if (p) free(p);
}

/* Function: esl_Free2D()
*
* Purpose: Free a 2D pointer array <p>, where first dimension is
@@ -335,6 +351,8 @@ esl_fatal(const char *format, ...)
* sparse arrays.
*
* Returns: void.
*
* DEPRECATED. Replace with esl_arr2_Destroy()
*/
void
esl_Free2D(void **p, int dim1)
@@ -356,6 +374,8 @@ esl_Free2D(void **p, int dim1)
* pointers being NULL, to allow sparse arrays.
*
* Returns: void.
*
* DEPRECATED. Replace with esl_arr3_Destroy()
*/
void
esl_Free3D(void ***p, int dim1, int dim2)
@@ -438,6 +438,7 @@ extern void esl_nonfatal_handler(int errcode, int use_errno, char *sourcefile, i
extern void esl_fatal(const char *format, ...) ESL_ATTRIBUTE_NORETURN;

/* 2. Memory allocation/deallocation conventions. */
extern void esl_free(void *p);
extern void esl_Free2D(void **p, int dim1);
extern void esl_Free3D(void ***p, int dim1, int dim2);

@@ -0,0 +1,57 @@
#include "esl_config.h"

#include <stdlib.h>
#include <string.h>

#include "easel.h"



/* Function: esl_arr2_SSizeof()
* Synopsis: Returns size of a 2D array of \0-terminated strings, in bytes
* Incept: SRE, Fri Nov 3 15:15:56 2017
*/
size_t
esl_arr2_SSizeof(char **s, int dim1)
{
size_t n = 0;
int i;

if (s)
{
n += sizeof(char *) * dim1;
for (i = 0; i < dim1; i++)
if (s[i])
n += sizeof(char) * (1 + strlen(s[i]));
}
return n;
}

/* Function: esl_arr2_Destroy()
* Synopsis: Free a 2D array.
* Incept: SRE, Fri Nov 3 15:32:21 2017
*
* Purpose: Free a 2D pointer array <p>, where first dimension is
* <dim1>. (That is, the array is <p[0..dim1-1][]>.)
* Tolerates any of the pointers being NULL, to allow
* sparse arrays.
*
* Returns: (void)
*
* Throws: (no abnormal error conditions)
*
* Xref: Was <esl_Free2D()>.
*/
void
esl_arr2_Destroy(void **p, int dim1)
{
int i;

if (p)
{
for (i = 0; i < dim1; i++)
if (p[i])
free(p[i]);
free(p);
}
}
@@ -0,0 +1,9 @@
#ifndef eslARR2_INCLUDED
#define eslARR2_INCLUDED

#include <stdlib.h>

extern size_t esl_arr2_SSizeof(char **s, int dim1);
extern void esl_arr2_Destroy(void **p, int dim1);

#endif // eslARR2_INCLUDED
@@ -0,0 +1,69 @@
#include "esl_config.h"

#include <stdlib.h>
#include <string.h>

#include "easel.h"

/* Function: esl_arr3_SSizeof()
* Synopsis: Returns size of 3D array of \0-terminated strings, in bytes
* Incept: SRE, Fri Nov 3 15:19:53 2017
*/
size_t
esl_arr3_SSizeof(char ***s, int dim1, int dim2)
{
size_t n = 0;
int i,j;

if (s)
{
n += sizeof(char **) * dim1;
for (i = 0; i < dim1; i++)
if (s[i])
{
n += sizeof(char *) * dim2;
for (j = 0; j < dim2; j++)
{
if (s[i][j])
n += sizeof(char) * (1 + strlen(s[i][j]));
}
}
}
return n;
}



/* Function: esl_arr3_Destroy()
* Synopsis: Free a 3D array.
* Incept: SRE, Fri Nov 3 15:38:39 2017
*
* Purpose: Free a 3D pointer array <p>, where first and second
* dimensions are <dim1>,<dim2>. (That is, the array is
* <p[0..dim1-1][0..dim2-1][]>.) Tolerates any of the
* pointers being NULL, to allow sparse arrays.
*
* Returns: (void)
*
* Throws: (no abnormal error conditions)
*
* Xref: Was <esl_Free3D()>.
*/
void
esl_arr3_Destroy(void ***p, int dim1, int dim2)
{
int i, j;

if (p)
{
for (i = 0; i < dim1; i++)
if (p[i])
{
for (j = 0; j < dim2; j++)
if (p[i][j])
free(p[i][j]);
free(p[i]);
}
free(p);
}
}
@@ -0,0 +1,9 @@
#ifndef eslARR3_INCLUDED
#define eslARR3_INCLUDED

#include <stdlib.h>

extern size_t esl_arr3_SSizeof(char ***s, int dim1, int dim2);
extern void esl_arr3_Destroy(void ***p, int dim1, int dim2);

#endif // eslARR3_INCLUDED
@@ -1414,6 +1414,7 @@ utest_XJukesCantorMx(ESL_ALPHABET *abc, char **as, ESL_DSQ **ax, int N)
#ifdef eslDISTANCE_TESTDRIVE
#include "easel.h"
#include "esl_alphabet.h"
#include "esl_arr2.h"
#include "esl_dmatrix.h"
#include "esl_getopts.h"
#include "esl_random.h"
@@ -1505,9 +1506,9 @@ main(int argc, char **argv)


esl_randomness_Destroy(r);
esl_Free2D((void **) as, N);
esl_alphabet_Destroy(abc);
esl_Free2D((void **) ax, N);
esl_arr2_Destroy((void **) as, N);
esl_arr2_Destroy((void **) ax, N);
return eslOK;

ERROR:
@@ -145,10 +145,13 @@ esl_keyhash_Sizeof(const ESL_KEYHASH *kh)
{
size_t n = 0;

n += sizeof(ESL_KEYHASH);
n += sizeof(int) * kh->hashsize;
n += sizeof(int) * kh->kalloc * 2;
n += sizeof(char) * kh->salloc;
if (kh)
{
n += sizeof(ESL_KEYHASH);
n += sizeof(int) * kh->hashsize;
n += sizeof(int) * kh->kalloc * 2;
n += sizeof(char) * kh->salloc;
}
return n;
}

0 comments on commit f8e929c

Please sign in to comment.
You can’t perform that action at this time.