Skip to content

Commit

Permalink
Allow compilation under cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
talaviram committed Aug 7, 2019
1 parent 8d472e2 commit cfb5dbd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ typedef long int32_t ;

#define MAKE_MAGIC(a,b,c,d,e,f) ((a) + ((b) << 4) + ((c) << 8) + ((d) << 12) + ((e) << 16) + ((f) << 20))

/*
** Adds casting needed if compiled/included within cpp
*/
#ifdef __cplusplus
#define ZERO_ALLOC(type, size) static_cast<type*>(calloc(1, size))
#else // __cplusplus
#define ZERO_ALLOC(type, size) calloc(1, size)
#endif

/*
** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html
*/
Expand Down
4 changes: 2 additions & 2 deletions src/samplerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ src_new (int converter_type, int channels, int *error)
return NULL ;
} ;

if ((psrc = calloc (1, sizeof (*psrc))) == NULL)
if ((psrc = ZERO_ALLOC (SRC_PRIVATE, sizeof (*psrc))) == NULL)
{ if (error)
*error = SRC_ERR_MALLOC_FAILED ;
return NULL ;
Expand Down Expand Up @@ -62,7 +62,7 @@ src_clone (SRC_STATE* orig, int *error)
if (error)
*error = SRC_ERR_NO_ERROR ;

if ((psrc = calloc (1, sizeof (*psrc))) == NULL)
if ((psrc = ZERO_ALLOC (SRC_PRIVATE, sizeof (*psrc))) == NULL)
{ if (error)
*error = SRC_ERR_MALLOC_FAILED ;
return NULL ;
Expand Down
4 changes: 2 additions & 2 deletions src/src_linear.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ linear_set_converter (SRC_PRIVATE *psrc, int src_enum)
} ;

if (psrc->private_data == NULL)
{ priv = calloc (1, sizeof (*priv) + psrc->channels * sizeof (float)) ;
{ priv = ZERO_ALLOC (LINEAR_DATA, sizeof (*priv) + psrc->channels * sizeof (float)) ;
psrc->private_data = priv ;
} ;

Expand Down Expand Up @@ -219,7 +219,7 @@ linear_copy (SRC_PRIVATE *from, SRC_PRIVATE *to)
LINEAR_DATA* from_priv = (LINEAR_DATA*) from->private_data ;
size_t private_size = sizeof (*to_priv) + from_priv->channels * sizeof (float) ;

if ((to_priv = calloc (1, private_size)) == NULL)
if ((to_priv = ZERO_ALLOC (LINEAR_DATA, private_size)) == NULL)
return SRC_ERR_MALLOC_FAILED ;

memcpy (to_priv, from_priv, private_size) ;
Expand Down
4 changes: 2 additions & 2 deletions src/src_sinc.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ sinc_set_converter (SRC_PRIVATE *psrc, int src_enum)
temp_filter.b_len = MAX (temp_filter.b_len, 4096) ;
temp_filter.b_len *= temp_filter.channels ;

if ((filter = calloc (1, sizeof (SINC_FILTER) + sizeof (filter->buffer [0]) * (temp_filter.b_len + temp_filter.channels))) == NULL)
if ((filter = ZERO_ALLOC (SINC_FILTER, sizeof (SINC_FILTER) + sizeof (filter->buffer [0]) * (temp_filter.b_len + temp_filter.channels))) == NULL)
return SRC_ERR_MALLOC_FAILED ;

*filter = temp_filter ;
Expand Down Expand Up @@ -265,7 +265,7 @@ sinc_copy (SRC_PRIVATE *from, SRC_PRIVATE *to)
SINC_FILTER* from_filter = (SINC_FILTER*) from->private_data ;
size_t private_length = sizeof (SINC_FILTER) + sizeof (from_filter->buffer [0]) * (from_filter->b_len + from_filter->channels) ;

if ((to_filter = calloc (1, private_length)) == NULL)
if ((to_filter = ZERO_ALLOC (SINC_FILTER, private_length)) == NULL)
return SRC_ERR_MALLOC_FAILED ;

memcpy (to_filter, from_filter, private_length) ;
Expand Down
4 changes: 2 additions & 2 deletions src/src_zoh.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ zoh_set_converter (SRC_PRIVATE *psrc, int src_enum)
} ;

if (psrc->private_data == NULL)
{ priv = calloc (1, sizeof (*priv) + psrc->channels * sizeof (float)) ;
{ priv = ZERO_ALLOC (ZOH_DATA, sizeof (*priv) + psrc->channels * sizeof (float)) ;
psrc->private_data = priv ;
} ;

Expand Down Expand Up @@ -210,7 +210,7 @@ zoh_copy (SRC_PRIVATE *from, SRC_PRIVATE *to)
ZOH_DATA* from_priv = (ZOH_DATA*) from->private_data ;
size_t private_size = sizeof (*to_priv) + from_priv->channels * sizeof (float) ;

if ((to_priv = calloc (1, private_size)) == NULL)
if ((to_priv = ZERO_ALLOC (ZOH_DATA, private_size)) == NULL)
return SRC_ERR_MALLOC_FAILED ;

memcpy (to_priv, from_priv, private_size) ;
Expand Down

0 comments on commit cfb5dbd

Please sign in to comment.