shithub: sf2mid

Download patch

ref: 805abd6f66415c22a934fe4e169cc7f1ef1ec681
parent: f199706563056d6af9641858cc83678e9d8e4231
author: Bernhard Schelling <14200249+schellingb@users.noreply.github.com>
date: Sun Nov 14 21:10:01 EST 2021

Avoid one malloc if tsf_copy is never used

--- a/tsf.h
+++ b/tsf.h
@@ -1272,9 +1272,6 @@
 		res->presets = (struct tsf_preset*)TSF_MALLOC(res->presetNum * sizeof(struct tsf_preset));
 		res->fontSamples = fontSamples;
 		res->outSampleRate = 44100.0f;
-		res->outputSampleSize = 0;
-		res->refCount = (int*)TSF_MALLOC(sizeof(int));
-		*res->refCount = 1;
 		fontSamples = TSF_NULL; //don't free below
 		tsf_load_presets(res, &hydra, fontSampleCount);
 	}
@@ -1287,8 +1284,10 @@
 
 TSFDEF tsf* tsf_copy(tsf* f)
 {
-	struct tsf* res;
+	tsf* res;
 	if (!f) return TSF_NULL;
+	if (!f->refCount)
+		*(f->refCount = (int*)TSF_MALLOC(sizeof(int))) = 1;
 	res = (tsf*)TSF_MALLOC(sizeof(tsf));
 	memcpy(res, f, sizeof(tsf));
 	res->voices = TSF_NULL;
@@ -1296,7 +1295,7 @@
 	res->channels = TSF_NULL;
 	res->outputSamples = TSF_NULL;
 	res->outputSampleSize = 0;
-	++(*res->refCount);
+	(*res->refCount)++;
 	return res;
 }
 
@@ -1304,7 +1303,7 @@
 {
 	struct tsf_preset *preset, *presetEnd;
 	if (!f) return;
-	if (--(*f->refCount) == 0)
+	if (!f->refCount || !--(*f->refCount))
 	{
 		for (preset = f->presets, presetEnd = preset + f->presetNum; preset != presetEnd; preset++)
 			TSF_FREE(preset->regions);
@@ -1312,9 +1311,9 @@
 		TSF_FREE(f->fontSamples);
 		TSF_FREE(f->refCount);
 	}
-	TSF_FREE(f->outputSamples);
+	TSF_FREE(f->channels);
 	TSF_FREE(f->voices);
-	if (f->channels) TSF_FREE(f->channels);
+	TSF_FREE(f->outputSamples);
 	TSF_FREE(f);
 }