Remove variable length arrays of non-PODs.

Variable length arrays of non-PODs are not part of the C++ standard, but
a GNU extension that never worked correctly. Instead, BStackOrHeap array
is used now, which makes sure that it's not too big for the stack, calls
all constructors and is valid C++.
This commit is contained in:
Jonathan Schleifer
2014-01-10 22:31:50 +01:00
committed by Stephan Aßmus
parent 4bda0212ce
commit f4c2f7ebdb
9 changed files with 30 additions and 19 deletions
@@ -16,6 +16,7 @@
#include <BufferProducer.h> #include <BufferProducer.h>
#include <MediaNode.h> #include <MediaNode.h>
#include <RealtimeAlloc.h> #include <RealtimeAlloc.h>
#include <StackOrHeapArray.h>
#include <StopWatch.h> #include <StopWatch.h>
#include <TimeSource.h> #include <TimeSource.h>
@@ -520,8 +521,9 @@ MixerCore::_MixThread()
uint64 bufferIndex = 0; uint64 bufferIndex = 0;
#endif #endif
RtList<chan_info> inputChanInfos[MAX_CHANNEL_TYPES]; typedef RtList<chan_info> chan_info_list;
RtList<chan_info> mixChanInfos[fMixBufferChannelCount]; chan_info_list inputChanInfos[MAX_CHANNEL_TYPES];
BStackOrHeapArray<chan_info_list, 16> mixChanInfos(fMixBufferChannelCount);
// TODO: this does not support changing output channel count // TODO: this does not support changing output channel count
bigtime_t eventTime = timeBase; bigtime_t eventTime = timeBase;
+3 -2
View File
@@ -20,6 +20,7 @@
#include <Message.h> #include <Message.h>
#include <Shape.h> #include <Shape.h>
#include <String.h> #include <String.h>
#include <StackOrHeapArray.h>
#include "messages.h" #include "messages.h"
@@ -99,7 +100,7 @@ FontDemoView::_DrawView(BView* view)
view->SetFont(&fFont, B_FONT_ALL); view->SetFont(&fFont, B_FONT_ALL);
const size_t size = fString.CountChars(); const size_t size = fString.CountChars();
BRect boundBoxes[size]; BStackOrHeapArray<BRect, 64> boundBoxes(size);
if (OutLineLevel()) if (OutLineLevel())
fFont.GetGlyphShapes(fString, size, fShapes); fFont.GetGlyphShapes(fString, size, fShapes);
@@ -456,4 +457,4 @@ FontDemoView::_NewBitmap(BRect rect)
delete fBitmap; delete fBitmap;
fBitmap = NULL; fBitmap = NULL;
} }
} }
@@ -14,6 +14,7 @@
#include <Message.h> #include <Message.h>
#include <MenuItem.h> #include <MenuItem.h>
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <StackOrHeapArray.h>
#include <Window.h> #include <Window.h>
#include "cursors.h" #include "cursors.h"
@@ -1703,7 +1704,7 @@ PathManipulator::_Nudge(BPoint direction)
int32 count = fromSelection ? fSelection->CountItems() int32 count = fromSelection ? fSelection->CountItems()
: fPath->CountPoints(); : fPath->CountPoints();
int32 indices[count]; int32 indices[count];
control_point points[count]; BStackOrHeapArray<control_point, 64> points(count);
// init indices and points // init indices and points
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
+2 -1
View File
@@ -11,6 +11,7 @@
#include <MediaDefs.h> #include <MediaDefs.h>
#include <Screen.h> #include <Screen.h>
#include <StackOrHeapArray.h>
#include <Window.h> #include <Window.h>
#include "DrawingTidbits.h" #include "DrawingTidbits.h"
@@ -108,7 +109,7 @@ VUView::_RenderLaunch(void *data)
void void
VUView::_RenderLoop() VUView::_RenderLoop()
{ {
rgb_color levels[fLevelCount][2]; BStackOrHeapArray<rgb_color[2], 64> levels(fLevelCount);
for (int32 i = 0; i < fLevelCount; i++) { for (int32 i = 0; i < fLevelCount; i++) {
levels[i][0] = levels[i][1] = back_color; levels[i][0] = levels[i][1] = back_color;
+2 -1
View File
@@ -18,6 +18,7 @@
#include <algorithm> #include <algorithm>
#include <StackOrHeapArray.h>
#include <String.h> #include <String.h>
#include "TermConst.h" #include "TermConst.h"
@@ -537,7 +538,7 @@ BasicTerminalBuffer::Find(const char* _pattern, const TermPos& start,
int32 patternByteLen = strlen(_pattern); int32 patternByteLen = strlen(_pattern);
// convert pattern to UTF8Char array // convert pattern to UTF8Char array
UTF8Char pattern[patternByteLen]; BStackOrHeapArray<UTF8Char, 64> pattern(patternByteLen);
int32 patternLen = 0; int32 patternLen = 0;
while (*_pattern != '\0') { while (*_pattern != '\0') {
int32 charLen = UTF8Char::ByteCount(*_pattern); int32 charLen = UTF8Char::ByteCount(*_pattern);
+3 -2
View File
@@ -25,6 +25,7 @@
#include <GradientConic.h> #include <GradientConic.h>
#include <Region.h> #include <Region.h>
#include <Shape.h> #include <Shape.h>
#include <StackOrHeapArray.h>
#include <ServerProtocol.h> #include <ServerProtocol.h>
@@ -96,11 +97,11 @@ ServerLink::ReadShape(BShape* shape)
fReceiver->Read(&opCount, sizeof(int32)); fReceiver->Read(&opCount, sizeof(int32));
fReceiver->Read(&ptCount, sizeof(int32)); fReceiver->Read(&ptCount, sizeof(int32));
uint32 opList[opCount]; BStackOrHeapArray<uint32, 64> opList(opCount);
if (opCount > 0) if (opCount > 0)
fReceiver->Read(opList, opCount * sizeof(uint32)); fReceiver->Read(opList, opCount * sizeof(uint32));
BPoint ptList[ptCount]; BStackOrHeapArray<BPoint, 64> ptList(ptCount);
if (ptCount > 0) if (ptCount > 0)
fReceiver->Read(ptList, ptCount * sizeof(BPoint)); fReceiver->Read(ptList, ptCount * sizeof(BPoint));
+3 -1
View File
@@ -11,6 +11,8 @@
#include <syslog.h> #include <syslog.h>
#include <typeinfo> #include <typeinfo>
#include <StackOrHeapArray.h>
namespace BPrivate { namespace BPrivate {
namespace Archiving { namespace Archiving {
@@ -163,7 +165,7 @@ BArchiveManager::ArchiverLeaving(const BArchiver* archiver, status_t err)
if (archiver == fCreator && fError == B_OK) { if (archiver == fCreator && fError == B_OK) {
// first, we must sort the objects into the order they were archived in // first, we must sort the objects into the order they were archived in
typedef std::pair<BMessage*, const BArchivable*> ArchivePair; typedef std::pair<BMessage*, const BArchivable*> ArchivePair;
ArchivePair pairs[fTokenMap.size()]; BStackOrHeapArray<ArchivePair, 64> pairs(fTokenMap.size());
for(TokenMap::iterator it = fTokenMap.begin(), end = fTokenMap.end(); for(TokenMap::iterator it = fTokenMap.begin(), end = fTokenMap.end();
it != end; it++) { it != end; it++) {
+7 -8
View File
@@ -34,6 +34,7 @@
#include <ScrollBar.h> #include <ScrollBar.h>
#include <Shape.h> #include <Shape.h>
#include <String.h> #include <String.h>
#include <StackOrHeapArray.h>
#include <FontPrivate.h> #include <FontPrivate.h>
#include <MessengerPrivate.h> #include <MessengerPrivate.h>
@@ -1856,10 +1857,9 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
size = 0.0f; size = 0.0f;
} }
// TODO: don't use the stack for this - numStrings could be large BStackOrHeapArray<float, 64> widthArray(numStrings);
float widthArray[numStrings]; BStackOrHeapArray<int32, 64> lengthArray(numStrings);
int32 lengthArray[numStrings]; BStackOrHeapArray<char*, 64> stringArray(numStrings);
char *stringArray[numStrings];
for (int32 i = 0; i < numStrings; i++) { for (int32 i = 0; i < numStrings; i++) {
// This version of ReadString allocates the strings, we free // This version of ReadString allocates the strings, we free
// them below // them below
@@ -1882,7 +1882,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
} }
fLink.StartMessage(B_OK); fLink.StartMessage(B_OK);
fLink.Attach(widthArray, sizeof(widthArray)); fLink.Attach(widthArray, numStrings * sizeof(float));
} else } else
fLink.StartMessage(B_BAD_VALUE); fLink.StartMessage(B_BAD_VALUE);
@@ -2497,8 +2497,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
link.Read<escapement_delta>(&deltaArray[i]); link.Read<escapement_delta>(&deltaArray[i]);
} }
// TODO: don't do this on the heap! (at least check the size before) BStackOrHeapArray<BRect, 64> rectArray(numStrings);
BRect rectArray[numStrings];
ServerFont font; ServerFont font;
bool success = false; bool success = false;
@@ -2513,7 +2512,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
if (font.GetBoundingBoxesForStrings(stringArray, lengthArray, if (font.GetBoundingBoxesForStrings(stringArray, lengthArray,
numStrings, rectArray, mode, deltaArray) == B_OK) { numStrings, rectArray, mode, deltaArray) == B_OK) {
fLink.StartMessage(B_OK); fLink.StartMessage(B_OK);
fLink.Attach(rectArray, sizeof(rectArray)); fLink.Attach(rectArray, numStrings * sizeof(BRect));
success = true; success = true;
} }
} }
+4 -1
View File
@@ -10,7 +10,10 @@
#include "DrawingEngine.h" #include "DrawingEngine.h"
#include <Bitmap.h> #include <Bitmap.h>
#include <StackOrHeapArray.h>
#include <stdio.h> #include <stdio.h>
#include <algorithm> #include <algorithm>
#include <stack> #include <stack>
@@ -469,7 +472,7 @@ DrawingEngine::CopyRegion(/*const*/ BRegion* region, int32 xOffset,
// TODO: make this step unnecessary // TODO: make this step unnecessary
// (by using different stack impl inside node) // (by using different stack impl inside node)
node nodes[count]; BStackOrHeapArray<node, 64> nodes(count);
for (int32 i= 0; i < count; i++) { for (int32 i= 0; i < count; i++) {
nodes[i].init(region->RectAt(i), count); nodes[i].init(region->RectAt(i), count);
} }