1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-18 00:54:19 +00:00

Reduced the memory footprint of the array classes.

This commit is contained in:
Julian Storer 2010-02-06 09:16:20 +00:00
parent ded4826413
commit 31a102008d
11 changed files with 542 additions and 489 deletions

View file

@ -104,7 +104,7 @@ public:
*/
void clear()
{
lock.enter();
data.enter();
while (numUsed > 0)
if (data.elements [--numUsed] != 0)
@ -113,7 +113,7 @@ public:
jassert (numUsed == 0);
data.setAllocatedSize (0);
lock.exit();
data.exit();
}
/** Returns the current number of objects in the array. */
@ -132,11 +132,11 @@ public:
*/
inline const ReferenceCountedObjectPtr<ObjectClass> operator[] (const int index) const throw()
{
lock.enter();
data.enter();
const ReferenceCountedObjectPtr<ObjectClass> result ((((unsigned int) index) < (unsigned int) numUsed)
? data.elements [index]
: (ObjectClass*) 0);
lock.exit();
data.exit();
return result;
}
@ -147,10 +147,10 @@ public:
*/
inline const ReferenceCountedObjectPtr<ObjectClass> getUnchecked (const int index) const throw()
{
lock.enter();
data.enter();
jassert (((unsigned int) index) < (unsigned int) numUsed);
const ReferenceCountedObjectPtr<ObjectClass> result (data.elements [index]);
lock.exit();
data.exit();
return result;
}
@ -161,10 +161,10 @@ public:
*/
inline const ReferenceCountedObjectPtr<ObjectClass> getFirst() const throw()
{
lock.enter();
data.enter();
const ReferenceCountedObjectPtr<ObjectClass> result ((numUsed > 0) ? data.elements [0]
: (ObjectClass*) 0);
lock.exit();
data.exit();
return result;
}
@ -176,10 +176,10 @@ public:
*/
inline const ReferenceCountedObjectPtr<ObjectClass> getLast() const throw()
{
lock.enter();
data.enter();
const ReferenceCountedObjectPtr<ObjectClass> result ((numUsed > 0) ? data.elements [numUsed - 1]
: (ObjectClass*) 0);
lock.exit();
data.exit();
return result;
}
@ -194,7 +194,7 @@ public:
{
int result = -1;
lock.enter();
data.enter();
ObjectClass** e = data.elements;
for (int i = numUsed; --i >= 0;)
@ -208,7 +208,7 @@ public:
++e;
}
lock.exit();
data.exit();
return result;
}
@ -219,21 +219,21 @@ public:
*/
bool contains (const ObjectClass* const objectToLookFor) const throw()
{
lock.enter();
data.enter();
ObjectClass** e = data.elements;
for (int i = numUsed; --i >= 0;)
{
if (objectToLookFor == *e)
{
lock.exit();
data.exit();
return true;
}
++e;
}
lock.exit();
data.exit();
return false;
}
@ -246,14 +246,14 @@ public:
*/
void add (ObjectClass* const newObject) throw()
{
lock.enter();
data.enter();
data.ensureAllocatedSize (numUsed + 1);
data.elements [numUsed++] = newObject;
if (newObject != 0)
newObject->incReferenceCount();
lock.exit();
data.exit();
}
/** Inserts a new object into the array at the given index.
@ -274,7 +274,7 @@ public:
{
if (indexToInsertAt >= 0)
{
lock.enter();
data.enter();
if (indexToInsertAt > numUsed)
indexToInsertAt = numUsed;
@ -293,7 +293,7 @@ public:
newObject->incReferenceCount();
++numUsed;
lock.exit();
data.exit();
}
else
{
@ -310,12 +310,12 @@ public:
*/
void addIfNotAlreadyThere (ObjectClass* const newObject) throw()
{
lock.enter();
data.enter();
if (! contains (newObject))
add (newObject);
lock.exit();
data.exit();
}
/** Replaces an object in the array with a different one.
@ -335,7 +335,7 @@ public:
{
if (indexToChange >= 0)
{
lock.enter();
data.enter();
if (newObject != 0)
newObject->incReferenceCount();
@ -353,7 +353,7 @@ public:
data.elements [numUsed++] = newObject;
}
lock.exit();
data.exit();
}
}
@ -371,7 +371,7 @@ public:
int numElementsToAdd = -1) throw()
{
arrayToAddFrom.lockArray();
lock.enter();
data.enter();
if (startIndex < 0)
{
@ -390,7 +390,7 @@ public:
add (arrayToAddFrom.getUnchecked (startIndex++));
}
lock.exit();
data.exit();
arrayToAddFrom.unlockArray();
}
@ -409,9 +409,9 @@ public:
void addSorted (ElementComparator& comparator,
ObjectClass* newObject) throw()
{
lock.enter();
data.enter();
insert (findInsertIndexInSortedArray (comparator, (ObjectClass**) data.elements, newObject, 0, numUsed), newObject);
lock.exit();
data.exit();
}
/** Inserts or replaces an object in the array, assuming it is sorted.
@ -423,7 +423,7 @@ public:
void addOrReplaceSorted (ElementComparator& comparator,
ObjectClass* newObject) throw()
{
lock.enter();
data.enter();
const int index = findInsertIndexInSortedArray (comparator, (ObjectClass**) data.elements, newObject, 0, numUsed);
if (index > 0 && comparator.compareElements (newObject, data.elements [index - 1]) == 0)
@ -431,7 +431,7 @@ public:
else
insert (index, newObject); // no match, so insert the new one
lock.exit();
data.exit();
}
//==============================================================================
@ -450,7 +450,7 @@ public:
*/
void remove (const int indexToRemove)
{
lock.enter();
data.enter();
if (((unsigned int) indexToRemove) < (unsigned int) numUsed)
{
@ -469,7 +469,7 @@ public:
minimiseStorageOverheads();
}
lock.exit();
data.exit();
}
/** Removes the first occurrence of a specified object from the array.
@ -482,9 +482,9 @@ public:
*/
void removeObject (ObjectClass* const objectToRemove)
{
lock.enter();
data.enter();
remove (indexOf (objectToRemove));
lock.exit();
data.exit();
}
/** Removes a range of objects from the array.
@ -505,7 +505,7 @@ public:
void removeRange (const int startIndex,
const int numberToRemove)
{
lock.enter();
data.enter();
const int start = jlimit (0, numUsed, startIndex);
const int end = jlimit (0, numUsed, startIndex + numberToRemove);
@ -537,7 +537,7 @@ public:
minimiseStorageOverheads();
}
lock.exit();
data.exit();
}
/** Removes the last n objects from the array.
@ -550,7 +550,7 @@ public:
*/
void removeLast (int howManyToRemove = 1)
{
lock.enter();
data.enter();
if (howManyToRemove > numUsed)
howManyToRemove = numUsed;
@ -558,7 +558,7 @@ public:
while (--howManyToRemove >= 0)
remove (numUsed - 1);
lock.exit();
data.exit();
}
/** Swaps a pair of objects in the array.
@ -569,7 +569,7 @@ public:
void swap (const int index1,
const int index2) throw()
{
lock.enter();
data.enter();
if (((unsigned int) index1) < (unsigned int) numUsed
&& ((unsigned int) index2) < (unsigned int) numUsed)
@ -578,7 +578,7 @@ public:
data.elements [index2]);
}
lock.exit();
data.exit();
}
/** Moves one of the objects to a different position.
@ -599,7 +599,7 @@ public:
{
if (currentIndex != newIndex)
{
lock.enter();
data.enter();
if (((unsigned int) currentIndex) < (unsigned int) numUsed)
{
@ -624,7 +624,7 @@ public:
data.elements [newIndex] = value;
}
lock.exit();
data.exit();
}
}
@ -636,12 +636,12 @@ public:
*/
void swapWithArray (ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse>& otherArray) throw()
{
lock.enter();
otherArray.lock.enter();
data.enter();
otherArray.data.enter();
data.swapWith (otherArray.data);
swapVariables (numUsed, otherArray.numUsed);
otherArray.lock.exit();
lock.exit();
otherArray.data.exit();
data.exit();
}
//==============================================================================
@ -652,7 +652,7 @@ public:
bool operator== (const ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse>& other) const throw()
{
other.lockArray();
lock.enter();
data.enter();
bool result = numUsed == other.numUsed;
@ -668,7 +668,7 @@ public:
}
}
lock.exit();
data.exit();
other.unlockArray();
return result;
@ -717,9 +717,9 @@ public:
(void) comparator; // if you pass in an object with a static compareElements() method, this
// avoids getting warning messages about the parameter being unused
lock.enter();
data.enter();
sortArray (comparator, (ObjectClass**) data.elements, 0, size() - 1, retainOrderOfEquivalentItems);
lock.exit();
data.exit();
}
//==============================================================================
@ -731,9 +731,9 @@ public:
*/
void minimiseStorageOverheads() throw()
{
lock.enter();
data.enter();
data.shrinkToNoMoreThan (numUsed);
lock.exit();
data.exit();
}
//==============================================================================
@ -746,7 +746,7 @@ public:
*/
void lockArray() const throw()
{
lock.enter();
data.enter();
}
/** Unlocks the array's CriticalSection.
@ -758,7 +758,7 @@ public:
*/
void unlockArray() const throw()
{
lock.exit();
data.exit();
}
@ -766,9 +766,8 @@ public:
juce_UseDebuggingNewOperator
private:
ArrayAllocationBase <ObjectClass*> data;
ArrayAllocationBase <ObjectClass*, TypeOfCriticalSectionToUse> data;
int numUsed;
TypeOfCriticalSectionToUse lock;
};