Skip to content

Commit

Permalink
Adds esl_arr2, esl_arr3, deprecating esl_Free2D(), esl_Free3D().
Browse files Browse the repository at this point in the history
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 f8e929c
Show file tree
Hide file tree
Showing 19 changed files with 434 additions and 160 deletions.
6 changes: 5 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down Expand Up @@ -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\
Expand Down
20 changes: 20 additions & 0 deletions easel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions easel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
57 changes: 57 additions & 0 deletions esl_arr2.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions esl_arr2.h
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions esl_arr3.c
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions esl_arr3.h
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions esl_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions esl_keyhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit f8e929c

Please sign in to comment.