app_server: use BStackOrHeapArray

Change-Id: Ieb335d51923bf28b0e4f830535f74dede42933a3
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2893
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
X512
2020-06-08 14:19:18 +00:00
committed by waddlesplash
parent c4bc688304
commit 9692db62e7
2 changed files with 23 additions and 68 deletions
+3 -11
View File
@@ -355,14 +355,9 @@ draw_polygon(void* _canvas, size_t numPoints, const BPoint viewPoints[],
if (numPoints == 0) if (numPoints == 0)
return; return;
const size_t kMaxStackCount = 200; BStackOrHeapArray<BPoint, 200> points(numPoints);
char stackData[kMaxStackCount * sizeof(BPoint)]; if (!points.IsValid())
BPoint* points = (BPoint*)stackData; return;
if (numPoints > kMaxStackCount) {
points = (BPoint*)malloc(numPoints * sizeof(BPoint));
if (points == NULL)
return;
}
canvas->PenToScreenTransform().Apply(points, viewPoints, numPoints); canvas->PenToScreenTransform().Apply(points, viewPoints, numPoints);
@@ -371,9 +366,6 @@ draw_polygon(void* _canvas, size_t numPoints, const BPoint viewPoints[],
canvas->GetDrawingEngine()->DrawPolygon(points, numPoints, polyFrame, canvas->GetDrawingEngine()->DrawPolygon(points, numPoints, polyFrame,
fill, isClosed && numPoints > 2); fill, isClosed && numPoints > 2);
if (numPoints > kMaxStackCount)
free(points);
} }
+20 -57
View File
@@ -47,6 +47,7 @@
#include <PortLink.h> #include <PortLink.h>
#include <ShapePrivate.h> #include <ShapePrivate.h>
#include <ServerProtocolStructs.h> #include <ServerProtocolStructs.h>
#include <StackOrHeapArray.h>
#include <ViewPrivate.h> #include <ViewPrivate.h>
#include <WindowInfo.h> #include <WindowInfo.h>
#include <WindowPrivate.h> #include <WindowPrivate.h>
@@ -3078,15 +3079,11 @@ ServerWindow::_DispatchViewDrawingMessage(int32 code,
break; break;
} }
const ssize_t kMaxStackStringSize = 4096; // NOTE: Careful, the + 1 is for termination!
char stackString[kMaxStackStringSize]; BStackOrHeapArray<char, 4096> string(
char* string = stackString; (info.stringLength + 1 + 63) / 64 * 64);
if (info.stringLength >= kMaxStackStringSize) { if (!string.IsValid())
// NOTE: Careful, the + 1 is for termination! break;
string = (char*)malloc((info.stringLength + 1 + 63) / 64 * 64);
if (string == NULL)
break;
}
escapement_delta* delta = NULL; escapement_delta* delta = NULL;
if (code == AS_DRAW_STRING_WITH_DELTA) { if (code == AS_DRAW_STRING_WITH_DELTA) {
@@ -3094,11 +3091,9 @@ ServerWindow::_DispatchViewDrawingMessage(int32 code,
delta = &info.delta; delta = &info.delta;
} }
if (link.Read(string, info.stringLength) != B_OK) { if (link.Read(string, info.stringLength) != B_OK)
if (string != stackString)
free(string);
break; break;
}
// Terminate the string, if nothing else, it's important // Terminate the string, if nothing else, it's important
// for the DTRACE call below... // for the DTRACE call below...
string[info.stringLength] = '\0'; string[info.stringLength] = '\0';
@@ -3113,8 +3108,6 @@ ServerWindow::_DispatchViewDrawingMessage(int32 code,
fCurrentView->ScreenToPenTransform().Apply(&penLocation); fCurrentView->ScreenToPenTransform().Apply(&penLocation);
fCurrentView->CurrentState()->SetPenLocation(penLocation); fCurrentView->CurrentState()->SetPenLocation(penLocation);
if (string != stackString)
free(string);
break; break;
} }
case AS_DRAW_STRING_WITH_OFFSETS: case AS_DRAW_STRING_WITH_OFFSETS:
@@ -3127,27 +3120,12 @@ ServerWindow::_DispatchViewDrawingMessage(int32 code,
if (link.Read<int32>(&glyphCount) != B_OK || glyphCount <= 0) if (link.Read<int32>(&glyphCount) != B_OK || glyphCount <= 0)
break; break;
const ssize_t kMaxStackStringSize = 512; // NOTE: Careful, the + 1 is for termination!
char stackString[kMaxStackStringSize]; BStackOrHeapArray<char, 512> string(
char* string = stackString; (stringLength + 1 + 63) / 64 * 64);
BPoint stackLocations[kMaxStackStringSize]; BStackOrHeapArray<BPoint, 512> locations(glyphCount);
BPoint* locations = stackLocations; if (!string.IsValid() || !locations.IsValid())
MemoryDeleter stringDeleter; break;
MemoryDeleter locationsDeleter;
if (stringLength >= kMaxStackStringSize) {
// NOTE: Careful, the + 1 is for termination!
string = (char*)malloc((stringLength + 1 + 63) / 64 * 64);
if (string == NULL)
break;
stringDeleter.SetTo(string);
}
if (glyphCount > kMaxStackStringSize) {
locations = (BPoint*)malloc(
((glyphCount * sizeof(BPoint)) + 63) / 64 * 64);
if (locations == NULL)
break;
locationsDeleter.SetTo(locations);
}
if (link.Read(string, stringLength) != B_OK) if (link.Read(string, stringLength) != B_OK)
break; break;
@@ -3647,27 +3625,12 @@ ServerWindow::_DispatchPictureMessage(int32 code, BPrivate::LinkReceiver& link)
if (link.Read<int32>(&glyphCount) != B_OK || glyphCount <= 0) if (link.Read<int32>(&glyphCount) != B_OK || glyphCount <= 0)
break; break;
const ssize_t kMaxStackStringSize = 512; // NOTE: Careful, the + 1 is for termination!
char stackString[kMaxStackStringSize]; BStackOrHeapArray<char, 512> string(
char* string = stackString; (stringLength + 1 + 63) / 64 * 64);
BPoint stackLocations[kMaxStackStringSize]; BStackOrHeapArray<BPoint, 512> locations(glyphCount);
BPoint* locations = stackLocations; if (!string.IsValid() || !locations.IsValid())
MemoryDeleter stringDeleter; break;
MemoryDeleter locationsDeleter;
if (stringLength >= kMaxStackStringSize) {
// NOTE: Careful, the + 1 is for termination!
string = (char*)malloc((stringLength + 1 + 63) / 64 * 64);
if (string == NULL)
break;
stringDeleter.SetTo(string);
}
if (glyphCount > kMaxStackStringSize) {
locations = (BPoint*)malloc(
((glyphCount * sizeof(BPoint)) + 63) / 64 * 64);
if (locations == NULL)
break;
locationsDeleter.SetTo(locations);
}
if (link.Read(string, stringLength) != B_OK) if (link.Read(string, stringLength) != B_OK)
break; break;