Commit 9b040cd7 authored by Davis King's avatar Davis King

Added steal_memory() to the matrix.

parent 74a3950c
......@@ -1169,6 +1169,12 @@ namespace dlib
}
std::unique_ptr<T[]> steal_memory(
)
{
return data.steal_memory();
}
matrix& operator=(const std::initializer_list<T>& l)
{
matrix temp(l);
......
......@@ -480,6 +480,21 @@ namespace dlib
- #nc() == 1
!*/
std::unique_ptr<T[]> steal_memory(
);
/*!
requires
- NR*NC==0
(i.e. this array isn't statically sized)
ensures
- Returns a pointer containing the memory block underlying this matrix.
After calling steal_memory() this matrix doesn't own the memory anymore
and is automatically set to the empty matrix.
- The returned pointer points to an array of size() T objects and in
particular is the pointer &(*this)(0,0).
- #size() == 0
!*/
template <typename U, size_t len>
matrix& operator= (
U (&array)[len]
......
......@@ -336,6 +336,14 @@ namespace dlib
nr_ = nr;
}
std::unique_ptr<T[]> steal_memory()
{
auto ret = pool.extract_array(data);
data = nullptr;
nr_ = 0;
return ret;
}
#ifdef MATLAB_MEX_FILE
void _private_set_mxArray ( mxArray* ) { DLIB_CASSERT(false, "This function should never be called."); }
mxArray* _private_release_mxArray(){DLIB_CASSERT(false, "This function should never be called."); }
......@@ -421,6 +429,14 @@ namespace dlib
nc_ = nc;
}
std::unique_ptr<T[]> steal_memory()
{
auto ret = pool.extract_array(data);
data = nullptr;
nc_ = 0;
return ret;
}
#ifdef MATLAB_MEX_FILE
void _private_set_mxArray ( mxArray* ) { DLIB_CASSERT(false, "This function should never be called."); }
mxArray* _private_release_mxArray(){DLIB_CASSERT(false, "This function should never be called."); }
......@@ -508,6 +524,15 @@ namespace dlib
nc_ = nc;
}
std::unique_ptr<T[]> steal_memory()
{
auto ret = pool.extract_array(data);
data = nullptr;
nr_ = 0;
nc_ = 0;
return ret;
}
#ifdef MATLAB_MEX_FILE
void _private_set_mxArray ( mxArray* ) { DLIB_CASSERT(false, "This function should never be called."); }
mxArray* _private_release_mxArray(){DLIB_CASSERT(false, "This function should never be called."); }
......@@ -783,6 +808,14 @@ namespace dlib
nr_ = nr;
}
std::unique_ptr<T[]> steal_memory()
{
auto ret = pool.extract_array(data);
data = nullptr;
nr_ = 0;
return ret;
}
#ifdef MATLAB_MEX_FILE
void _private_set_mxArray ( mxArray* ) { DLIB_CASSERT(false, "This function should never be called."); }
mxArray* _private_release_mxArray(){DLIB_CASSERT(false, "This function should never be called."); }
......@@ -868,6 +901,14 @@ namespace dlib
nc_ = nc;
}
std::unique_ptr<T[]> steal_memory()
{
auto ret = pool.extract_array(data);
data = nullptr;
nc_ = 0;
return ret;
}
#ifdef MATLAB_MEX_FILE
void _private_set_mxArray ( mxArray* ) { DLIB_CASSERT(false, "This function should never be called."); }
mxArray* _private_release_mxArray(){DLIB_CASSERT(false, "This function should never be called."); }
......@@ -962,6 +1003,15 @@ namespace dlib
nc_ = nc;
}
std::unique_ptr<T[]> steal_memory()
{
auto ret = pool.extract_array(data);
data = nullptr;
nr_ = 0;
nc_ = 0;
return ret;
}
private:
T* data;
long nr_;
......@@ -1104,6 +1154,16 @@ namespace dlib
nc_ = nc;
}
std::unique_ptr<T[]> steal_memory()
{
DLIB_CASSERT(!owned_by_matlab, "You can't steal the memory from a matrix if it's owned by MATLAB.");
std::unique_ptr<T[]> ret(data);
data = nullptr;
nr_ = 0;
nc_ = 0;
return ret;
}
private:
double* data;
long nr_;
......@@ -1247,6 +1307,16 @@ namespace dlib
nc_ = nc;
}
std::unique_ptr<T[]> steal_memory()
{
DLIB_CASSERT(!owned_by_matlab, "You can't steal the memory from a matrix if it's owned by MATLAB.");
std::unique_ptr<T[]> ret(data);
data = nullptr;
nr_ = 0;
nc_ = 0;
return ret;
}
private:
float* data;
long nr_;
......
......@@ -4,6 +4,7 @@
#define DLIB_MEMORY_MANAGER_STATELESs_1_
#include "memory_manager_stateless_kernel_abstract.h"
#include <memory>
namespace dlib
{
......@@ -63,6 +64,20 @@ namespace dlib
void swap (memory_manager_stateless_kernel_1&)
{}
std::unique_ptr<T> extract(
T* item
)
{
return std::unique_ptr<T>(item);
}
std::unique_ptr<T[]> extract_array(
T* item
)
{
return std::unique_ptr<T[]>(item);
}
private:
// restricted functions
......
......@@ -4,6 +4,7 @@
#ifdef DLIB_MEMORY_MANAGER_STATELESs_ABSTRACT_
#include "../algs.h"
#include <memory>
namespace dlib
{
......@@ -113,10 +114,40 @@ namespace dlib
/*!
ensures
- this function has no effect on *this or item. It is just provided
to make this object's interface more compatable with the other
to make this object's interface more compatible with the other
memory managers.
!*/
std::unique_ptr<T> extract(
T* item
);
/*!
requires
- item == is a pointer to memory that was obtained from a call to
allocate().
ensures
- returns a unique_ptr that owns item. That is, if the returned ptr is
PTR then PTR.get() == item. Therefore, this function extracts item
from the memory manager's internal pool. Therefore, you shouldn't
call deallocate(item) after this.
- Note that not all memory managers implement extract().
!*/
std::unique_ptr<T[]> extract_array(
T* item
);
/*!
requires
- item == is a pointer to memory that was obtained from a call to
allocate_array().
ensures
- returns a unique_ptr that owns item. That is, if the returned ptr is
PTR then PTR.get() == item. Therefore, this function extracts item
from the memory manager's internal pool. Therefore, you shouldn't
call deallocate_array(item) after this.
- Note that not all memory managers implement extract().
!*/
private:
// restricted functions
......
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