From 1ca99720c51df2e0ab35df5c207cfdd696c0d88f Mon Sep 17 00:00:00 2001 From: ser1zw Date: Thu, 28 Apr 2011 00:36:34 +0900 Subject: [PATCH] implemented rb_cvCreateMemStorage and replaced cvCreateMemStorage with rb_cvCreateMemStorage to create CvMemStorage as much as possible --- ext/opencv/cvchain.cpp | 2 +- ext/opencv/cvcontour.cpp | 2 +- ext/opencv/cvmemstorage.cpp | 4 ++-- ext/opencv/cvseq.cpp | 2 +- ext/opencv/opencv.cpp | 31 ++++++++++++++++++++++++++++++- ext/opencv/opencv.h | 1 + 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ext/opencv/cvchain.cpp b/ext/opencv/cvchain.cpp index c25991a..5560cf6 100644 --- a/ext/opencv/cvchain.cpp +++ b/ext/opencv/cvchain.cpp @@ -91,7 +91,7 @@ rb_initialize(int argc, VALUE *argv, VALUE self) storage = CVMEMSTORAGE(storage_value); } else - storage = cvCreateMemStorage(0); + storage = rb_cvCreateMemStorage(0); DATA_PTR(self) = (CvChain*)cvCreateSeq(CV_SEQ_ELTYPE_CODE, sizeof(CvChain), sizeof(char), storage); diff --git a/ext/opencv/cvcontour.cpp b/ext/opencv/cvcontour.cpp index df0d378..52a9ade 100644 --- a/ext/opencv/cvcontour.cpp +++ b/ext/opencv/cvcontour.cpp @@ -104,7 +104,7 @@ rb_initialize(int argc, VALUE *argv, VALUE self) storage = CVMEMSTORAGE(storage_value); } else - storage = cvCreateMemStorage(0); + storage = rb_cvCreateMemStorage(0); DATA_PTR(self) = (CvContour*)cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvContour), sizeof(CvPoint), storage); diff --git a/ext/opencv/cvmemstorage.cpp b/ext/opencv/cvmemstorage.cpp index 10220be..f197204 100644 --- a/ext/opencv/cvmemstorage.cpp +++ b/ext/opencv/cvmemstorage.cpp @@ -41,7 +41,7 @@ define_ruby_class() VALUE rb_allocate(VALUE klass) { - CvMemStorage *storage = cvCreateMemStorage(); + CvMemStorage *storage = rb_cvCreateMemStorage(0); return Data_Wrap_Struct(klass, 0, cvmemstorage_free, storage); } @@ -54,7 +54,7 @@ cvmemstorage_free(void *ptr) VALUE new_object(int blocksize) { - CvMemStorage *storage = cvCreateMemStorage(blocksize); + CvMemStorage *storage = rb_cvCreateMemStorage(blocksize); return Data_Wrap_Struct(rb_klass, 0, cvmemstorage_free, storage); } diff --git a/ext/opencv/cvseq.cpp b/ext/opencv/cvseq.cpp index e16099f..74bd302 100644 --- a/ext/opencv/cvseq.cpp +++ b/ext/opencv/cvseq.cpp @@ -145,7 +145,7 @@ rb_initialize(int argc, VALUE *argv, VALUE self) storage = CVMEMSTORAGE(storage_value); } else - storage = cvCreateMemStorage(0); + storage = rb_cvCreateMemStorage(0); if(!rb_obj_is_kind_of(klass, rb_cClass)) rb_raise(rb_eTypeError, "argument 1 (sequence-block class) should be %s.", rb_class2name(rb_cClass)); diff --git a/ext/opencv/opencv.cpp b/ext/opencv/opencv.cpp index 6b357b3..ad9dbf6 100644 --- a/ext/opencv/opencv.cpp +++ b/ext/opencv/opencv.cpp @@ -220,7 +220,7 @@ rb_cvCreateImage(CvSize size, int depth, int channels) * Creates a structuring element * When memory allocation is failed, run GC and retry it */ -IplConvKernel * +IplConvKernel* rb_cvCreateStructuringElementEx(int cols, int rows, int anchorX, int anchorY, int shape, int *values) @@ -247,6 +247,35 @@ rb_cvCreateStructuringElementEx(int cols, int rows, return ptr; } +/* + * Creates memory storage + * When memory allocation is failed, run GC and retry it + */ +CvMemStorage* +rb_cvCreateMemStorage(int block_size) +{ + CvMemStorage* ptr = NULL; + try { + ptr = cvCreateMemStorage(block_size); + } + catch(cv::Exception& e) { + if (e.code != CV_StsNoMem) + rb_raise(rb_eRuntimeError, "%s", e.what()); + + rb_gc_start(); + try { + ptr = cvCreateMemStorage(block_size); + } + catch (cv::Exception& e) { + if (e.code == CV_StsNoMem) + rb_raise(rb_eNoMemError, "%s", e.what()); + else + rb_raise(rb_eRuntimeError, "%s", e.what()); + } + } + return ptr; +} + VALUE rb_module; VALUE rb_opencv_constants; diff --git a/ext/opencv/opencv.h b/ext/opencv/opencv.h index 07dbfc4..945cd66 100644 --- a/ext/opencv/opencv.h +++ b/ext/opencv/opencv.h @@ -189,6 +189,7 @@ void* rb_cvAlloc(size_t size); CvMat* rb_cvCreateMat(int height, int width, int type); IplImage* rb_cvCreateImage(CvSize size, int depth, int channels); IplConvKernel* rb_cvCreateStructuringElementEx(int cols, int rows, int anchorX, int anchorY, int shape, int *values); +CvMemStorage* rb_cvCreateMemStorage(int block_size); // Ruby/OpenCV inline functions inline CvArr*