From b0f02f2065e649bdd75d6117bfa9db9449d6efdf Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Sun, 6 Nov 2005 22:49:19 +0000 Subject: [PATCH] Handle out of memory conditions in the constructors more gracefully. We might end up using set_size() for every allocation (would probably make more sense) git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14722 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/Region.cpp | 39 +++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/kits/interface/Region.cpp b/src/kits/interface/Region.cpp index 1a00a75382..013626a596 100644 --- a/src/kits/interface/Region.cpp +++ b/src/kits/interface/Region.cpp @@ -49,15 +49,20 @@ #include +const static int32 kInitialDataSize = 8; + + /*! \brief Initializes a region. The region will have no rects, and its bound will be invalid. */ BRegion::BRegion() : - data_size(8), + data_size(0), data(NULL) { - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); + data = (clipping_rect *)malloc(kInitialDataSize * sizeof(clipping_rect)); + if (data != NULL) + data_size = kInitialDataSize; Support::ZeroRegion(*this); } @@ -68,18 +73,19 @@ BRegion::BRegion() */ BRegion::BRegion(const BRegion ®ion) : + data_size(0), data(NULL) { - bound = region.bound; - count = region.count; - data_size = region.data_size; - - if (data_size <= 0) - data_size = 1; + const int32 size = region.data_size > 0 ? region.data_size : 1; - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); - - memcpy(data, region.data, count * sizeof(clipping_rect)); + data = (clipping_rect *)malloc(size * sizeof(clipping_rect)); + if (data != NULL) { + data_size = size; + bound = region.bound; + count = region.count; + memcpy(data, region.data, count * sizeof(clipping_rect)); + } else + Support::ZeroRegion(*this); } @@ -88,12 +94,15 @@ BRegion::BRegion(const BRegion ®ion) */ BRegion::BRegion(const BRect rect) : - data_size(8), + data_size(0), data(NULL) { - data = (clipping_rect *)malloc(data_size * sizeof(clipping_rect)); - - Set(rect); + data = (clipping_rect *)malloc(kInitialDataSize * sizeof(clipping_rect)); + if (data != NULL) { + data_size = kInitialDataSize; + Set(rect); + } else + Support::ZeroRegion(*this); }