Skip to content

Commit 68e9fb1

Browse files
committed
More tweaks to use less stack space
1 parent d1104c7 commit 68e9fb1

2 files changed

Lines changed: 22 additions & 39 deletions

File tree

Source/GSPrivate.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,35 +107,37 @@ __attribute__((unused)) static void GSFreeTempBuffer(void **b)
107107
if (NULL != *b) free(*b);
108108
}
109109
# define GS_BEGINITEMBUF(P, S, T) { \
110-
T _ibuf[GS_MAX_OBJECTS_FROM_STACK];\
110+
unsigned _ilen = (S) > 0 ? (S) : 1; \
111+
T _ibuf[_ilen <= GS_MAX_OBJECTS_FROM_STACK ? _ilen : 1]; \
111112
T *P = _ibuf;\
112113
__attribute__((cleanup(GSFreeTempBuffer))) void *_base = 0;\
113-
if (S > GS_MAX_OBJECTS_FROM_STACK)\
114+
if (_ilen > GS_MAX_OBJECTS_FROM_STACK)\
114115
{\
115-
_base = malloc((S) * sizeof(T));\
116+
_base = malloc(_ilen * sizeof(T));\
116117
P = _base;\
117118
}
118119
# define GS_BEGINITEMBUF2(P, S, T) { \
119-
T _ibuf2[GS_MAX_OBJECTS_FROM_STACK];\
120+
unsigned _ilen2 = (S) > 0 ? (S) : 1; \
121+
T _ibuf2[_ilen2 <= GS_MAX_OBJECTS_FROM_STACK ? _ilen2 : 1]; \
120122
T *P = _ibuf2;\
121123
__attribute__((cleanup(GSFreeTempBuffer))) void *_base2 = 0;\
122-
if (S > GS_MAX_OBJECTS_FROM_STACK)\
124+
if (_ilen2 > GS_MAX_OBJECTS_FROM_STACK)\
123125
{\
124-
_base2 = malloc((S) * sizeof(T));\
126+
_base2 = malloc(_ilen2 * sizeof(T));\
125127
P = _base2;\
126128
}
127129
#else
128-
/* Make minimum size of _ibuf 1 to avoid compiler warnings.
129-
*/
130130
# define GS_BEGINITEMBUF(P, S, T) { \
131-
T _ibuf[(S) > 0 && (S) <= GS_MAX_OBJECTS_FROM_STACK ? (S) : 1]; \
132-
T *_base = ((S) <= GS_MAX_OBJECTS_FROM_STACK) ? _ibuf \
133-
: (T*)malloc((S) * sizeof(T)); \
131+
unsigned _ilen = (S) > 0 ? (S) : 1; \
132+
T _ibuf[_ilen <= GS_MAX_OBJECTS_FROM_STACK ? _ilen : 1]; \
133+
T *_base = (_ilen <= GS_MAX_OBJECTS_FROM_STACK) ? _ibuf \
134+
: (T*)malloc(_ilen * sizeof(T)); \
134135
T *(P) = _base;
135136
# define GS_BEGINITEMBUF2(P, S, T) { \
136-
T _ibuf2[(S) > 0 && (S) <= GS_MAX_OBJECTS_FROM_STACK ? (S) : 1]; \
137-
T *_base2 = ((S) <= GS_MAX_OBJECTS_FROM_STACK) ? _ibuf2 \
138-
: (T*)malloc((S) * sizeof(T)); \
137+
unsigned _ilen2 = (S) > 0 ? (S) : 1; \
138+
T _ibuf2[_ilen2 <= GS_MAX_OBJECTS_FROM_STACK ? _ilen2 : 1]; \
139+
T *_base2 = (_ilen2 <= GS_MAX_OBJECTS_FROM_STACK) ? _ibuf2 \
140+
: (T*)malloc(_ilen2 * sizeof(T)); \
139141
T *(P) = _base2;
140142
#endif
141143

Source/NSString.m

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5974,37 +5974,18 @@ - (NSComparisonResult) compare: (NSString *)string
59745974
{
59755975
NSUInteger countSelf = compareRange.length;
59765976
NSUInteger countOther = [string length];
5977-
unichar *charsSelf;
5978-
unichar *charsOther;
59795977
UCollationResult result;
5980-
NSUInteger sizeSelf = countSelf * sizeof(unichar);
5981-
NSUInteger sizeOther = countOther * sizeof(unichar);
5982-
BOOL useStack = (sizeSelf + sizeOther) < 128;
5983-
5984-
if (useStack)
5985-
{
5986-
charsSelf = alloca(sizeSelf ? sizeSelf : 1);
5987-
charsOther = alloca(sizeOther ? sizeOther : 1);
5988-
}
5989-
else
5990-
{
5991-
charsSelf = malloc(sizeSelf ? sizeSelf : 1);
5992-
charsOther = malloc(sizeOther ? sizeOther : 1);
5993-
}
5978+
GS_BEGINITEMBUF(chars, countOther + countSelf, unichar)
59945979

59955980
// Copy to buffer
5996-
[self getCharacters: charsSelf range: compareRange];
5997-
[string getCharacters: charsOther range: NSMakeRange(0, countOther)];
5981+
[self getCharacters: chars range: compareRange];
5982+
[string getCharacters: chars + countSelf
5983+
range: NSMakeRange(0, countOther)];
59985984

59995985
result = ucol_strcoll(coll,
6000-
charsSelf, countSelf, charsOther, countOther);
5986+
chars, countSelf, chars + countSelf, countOther);
5987+
GS_ENDITEMBUF()
60015988

6002-
if (!useStack)
6003-
{
6004-
free(charsSelf);
6005-
free(charsOther);
6006-
}
6007-
60085989
/* UCollationResult enums are stable and match
60095990
* NSComparisonResult enums
60105991
*/

0 commit comments

Comments
 (0)