Commit e2b8f785 authored by hybrid's avatar hybrid

Fix illegal memory access on push_front, and merge push methods into one base function.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2825 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 00709489
......@@ -30,6 +30,7 @@ public:
{
}
//! Constructs an array and allocates an initial chunk of memory.
/** \param start_count Amount of elements to pre-allocate. */
array(u32 start_count)
......@@ -48,7 +49,6 @@ public:
}
//! Destructor.
/** Frees allocated memory, if set_free_when_destroyed was not set to
false by the user before. */
......@@ -102,18 +102,44 @@ public:
strategy = newStrategy;
}
//! Adds an element at back of array.
/** If the array is too small to add this new element it is made bigger.
\param element: Element to add at the back of the array. */
void push_back(const T& element)
{
insert(element, used);
}
//! Adds an element at the front of the array.
/** If the array is to small to add this new element, the array is
made bigger. Please note that this is slow, because the whole array
needs to be copied for this.
\param element Element to add at the back of the array. */
void push_front(const T& element)
{
insert(element);
}
//! Insert item into array at specified position.
/** Please use this only if you know what you are doing (possible
performance loss). The preferred method of adding elements should be
push_back().
\param element: Element to be inserted
\param index: Where position to insert the new element. */
void insert(const T& element, u32 index=0)
{
_IRR_DEBUG_BREAK_IF(index>used) // access violation
if (used + 1 > allocated)
{
// this doesn't work if the element is in the same array. So
// we'll copy the element first to be sure we'll get no data
// corruption
// this doesn't work if the element is in the same
// array. So we'll copy the element first to be sure
// we'll get no data corruption
const T e(element);
T e(element);
//reallocate(used * 2 +1); // increase data block
// TA: okt, 2008. it's only allowed to alloc one element, if
// default constructor has to be called
......@@ -133,44 +159,19 @@ public:
}
reallocate( newAlloc);
// construct new element
// Attention!. in missing default constructors for faster alloc methods
allocator.construct(&data[used++], e); // data[used++] = e; // push_back
}
else
for (u32 i=used; i>index; --i)
{
//data[used++] = element;
// instead of using this here, we copy it the safe way:
allocator.construct(&data[used++], element);
}
is_sorted = false;
if (i<used)
allocator.destruct(&data[i]);
allocator.construct(&data[i], data[i-1]); // data[i] = data[i-1];
}
//! Adds an element at the front of the array.
/** If the array is to small to add this new element, the array is
made bigger. Please note that this is slow, because the whole array
needs to be copied for this.
\param element Element to add at the back of the array. */
void push_front(const T& element)
{
insert(element);
if (used > index)
allocator.destruct(&data[index]);
allocator.construct(&data[index], e); // data[index] = e;
}
//! Insert item into array at specified position.
/** Please use this only if you know what you are doing (possible
performance loss). The preferred method of adding elements should be
push_back().
\param element: Element to be inserted
\param index: Where position to insert the new element. */
void insert(const T& element, u32 index=0)
else
{
_IRR_DEBUG_BREAK_IF(index>used) // access violation
if (used + 1 > allocated)
reallocate(used +1);
for (u32 i=used; i>index; --i)
{
if (i<used)
......@@ -181,6 +182,7 @@ public:
if (used > index)
allocator.destruct(&data[index]);
allocator.construct(&data[index], element); // data[index] = element;
}
is_sorted = false;
++used;
}
......@@ -279,6 +281,7 @@ public:
return true;
}
//! Inequality operator
bool operator != (const array<T>& other) const
{
......@@ -473,6 +476,7 @@ public:
return index;
}
//! Finds an element in linear time, which is very slow.
/** Use binary_search for faster finding. Only works if ==operator is
implemented.
......@@ -559,8 +563,8 @@ public:
is_sorted = _is_sorted;
}
private:
private:
T* data;
u32 allocated;
u32 used;
......@@ -574,6 +578,5 @@ public:
} // end namespace core
} // end namespace irr
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment