shithub: libsamplerate

Download patch

ref: 4d5872263236aa13a90352d9ba3b0bb13434a065
parent: 8d472e29e9609d0237274d88b516eb1b9b3ef825
author: Tal Aviram <me@talaviram.com>
date: Fri Jul 19 13:52:14 EDT 2019

Make allocation more C++ friendly.

--- a/src/common.h
+++ b/src/common.h
@@ -33,6 +33,15 @@
 #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
 */
 #ifdef UNUSED
--- a/src/samplerate.c
+++ b/src/samplerate.c
@@ -32,7 +32,7 @@
 		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 ;
@@ -62,7 +62,7 @@
 	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 ;
--- a/src/src_linear.c
+++ b/src/src_linear.c
@@ -171,7 +171,7 @@
 		} ;
 
 	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 ;
 		} ;
 
@@ -219,7 +219,7 @@
 	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) ;
--- a/src/src_sinc.c
+++ b/src/src_sinc.c
@@ -216,7 +216,7 @@
 	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 ;
@@ -265,7 +265,7 @@
 	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) ;
--- a/src/src_zoh.c
+++ b/src/src_zoh.c
@@ -162,7 +162,7 @@
 		} ;
 
 	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 ;
 		} ;
 
@@ -210,7 +210,7 @@
 	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) ;