summaryrefslogtreecommitdiff
path: root/src/context
diff options
context:
space:
mode:
authorTim King <taking@cs.nyu.edu>2010-02-26 18:40:08 +0000
committerTim King <taking@cs.nyu.edu>2010-02-26 18:40:08 +0000
commit12cc3d32407cabbc8c3ed3d980199a020b61a883 (patch)
tree134f3d0b284f49038fbc594401f7f9ea54b52e80 /src/context
parent73265c6f26b735894375aa9d793d7165848b16e7 (diff)
Fixed a bug in CDList reallocation. (Also corrected a couple whitespace problems.)
Diffstat (limited to 'src/context')
-rw-r--r--src/context/context.h35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/context/context.h b/src/context/context.h
index 403235bc9..a12eb11a5 100644
--- a/src/context/context.h
+++ b/src/context/context.h
@@ -20,6 +20,7 @@
#include "util/Assert.h"
#include <cstdlib>
#include <cstring>
+#include <new>
namespace CVC4 {
namespace context {
@@ -569,7 +570,7 @@ public:
* For convenience, define operator= that takes an object of type T.
*/
CDO<T>& operator=(const T& data) { set(data); return *this; }
-
+
}; /* class CDO */
/**
@@ -623,13 +624,13 @@ class CDList :public ContextObj {
if (d_callDestructor) {
unsigned size = ((CDList<T>*)data)->d_size;
while (d_size != size) {
- --d_size;
- d_list[d_size].~T();
+ --d_size;
+ d_list[d_size].~T();
}
}
else {
d_size = ((CDList<T>*)data)->d_size;
- }
+ }
}
/**
@@ -642,23 +643,25 @@ class CDList :public ContextObj {
/**
* Reallocate the array with more space.
+ * Throws bad_alloc if memory allocation fails.
*/
void grow() {
if (d_list == NULL) {
// Allocate an initial list if one does not yet exist
d_sizeAlloc = 10;
d_list = (T*)malloc(sizeof(T)*d_sizeAlloc);
+ if(d_list == NULL){
+ throw std::bad_alloc();
+ }
}
else {
// Allocate a new array with double the size
d_sizeAlloc *= 2;
- T* newList = (T*)malloc(sizeof(T)*d_sizeAlloc);
-
- // Copy the old data
- memcpy(d_list, newList, sizeof(T)*d_size);
-
- // Free the old list
- free(d_list);
+ T* tmpList = (T*)realloc(d_list, sizeof(T)*d_sizeAlloc);
+ if(tmpList == NULL){
+ throw std::bad_alloc();
+ }
+ d_list = tmpList;
}
}
@@ -667,7 +670,7 @@ public:
* Main constructor: d_list starts as NULL, size is 0
*/
CDList(Context* context, bool callDestructor = true)
- : ContextObj(context), d_list(NULL), d_callDestructor(callDestructor),
+ : ContextObj(context), d_list(NULL), d_callDestructor(callDestructor),
d_size(0), d_sizeAlloc(0) { }
/**
@@ -676,10 +679,10 @@ public:
~CDList() {
if(d_list != NULL) {
if (d_callDestructor) {
- while (d_size != 0) {
- --d_size;
- d_list[d_size].~T();
- }
+ while (d_size != 0) {
+ --d_size;
+ d_list[d_size].~T();
+ }
}
delete d_list;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback