@@ -37,60 +37,60 @@ raise_compatible_typeerror(VALUE object, const char* expected_class_name)
3737
3838/*
3939 * Allocates a memory buffer
40- * When memory allocation is failed, run GC and retry it
40+ * see cv::fastMalloc()
4141 */
4242void *
43- rb_cvAlloc (size_t size)
43+ rbFastMalloc (size_t size)
4444{
45- void * ptr = NULL ;
46- try {
47- ptr = cvAlloc (size );
45+ uchar* udata = (uchar*) xmalloc (size + sizeof ( void *) + CV_MALLOC_ALIGN) ;
46+ if (!udata) {
47+ rb_raise (rb_eNoMemError, " Failed to allocate memory " );
4848 }
49- catch (cv::Exception& e) {
50- if (e.code != CV_StsNoMem)
51- rb_raise (rb_eRuntimeError, " %s" , e.what ());
49+ uchar** adata = cv::alignPtr ((uchar**)udata + 1 , CV_MALLOC_ALIGN);
50+ adata[-1 ] = udata;
51+ return adata;
52+ }
5253
53- rb_gc_start ();
54- try {
55- ptr = cvAlloc (size);
56- }
57- catch (cv::Exception& e) {
58- if (e.code == CV_StsNoMem)
59- rb_raise (rb_eNoMemError, " %s" , e.what ());
60- else
61- rb_raise (rb_eRuntimeError, " %s" , e.what ());
62- }
63- }
64- return ptr;
54+ /*
55+ * Allocates a memory buffer
56+ * When memory allocation is failed, run GC and retry it
57+ */
58+ void *
59+ rb_cvAlloc (size_t size)
60+ {
61+ return rbFastMalloc (size);
6562}
6663
6764/*
6865 * Creates CvMat and underlying data
6966 * When memory allocation is failed, run GC and retry it
7067 */
7168CvMat*
72- rb_cvCreateMat (int height , int width , int type)
69+ rb_cvCreateMat (int rows , int cols , int type)
7370{
74- CvMat* ptr = NULL ;
71+ CvMat* mat = NULL ;
7572 try {
76- ptr = cvCreateMat (height, width, type);
73+ mat = cvCreateMatHeader (rows, cols, type);
74+ if (mat) {
75+ // see OpenCV's cvCreateData()
76+ size_t step = mat->step ;
77+ size_t total_size = step * mat->rows + sizeof (int ) + CV_MALLOC_ALIGN;
78+
79+ mat->refcount = (int *)rbFastMalloc (total_size);
80+ mat->data .ptr = (uchar*)cvAlignPtr (mat->refcount + 1 , CV_MALLOC_ALIGN);
81+ *mat->refcount = 1 ;
82+ }
83+ else {
84+ rb_raise (rb_eRuntimeError, " Failed to create mat header" );
85+ }
7786 }
7887 catch (cv::Exception& e) {
79- if (e.code != CV_StsNoMem)
80- rb_raise (rb_eRuntimeError, " %s" , e.what ());
81-
82- rb_gc_start ();
83- try {
84- ptr = cvCreateMat (height, width, type);
85- }
86- catch (cv::Exception& e) {
87- if (e.code == CV_StsNoMem)
88- rb_raise (rb_eNoMemError, " %s" , e.what ());
89- else
90- rb_raise (rb_eRuntimeError, " %s" , e.what ());
88+ if (mat) {
89+ cvReleaseMat (&mat);
9190 }
91+ rb_raise (rb_eRuntimeError, " %s" , e.what ());
9292 }
93- return ptr ;
93+ return mat ;
9494}
9595
9696/*
@@ -102,22 +102,20 @@ rb_cvCreateImage(CvSize size, int depth, int channels)
102102{
103103 IplImage* ptr = NULL ;
104104 try {
105- ptr = cvCreateImage (size, depth, channels);
105+ ptr = cvCreateImageHeader (size, depth, channels);
106+ if (ptr) {
107+ // see OpenCV's cvCreateData()
108+ ptr->imageData = ptr->imageDataOrigin = (char *)rbFastMalloc ((size_t )ptr->imageSize );
109+ }
110+ else {
111+ rb_raise (rb_eRuntimeError, " Failed to create image header" );
112+ }
106113 }
107114 catch (cv::Exception& e) {
108- if (e.code != CV_StsNoMem)
109- rb_raise (rb_eRuntimeError, " %s" , e.what ());
110-
111- rb_gc_start ();
112- try {
113- ptr = cvCreateImage (size, depth, channels);
114- }
115- catch (cv::Exception& e) {
116- if (e.code == CV_StsNoMem)
117- rb_raise (rb_eNoMemError, " %s" , e.what ());
118- else
119- rb_raise (rb_eRuntimeError, " %s" , e.what ());
115+ if (ptr) {
116+ cvReleaseImage (&ptr);
120117 }
118+ rb_raise (rb_eRuntimeError, " %s" , e.what ());
121119 }
122120 return ptr;
123121}
0 commit comments