diff --git a/headers/build/os/support/StackOrHeapArray.h b/headers/build/os/support/StackOrHeapArray.h new file mode 100644 index 0000000000..45049a4129 --- /dev/null +++ b/headers/build/os/support/StackOrHeapArray.h @@ -0,0 +1,43 @@ +/* + * Copyright 2012, Jonathan Schleifer . All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _SUPPORT_STACKORHEAPARRAY_H +#define _SUPPORT_STACKORHEAPARRAY_H + +#include +#include + +template +class BStackOrHeapArray { +public: + BStackOrHeapArray(size_t count) + { + if (count > StackSize) + fData = new(std::nothrow) Type[count]; + else + fData = fStackData; + } + + ~BStackOrHeapArray() + { + if (fData != fStackData) + delete[] fData; + } + + bool IsValid() const + { + return fData != NULL; + } + + operator Type*() + { + return fData; + } + +private: + Type fStackData[StackSize]; + Type* fData; +}; + +#endif diff --git a/src/tools/locale/DefaultCatalog.cpp b/src/tools/locale/DefaultCatalog.cpp index 7c97d07918..2a1e526160 100644 --- a/src/tools/locale/DefaultCatalog.cpp +++ b/src/tools/locale/DefaultCatalog.cpp @@ -8,7 +8,6 @@ */ -#include #include #include @@ -24,6 +23,7 @@ #include #include #include +#include #include #include @@ -32,7 +32,6 @@ using BPrivate::DefaultCatalog; -using std::auto_ptr; /*! This file implements the default catalog-type for the opentracker locale @@ -150,12 +149,12 @@ DefaultCatalog::ReadFromFile(const char *path) return res; } - auto_ptr buf(new(std::nothrow) char [sz]); - if (buf.get() == NULL) { + BStackOrHeapArray buf(sz); + if (!buf.IsValid()) { fprintf(stderr, "couldn't allocate array of %Ld chars\n", sz); return B_NO_MEMORY; } - res = catalogFile.Read(buf.get(), sz); + res = catalogFile.Read(buf, sz); if (res < B_OK) { fprintf(stderr, "couldn't read from catalog-file %s\n", path); return res; @@ -166,7 +165,7 @@ DefaultCatalog::ReadFromFile(const char *path) path); return res; } - BMemoryIO memIO(buf.get(), sz); + BMemoryIO memIO(buf, sz); res = Unflatten(&memIO); if (res == B_OK) { diff --git a/src/tools/locale/PlainTextCatalog.cpp b/src/tools/locale/PlainTextCatalog.cpp index 6f89a374d9..20a9d37511 100644 --- a/src/tools/locale/PlainTextCatalog.cpp +++ b/src/tools/locale/PlainTextCatalog.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -32,7 +31,6 @@ using BPrivate::PlainTextCatalog; -using std::auto_ptr; using std::min; using std::max; using std::pair;