From be028f787a37b5ee6e04f107b91e4e60dfb7b9b1 Mon Sep 17 00:00:00 2001 From: Adam Pocock Date: Sat, 22 Feb 2014 20:27:20 -0500 Subject: [PATCH] Fixed a stupid bug where the ouptut of calloc wasn't checked. Now it checks the output, and calls the appropriate error function for C or Matlab. --- ArrayOperations.c | 36 ++++++++++++++++------- ArrayOperations.h | 11 +++++-- CalculateProbability.c | 57 +++++++++++++++++++------------------ MIToolboxMex.c | 3 +- Makefile | 1 + MutualInformation.c | 5 ++-- RenyiEntropy.c | 11 +++---- RenyiMIToolboxMex.c | 4 ++- RenyiMutualInformation.c | 3 +- RenyiMutualInformation.h | 7 +++-- WeightedMIToolboxMex.c | 3 +- WeightedMutualInformation.c | 2 +- 12 files changed, 87 insertions(+), 56 deletions(-) diff --git a/ArrayOperations.c b/ArrayOperations.c index f3a85ca..434d10b 100644 --- a/ArrayOperations.c +++ b/ArrayOperations.c @@ -1,5 +1,5 @@ /******************************************************************************* -** ArrayOperations.cpp +** ArrayOperations.c ** Part of the mutual information toolbox ** ** Contains functions to floor arrays, and to merge arrays into a joint @@ -7,8 +7,9 @@ ** ** Author: Adam Pocock ** Created 17/2/2010 +** Updated - 22/02/2014 - Added checking on calloc. ** -** Copyright 2010 Adam Pocock, The University Of Manchester +** Copyright 2010,2014 Adam Pocock, The University Of Manchester ** www.cs.manchester.ac.uk ** ** This file is part of MIToolbox. @@ -28,9 +29,24 @@ ** *******************************************************************************/ +#include #include "MIToolbox.h" #include "ArrayOperations.h" +void* checkedCalloc(size_t vectorLength, size_t sizeOfType) { + void *allocated = CALLOC_FUNC(vectorLength, sizeOfType); + if(allocated == NULL) { +#ifdef MEX_IMPLEMENTATION + /* This call returns control to Matlab, with the associated error message */ + mexErrMsgTxt("Failed to allocate memory\n"); +#else + fprintf(stderr, "Error: %s\n", strerror(errno)); + exit(EXIT_FAILURE); +#endif + } + return allocated; +} + void printDoubleVector(double *vector, int vectorLength) { int i; @@ -53,7 +69,7 @@ void printIntVector(int *vector, int vectorLength) int numberOfUniqueValues(double *featureVector, int vectorLength) { int uniqueValues = 0; - double *valuesArray = (double *) CALLOC_FUNC(vectorLength,sizeof(double)); + double *valuesArray = (double *) checkedCalloc(vectorLength,sizeof(double)); int found = 0; int j = 0; @@ -152,8 +168,8 @@ int mergeArrays(double *firstVector, double *secondVector, double *outputVector, int stateCount; int curIndex; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); + secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength); @@ -162,7 +178,7 @@ int mergeArrays(double *firstVector, double *secondVector, double *outputVector, ** printVector(firstNormalisedVector,vectorLength); ** printVector(secondNormalisedVector,vectorLength); */ - stateMap = (int *) CALLOC_FUNC(firstNumStates*secondNumStates,sizeof(int)); + stateMap = (int *) checkedCalloc(firstNumStates*secondNumStates,sizeof(int)); stateCount = 1; for (i = 0; i < vectorLength; i++) { @@ -195,8 +211,8 @@ int mergeArraysArities(double *firstVector, int numFirstStates, double *secondVe int totalStates; int firstStateCheck, secondStateCheck; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); + secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); firstStateCheck = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondStateCheck = normaliseArray(secondVector,secondNormalisedVector,vectorLength); @@ -241,7 +257,7 @@ int mergeMultipleArrays(double *inputMatrix, double *outputVector, int matrixWid } else { - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); currentNumStates = normaliseArray(inputMatrix,normalisedVector,vectorLength); for (i = 0; i < vectorLength; i++) { @@ -273,7 +289,7 @@ int mergeMultipleArraysArities(double *inputMatrix, double *outputVector, int ma } else { - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); currentNumStates = normaliseArray(inputMatrix,normalisedVector,vectorLength); for (i = 0; i < vectorLength; i++) { diff --git a/ArrayOperations.h b/ArrayOperations.h index 3cc9025..3700258 100644 --- a/ArrayOperations.h +++ b/ArrayOperations.h @@ -7,8 +7,9 @@ ** ** Author: Adam Pocock ** Created 17/2/2010 +** Updated - 22/02/2014 - Added checking on calloc. ** -** Copyright 2010 Adam Pocock, The University Of Manchester +** Copyright 2010,2014 Adam Pocock, The University Of Manchester ** www.cs.manchester.ac.uk ** ** This file is part of MIToolbox. @@ -36,10 +37,14 @@ extern "C" { #endif /******************************************************************************* -** Simple print function for debugging +** A version of calloc which checks to see if memory was allocated. *******************************************************************************/ -void printDoubleVector(double *vector, int vectorLength); +void* checkedCalloc(size_t vectorLength, size_t sizeOfType); +/******************************************************************************* +** Simple print functions for debugging +*******************************************************************************/ +void printDoubleVector(double *vector, int vectorlength); void printIntVector(int *vector, int vectorLength); /******************************************************************************* diff --git a/CalculateProbability.c b/CalculateProbability.c index 7d6d872..c881956 100644 --- a/CalculateProbability.c +++ b/CalculateProbability.c @@ -7,9 +7,10 @@ ** ** Author: Adam Pocock ** Created: 17/02/2010 -** Modified: 04/07/2011 - added weighted probability functions +** Modified - 04/07/2011 - added weighted probability functions +** Updated - 22/02/2014 - Added checking on calloc. ** -** Copyright 2010/2011 Adam Pocock, The University Of Manchester +** Copyright 2010-2014 Adam Pocock, The University Of Manchester ** www.cs.manchester.ac.uk ** ** This file is part of MIToolbox. @@ -50,20 +51,20 @@ JointProbabilityState calculateJointProbability(double *firstVector, double *sec double length = vectorLength; JointProbabilityState state; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); + secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength); jointNumStates = firstNumStates * secondNumStates; - firstStateCounts = (int *) CALLOC_FUNC(firstNumStates,sizeof(int)); - secondStateCounts = (int *) CALLOC_FUNC(secondNumStates,sizeof(int)); - jointStateCounts = (int *) CALLOC_FUNC(jointNumStates,sizeof(int)); + firstStateCounts = (int *) checkedCalloc(firstNumStates,sizeof(int)); + secondStateCounts = (int *) checkedCalloc(secondNumStates,sizeof(int)); + jointStateCounts = (int *) checkedCalloc(jointNumStates,sizeof(int)); - firstStateProbs = (double *) CALLOC_FUNC(firstNumStates,sizeof(double)); - secondStateProbs = (double *) CALLOC_FUNC(secondNumStates,sizeof(double)); - jointStateProbs = (double *) CALLOC_FUNC(jointNumStates,sizeof(double)); + firstStateProbs = (double *) checkedCalloc(firstNumStates,sizeof(double)); + secondStateProbs = (double *) checkedCalloc(secondNumStates,sizeof(double)); + jointStateProbs = (double *) checkedCalloc(jointNumStates,sizeof(double)); /* optimised version, less numerically stable double fractionalState = 1.0 / vectorLength; @@ -153,24 +154,24 @@ WeightedJointProbState calculateWeightedJointProbability(double *firstVector, do double length = vectorLength; WeightedJointProbState state; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); + secondNormalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength); jointNumStates = firstNumStates * secondNumStates; - firstStateCounts = (int *) CALLOC_FUNC(firstNumStates,sizeof(int)); - secondStateCounts = (int *) CALLOC_FUNC(secondNumStates,sizeof(int)); - jointStateCounts = (int *) CALLOC_FUNC(jointNumStates,sizeof(int)); + firstStateCounts = (int *) checkedCalloc(firstNumStates,sizeof(int)); + secondStateCounts = (int *) checkedCalloc(secondNumStates,sizeof(int)); + jointStateCounts = (int *) checkedCalloc(jointNumStates,sizeof(int)); - firstStateProbs = (double *) CALLOC_FUNC(firstNumStates,sizeof(double)); - secondStateProbs = (double *) CALLOC_FUNC(secondNumStates,sizeof(double)); - jointStateProbs = (double *) CALLOC_FUNC(jointNumStates,sizeof(double)); + firstStateProbs = (double *) checkedCalloc(firstNumStates,sizeof(double)); + secondStateProbs = (double *) checkedCalloc(secondNumStates,sizeof(double)); + jointStateProbs = (double *) checkedCalloc(jointNumStates,sizeof(double)); - firstWeightVec = (double *) CALLOC_FUNC(firstNumStates,sizeof(double)); - secondWeightVec = (double *) CALLOC_FUNC(secondNumStates,sizeof(double)); - jointWeightVec = (double *) CALLOC_FUNC(jointNumStates,sizeof(double)); + firstWeightVec = (double *) checkedCalloc(firstNumStates,sizeof(double)); + secondWeightVec = (double *) checkedCalloc(secondNumStates,sizeof(double)); + jointWeightVec = (double *) checkedCalloc(jointNumStates,sizeof(double)); for (i = 0; i < vectorLength; i++) { @@ -261,12 +262,12 @@ ProbabilityState calculateProbability(double *dataVector, int vectorLength) int i; double length = vectorLength; - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); numStates = normaliseArray(dataVector,normalisedVector,vectorLength); - stateCounts = (int *) CALLOC_FUNC(numStates,sizeof(int)); - stateProbs = (double *) CALLOC_FUNC(numStates,sizeof(double)); + stateCounts = (int *) checkedCalloc(numStates,sizeof(int)); + stateProbs = (double *) checkedCalloc(numStates,sizeof(double)); /* optimised version, may have floating point problems fractionalState = 1.0 / vectorLength; @@ -312,13 +313,13 @@ WeightedProbState calculateWeightedProbability(double *dataVector, double *examp int i; double length = vectorLength; - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = (int *) checkedCalloc(vectorLength,sizeof(int)); numStates = normaliseArray(dataVector,normalisedVector,vectorLength); - stateCounts = (int *) CALLOC_FUNC(numStates,sizeof(int)); - stateProbs = (double *) CALLOC_FUNC(numStates,sizeof(double)); - stateWeights = (double *) CALLOC_FUNC(numStates,sizeof(double)); + stateCounts = (int *) checkedCalloc(numStates,sizeof(int)); + stateProbs = (double *) checkedCalloc(numStates,sizeof(double)); + stateWeights = (double *) checkedCalloc(numStates,sizeof(double)); for (i = 0; i < vectorLength; i++) { diff --git a/MIToolboxMex.c b/MIToolboxMex.c index 0c44297..c65b23c 100644 --- a/MIToolboxMex.c +++ b/MIToolboxMex.c @@ -1,6 +1,6 @@ /******************************************************************************* ** -** MIToolboxMex.cpp +** MIToolboxMex.c ** is the MATLAB entry point for the MIToolbox functions when called from ** a MATLAB/OCTAVE script. ** @@ -23,6 +23,7 @@ ** along with MIToolbox. If not, see . ** *******************************************************************************/ + #include "MIToolbox.h" #include "ArrayOperations.h" #include "CalculateProbability.h" diff --git a/Makefile b/Makefile index 853d198..0832657 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ #makefile for MIToolbox #Author: Adam Pocock, apocock@cs.man.ac.uk #Created 11/3/2010 +#Updated 22/2/2014 - Added make install # # #Copyright 2010 Adam Pocock, The University Of Manchester diff --git a/MutualInformation.c b/MutualInformation.c index 2af401e..e1cfad6 100644 --- a/MutualInformation.c +++ b/MutualInformation.c @@ -9,8 +9,9 @@ ** ** Author: Adam Pocock ** Created 19/2/2010 +** Updated - 22/02/2014 - Added checking on calloc. ** -** Copyright 2010 Adam Pocock, The University Of Manchester +** Copyright 2010-2014 Adam Pocock, The University Of Manchester ** www.cs.manchester.ac.uk ** ** This file is part of MIToolbox. @@ -76,7 +77,7 @@ double calculateConditionalMutualInformation(double *dataVector, double *targetV { double mutualInformation = 0.0; double firstCondition, secondCondition; - double *mergedVector = (double *) CALLOC_FUNC(vectorLength,sizeof(double)); + double *mergedVector = (double *) checkedCalloc(vectorLength,sizeof(double)); mergeArrays(targetVector,conditionVector,mergedVector,vectorLength); diff --git a/RenyiEntropy.c b/RenyiEntropy.c index 32c5ff9..bf73b9d 100644 --- a/RenyiEntropy.c +++ b/RenyiEntropy.c @@ -1,5 +1,5 @@ /******************************************************************************* -** RenyiEntropy.cpp +** RenyiEntropy.c ** Part of the mutual information toolbox ** ** Contains functions to calculate the Renyi alpha entropy of a single variable @@ -8,8 +8,9 @@ ** ** Author: Adam Pocock ** Created 26/3/2010 +** Updated - 22/02/2014 - Added checking on calloc. ** -** Copyright 2010 Adam Pocock, The University Of Manchester +** Copyright 2010-2014 Adam Pocock, The University Of Manchester ** www.cs.manchester.ac.uk ** ** This file is part of MIToolbox. @@ -111,8 +112,8 @@ double calcCondRenyiEnt(double alpha, double *dataVector, double *conditionVecto ** first generate the seperate variables */ - double *seperateVectors = (double *) CALLOC_FUNC(uniqueInCondVector*vectorLength,sizeof(double)); - int *seperateVectorCount = (int *) CALLOC_FUNC(uniqueInCondVector,sizeof(int)); + double *seperateVectors = (double *) checkedCalloc(uniqueInCondVector*vectorLength,sizeof(double)); + int *seperateVectorCount = (int *) checkedCalloc(uniqueInCondVector,sizeof(int)); double seperateVectorProb = 0.0; int i,j; double entropy = 0.0; @@ -121,7 +122,7 @@ double calcCondRenyiEnt(double alpha, double *dataVector, double *conditionVecto double tempEntropy; ProbabilityState state; - double **seperateVectors2D = (double **) CALLOC_FUNC(uniqueInCondVector,sizeof(double*)); + double **seperateVectors2D = (double **) checkedCalloc(uniqueInCondVector,sizeof(double*)); for(j=0; j < uniqueInCondVector; j++) seperateVectors2D[j] = seperateVectors + (int)j*vectorLength; diff --git a/RenyiMIToolboxMex.c b/RenyiMIToolboxMex.c index aa92371..90e3ff7 100644 --- a/RenyiMIToolboxMex.c +++ b/RenyiMIToolboxMex.c @@ -1,6 +1,6 @@ /******************************************************************************* ** -** RenyiMIToolboxMex.cpp +** RenyiMIToolboxMex.c ** is the MATLAB entry point for the Renyi Entropy and MI MIToolbox functions ** when called from a MATLAB/OCTAVE script. ** @@ -23,7 +23,9 @@ ** along with MIToolbox. If not, see . ** *******************************************************************************/ + #include "MIToolbox.h" +#include "ArrayOperations.h" #include "RenyiEntropy.h" #include "RenyiMutualInformation.h" diff --git a/RenyiMutualInformation.c b/RenyiMutualInformation.c index dc6fd51..baa3f6a 100644 --- a/RenyiMutualInformation.c +++ b/RenyiMutualInformation.c @@ -1,5 +1,5 @@ /******************************************************************************* -** RenyiMutualInformation.cpp +** RenyiMutualInformation.c ** Part of the mutual information toolbox ** ** Contains functions to calculate the Renyi mutual information of @@ -30,6 +30,7 @@ *******************************************************************************/ #include "MIToolbox.h" +#include "ArrayOperations.h" #include "CalculateProbability.h" #include "RenyiEntropy.h" #include "RenyiMutualInformation.h" diff --git a/RenyiMutualInformation.h b/RenyiMutualInformation.h index 07eff09..d0d75ca 100644 --- a/RenyiMutualInformation.h +++ b/RenyiMutualInformation.h @@ -47,10 +47,11 @@ extern "C" { *******************************************************************************/ double calculateRenyiMIDivergence(double alpha, double *dataVector, double *targetVector, int vectorLength); -/* This function returns a different value to the alpha divergence mutual -** information, and thus is not a correct mutual information +/****************************************************************************** +** This function returns a different value to the alpha divergence mutual +** information, and thus is not a correct mutual information. +******************************************************************************/ double calculateRenyiMIJoint(double alpha, double *dataVector, double *targetVector, int vectorLength); -*/ #ifdef __cplusplus } diff --git a/WeightedMIToolboxMex.c b/WeightedMIToolboxMex.c index aab478f..71e64af 100644 --- a/WeightedMIToolboxMex.c +++ b/WeightedMIToolboxMex.c @@ -1,6 +1,6 @@ /******************************************************************************* ** -** WeightedMIToolboxMex.cpp +** WeightedMIToolboxMex.c ** is the MATLAB entry point for the WeightedMIToolbox functions when called ** from a MATLAB/OCTAVE script. ** @@ -23,6 +23,7 @@ ** along with MIToolbox. If not, see . ** *******************************************************************************/ + #include "MIToolbox.h" #include "ArrayOperations.h" #include "CalculateProbability.h" diff --git a/WeightedMutualInformation.c b/WeightedMutualInformation.c index 1dc5ae4..357ce75 100644 --- a/WeightedMutualInformation.c +++ b/WeightedMutualInformation.c @@ -77,7 +77,7 @@ double calculateWeightedConditionalMutualInformation(double *dataVector, double { double mutualInformation = 0.0; double firstCondition, secondCondition; - double *mergedVector = (double *) CALLOC_FUNC(vectorLength,sizeof(double)); + double *mergedVector = (double *) checkedCalloc(vectorLength,sizeof(double)); mergeArrays(targetVector,conditionVector,mergedVector,vectorLength);