Vector/Matrix 内存管理
/* ------------- Vector/Matrix Memory Management -------------- */
/*
Vector的实际数据都是存储在都是从1到n的下标,下标0记录此vector有多少个元素。需要多申请一个元素的字节大小。
Matrix本质上是一块连续内存(和本人原来分配矩阵内存方式不太一样)。首先申请所有需要的内存大小size.然后从0到第R个元素可以认为是一个vector,第0个元素记录该矩阵一共有几行,第1到R个元素用于保存第1行到第R行的起始地址,每行都有C(列数)+1个元素。所以一个Matrix总共需要的内存size=(R+1)*sizeof(Ptr)+R* vectorElemSize(C)
Shared vectors and matrices have an extra 2 * sizeof(Ptr) bytes
prepended to hold a usage count and a hook.
*/
Matrix内存结构示意
TriMatrix内存结构示意
sdf
所需内存R*sizeof(float)+C*sizeof(float)+sizeof(float)*C(C+1)/2
跟代码表述不同,含义相同。请参考代码阅读理解。
/*Vectors are pointers to arrays of float (ie float*); matrices are pointers
to an array of vectors (ie float**). All indexing is v[1..n] and
m[1..r][1..c]. The actual size of each vector is stored in v[0]. For
matrices, the row lengths (number of columns) are stored in every
row since they are genuine vectors, the number of rows is stored in m[0].
Triangular matrices are the same except that the rows increase in length
with the first row being a vector of one element.
Short and IntVecs are arranged in a similar way to Vectors.
Shared vectors and matrices have an extra 2 * sizeof(Ptr) bytes
prepended to hold a usage count and a hook.
*/
/*下面类型函数:申请size个元素的vector总共需要多大空间*/
/* EXPORT->vectorElemSize: size of vectors for creating heaps */
size_t ShortVecElemSize(int size) { return (size+1)*sizeof(short); }
size_t IntVecElemSize(int size) { return (size+1)*sizeof(int); }
size_t VectorElemSize(int size) { return (size+1)*sizeof(float); }
size_t DVectorElemSize(int size){ return (size+1)*sizeof(double);}
size_t SVectorElemSize(int size){ return (size+1)*sizeof(float)+2*sizeof(Ptr); }*/
后面的不一一列举了