Commit b4d4c120 authored by incardon's avatar incardon
Browse files

Fixed memory_c

parent 4f3e04c3
......@@ -456,8 +456,6 @@ class grid_cpu
//! Is external
isExternal = true;
m.incRef();
//! Create and set the memory allocator
data.setMemory(m);
......@@ -650,6 +648,25 @@ class grid_cpu
this->swap(grid_new);
}
/*! \brief Remove one element valid only on 1D
*
*
*/
void remove(size_t key)
{
if (dim != 1)
{
#ifdef DEBUG
std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " trying to remove " << "\n";
#endif
return;
}
// It is safe to a memory copy
data.move(&get<0>());
}
/*! \brief Resize the space
*
* Resize the space to a new grid, the element are retained on the new grid,
......
......@@ -12,4 +12,10 @@ size_t delete_data;
// structure that store all the active pointer
std::map<byte_ptr, size_t> active_ptr;
// Running process id
size_t process_v_cl;
// Process to print
long int process_to_print = 0;
#endif
......@@ -2,6 +2,9 @@
#include <iostream>
#include <map>
#ifndef MEMLEAK_CHECK_HPP
#define MEMLEAK_CHECK_HPP
typedef unsigned char * byte_ptr;
#ifdef MEMLEAK_CHECK
......@@ -11,6 +14,9 @@ extern size_t delete_data;
extern std::map<byte_ptr,size_t> active_ptr;
extern size_t process_v_cl;
extern long int process_to_print;
/*! \brief Check and remove the active pointer
*
* Check and remove the pointer from the active list
......@@ -61,7 +67,8 @@ static void check_new(void * data, size_t sz)
// Add a new pointer
new_data++;
active_ptr[(byte_ptr)data] = sz;
std::cout << "New data: " << new_data << " " << data << "\n";
if (process_to_print < 0 || process_to_print == process_v_cl)
std::cout << "New data: " << new_data << " " << data << "\n";
}
/*! \brief check and delete a pointer
......@@ -77,7 +84,8 @@ static void check_delete(void * data)
// Delete the pointer
delete_data++;
remove_ptr(data);
std::cout << "Delete data: " << delete_data << " " << data << "\n";
if (process_to_print < 0 || process_to_print == process_v_cl)
std::cout << "Delete data: " << delete_data << " " << data << "\n";
}
/*! \brief check if the access is valid
......@@ -100,7 +108,8 @@ static void check_valid(void * ptr, size_t size_access)
// if there is no memory that satisfy the request
if (l_b == active_ptr.end())
{
std::cout << "Error invalid pointer: " << __FILE__ << " " << __LINE__ << " " << ptr << "\n";
if (process_to_print < 0 || process_to_print == process_v_cl)
std::cout << "Error invalid pointer: " << __FILE__ << " " << __LINE__ << " " << ptr << "\n";
return;
}
......@@ -110,7 +119,8 @@ static void check_valid(void * ptr, size_t size_access)
if (((unsigned char *)l_b->first) + sz < ((unsigned char *)ptr) + size_access)
{
std::cout << "Error invalid pointer: " << __FILE__ << " " << __LINE__ << " " << ptr << "\n";
if (process_to_print < 0 || process_to_print == process_v_cl)
std::cout << "Error invalid pointer: " << __FILE__ << " " << __LINE__ << " " << ptr << "\n";
}
}
......@@ -118,4 +128,5 @@ static void check_valid(void * ptr, size_t size_access)
#endif
#endif
......@@ -435,13 +435,53 @@ namespace openfpm
base.template resize<Memory>(sz);
}
//! copy the element
//! copy the added element
base.set(v_size,v);
//! increase the vector size
v_size++;
}
/*! \brief Remove one entry from the vector
*
* \param keys objects id to remove
* \param start key starting point
*
*/
void remove(openfpm::vector<size_t> keys, size_t start)
{
// Nothing to remove return
if (keys.size() <= start )
return;
size_t a_key = start;
size_t d_k = keys.get(a_key);
size_t s_k = keys.get(a_key) + 1;
// keys
while (s_k < size())
{
// s_k should always point to a key that is not going to be deleted
while (a_key < keys.size() && s_k == keys.get(a_key+1))
{
a_key++;
s_k = keys.get(a_key) + 1;
}
// In case of overflow
if (s_k >= size())
break;
set(d_k,get(s_k));
d_k++;
s_k++;
}
// re-calculate the vector size
v_size -= keys.size() - start;
}
/*! \brief Get an element of the vector
*
* Get an element of the vector
......
......@@ -59,6 +59,14 @@ class memory_c
void setMemory(memory & mem)
{
if (this->mem != NULL)
{
this->mem->decRef();
if (this->mem->ref() == 0)
delete(this->mem);
}
// this->mem->decRef();
mem.incRef();
this->mem = &mem;
}
......@@ -133,6 +141,7 @@ class memory_c
if (mem != NULL)
{
mem->decRef();
if (mem->ref() == 0)
delete(mem);
}
......@@ -227,8 +236,8 @@ struct mult<T,1>
*
*/
template<typename T>
class memory_c<multi_array<T>, memory>
template<typename T, typename D>
class memory_c<multi_array<T>, D>
{
//! define T
// typedef T type;
......@@ -283,6 +292,13 @@ class memory_c<multi_array<T>, memory>
void setMemory(memory & mem)
{
if (this->mem != NULL)
{
this->mem->decRef();
if (this->mem->ref() == 0)
delete(this->mem);
}
mem.incRef();
this->mem = &mem;
}
......@@ -338,15 +354,14 @@ class memory_c<multi_array<T>, memory>
//! basically we remove the index 0 of the multi_array
typedef boost::multi_array<base,size_p::value> type;
//! Reference to an object to allocate memory
D * mem;
//! object that represent the memory as an multi-dimensional array of objects T
boost::multi_array_ref<base,boost::mpl::size<T>::value> * mem_r;
//! Reference to an object to allocate memory
memory * mem;
//! constructor
memory_c()
{}
memory_c():mem(NULL),mem_r(NULL){}
//! destructor
~memory_c()
......
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