commit 1aa22f807513ec3af18e7f622b6ba301a0109cd5
parent 17ea1d880c21cf80abe2c1bdd043e30cc871a8c4
Author: Johannes Lorenz <[email protected]>
Date: Sun, 8 Dec 2024 18:21:48 +0100
Add trailing whitespace check
Diffstat:
25 files changed, 371 insertions(+), 334 deletions(-)
diff --git a/src/Containers/NotePool.h b/src/Containers/NotePool.h
@@ -131,7 +131,7 @@ class NotePool
void applyLegato(note_t note, const LegatoParams &par, PortamentoRealtime *portamento_realtime=NULL);
void makeUnsustainable(note_t note);
-
+
void releaseLatched();
bool full(void) const;
@@ -150,7 +150,7 @@ class NotePool
void releaseNote(note_t note);
void release(NoteDescriptor &d);
void latch(NoteDescriptor &d);
-
+
void killAllNotes(void);
void killNote(note_t note);
diff --git a/src/DSP/AnalogFilter.cpp b/src/DSP/AnalogFilter.cpp
@@ -295,7 +295,7 @@ void AnalogFilter::setfreq(float frequency)
}
if (recompute)
q = newq;
-
+
if (beforeFirstTick) {
freq_smoothing.reset( freq );
beforeFirstTick=false;
diff --git a/src/DSP/CombFilter.cpp b/src/DSP/CombFilter.cpp
@@ -46,7 +46,7 @@ inline float CombFilter::sampleLerp(float *smp, float pos) {
int poshi = (int)pos; // integer part (pos >= 0)
float poslo = pos - (float) poshi; // decimal part
// linear interpolation between samples
- return smp[poshi] + poslo * (smp[poshi+1]-smp[poshi]);
+ return smp[poshi] + poslo * (smp[poshi+1]-smp[poshi]);
}
void CombFilter::filterout(float *smp)
@@ -61,8 +61,8 @@ void CombFilter::filterout(float *smp)
float pos = float(mem_size-buffersize+i)-delay;
// add the fwd and bwd feedback samples to current sample
smp[i] = smp[i]*gain + tanhX(
- gainfwd * sampleLerp( input, pos) -
- gainbwd * sampleLerp(output, pos));
+ gainfwd * sampleLerp( input, pos) -
+ gainbwd * sampleLerp(output, pos));
// copy new sample to output buffer
output[mem_size-buffersize+i] = smp[i];
// apply output gain
diff --git a/src/DSP/CombFilter.h b/src/DSP/CombFilter.h
@@ -1,7 +1,7 @@
/*
ZynAddSubFX - a software synthesizer
- CombFilter.h - Several analog filters
+ CombFilter.h - Several analog filters
Copyright (C) 2021-2021 Michael Kirchner
Author: Michael Kirchner
@@ -32,7 +32,7 @@ class CombFilter:public Filter
void settype(unsigned char type);
private:
-
+
float* input;
float* output;
float gain;
@@ -46,8 +46,8 @@ class CombFilter:public Filter
float gainfwd;
float gainbwd;
- float delay;
-
+ float delay;
+
Allocator &memory;
int mem_size;
diff --git a/src/DSP/MoogFilter.cpp b/src/DSP/MoogFilter.cpp
@@ -17,7 +17,7 @@ MoogFilter::MoogFilter(unsigned char Ftype, float Ffreq, float Fq,
{
setfreq_and_q(Ffreq/srate, Fq);
settype(Ftype); // q must be set before
-
+
// initialize state but not to 0, in case of being used as denominator
for (unsigned int i = 0; i<(sizeof(state)/sizeof(*state)); i++)
{
@@ -41,16 +41,16 @@ inline float MoogFilter::tanhX(const float x) const
inline float MoogFilter::tanhXdivX(float x) const
{
-
+
//add DC offset to raise even harmonics (like transistor bias current)
x+= 0.1f;
// pre calc often used term
float x2 = x*x;
// Pade approximation for tanh(x)/x used in filter stages (5x per sample)
- //~ return ((15.0+x2)/(15.0+6.0*x2));
- // faster approximation without division
+ //~ return ((15.0+x2)/(15.0+6.0*x2));
+ // faster approximation without division
// tuned for more distortion in self oscillation
- return (1.0f-(0.35f*x2)+(0.06f*x2*x2));
+ return (1.0f-(0.35f*x2)+(0.06f*x2*x2));
}
inline float MoogFilter::step(float input)
@@ -74,7 +74,7 @@ inline float MoogFilter::step(float input)
dp1 * state[3];
// mix input and gained feedback estimate for
- // cheaper feedback gain compensation. Idea from
+ // cheaper feedback gain compensation. Idea from
// Antti Huovilainen and Vesa Välimäki, "NEW APPROACHES TO DIGITAL SUBTRACTIVE SYNTHESIS"
float u = input - tanhX(feedbackGain * (y3Estimate - 0.5f*input));
// output of all stages
@@ -123,7 +123,7 @@ inline float MoogFilter::tan_2(const float x) const
void MoogFilter::setfreq(float ff)
{
// pre warp cutoff to map to reality
- c = tan_2(PI * ff);
+ c = tan_2(PI * ff);
// limit cutoff to prevent overflow
c = limit(c,0.0006f,1.5f);
// pre calculate some stuff outside the hot zone
diff --git a/src/Effects/Distortion.cpp b/src/Effects/Distortion.cpp
@@ -54,7 +54,7 @@ rtosc::Ports Distortion::ports = {
rOptions(Arctangent, Asymmetric, Pow, Sine, Quantisize,
Zigzag, Limiter, Upper Limiter, Lower Limiter,
Inverse Limiter, Clip, Asym2, Pow2, Sigmoid, Tanh,
- Cubic, Square), rLinear(0,127),
+ Cubic, Square), rLinear(0,127),
rPresets(Arctangent, Asymmetric, Zigzag,
Asymmetric, Pow, Quantisize),
"Distortion Shape"),
diff --git a/src/Effects/EffectMgr.cpp b/src/Effects/EffectMgr.cpp
@@ -136,7 +136,7 @@ static const rtosc::Ports local_ports = {
case 2: // Echo
// invert:
// delay = (Pdelay / 127.0f * 1.5f); //0 .. 1.5 sec
- Pdelay = (int)roundf((20320.0f / (float)eff->time->tempo) *
+ Pdelay = (int)roundf((20320.0f / (float)eff->time->tempo) *
((float)eff->numerator / (float)eff->denominator));
if (eff->numerator&&eff->denominator)
eff->seteffectparrt(2, Pdelay);
@@ -145,8 +145,8 @@ static const rtosc::Ports local_ports = {
case 4: // Phaser
case 5: // Alienwah
case 8: // DynamicFilter
- freq = ((float)eff->time->tempo *
- (float)eff->denominator /
+ freq = ((float)eff->time->tempo *
+ (float)eff->denominator /
(240.0f * (float)eff->numerator));
// invert:
// (powf(2.0f, Pfreq / 127.0f * 10.0f) - 1.0f) * 0.03f
@@ -164,7 +164,7 @@ static const rtosc::Ports local_ports = {
}
d.broadcast(d.loc, "i", val);
} else {
- d.reply(d.loc, "i", eff->numerator);
+ d.reply(d.loc, "i", eff->numerator);
}
}},
{"denominator::i", rShort("dem") rDefault(4) rLinear(1,99)
@@ -183,7 +183,7 @@ static const rtosc::Ports local_ports = {
case 2: // Echo
// invert:
// delay = (Pdelay / 127.0f * 1.5f); //0 .. 1.5 sec
- Pdelay = (int)roundf((20320.0f / (float)eff->time->tempo) *
+ Pdelay = (int)roundf((20320.0f / (float)eff->time->tempo) *
((float)eff->numerator / (float)eff->denominator));
if (eff->numerator&&eff->denominator)
eff->seteffectparrt(2, Pdelay);
@@ -192,8 +192,8 @@ static const rtosc::Ports local_ports = {
case 4: // Phaser
case 5: // Alienwah
case 8: // DynamicFilter
- freq = ((float)eff->time->tempo *
- (float)eff->denominator /
+ freq = ((float)eff->time->tempo *
+ (float)eff->denominator /
(240.0f * (float)eff->numerator));
// invert:
// (powf(2.0f, Pfreq / 127.0f * 10.0f) - 1.0f) * 0.03f
@@ -211,7 +211,7 @@ static const rtosc::Ports local_ports = {
}
d.broadcast(d.loc, "i", val);
} else {
- d.reply(d.loc, "i", eff->denominator);
+ d.reply(d.loc, "i", eff->denominator);
}
}},
{"eq-coeffs:", rProp(internal) rDoc("Get equalizer Coefficients"), NULL,
@@ -351,8 +351,8 @@ void EffectMgr::changeeffectrt(int _nefx, bool avoidSmash)
efx = NULL;
break; //no effect (thru)
}
-
- // set freq / delay params according to bpm ratio
+
+ // set freq / delay params according to bpm ratio
int Pdelay, Pfreq;
float freq;
if (numerator>0) {
@@ -360,7 +360,7 @@ void EffectMgr::changeeffectrt(int _nefx, bool avoidSmash)
case 2: // Echo
// invert:
// delay = (Pdelay / 127.0f * 1.5f); //0 .. 1.5 sec
- Pdelay = (int)roundf((20320.0f / (float)time->tempo) *
+ Pdelay = (int)roundf((20320.0f / (float)time->tempo) *
((float)numerator / (float)denominator));
if (numerator&&denominator)
seteffectparrt(2, Pdelay);
@@ -369,8 +369,8 @@ void EffectMgr::changeeffectrt(int _nefx, bool avoidSmash)
case 4: // Phaser
case 5: // Alienwah
case 8: // DynamicFilter
- freq = ((float)time->tempo *
- (float)denominator /
+ freq = ((float)time->tempo *
+ (float)denominator /
(240.0f * (float)numerator));
// invert:
// (powf(2.0f, Pfreq / 127.0f * 10.0f) - 1.0f) * 0.03f
@@ -385,7 +385,7 @@ void EffectMgr::changeeffectrt(int _nefx, bool avoidSmash)
break;
}
}
-
+
} catch (std::bad_alloc &ba) {
std::cerr << "failed to change effect " << _nefx << ": " << ba.what() << std::endl;
return;
diff --git a/src/Effects/EffectMgr.h b/src/Effects/EffectMgr.h
@@ -73,10 +73,10 @@ class EffectMgr:public Presets
int nefx;
Effect *efx;
const AbsTime *time;
-
+
int numerator;
int denominator;
-
+
private:
//Parameters Prior to initialization
@@ -110,8 +110,8 @@ class EffectMgr:public Presets
bool dryonly;
Allocator &memory;
const SYNTH_T &synth;
-
-
+
+
};
}
diff --git a/src/Effects/Sympathetic.h b/src/Effects/Sympathetic.h
@@ -26,9 +26,9 @@ const float gainbwd_factor = 0.001f;
// number of piano keys with single string
const unsigned int num_single_strings = 12;
-// number of piano keys with triple strings
+// number of piano keys with triple strings
const unsigned int num_triple_strings = 48;
-
+
// frequencies of a guitar in standard e tuning
const float guitar_freqs[6] = {82.4f, 110.0f, 146.8f, 196.0f, 246.9f, 329.6f};
diff --git a/src/Misc/Master.cpp b/src/Misc/Master.cpp
@@ -775,7 +775,7 @@ Master::Master(const SYNTH_T &synth_, Config* config)
SaveFullXml=(config->cfg.SaveFullXml==1);
bToU = NULL;
uToB = NULL;
-
+
// set default tempo
time.tempo = 120;
diff --git a/src/Misc/MiddleWare.cpp b/src/Misc/MiddleWare.cpp
@@ -328,7 +328,7 @@ void connectMidiLearn(int par, int chan, bool isNrpn, string path, rtosc::MidiMa
printf("mapping midi NRPN: %d, CH: %d to Port: %s\n", par, chan, path.c_str());
else
printf("mapping midi CC: %d, CH: %d to Port: %s\n", par, chan, path.c_str());
-
+
if(chan<1) chan=1;
int ID = (isNrpn<<18) + (((chan-1)&0x0f)<<14) + par;
//~ printf("ID = %d\n", ID);
diff --git a/src/Misc/Part.cpp b/src/Misc/Part.cpp
@@ -643,7 +643,7 @@ bool Part::NoteOnInternal(note_t note,
if(Ppolymode)
notePool.makeUnsustainable(note);
-
+
// in latch mode release latched notes before creating the new one
if(Platchmode)
notePool.releaseLatched();
diff --git a/src/Misc/Time.h b/src/Misc/Time.h
@@ -31,7 +31,7 @@ class AbsTime
private:
int64_t frames;
const SYNTH_T &s;
-
+
};
//Marker for an event relative to some position of the absolute timer
diff --git a/src/Nio/AudioOut.h b/src/Nio/AudioOut.h
@@ -41,7 +41,7 @@ class AudioOut:public virtual Engine
virtual void setAudioEn(bool nval) = 0;
virtual bool getAudioEn() const = 0;
-
+
bool isOutputCompressionEnabled = 0;
protected:
@@ -52,7 +52,7 @@ class AudioOut:public virtual Engine
const SYNTH_T &synth;
int samplerate;
int bufferSize;
-
+
};
}
diff --git a/src/Nio/Nio.h b/src/Nio/Nio.h
@@ -56,7 +56,7 @@ namespace Nio
void waveStart(void);
void waveStop(void);
void waveEnd(void);
-
+
void setAudioCompressor(bool isEnabled);
bool getAudioCompressor(void);
diff --git a/src/Nio/OutMgr.h b/src/Nio/OutMgr.h
@@ -56,7 +56,7 @@ class OutMgr
bool setSink(std::string name);
std::string getSink() const;
-
+
void setAudioCompressor(bool isEnabled);
bool getAudioCompressor(void);
diff --git a/src/Synth/LFO.cpp b/src/Synth/LFO.cpp
@@ -28,12 +28,12 @@ LFO::LFO(const LFOParams &lfopars_, float basefreq_, const AbsTime &t, WatchMana
delayTime(t, lfopars_.delay), //0..4 sec
deterministic(!lfopars_.Pfreqrand),
dt(t.dt()),
- lfopars(lfopars_),
+ lfopars(lfopars_),
basefreq(basefreq_),
watchOut(m, watch_prefix, "out")
{
updatePars();
-
+
if(!lfopars.Pcontinous) {
if(!lfopars.Pstartphase)
phase = RND;
@@ -62,7 +62,7 @@ LFO::LFO(const LFOParams &lfopars_, float basefreq_, const AbsTime &t, WatchMana
}
lfo_state=lfo_state_type::delaying;
-
+
rampUp = 0.0f;
rampDown = 1.0f;
@@ -90,13 +90,13 @@ void LFO::updatePars()
float lfofreq;
if (!lfopars.numerator || !lfopars.denominator) {
- lfofreq = lfopars.freq * lfostretch;
+ lfofreq = lfopars.freq * lfostretch;
} else {
tempo = time.tempo;
lfofreq = float(tempo) * float(lfopars.denominator)/(240.0f * float(lfopars.numerator));
}
phaseInc = fabsf(lfofreq) * dt;
-
+
//Limit the Frequency(or else...)
if(phaseInc > 0.49999999f)
phaseInc = 0.499999999f;
@@ -177,8 +177,8 @@ void LFO::releasekey()
}
// store current ramp value in case of release while fading in
rampOnRelease = rampUp;
- // burn in the current amount of outStartValue
- // therefor multiply its current reverse ramping factor
+ // burn in the current amount of outStartValue
+ // therefor multiply its current reverse ramping factor
// and divide by current ramp factor. It will be multiplied during fading out.
outStartValue *= (1.0f - rampOnRelease);
// store current time
@@ -195,20 +195,20 @@ float LFO::lfoout()
{
updatePars();
switch(lfopars.fel) {
-
+
case consumer_location_type_t::amp:
lfointensity = lfopars.Pintensity / 127.0f; // [0...1]
break;
case consumer_location_type_t::filter:
lfointensity = lfopars.Pintensity / 127.0f * 4.0f; // [0...4] octaves
- break;
+ break;
case consumer_location_type_t::freq:
case consumer_location_type_t::unspecified:
lfointensity = powf(2, lfopars.Pintensity / 127.0f * 11.0f) - 1.0f; // [0...2047] cent
break;
}
}
-
+
// refresh freq if tempo has changed
if (lfopars.numerator && lfopars.denominator && tempo != time.tempo) {
tempo = time.tempo;
@@ -221,12 +221,12 @@ float LFO::lfoout()
out *= lfointensity * (amp1 + phaseWithStartphase * (amp2 - amp1));
else
out *= lfointensity * amp2;
-
-
+
+
// handle lfo state (delay, fade in, fade out)
switch(lfo_state) {
case delaying:
-
+
outStartValue = out; // keep start value to prevent jump
if (delayTime.inFuture()) {
return out;
@@ -235,40 +235,40 @@ float LFO::lfoout()
fadeInDuration = lfopars.fadein * lfopars.time->framesPerSec();
lfo_state = lfo_state_type::fadingIn;
}
-
+
break;
case fadingIn:
-
+
if (fadeInDuration && rampUp < 1.0) {
rampUp = ((float)(lfopars.time->time() - fadeInTimestamp) / (float)fadeInDuration);
rampUp *= rampUp; // square for soft start
-
- }
+
+ }
else {
rampUp = 1.0f;
lfo_state = lfo_state_type::running;
- }
-
+ }
+
out *= rampUp;
out += outStartValue * (1.0f-rampUp);
-
+
break;
-
+
case fadingOut:
if(fadeOutDuration && rampDown) {// no division by zero, please
rampDown = 1.0f - ( (float)(lfopars.time->time() - releaseTimestamp) / (float)fadeOutDuration );
rampDown *= rampDown; // square for soft end
}
else // no ramp down
- rampDown = 0.0f;
-
-
+ rampDown = 0.0f;
+
+
out *= rampOnRelease * rampDown;
out += outStartValue*rampDown;
-
+
break;
-
+
case running:
default:
break;
@@ -288,7 +288,7 @@ float LFO::lfoout()
computeNextFreqRnd();
}
-
+
float watch_data[2] = {phaseWithStartphase, out};
watchOut(watch_data, 2);
diff --git a/src/Synth/LFO.h b/src/Synth/LFO.h
@@ -40,18 +40,18 @@ class LFO
void releasekey();
private:
typedef enum lfo_state_type{
- delaying,
+ delaying,
fadingIn,
running,
fadingOut
} lfo_state_type;
-
+
float baseOut(const char waveShape, const float phase);
float biquad(float input);
void updatePars();
-
+
lfo_state_type lfo_state;
-
+
//tempo stored to detect changes
unsigned int tempo;
//Phase of Oscillator
@@ -83,7 +83,7 @@ class LFO
//Timestamp of noteoff
int64_t releaseTimestamp;
//Time to ramp out
-
+
int64_t fadeOutDuration;
float rampUp, rampDown, rampOnRelease;
// store the constant out value before oscillating starts
diff --git a/src/Synth/OscilGen.cpp b/src/Synth/OscilGen.cpp
@@ -1975,7 +1975,7 @@ FILTER(lpsk)
float vOut = tmp2PIf * tmp2PIf;
std::complex<float> vIn = s*s + tmp2PIf*s/((par2)+(2.0f*par*par2)+0.5f) + tmp2PIf*tmp2PIf;
return std::abs((vOut*vOut*vOut) / (vIn*vIn*vIn));
-
+
}
#undef FILTER
diff --git a/src/Tests/AdNoteTest.cpp b/src/Tests/AdNoteTest.cpp
@@ -65,7 +65,7 @@ class AdNoteTest
lfop->Pcontinous = randval(0,1);
lfop->Pstretch = randval(0,255);
lfop->fel = (consumer_location_type_t) randval(1,2);
-
+
}
void run_lfo_randomtest(void)
diff --git a/src/Tests/CMakeLists.txt b/src/Tests/CMakeLists.txt
@@ -100,3 +100,7 @@ if(NOT (${CMAKE_SYSTEM_NAME} STREQUAL "Windows"))
endif()
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+ add_test(TrailingWhitespace ${CMAKE_SOURCE_DIR}/src/Tests/trailing-whitespace)
+endif()
+
diff --git a/src/Tests/trailing-whitespace b/src/Tests/trailing-whitespace
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+trailing_whitespace=0
+
+toplevel=$(git rev-parse --show-toplevel)
+
+if ! [ "$toplevel" ]
+then
+ echo "This script must be started from inside a clone"
+ exit 1
+fi
+
+for codefile in $(cd "$toplevel" && git ls-files '*.h' '*.cpp')
+do
+ if grep -H '[[:space:]]\+$' "$toplevel/$codefile"
+ then
+ trailing_whitespace=1
+ fi
+done
+
+if [ "$trailing_whitespace" -eq 1 ]
+then
+ echo "Error: Some lines of code end with whitespace"
+ echo "You can tell your editor to cleanup whitespace, or run this"
+ echo "in the toplevel dir of your git repo:"
+ echo
+ cat - << 'EOF'
+ for i in $(for codefile in $(git ls-files '*.h' '*.cpp'); do grep -H '[[:space:]]\+$' $codefile; done | cut -d: -f1 | sort -u); do sed 's#[[:space:]]\+$##g' -i $i; done
+EOF
+ echo
+fi
+
+exit $trailing_whitespace
diff --git a/src/main.cpp b/src/main.cpp
@@ -609,7 +609,7 @@ int main(int argc, char *argv[])
if(altered_master)
middleware->updateResources(master);
-
+
//Run the Nio system
printf("[INFO] Nio::start()\n");
bool ioGood = Nio::start();
diff --git a/tlsf/tlsf.h b/tlsf/tlsf.h
@@ -1,73 +1,73 @@
-#ifndef INCLUDED_tlsf
-#define INCLUDED_tlsf
-
-/*
-** Two Level Segregated Fit memory allocator, version 3.0.
-** Written by Matthew Conte, and placed in the Public Domain.
-** http://tlsf.baisoku.org
-**
-** Based on the original documentation by Miguel Masmano:
-** http://rtportal.upv.es/rtmalloc/allocators/tlsf/index.shtml
-**
-** Please see the accompanying Readme.txt for implementation
-** notes and caveats.
-**
-** This implementation was written to the specification
-** of the document, therefore no GPL restrictions apply.
-*/
-
-#include <stddef.h>
-
-#if defined(__cplusplus)
-extern "C" {
-#endif
-
-/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
-/* pool_t: a block of memory that TLSF can manage. */
-typedef void* tlsf_t;
-typedef void* pool_t;
-
-/* Create/destroy a memory pool. */
-tlsf_t tlsf_create(void* mem);
-tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
-void tlsf_destroy(tlsf_t tlsf);
-pool_t tlsf_get_pool(tlsf_t tlsf);
-
-/* Add/remove memory pools. */
-pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
-void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
-
-/* malloc/memalign/realloc/free replacements. */
-void* tlsf_malloc(tlsf_t tlsf, size_t bytes);
-void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes);
-void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
-void tlsf_free(tlsf_t tlsf, void* ptr);
-
-/* Returns internal block size, not original request size */
-size_t tlsf_block_size(void* ptr);
-
-/* Overheads/limits of internal structures. */
-size_t tlsf_size();
-size_t tlsf_align_size();
-size_t tlsf_block_size_min();
-size_t tlsf_block_size_max();
-size_t tlsf_pool_overhead();
-size_t tlsf_alloc_overhead();
-
-/* Debugging. */
-typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
-void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
-/* Returns nonzero if any internal consistency check fails. */
-int tlsf_check(tlsf_t tlsf);
-int tlsf_check_pool(pool_t pool);
-
-/* TODO add utilities for
- * - Find Unused Pool Chunks
- * - Define if the pool is running low
- */
-
-#if defined(__cplusplus)
-};
-#endif
-
-#endif
+#ifndef INCLUDED_tlsf
+#define INCLUDED_tlsf
+
+/*
+** Two Level Segregated Fit memory allocator, version 3.0.
+** Written by Matthew Conte, and placed in the Public Domain.
+** http://tlsf.baisoku.org
+**
+** Based on the original documentation by Miguel Masmano:
+** http://rtportal.upv.es/rtmalloc/allocators/tlsf/index.shtml
+**
+** Please see the accompanying Readme.txt for implementation
+** notes and caveats.
+**
+** This implementation was written to the specification
+** of the document, therefore no GPL restrictions apply.
+*/
+
+#include <stddef.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
+/* pool_t: a block of memory that TLSF can manage. */
+typedef void* tlsf_t;
+typedef void* pool_t;
+
+/* Create/destroy a memory pool. */
+tlsf_t tlsf_create(void* mem);
+tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
+void tlsf_destroy(tlsf_t tlsf);
+pool_t tlsf_get_pool(tlsf_t tlsf);
+
+/* Add/remove memory pools. */
+pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
+void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
+
+/* malloc/memalign/realloc/free replacements. */
+void* tlsf_malloc(tlsf_t tlsf, size_t bytes);
+void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes);
+void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
+void tlsf_free(tlsf_t tlsf, void* ptr);
+
+/* Returns internal block size, not original request size */
+size_t tlsf_block_size(void* ptr);
+
+/* Overheads/limits of internal structures. */
+size_t tlsf_size();
+size_t tlsf_align_size();
+size_t tlsf_block_size_min();
+size_t tlsf_block_size_max();
+size_t tlsf_pool_overhead();
+size_t tlsf_alloc_overhead();
+
+/* Debugging. */
+typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
+void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
+/* Returns nonzero if any internal consistency check fails. */
+int tlsf_check(tlsf_t tlsf);
+int tlsf_check_pool(pool_t pool);
+
+/* TODO add utilities for
+ * - Find Unused Pool Chunks
+ * - Define if the pool is running low
+ */
+
+#if defined(__cplusplus)
+};
+#endif
+
+#endif
diff --git a/tlsf/tlsfbits.h b/tlsf/tlsfbits.h
@@ -1,180 +1,180 @@
-#ifndef INCLUDED_tlsfbits
-#define INCLUDED_tlsfbits
-
-#if defined(__cplusplus)
-#define tlsf_decl inline
-#else
-#define tlsf_decl static
-#endif
-
-/*
-** Architecture-specific bit manipulation routines.
-**
-** TLSF achieves O(1) cost for malloc and free operations by limiting
-** the search for a free block to a free list of guaranteed size
-** adequate to fulfill the request, combined with efficient free list
-** queries using bitmasks and architecture-specific bit-manipulation
-** routines.
-**
-** Most modern processors provide instructions to count leading zeroes
-** in a word, find the lowest and highest set bit, etc. These
-** specific implementations will be used when available, falling back
-** to a reasonably efficient generic implementation.
-**
-** NOTE: TLSF spec relies on ffs/fls returning value 0..31.
-** ffs/fls return 1-32 by default, returning 0 for error.
-*/
-
-/*
-** Detect whether or not we are building for a 32- or 64-bit (LP/LLP)
-** architecture. There is no reliable portable method at compile-time.
-*/
-#if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) \
- || defined (_WIN64) || defined (__LP64__) || defined (__LLP64__)
-#define TLSF_64BIT
-#endif
-
-/*
-** gcc 3.4 and above have builtin support, specialized for architecture.
-** Some compilers masquerade as gcc; patchlevel test filters them out.
-*/
-#if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \
- && defined (__GNUC_PATCHLEVEL__)
-
-tlsf_decl int tlsf_ffs(unsigned int word)
-{
- return __builtin_ffs(word) - 1;
-}
-
-tlsf_decl int tlsf_fls(unsigned int word)
-{
- const int bit = word ? 32 - __builtin_clz(word) : 0;
- return bit - 1;
-}
-
-#elif defined (_MSC_VER) && (_MSC_VER >= 1400) && (defined (_M_IX86) || defined (_M_X64))
-/* Microsoft Visual C++ support on x86/X64 architectures. */
-
-#include <intrin.h>
-
-#pragma intrinsic(_BitScanReverse)
-#pragma intrinsic(_BitScanForward)
-
-tlsf_decl int tlsf_fls(unsigned int word)
-{
- unsigned long index;
- return _BitScanReverse(&index, word) ? index : -1;
-}
-
-tlsf_decl int tlsf_ffs(unsigned int word)
-{
- unsigned long index;
- return _BitScanForward(&index, word) ? index : -1;
-}
-
-#elif defined (_MSC_VER) && defined (_M_PPC)
-/* Microsoft Visual C++ support on PowerPC architectures. */
-
-#include <ppcintrinsics.h>
-
-tlsf_decl int tlsf_fls(unsigned int word)
-{
- const int bit = 32 - _CountLeadingZeros(word);
- return bit - 1;
-}
-
-tlsf_decl int tlsf_ffs(unsigned int word)
-{
- const unsigned int reverse = word & (~word + 1);
- const int bit = 32 - _CountLeadingZeros(reverse);
- return bit - 1;
-}
-
-#elif defined (__ARMCC_VERSION)
-/* RealView Compilation Tools for ARM */
-
-tlsf_decl int tlsf_ffs(unsigned int word)
-{
- const unsigned int reverse = word & (~word + 1);
- const int bit = 32 - __clz(reverse);
- return bit - 1;
-}
-
-tlsf_decl int tlsf_fls(unsigned int word)
-{
- const int bit = word ? 32 - __clz(word) : 0;
- return bit - 1;
-}
-
-#elif defined (__ghs__)
-/* Green Hills support for PowerPC */
-
-#include <ppc_ghs.h>
-
-tlsf_decl int tlsf_ffs(unsigned int word)
-{
- const unsigned int reverse = word & (~word + 1);
- const int bit = 32 - __CLZ32(reverse);
- return bit - 1;
-}
-
-tlsf_decl int tlsf_fls(unsigned int word)
-{
- const int bit = word ? 32 - __CLZ32(word) : 0;
- return bit - 1;
-}
-
-#else
-/* Fall back to generic implementation. */
-
-tlsf_decl int tlsf_fls_generic(unsigned int word)
-{
- int bit = 32;
-
- if (!word) bit -= 1;
- if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }
- if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }
- if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }
- if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }
- if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }
-
- return bit;
-}
-
-/* Implement ffs in terms of fls. */
-tlsf_decl int tlsf_ffs(unsigned int word)
-{
- return tlsf_fls_generic(word & (~word + 1)) - 1;
-}
-
-tlsf_decl int tlsf_fls(unsigned int word)
-{
- return tlsf_fls_generic(word) - 1;
-}
-
-#endif
-
-/* Possibly 64-bit version of tlsf_fls. */
-#if defined (TLSF_64BIT)
-tlsf_decl int tlsf_fls_sizet(size_t size)
-{
- int high = (int)(size >> 32);
- int bits = 0;
- if (high)
- {
- bits = 32 + tlsf_fls(high);
- }
- else
- {
- bits = tlsf_fls((int)size & 0xffffffff);
-
- }
- return bits;
-}
-#else
-#define tlsf_fls_sizet tlsf_fls
-#endif
-
-#undef tlsf_decl
-
-#endif
+#ifndef INCLUDED_tlsfbits
+#define INCLUDED_tlsfbits
+
+#if defined(__cplusplus)
+#define tlsf_decl inline
+#else
+#define tlsf_decl static
+#endif
+
+/*
+** Architecture-specific bit manipulation routines.
+**
+** TLSF achieves O(1) cost for malloc and free operations by limiting
+** the search for a free block to a free list of guaranteed size
+** adequate to fulfill the request, combined with efficient free list
+** queries using bitmasks and architecture-specific bit-manipulation
+** routines.
+**
+** Most modern processors provide instructions to count leading zeroes
+** in a word, find the lowest and highest set bit, etc. These
+** specific implementations will be used when available, falling back
+** to a reasonably efficient generic implementation.
+**
+** NOTE: TLSF spec relies on ffs/fls returning value 0..31.
+** ffs/fls return 1-32 by default, returning 0 for error.
+*/
+
+/*
+** Detect whether or not we are building for a 32- or 64-bit (LP/LLP)
+** architecture. There is no reliable portable method at compile-time.
+*/
+#if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__) \
+ || defined (_WIN64) || defined (__LP64__) || defined (__LLP64__)
+#define TLSF_64BIT
+#endif
+
+/*
+** gcc 3.4 and above have builtin support, specialized for architecture.
+** Some compilers masquerade as gcc; patchlevel test filters them out.
+*/
+#if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \
+ && defined (__GNUC_PATCHLEVEL__)
+
+tlsf_decl int tlsf_ffs(unsigned int word)
+{
+ return __builtin_ffs(word) - 1;
+}
+
+tlsf_decl int tlsf_fls(unsigned int word)
+{
+ const int bit = word ? 32 - __builtin_clz(word) : 0;
+ return bit - 1;
+}
+
+#elif defined (_MSC_VER) && (_MSC_VER >= 1400) && (defined (_M_IX86) || defined (_M_X64))
+/* Microsoft Visual C++ support on x86/X64 architectures. */
+
+#include <intrin.h>
+
+#pragma intrinsic(_BitScanReverse)
+#pragma intrinsic(_BitScanForward)
+
+tlsf_decl int tlsf_fls(unsigned int word)
+{
+ unsigned long index;
+ return _BitScanReverse(&index, word) ? index : -1;
+}
+
+tlsf_decl int tlsf_ffs(unsigned int word)
+{
+ unsigned long index;
+ return _BitScanForward(&index, word) ? index : -1;
+}
+
+#elif defined (_MSC_VER) && defined (_M_PPC)
+/* Microsoft Visual C++ support on PowerPC architectures. */
+
+#include <ppcintrinsics.h>
+
+tlsf_decl int tlsf_fls(unsigned int word)
+{
+ const int bit = 32 - _CountLeadingZeros(word);
+ return bit - 1;
+}
+
+tlsf_decl int tlsf_ffs(unsigned int word)
+{
+ const unsigned int reverse = word & (~word + 1);
+ const int bit = 32 - _CountLeadingZeros(reverse);
+ return bit - 1;
+}
+
+#elif defined (__ARMCC_VERSION)
+/* RealView Compilation Tools for ARM */
+
+tlsf_decl int tlsf_ffs(unsigned int word)
+{
+ const unsigned int reverse = word & (~word + 1);
+ const int bit = 32 - __clz(reverse);
+ return bit - 1;
+}
+
+tlsf_decl int tlsf_fls(unsigned int word)
+{
+ const int bit = word ? 32 - __clz(word) : 0;
+ return bit - 1;
+}
+
+#elif defined (__ghs__)
+/* Green Hills support for PowerPC */
+
+#include <ppc_ghs.h>
+
+tlsf_decl int tlsf_ffs(unsigned int word)
+{
+ const unsigned int reverse = word & (~word + 1);
+ const int bit = 32 - __CLZ32(reverse);
+ return bit - 1;
+}
+
+tlsf_decl int tlsf_fls(unsigned int word)
+{
+ const int bit = word ? 32 - __CLZ32(word) : 0;
+ return bit - 1;
+}
+
+#else
+/* Fall back to generic implementation. */
+
+tlsf_decl int tlsf_fls_generic(unsigned int word)
+{
+ int bit = 32;
+
+ if (!word) bit -= 1;
+ if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }
+ if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }
+ if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }
+ if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }
+ if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }
+
+ return bit;
+}
+
+/* Implement ffs in terms of fls. */
+tlsf_decl int tlsf_ffs(unsigned int word)
+{
+ return tlsf_fls_generic(word & (~word + 1)) - 1;
+}
+
+tlsf_decl int tlsf_fls(unsigned int word)
+{
+ return tlsf_fls_generic(word) - 1;
+}
+
+#endif
+
+/* Possibly 64-bit version of tlsf_fls. */
+#if defined (TLSF_64BIT)
+tlsf_decl int tlsf_fls_sizet(size_t size)
+{
+ int high = (int)(size >> 32);
+ int bits = 0;
+ if (high)
+ {
+ bits = 32 + tlsf_fls(high);
+ }
+ else
+ {
+ bits = tlsf_fls((int)size & 0xffffffff);
+
+ }
+ return bits;
+}
+#else
+#define tlsf_fls_sizet tlsf_fls
+#endif
+
+#undef tlsf_decl
+
+#endif