Skip to content

Commit

Permalink
add additional old_size parameter to sg_realloc to enable new[] based…
Browse files Browse the repository at this point in the history
… resizing
  • Loading branch information
Soeren Sonnenburg committed Nov 18, 2012
1 parent 2e5dcdb commit afb2445
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/shogun/base/DynArray.h
Expand Up @@ -338,7 +338,7 @@ template <class T> class DynArray
T* p;

if (use_sg_mallocs)
p = SG_REALLOC(T, array, new_num_elements);
p = SG_REALLOC(T, array, num_elements, new_num_elements);
else
p = (T*) realloc(array, new_num_elements*sizeof(T));
if (p)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/svm/OnlineLibLinear.cpp
Expand Up @@ -131,7 +131,7 @@ void COnlineLibLinear::train_one(SGVector<float32_t> ex, float64_t label)

if (ex.vlen > w_dim)
{
w = SG_REALLOC(float32_t, w, ex.vlen);
w = SG_REALLOC(float32_t, w, w_dim, ex.vlen);
memset(&w[w_dim], 0, (ex.vlen - w_dim)*sizeof(float32_t));
w_dim = ex.vlen;
}
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/StringFileFeatures.cpp
Expand Up @@ -97,7 +97,7 @@ template <class ST> void CStringFileFeatures<ST>::fetch_meta_info_from_file(int3
{
if (CStringFeatures<ST>::num_vectors > buffer_size)
{
CStringFeatures<ST>::features = SG_REALLOC(SGString<ST>, CStringFeatures<ST>::features, buffer_size+granularity);
CStringFeatures<ST>::features = SG_REALLOC(SGString<ST>, CStringFeatures<ST>::features, buffer_size, buffer_size+granularity);
buffer_size+=granularity;
}

Expand All @@ -117,7 +117,7 @@ template <class ST> void CStringFileFeatures<ST>::fetch_meta_info_from_file(int3
if (!CStringFeatures<ST>::alphabet->check_alphabet_size() || !CStringFeatures<ST>::alphabet->check_alphabet())
CStringFileFeatures<ST>::cleanup();

CStringFeatures<ST>::features=SG_REALLOC(SGString<ST>, CStringFeatures<ST>::features, CStringFeatures<ST>::num_vectors);
CStringFeatures<ST>::features=SG_REALLOC(SGString<ST>, CStringFeatures<ST>::features, buffer_size, CStringFeatures<ST>::num_vectors);
}

template class CStringFileFeatures<bool>;
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/streaming/StreamingDotFeatures.cpp
Expand Up @@ -55,7 +55,7 @@ void CStreamingDotFeatures::expand_if_required(float32_t*& vec, int32_t &len)
int32_t dim = get_dim_feature_space();
if (dim > len)
{
vec = SG_REALLOC(float32_t, vec, dim);
vec = SG_REALLOC(float32_t, vec, len, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float32_t));
len = dim;
}
Expand All @@ -66,7 +66,7 @@ void CStreamingDotFeatures::expand_if_required(float64_t*& vec, int32_t &len)
int32_t dim = get_dim_feature_space();
if (dim > len)
{
vec = SG_REALLOC(float64_t, vec, dim);
vec = SG_REALLOC(float64_t, vec, len, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float64_t));
len = dim;
}
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/streaming/StreamingSparseFeatures.cpp
Expand Up @@ -71,7 +71,7 @@ void CStreamingSparseFeatures<T>::expand_if_required(float32_t*& vec, int32_t &l
int32_t dim = get_dim_feature_space();
if (dim > len)
{
vec = SG_REALLOC(float32_t, vec, dim);
vec = SG_REALLOC(float32_t, vec, len, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float32_t));
len = dim;
}
Expand All @@ -83,7 +83,7 @@ void CStreamingSparseFeatures<T>::expand_if_required(float64_t*& vec, int32_t &l
int32_t dim = get_dim_feature_space();
if (dim > len)
{
vec = SG_REALLOC(float64_t, vec, dim);
vec = SG_REALLOC(float64_t, vec, len, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float64_t));
len = dim;
}
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/features/streaming/StreamingVwFeatures.cpp
Expand Up @@ -91,7 +91,7 @@ void CStreamingVwFeatures::expand_if_required(float32_t*& vec, int32_t& len)
int32_t dim = 1 << env->num_bits;
if (dim > len)
{
vec = SG_REALLOC(float32_t, vec, dim);
vec = SG_REALLOC(float32_t, vec, len, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float32_t));
len = dim;
}
Expand All @@ -102,7 +102,7 @@ void CStreamingVwFeatures::expand_if_required(float64_t*& vec, int32_t& len)
int32_t dim = 1 << env->num_bits;
if (dim > len)
{
vec = SG_REALLOC(float64_t, vec, dim);
vec = SG_REALLOC(float64_t, vec, len, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float64_t));
len = dim;
}
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/io/streaming/StreamingAsciiFile.cpp
Expand Up @@ -89,7 +89,7 @@ void CStreamingAsciiFile::get_vector(sg_type*& vector, int32_t& num_feat) \
\
/* now copy data into vector */ \
if (old_len < num_feat) \
vector=SG_REALLOC(sg_type, vector, num_feat); \
vector=SG_REALLOC(sg_type, vector, old_len, num_feat); \
\
for (int32_t i=0; i<num_feat; i++) \
{ \
Expand Down Expand Up @@ -134,7 +134,7 @@ GET_VECTOR(get_longreal_vector, atoi, floatmax_t)
substring* feature_start = &words[0]; \
\
if (len > old_len) \
vector = SG_REALLOC(sg_type, vector, len); \
vector = SG_REALLOC(sg_type, vector, old_len, len); \
\
int32_t j=0; \
for (substring* i = feature_start; i != words.end; i++) \
Expand Down Expand Up @@ -207,7 +207,7 @@ GET_FLOAT_VECTOR(float64_t)
label=atof(items->get_element(0)); \
/* now copy rest of the data into vector */ \
if (old_len < num_feat - 1) \
vector=SG_REALLOC(sg_type, vector, num_feat-1); \
vector=SG_REALLOC(sg_type, vector, old_len, num_feat-1); \
\
for (int32_t i=1; i<num_feat; i++) \
{ \
Expand Down Expand Up @@ -255,7 +255,7 @@ GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t)
substring* feature_start = &words[1]; \
\
if (len > old_len) \
vector = SG_REALLOC(sg_type, vector, len); \
vector = SG_REALLOC(sg_type, vector, old_len, len); \
\
int32_t j=0; \
for (substring* i = feature_start; i != words.end; i++) \
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/SGVector.cpp
Expand Up @@ -94,7 +94,7 @@ template<class T> void SGVector<T>::set_element(const T& p_element, index_t inde

template<class T> void SGVector<T>::resize_vector(int32_t n)
{
vector=SG_REALLOC(T, vector, n);
vector=SG_REALLOC(T, vector, vlen, n);

if (n > vlen)
memset(&vector[vlen], 0, (n-vlen)*sizeof(T));
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/SGVector.h
Expand Up @@ -207,7 +207,7 @@ template<class T> class SGVector : public SGReferencedData
if (old_size==new_size)
return;

data = SG_REALLOC(T, data, new_size);
data = SG_REALLOC(T, data, old_size, new_size);
}

/// || x ||_2
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/lib/Trie.h
Expand Up @@ -520,8 +520,9 @@ IGNORE_IN_CLASSLIST template <class Trie> class CTrie : public CSGObject
return;
SG_DEBUG( "Extending TreeMem from %i to %i elements\n",
TreeMemPtrMax, (int32_t) ((float64_t)TreeMemPtrMax*1.2));
int32_t old_sz=TreeMemPtrMax;
TreeMemPtrMax = (int32_t) ((float64_t)TreeMemPtrMax*1.2);
TreeMem = SG_REALLOC(Trie, TreeMem, TreeMemPtrMax);
TreeMem = SG_REALLOC(Trie, TreeMem, old_sz, TreeMemPtrMax);
}

/** set weights in tree
Expand Down
12 changes: 7 additions & 5 deletions src/shogun/lib/external/shogun_libsvm.cpp
Expand Up @@ -158,7 +158,7 @@ int32_t Cache::get_data(const int32_t index, Qfloat **data, int32_t len)
}

// allocate new space
h->data = SG_REALLOC(Qfloat, h->data, len);
h->data = SG_REALLOC(Qfloat, h->data, h->len, len);
size -= more;
CMath::swap(h->len,len);
}
Expand Down Expand Up @@ -2306,9 +2306,10 @@ void svm_group_classes(
{
if(nr_class == max_nr_class)
{
int32_t old_max_nr_class=max_nr_class;
max_nr_class *= 2;
label=SG_REALLOC(int32_t, label,max_nr_class);
count=SG_REALLOC(int32_t, count,max_nr_class);
label=SG_REALLOC(int32_t, label,old_max_nr_class, max_nr_class);
count=SG_REALLOC(int32_t, count,old_max_nr_class, max_nr_class);
}
label[nr_class] = this_label;
count[nr_class] = 1;
Expand Down Expand Up @@ -2680,9 +2681,10 @@ const char *svm_check_parameter(
{
if(nr_class == max_nr_class)
{
int32_t old_max_nr_class = max_nr_class;
max_nr_class *= 2;
label=SG_REALLOC(int32_t, label, max_nr_class);
count=SG_REALLOC(int32_t, count, max_nr_class);
label=SG_REALLOC(int32_t, label, old_max_nr_class, max_nr_class);
count=SG_REALLOC(int32_t, count, old_max_nr_class, max_nr_class);
}
label[nr_class] = this_label;
count[nr_class] = 1;
Expand Down
18 changes: 14 additions & 4 deletions src/shogun/lib/memory.cpp
Expand Up @@ -271,9 +271,14 @@ template<> type* sg_generic_calloc<type >(size_t len, const char* file, int line
return new type[len](); \
} \
\
template<> type* sg_generic_realloc<type >(type* ptr, size_t len, const char* file, int line) \
template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len, const char* file, int line) \
{ \
return NULL; \
type* new_ptr = new type[len](); \
size_t min_len=CMath::min(old_len, len); \
for (size_t i=0; i<min_len; i++) \
new_ptr[i]=ptr[i]; \
delete[] ptr; \
return new_ptr; \
} \
\
template<> void sg_generic_free<type >(type* ptr) \
Expand All @@ -294,9 +299,14 @@ template<> type* sg_generic_calloc<type >(size_t len) \
return new type[len](); \
} \
\
template<> type* sg_generic_realloc<type >(type* ptr, size_t len) \
template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len) \
{ \
return NULL; \
type* new_ptr = new type[len](); \
size_t min_len=CMath::min(old_len, len); \
for (size_t i=0; i<min_len; i++) \
new_ptr[i]=ptr[i]; \
delete[] ptr; \
return new_ptr; \
} \
\
template<> void sg_generic_free<type >(type* ptr) \
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/lib/memory.h
Expand Up @@ -22,7 +22,7 @@
#ifdef TRACE_MEMORY_ALLOCS
#define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len), __FILE__, __LINE__)
#define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len), __FILE__, __LINE__)
#define SG_REALLOC(type, ptr, len) sg_generic_realloc<type>(ptr, size_t(len), __FILE__, __LINE__)
#define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len), __FILE__, __LINE__)
#define SG_FREE(ptr) sg_generic_free(ptr)

void* sg_malloc(size_t size, const char* file, int line);
Expand All @@ -39,7 +39,7 @@ template <class T> T* sg_generic_calloc(size_t len, const char* file, int line)
}

void* sg_realloc(void* ptr, size_t size, const char* file, int line);
template <class T> T* sg_generic_realloc(T* ptr, size_t len, const char* file, int line)
template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len, const char* file, int line)
{
return (T*) sg_realloc(ptr, sizeof(T)*len, file, line);
}
Expand All @@ -53,7 +53,7 @@ template <class T> void sg_generic_free(T* ptr)

#define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len))
#define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len))
#define SG_REALLOC(type, ptr, len) sg_generic_realloc<type>(ptr, size_t(len))
#define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len))
#define SG_FREE(ptr) sg_generic_free(ptr)

void* sg_malloc(size_t size);
Expand All @@ -63,7 +63,7 @@ template <class T> T* sg_generic_malloc(size_t len)
}

void* sg_realloc(void* ptr, size_t size);
template <class T> T* sg_generic_realloc(T* ptr, size_t len)
template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len)
{
return (T*) sg_realloc(ptr, sizeof(T)*len);
}
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/lib/v_array.h
Expand Up @@ -170,7 +170,7 @@ inline void v_array<T>::push(const T &new_elem)
size_t old_length = end_array - begin;
size_t new_length = 2 * old_length + 3;
//size_t new_length = old_length + 1;
begin = SG_REALLOC(T, begin, new_length);
begin = SG_REALLOC(T, begin, old_length, new_length);
end = begin + old_length;
end_array = begin + new_length;
}
Expand All @@ -185,7 +185,7 @@ inline void v_array<T>::push_many(const T* new_elem, size_t num)
size_t length = end - begin;
size_t new_length = CMath::max(2 * (size_t)(end_array - begin) + 3,
end - begin + num);
begin = SG_REALLOC(T, begin, new_length);
begin = SG_REALLOC(T, begin, length, new_length);
end = begin + length;
end_array = begin + new_length;
}
Expand All @@ -197,7 +197,7 @@ template<class T>
inline void v_array<T>::reserve(size_t length)
{
size_t old_length = end_array-begin;
begin = SG_REALLOC(T, begin, length);
begin = SG_REALLOC(T, begin, old_length, length);
if (old_length < length)
memset(begin + old_length, 0, (length - old_length)*sizeof(T));

Expand Down
14 changes: 7 additions & 7 deletions src/shogun/multiclass/LaRank.cpp
Expand Up @@ -172,13 +172,13 @@ namespace shogun
int32_t nl = CMath::max (256, ol);
while (nl < n)
nl = nl + nl;
self->i2r = SG_REALLOC (int32_t, self->i2r, nl);
self->r2i = SG_REALLOC (int32_t, self->r2i, nl);
self->rsize = SG_REALLOC (int32_t, self->rsize, nl);
self->qnext = SG_REALLOC (int32_t, self->qnext, (1 + nl));
self->qprev = SG_REALLOC (int32_t, self->qprev, (1 + nl));
self->rdiag = SG_REALLOC (float32_t, self->rdiag, nl);
self->rdata = SG_REALLOC (float32_t*, self->rdata, nl);
self->i2r = SG_REALLOC (int32_t, self->i2r, self->l, nl);
self->r2i = SG_REALLOC (int32_t, self->r2i, self->l, nl);
self->rsize = SG_REALLOC (int32_t, self->rsize, self->l, nl);
self->qnext = SG_REALLOC (int32_t, self->qnext, 1+self->l, (1 + nl));
self->qprev = SG_REALLOC (int32_t, self->qprev, 1+self->l, (1 + nl));
self->rdiag = SG_REALLOC (float32_t, self->rdiag, self->l, nl);
self->rdata = SG_REALLOC (float32_t*, self->rdata, self->l, nl);
self->rnext = self->qnext + 1;
self->rprev = self->qprev + 1;
for (i = ol; i < nl; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/preprocessor/HomogeneousKernelMap.cpp
Expand Up @@ -115,7 +115,7 @@ void CHomogeneousKernelMap::init()
size_t numElements = (tableHeight * tableWidth + 2*(1+m_order));
if (unsigned(m_table.vlen) != numElements) {
SG_DEBUG ("reallocating... %d -> %d\n", m_table.vlen, numElements);
m_table.vector = SG_REALLOC (float64_t, m_table.vector, numElements);
m_table.vector = SG_REALLOC (float64_t, m_table.vector, m_table.vlen, numElements);
m_table.vlen = numElements;
}

Expand Down
7 changes: 3 additions & 4 deletions src/shogun/structure/IntronList.cpp
Expand Up @@ -35,8 +35,7 @@ void CIntronList::init_list(int32_t* all_pos, int32_t len)
memcpy(m_all_pos, all_pos, len*sizeof(int32_t));
m_intron_list = SG_MALLOC(int32_t*, len);
m_quality_list = SG_MALLOC(int32_t*, len);
if (m_intron_list==NULL||m_quality_list==NULL)
SG_ERROR("IntronList: Out of mem 1");

//initialize all elements with an array of length one
int32_t* one;
for (int i=0;i<m_length;i++)
Expand Down Expand Up @@ -74,7 +73,7 @@ void CIntronList::read_introns(int32_t* start_pos, int32_t* end_pos, int32_t* qu
// intron list
//------------
int32_t from_list_len = m_intron_list[i][0];
int32_t* new_list = SG_REALLOC(int32_t, m_intron_list[i], (from_list_len+1));
int32_t* new_list = SG_REALLOC(int32_t, m_intron_list[i], from_list_len, (from_list_len+1));
if (new_list == NULL)
SG_ERROR("IntronList: Out of mem 4");
new_list[from_list_len]= start_pos[k];
Expand All @@ -85,7 +84,7 @@ void CIntronList::read_introns(int32_t* start_pos, int32_t* end_pos, int32_t* qu
int32_t q_list_len = m_quality_list[i][0];
//SG_PRINT("\t q_list_len:%i, from_list_len:%i \n",q_list_len, from_list_len);
ASSERT(q_list_len==from_list_len);
new_list = SG_REALLOC(int32_t, m_quality_list[i], (q_list_len+1));
new_list = SG_REALLOC(int32_t, m_quality_list[i], q_list_len, (q_list_len+1));
if (new_list == NULL)
SG_ERROR("IntronList: Out of mem 5");
new_list[q_list_len]= quality[k];
Expand Down

0 comments on commit afb2445

Please sign in to comment.