From 7554373ada8b8486cf12625d870175dd7dcfd19d Mon Sep 17 00:00:00 2001
From: Jonathan Schleifer <js@webkeks.org>
Date: Fri, 10 Jan 2014 21:06:36 +0100
Subject: [PATCH 04/10] 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++.
---
src/add-ons/media/media-add-ons/mixer/MixerCore.cpp | 6 ++++--
src/apps/fontdemo/FontDemoView.cpp | 5 +++--
src/apps/icon-o-matic/shape/PathManipulator.cpp | 3 ++-
src/apps/soundrecorder/VUView.cpp | 3 ++-
src/apps/terminal/BasicTerminalBuffer.cpp | 3 ++-
src/kits/app/ServerLink.cpp | 5 +++--
src/kits/support/ArchivingManagers.cpp | 4 +++-
src/servers/app/ServerApp.cpp | 15 +++++++--------
src/servers/app/drawing/DrawingEngine.cpp | 5 ++++-
9 files changed, 30 insertions(+), 19 deletions(-)
diff --git a/src/add-ons/media/media-add-ons/mixer/MixerCore.cpp b/src/add-ons/media/media-add-ons/mixer/MixerCore.cpp
index 4931f45..5af27a6 100644
a
|
b
|
|
16 | 16 | #include <BufferProducer.h> |
17 | 17 | #include <MediaNode.h> |
18 | 18 | #include <RealtimeAlloc.h> |
| 19 | #include <StackOrHeapArray.h> |
19 | 20 | #include <StopWatch.h> |
20 | 21 | #include <TimeSource.h> |
21 | 22 | |
… |
… |
MixerCore::_MixThread()
|
520 | 521 | uint64 bufferIndex = 0; |
521 | 522 | #endif |
522 | 523 | |
523 | | RtList<chan_info> inputChanInfos[MAX_CHANNEL_TYPES]; |
524 | | RtList<chan_info> mixChanInfos[fMixBufferChannelCount]; |
| 524 | typedef RtList<chan_info> chan_info_list; |
| 525 | chan_info_list inputChanInfos[MAX_CHANNEL_TYPES]; |
| 526 | BStackOrHeapArray<chan_info_list, 16> mixChanInfos(fMixBufferChannelCount); |
525 | 527 | // TODO: this does not support changing output channel count |
526 | 528 | |
527 | 529 | bigtime_t eventTime = timeBase; |
diff --git a/src/apps/fontdemo/FontDemoView.cpp b/src/apps/fontdemo/FontDemoView.cpp
index af4205e..ec1ab54 100644
a
|
b
|
|
20 | 20 | #include <Message.h> |
21 | 21 | #include <Shape.h> |
22 | 22 | #include <String.h> |
| 23 | #include <StackOrHeapArray.h> |
23 | 24 | |
24 | 25 | #include "messages.h" |
25 | 26 | |
… |
… |
FontDemoView::_DrawView(BView* view)
|
99 | 100 | view->SetFont(&fFont, B_FONT_ALL); |
100 | 101 | |
101 | 102 | const size_t size = fString.CountChars(); |
102 | | BRect boundBoxes[size]; |
| 103 | BStackOrHeapArray<BRect, 64> boundBoxes(size); |
103 | 104 | |
104 | 105 | if (OutLineLevel()) |
105 | 106 | fFont.GetGlyphShapes(fString, size, fShapes); |
… |
… |
FontDemoView::_NewBitmap(BRect rect)
|
456 | 457 | delete fBitmap; |
457 | 458 | fBitmap = NULL; |
458 | 459 | } |
459 | | } |
460 | | No newline at end of file |
| 460 | } |
diff --git a/src/apps/icon-o-matic/shape/PathManipulator.cpp b/src/apps/icon-o-matic/shape/PathManipulator.cpp
index fa30575..4e50c11 100644
a
|
b
|
|
14 | 14 | #include <Message.h> |
15 | 15 | #include <MenuItem.h> |
16 | 16 | #include <PopUpMenu.h> |
| 17 | #include <StackOrHeapArray.h> |
17 | 18 | #include <Window.h> |
18 | 19 | |
19 | 20 | #include "cursors.h" |
… |
… |
PathManipulator::_Nudge(BPoint direction)
|
1703 | 1704 | int32 count = fromSelection ? fSelection->CountItems() |
1704 | 1705 | : fPath->CountPoints(); |
1705 | 1706 | int32 indices[count]; |
1706 | | control_point points[count]; |
| 1707 | BStackOrHeapArray<control_point, 64> points(count); |
1707 | 1708 | |
1708 | 1709 | // init indices and points |
1709 | 1710 | for (int32 i = 0; i < count; i++) { |
diff --git a/src/apps/soundrecorder/VUView.cpp b/src/apps/soundrecorder/VUView.cpp
index c90afc5..2f6112b 100644
a
|
b
|
|
11 | 11 | |
12 | 12 | #include <MediaDefs.h> |
13 | 13 | #include <Screen.h> |
| 14 | #include <StackOrHeapArray.h> |
14 | 15 | #include <Window.h> |
15 | 16 | |
16 | 17 | #include "DrawingTidbits.h" |
… |
… |
VUView::_RenderLaunch(void *data)
|
108 | 109 | void |
109 | 110 | VUView::_RenderLoop() |
110 | 111 | { |
111 | | rgb_color levels[fLevelCount][2]; |
| 112 | BStackOrHeapArray<rgb_color[2], 64> levels(fLevelCount); |
112 | 113 | |
113 | 114 | for (int32 i = 0; i < fLevelCount; i++) { |
114 | 115 | levels[i][0] = levels[i][1] = back_color; |
diff --git a/src/apps/terminal/BasicTerminalBuffer.cpp b/src/apps/terminal/BasicTerminalBuffer.cpp
index dfbb54e..0006066 100644
a
|
b
|
|
18 | 18 | |
19 | 19 | #include <algorithm> |
20 | 20 | |
| 21 | #include <StackOrHeapArray.h> |
21 | 22 | #include <String.h> |
22 | 23 | |
23 | 24 | #include "TermConst.h" |
… |
… |
BasicTerminalBuffer::Find(const char* _pattern, const TermPos& start,
|
537 | 538 | int32 patternByteLen = strlen(_pattern); |
538 | 539 | |
539 | 540 | // convert pattern to UTF8Char array |
540 | | UTF8Char pattern[patternByteLen]; |
| 541 | BStackOrHeapArray<UTF8Char, 64> pattern(patternByteLen); |
541 | 542 | int32 patternLen = 0; |
542 | 543 | while (*_pattern != '\0') { |
543 | 544 | int32 charLen = UTF8Char::ByteCount(*_pattern); |
diff --git a/src/kits/app/ServerLink.cpp b/src/kits/app/ServerLink.cpp
index ea7664a..05ac105 100644
a
|
b
|
|
25 | 25 | #include <GradientConic.h> |
26 | 26 | #include <Region.h> |
27 | 27 | #include <Shape.h> |
| 28 | #include <StackOrHeapArray.h> |
28 | 29 | |
29 | 30 | #include <ServerProtocol.h> |
30 | 31 | |
… |
… |
ServerLink::ReadShape(BShape* shape)
|
96 | 97 | fReceiver->Read(&opCount, sizeof(int32)); |
97 | 98 | fReceiver->Read(&ptCount, sizeof(int32)); |
98 | 99 | |
99 | | uint32 opList[opCount]; |
| 100 | BStackOrHeapArray<uint32, 64> opList(opCount); |
100 | 101 | if (opCount > 0) |
101 | 102 | fReceiver->Read(opList, opCount * sizeof(uint32)); |
102 | 103 | |
103 | | BPoint ptList[ptCount]; |
| 104 | BStackOrHeapArray<BPoint, 64> ptList(ptCount); |
104 | 105 | if (ptCount > 0) |
105 | 106 | fReceiver->Read(ptList, ptCount * sizeof(BPoint)); |
106 | 107 | |
diff --git a/src/kits/support/ArchivingManagers.cpp b/src/kits/support/ArchivingManagers.cpp
index 3405b53..069b339 100644
a
|
b
|
|
11 | 11 | #include <syslog.h> |
12 | 12 | #include <typeinfo> |
13 | 13 | |
| 14 | #include <StackOrHeapArray.h> |
| 15 | |
14 | 16 | |
15 | 17 | namespace BPrivate { |
16 | 18 | namespace Archiving { |
… |
… |
BArchiveManager::ArchiverLeaving(const BArchiver* archiver, status_t err)
|
163 | 165 | if (archiver == fCreator && fError == B_OK) { |
164 | 166 | // first, we must sort the objects into the order they were archived in |
165 | 167 | typedef std::pair<BMessage*, const BArchivable*> ArchivePair; |
166 | | ArchivePair pairs[fTokenMap.size()]; |
| 168 | BStackOrHeapArray<ArchivePair, 64> pairs(fTokenMap.size()); |
167 | 169 | |
168 | 170 | for(TokenMap::iterator it = fTokenMap.begin(), end = fTokenMap.end(); |
169 | 171 | it != end; it++) { |
diff --git a/src/servers/app/ServerApp.cpp b/src/servers/app/ServerApp.cpp
index 057df07..8c0122a 100644
a
|
b
|
|
34 | 34 | #include <ScrollBar.h> |
35 | 35 | #include <Shape.h> |
36 | 36 | #include <String.h> |
| 37 | #include <StackOrHeapArray.h> |
37 | 38 | |
38 | 39 | #include <FontPrivate.h> |
39 | 40 | #include <MessengerPrivate.h> |
… |
… |
ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
|
1856 | 1857 | size = 0.0f; |
1857 | 1858 | } |
1858 | 1859 | |
1859 | | // TODO: don't use the stack for this - numStrings could be large |
1860 | | float widthArray[numStrings]; |
1861 | | int32 lengthArray[numStrings]; |
1862 | | char *stringArray[numStrings]; |
| 1860 | BStackOrHeapArray<float, 64> widthArray(numStrings); |
| 1861 | BStackOrHeapArray<int32, 64> lengthArray(numStrings); |
| 1862 | BStackOrHeapArray<char*, 64> stringArray(numStrings); |
1863 | 1863 | for (int32 i = 0; i < numStrings; i++) { |
1864 | 1864 | // This version of ReadString allocates the strings, we free |
1865 | 1865 | // them below |
… |
… |
ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
|
1882 | 1882 | } |
1883 | 1883 | |
1884 | 1884 | fLink.StartMessage(B_OK); |
1885 | | fLink.Attach(widthArray, sizeof(widthArray)); |
| 1885 | fLink.Attach(widthArray, numStrings * sizeof(float)); |
1886 | 1886 | } else |
1887 | 1887 | fLink.StartMessage(B_BAD_VALUE); |
1888 | 1888 | |
… |
… |
ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
|
2497 | 2497 | link.Read<escapement_delta>(&deltaArray[i]); |
2498 | 2498 | } |
2499 | 2499 | |
2500 | | // TODO: don't do this on the heap! (at least check the size before) |
2501 | | BRect rectArray[numStrings]; |
| 2500 | BStackOrHeapArray<BRect, 64> rectArray(numStrings); |
2502 | 2501 | |
2503 | 2502 | ServerFont font; |
2504 | 2503 | bool success = false; |
… |
… |
ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)
|
2513 | 2512 | if (font.GetBoundingBoxesForStrings(stringArray, lengthArray, |
2514 | 2513 | numStrings, rectArray, mode, deltaArray) == B_OK) { |
2515 | 2514 | fLink.StartMessage(B_OK); |
2516 | | fLink.Attach(rectArray, sizeof(rectArray)); |
| 2515 | fLink.Attach(rectArray, numStrings * sizeof(BRect)); |
2517 | 2516 | success = true; |
2518 | 2517 | } |
2519 | 2518 | } |
diff --git a/src/servers/app/drawing/DrawingEngine.cpp b/src/servers/app/drawing/DrawingEngine.cpp
index 4ec70d7..6ef0b85 100644
a
|
b
|
|
10 | 10 | #include "DrawingEngine.h" |
11 | 11 | |
12 | 12 | #include <Bitmap.h> |
| 13 | #include <StackOrHeapArray.h> |
| 14 | |
13 | 15 | #include <stdio.h> |
| 16 | |
14 | 17 | #include <algorithm> |
15 | 18 | #include <stack> |
16 | 19 | |
… |
… |
DrawingEngine::CopyRegion(/*const*/ BRegion* region, int32 xOffset,
|
469 | 472 | |
470 | 473 | // TODO: make this step unnecessary |
471 | 474 | // (by using different stack impl inside node) |
472 | | node nodes[count]; |
| 475 | BStackOrHeapArray<node, 64> nodes(count); |
473 | 476 | for (int32 i= 0; i < count; i++) { |
474 | 477 | nodes[i].init(region->RectAt(i), count); |
475 | 478 | } |