This commit is contained in:
Davis King 2016-07-21 22:33:27 -04:00
commit 5ecd904d76
5 changed files with 339 additions and 294 deletions

View File

@ -54,7 +54,7 @@ INCLUDE_DIRECTORIES("${dlib_matlab_binding_path}")
# Also add dlib to the include search path
INCLUDE_DIRECTORIES(${dlib_matlab_binding_path}/../..)
include(${dlib_matlab_binding_path}/../add_global_compiler_switch.cmake)
include(${dlib_matlab_binding_path}/../cmake_utils/add_global_compiler_switch.cmake)
add_global_define(MATLAB_MEX_FILE)
# Determine the path to our CMakeLists.txt file. This is the file that

View File

@ -1044,7 +1044,7 @@ namespace mex_binding
matrix_colmajor& item
)
{
if(!item._private_is_persistent())
if(item._private_is_owned_by_matlab())
{
// Don't need to do a copy if it's this kind of matrix since we can just
// pull the underlying mxArray out directly and thus avoid a copy.
@ -1065,7 +1065,7 @@ namespace mex_binding
fmatrix_colmajor& item
)
{
if(!item._private_is_persistent())
if(item._private_is_owned_by_matlab())
{
// Don't need to do a copy if it's this kind of matrix since we can just
// pull the underlying mxArray out directly and thus avoid a copy.
@ -1152,10 +1152,10 @@ namespace mex_binding
// ----------------------------------------------------------------------------------------
template <typename T>
void mark_non_persistent (const T&){}
void mark_owned_by_matlab (const T&){}
void mark_non_persistent(matrix_colmajor& item) { item._private_mark_non_persistent(); }
void mark_non_persistent(fmatrix_colmajor& item) { item._private_mark_non_persistent(); }
void mark_owned_by_matlab(matrix_colmajor& item) { item._private_mark_owned_by_matlab(); }
void mark_owned_by_matlab(fmatrix_colmajor& item) { item._private_mark_owned_by_matlab(); }
// ----------------------------------------------------------------------------------------
@ -1178,7 +1178,7 @@ namespace mex_binding
typename basic_type<arg1_type>::type A1;
mark_non_persistent(A1);
mark_owned_by_matlab(A1);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1206,8 +1206,8 @@ namespace mex_binding
typename basic_type<arg1_type>::type A1;
typename basic_type<arg2_type>::type A2;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1239,9 +1239,9 @@ namespace mex_binding
typename basic_type<arg2_type>::type A2;
typename basic_type<arg3_type>::type A3;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1277,10 +1277,10 @@ namespace mex_binding
typename basic_type<arg3_type>::type A3;
typename basic_type<arg4_type>::type A4;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1321,11 +1321,11 @@ namespace mex_binding
typename basic_type<arg4_type>::type A4;
typename basic_type<arg5_type>::type A5;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1371,12 +1371,12 @@ namespace mex_binding
typename basic_type<arg5_type>::type A5;
typename basic_type<arg6_type>::type A6;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1426,13 +1426,13 @@ namespace mex_binding
typename basic_type<arg6_type>::type A6;
typename basic_type<arg7_type>::type A7;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1486,14 +1486,14 @@ namespace mex_binding
typename basic_type<arg7_type>::type A7;
typename basic_type<arg8_type>::type A8;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1551,15 +1551,15 @@ namespace mex_binding
typename basic_type<arg8_type>::type A8;
typename basic_type<arg9_type>::type A9;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1622,16 +1622,16 @@ namespace mex_binding
typename basic_type<arg9_type>::type A9;
typename basic_type<arg10_type>::type A10;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1696,17 +1696,17 @@ namespace mex_binding
typename basic_type<arg10_type>::type A10;
typename basic_type<arg11_type>::type A11;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1775,18 +1775,18 @@ namespace mex_binding
typename basic_type<arg11_type>::type A11;
typename basic_type<arg12_type>::type A12;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1859,19 +1859,19 @@ namespace mex_binding
typename basic_type<arg12_type>::type A12;
typename basic_type<arg13_type>::type A13;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -1948,20 +1948,20 @@ namespace mex_binding
typename basic_type<arg13_type>::type A13;
typename basic_type<arg14_type>::type A14;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -2042,21 +2042,21 @@ namespace mex_binding
typename basic_type<arg14_type>::type A14;
typename basic_type<arg15_type>::type A15;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_non_persistent(A15);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
mark_owned_by_matlab(A15);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -2141,22 +2141,22 @@ namespace mex_binding
typename basic_type<arg15_type>::type A15;
typename basic_type<arg16_type>::type A16;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_non_persistent(A15);
mark_non_persistent(A16);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
mark_owned_by_matlab(A15);
mark_owned_by_matlab(A16);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -2245,23 +2245,23 @@ namespace mex_binding
typename basic_type<arg16_type>::type A16;
typename basic_type<arg17_type>::type A17;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_non_persistent(A15);
mark_non_persistent(A16);
mark_non_persistent(A17);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
mark_owned_by_matlab(A15);
mark_owned_by_matlab(A16);
mark_owned_by_matlab(A17);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -2354,24 +2354,24 @@ namespace mex_binding
typename basic_type<arg17_type>::type A17;
typename basic_type<arg18_type>::type A18;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_non_persistent(A15);
mark_non_persistent(A16);
mark_non_persistent(A17);
mark_non_persistent(A18);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
mark_owned_by_matlab(A15);
mark_owned_by_matlab(A16);
mark_owned_by_matlab(A17);
mark_owned_by_matlab(A18);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -2468,25 +2468,25 @@ namespace mex_binding
typename basic_type<arg18_type>::type A18;
typename basic_type<arg19_type>::type A19;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_non_persistent(A15);
mark_non_persistent(A16);
mark_non_persistent(A17);
mark_non_persistent(A18);
mark_non_persistent(A19);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
mark_owned_by_matlab(A15);
mark_owned_by_matlab(A16);
mark_owned_by_matlab(A17);
mark_owned_by_matlab(A18);
mark_owned_by_matlab(A19);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;
@ -2587,26 +2587,26 @@ namespace mex_binding
typename basic_type<arg19_type>::type A19;
typename basic_type<arg20_type>::type A20;
mark_non_persistent(A1);
mark_non_persistent(A2);
mark_non_persistent(A3);
mark_non_persistent(A4);
mark_non_persistent(A5);
mark_non_persistent(A6);
mark_non_persistent(A7);
mark_non_persistent(A8);
mark_non_persistent(A9);
mark_non_persistent(A10);
mark_non_persistent(A11);
mark_non_persistent(A12);
mark_non_persistent(A13);
mark_non_persistent(A14);
mark_non_persistent(A15);
mark_non_persistent(A16);
mark_non_persistent(A17);
mark_non_persistent(A18);
mark_non_persistent(A19);
mark_non_persistent(A20);
mark_owned_by_matlab(A1);
mark_owned_by_matlab(A2);
mark_owned_by_matlab(A3);
mark_owned_by_matlab(A4);
mark_owned_by_matlab(A5);
mark_owned_by_matlab(A6);
mark_owned_by_matlab(A7);
mark_owned_by_matlab(A8);
mark_owned_by_matlab(A9);
mark_owned_by_matlab(A10);
mark_owned_by_matlab(A11);
mark_owned_by_matlab(A12);
mark_owned_by_matlab(A13);
mark_owned_by_matlab(A14);
mark_owned_by_matlab(A15);
mark_owned_by_matlab(A16);
mark_owned_by_matlab(A17);
mark_owned_by_matlab(A18);
mark_owned_by_matlab(A19);
mark_owned_by_matlab(A20);
int i = 0;
if (i < nrhs && is_input_type<arg1_type>::value) {validate_and_populate_arg(i,prhs[i],A1); ++i;} ELSE_ASSIGN_ARG_1;

View File

@ -434,9 +434,20 @@ namespace dlib
stderr_pipe.close();
char* argv[] = {(char*)program_name, nullptr};
char* envp[] = {nullptr};
char* cudadevs = getenv("CUDA_VISIBLE_DEVICES");
if (cudadevs)
{
std::string extra = std::string("CUDA_VISIBLE_DEVICES=") + cudadevs;
char* envp[] = {(char*)extra.c_str(), nullptr};
execve(argv[0], argv, envp);
}
else
{
char* envp[] = {nullptr};
execve(argv[0], argv, envp);
}
execve(argv[0], argv, envp);
// If launching the child didn't work then bail immediately so the parent
// process has no chance to get tweaked out (*cough* MATLAB *cough*).
_Exit(1);

View File

@ -1174,8 +1174,8 @@ namespace dlib
{
#ifdef MATLAB_MEX_FILE
// You can't move memory around when compiled in a matlab mex file and the
// different locations have different persistence settings.
if (data._private_is_persistent() == item.data._private_is_persistent())
// different locations have different ownership settings.
if (data._private_is_owned_by_matlab() == item.data._private_is_owned_by_matlab())
{
swap(item);
}
@ -1195,8 +1195,8 @@ namespace dlib
{
#ifdef MATLAB_MEX_FILE
// You can't move memory around when compiled in a matlab mex file and the
// different locations have different persistence settings.
if (data._private_is_persistent() == rhs.data._private_is_persistent())
// different locations have different ownership settings.
if (data._private_is_owned_by_matlab() == rhs.data._private_is_owned_by_matlab())
{
swap(rhs);
}
@ -1343,14 +1343,14 @@ namespace dlib
return data._private_release_mxArray();
}
void _private_mark_non_persistent()
void _private_mark_owned_by_matlab()
{
data._private_mark_non_persistent();
data._private_mark_owned_by_matlab();
}
bool _private_is_persistent()
bool _private_is_owned_by_matlab()
{
return data._private_is_persistent();
return data._private_is_owned_by_matlab();
}
#endif

View File

@ -187,8 +187,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -257,8 +257,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -339,8 +339,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -424,8 +424,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -511,8 +511,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
T* data;
@ -634,8 +634,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -704,8 +704,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -786,8 +786,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -871,8 +871,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
private:
@ -938,8 +938,8 @@ namespace dlib
#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."); }
void _private_mark_non_persistent() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_persistent() const { return true; }
void _private_mark_owned_by_matlab() {DLIB_CASSERT(false, "This function should never be called."); }
bool _private_is_owned_by_matlab() const { return false; }
#endif
long nr (
@ -981,14 +981,22 @@ namespace dlib
const static long NC = num_cols;
layout (
): data(0), nr_(0), nc_(0), make_persistent(true),set_by_private_set_mxArray(false),mem(0) { }
): data(0), nr_(0), nc_(0), owned_by_matlab(false),set_by_private_set_mxArray(false),mem(0) { }
~layout ()
{
if (!set_by_private_set_mxArray && mem)
if (owned_by_matlab)
{
mxDestroyArray(mem);
mem = 0;
if (!set_by_private_set_mxArray && mem)
{
mxDestroyArray(mem);
mem = 0;
data = 0;
}
}
else if (data)
{
delete [] data;
data = 0;
}
}
@ -1015,9 +1023,11 @@ namespace dlib
mxArray* mem_
)
{
DLIB_CASSERT(mem == 0 && data == 0,"You can't call this function on an already allocated matrix.");
// We don't own the pointer, so make note of that so we won't try to free
// it.
set_by_private_set_mxArray = true;
owned_by_matlab = true;
mem = mem_;
data = mxGetPr(mem);
nr_ = mxGetM(mem);
@ -1026,7 +1036,7 @@ namespace dlib
mxArray* _private_release_mxArray()
{
DLIB_CASSERT(!make_persistent,"");
DLIB_CASSERT(owned_by_matlab,"");
mxArray* temp = mem;
mem = 0;
set_by_private_set_mxArray = false;
@ -1036,21 +1046,21 @@ namespace dlib
return temp;
}
void _private_mark_non_persistent()
void _private_mark_owned_by_matlab()
{
DLIB_CASSERT(mem == 0,"You can't convert a persistent matlab array to non-persistent.");
make_persistent = false;
DLIB_CASSERT(mem == 0 && data == 0,"You can't say a matrix should be owned by matlab after it's been allocated.");
owned_by_matlab = true;
}
bool _private_is_persistent() const
bool _private_is_owned_by_matlab() const
{
return make_persistent;
return owned_by_matlab;
}
void swap(
layout& item
)
{
std::swap(item.make_persistent,make_persistent);
std::swap(item.owned_by_matlab,owned_by_matlab);
std::swap(item.set_by_private_set_mxArray,set_by_private_set_mxArray);
std::swap(item.mem,mem);
std::swap(item.data,data);
@ -1069,20 +1079,27 @@ namespace dlib
long nc
)
{
if (!set_by_private_set_mxArray && mem)
if (owned_by_matlab)
{
mxDestroyArray(mem);
mem = 0;
data = 0;
}
set_by_private_set_mxArray = false;
if (!set_by_private_set_mxArray && mem)
{
mxDestroyArray(mem);
mem = 0;
data = 0;
}
set_by_private_set_mxArray = false;
mem = mxCreateDoubleMatrix(nr, nc, mxREAL);
if (mem == 0)
throw std::bad_alloc();
if (make_persistent)
mexMakeArrayPersistent(mem);
data = mxGetPr(mem);
mem = mxCreateDoubleMatrix(nr, nc, mxREAL);
if (mem == 0)
throw std::bad_alloc();
data = mxGetPr(mem);
}
else
{
if (data)
delete [] data;
data = new double[nr*nc];
}
nr_ = nr;
nc_ = nc;
}
@ -1091,7 +1108,7 @@ namespace dlib
double* data;
long nr_;
long nc_;
bool make_persistent;
bool owned_by_matlab;
bool set_by_private_set_mxArray;
mxArray* mem;
};
@ -1107,14 +1124,22 @@ namespace dlib
const static long NC = num_cols;
layout (
): data(0), nr_(0), nc_(0), make_persistent(true),set_by_private_set_mxArray(false),mem(0) { }
): data(0), nr_(0), nc_(0), owned_by_matlab(false),set_by_private_set_mxArray(false),mem(0) { }
~layout ()
{
if (!set_by_private_set_mxArray && mem)
if (owned_by_matlab)
{
mxDestroyArray(mem);
mem = 0;
if (!set_by_private_set_mxArray && mem)
{
mxDestroyArray(mem);
mem = 0;
data = 0;
}
}
else if (data)
{
delete [] data;
data = 0;
}
}
@ -1141,9 +1166,11 @@ namespace dlib
mxArray* mem_
)
{
DLIB_CASSERT(mem == 0 && data == 0,"You can't call this function on an already allocated matrix.");
// We don't own the pointer, so make note of that so we won't try to free
// it.
set_by_private_set_mxArray = true;
owned_by_matlab = true;
mem = mem_;
data = (float*)mxGetData(mem);
nr_ = mxGetM(mem);
@ -1152,7 +1179,7 @@ namespace dlib
mxArray* _private_release_mxArray()
{
DLIB_CASSERT(!make_persistent,"");
DLIB_CASSERT(owned_by_matlab,"");
mxArray* temp = mem;
mem = 0;
set_by_private_set_mxArray = false;
@ -1162,21 +1189,21 @@ namespace dlib
return temp;
}
void _private_mark_non_persistent()
void _private_mark_owned_by_matlab()
{
DLIB_CASSERT(mem == 0,"You can't convert a persistent matlab array to non-persistent.");
make_persistent = false;
DLIB_CASSERT(mem == 0 && data == 0,"You can't say a matrix should be owned by matlab after it's been allocated.");
owned_by_matlab = true;
}
bool _private_is_persistent() const
bool _private_is_owned_by_matlab() const
{
return make_persistent;
return owned_by_matlab;
}
void swap(
layout& item
)
{
std::swap(item.make_persistent,make_persistent);
std::swap(item.owned_by_matlab,owned_by_matlab);
std::swap(item.set_by_private_set_mxArray,set_by_private_set_mxArray);
std::swap(item.mem,mem);
std::swap(item.data,data);
@ -1195,20 +1222,27 @@ namespace dlib
long nc
)
{
if (!set_by_private_set_mxArray && mem)
if (owned_by_matlab)
{
mxDestroyArray(mem);
mem = 0;
data = 0;
}
set_by_private_set_mxArray = false;
if (!set_by_private_set_mxArray && mem)
{
mxDestroyArray(mem);
mem = 0;
data = 0;
}
set_by_private_set_mxArray = false;
mem = mxCreateNumericMatrix(nr, nc, mxSINGLE_CLASS, mxREAL);
if (mem == 0)
throw std::bad_alloc();
if (make_persistent)
mexMakeArrayPersistent(mem);
data = (float*)mxGetData(mem);
mem = mxCreateNumericMatrix(nr, nc, mxSINGLE_CLASS, mxREAL);
if (mem == 0)
throw std::bad_alloc();
data = (float*)mxGetData(mem);
}
else
{
if (data)
delete [] data;
data = new float[nr*nc];
}
nr_ = nr;
nc_ = nc;
}
@ -1217,7 +1251,7 @@ namespace dlib
float* data;
long nr_;
long nc_;
bool make_persistent;
bool owned_by_matlab;
bool set_by_private_set_mxArray;
mxArray* mem;
};