Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab309e4

Browse files
earlephilhowerme-no-dev
authored andcommittedApr 26, 2019
Copy ESP8266 String w/SSO to ESP32 repo (espressif#2715)
I redid the ESP8266 WString library to enable small string optimization (SSO) a while back, and think it would be helpful even on the ESP32 with its higher memory complement. SSO avoids lots of tiny mallocs() on the heap which cause fragmentation by using the memory in the class object itself to store the actual string and only mallocing() for buffers that are larger than what can fit in thie class object. Modern C++ std::string implementations have this optimization as well, but since we're using Arduino strings we had to roll our own.
1 parent 932666a commit ab309e4

File tree

3 files changed

+656
-729
lines changed

3 files changed

+656
-729
lines changed
 

‎cores/esp32/StreamString.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
StreamString.cpp
33
44
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
56
67
This library is free software; you can redistribute it and/or
78
modify it under the terms of the GNU Lesser General Public
@@ -22,31 +23,27 @@
2223
#include <Arduino.h>
2324
#include "StreamString.h"
2425

25-
size_t StreamString::write(const uint8_t *data, size_t size)
26-
{
26+
size_t StreamString::write(const uint8_t *data, size_t size) {
2727
if(size && data) {
2828
if(reserve(length() + size + 1)) {
29-
memcpy((void *) (buffer + len), (const void *) data, size);
30-
len += size;
31-
*(buffer + len) = 0x00; // add null for string end
29+
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
30+
setLen(len() + size);
31+
*(wbuffer() + len()) = 0x00; // add null for string end
3232
return size;
3333
}
3434
}
3535
return 0;
3636
}
3737

38-
size_t StreamString::write(uint8_t data)
39-
{
38+
size_t StreamString::write(uint8_t data) {
4039
return concat((char) data);
4140
}
4241

43-
int StreamString::available()
44-
{
42+
int StreamString::available() {
4543
return length();
4644
}
4745

48-
int StreamString::read()
49-
{
46+
int StreamString::read() {
5047
if(length()) {
5148
char c = charAt(0);
5249
remove(0, 1);
@@ -56,16 +53,14 @@ int StreamString::read()
5653
return -1;
5754
}
5855

59-
int StreamString::peek()
60-
{
56+
int StreamString::peek() {
6157
if(length()) {
6258
char c = charAt(0);
6359
return c;
6460
}
6561
return -1;
6662
}
6763

68-
void StreamString::flush()
69-
{
64+
void StreamString::flush() {
7065
}
7166

‎cores/esp32/WString.cpp

Lines changed: 386 additions & 451 deletions
Large diffs are not rendered by default.

‎cores/esp32/WString.h

Lines changed: 260 additions & 263 deletions
Original file line numberDiff line numberDiff line change
@@ -35,295 +35,292 @@ class StringSumHelper;
3535
// an abstract class used as a means to proide a unique pointer type
3636
// but really has no body
3737
class __FlashStringHelper;
38-
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
38+
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
39+
#define F(string_literal) (FPSTR(PSTR(string_literal)))
3940

4041
// The string class
41-
class String
42-
{
43-
// use a function pointer to allow for "if (s)" without the
44-
// complications of an operator bool(). for more information, see:
45-
// http://www.artima.com/cppsource/safebool.html
46-
typedef void (String::*StringIfHelperType)() const;
47-
void StringIfHelper() const
48-
{
49-
}
42+
class String {
43+
// use a function pointer to allow for "if (s)" without the
44+
// complications of an operator bool(). for more information, see:
45+
// http://www.artima.com/cppsource/safebool.html
46+
typedef void (String::*StringIfHelperType)() const;
47+
void StringIfHelper() const {
48+
}
5049

51-
public:
52-
// constructors
53-
// creates a copy of the initial value.
54-
// if the initial value is null or invalid, or if memory allocation
55-
// fails, the string will be marked as invalid (i.e. "if (s)" will
56-
// be false).
57-
String(const char *cstr = "");
58-
String(const String &str);
59-
String(const __FlashStringHelper *str) : String(reinterpret_cast<const char *>(str)) {};
50+
public:
51+
// constructors
52+
// creates a copy of the initial value.
53+
// if the initial value is null or invalid, or if memory allocation
54+
// fails, the string will be marked as invalid (i.e. "if (s)" will
55+
// be false).
56+
String(const char *cstr = "");
57+
String(const String &str);
58+
String(const __FlashStringHelper *str);
6059
#ifdef __GXX_EXPERIMENTAL_CXX0X__
61-
String(String &&rval);
62-
String(StringSumHelper &&rval);
60+
String(String &&rval);
61+
String(StringSumHelper &&rval);
6362
#endif
64-
explicit String(char c);
65-
explicit String(unsigned char, unsigned char base = 10);
66-
explicit String(int, unsigned char base = 10);
67-
explicit String(unsigned int, unsigned char base = 10);
68-
explicit String(long, unsigned char base = 10);
69-
explicit String(unsigned long, unsigned char base = 10);
70-
explicit String(float, unsigned char decimalPlaces = 2);
71-
explicit String(double, unsigned char decimalPlaces = 2);
72-
~String(void);
63+
explicit String(char c);
64+
explicit String(unsigned char, unsigned char base = 10);
65+
explicit String(int, unsigned char base = 10);
66+
explicit String(unsigned int, unsigned char base = 10);
67+
explicit String(long, unsigned char base = 10);
68+
explicit String(unsigned long, unsigned char base = 10);
69+
explicit String(float, unsigned char decimalPlaces = 2);
70+
explicit String(double, unsigned char decimalPlaces = 2);
71+
~String(void);
7372

74-
// memory management
75-
// return true on success, false on failure (in which case, the string
76-
// is left unchanged). reserve(0), if successful, will validate an
77-
// invalid string (i.e., "if (s)" will be true afterwards)
78-
unsigned char reserve(unsigned int size);
79-
inline unsigned int length(void) const
80-
{
81-
if(buffer) {
82-
return len;
83-
} else {
84-
return 0;
73+
// memory management
74+
// return true on success, false on failure (in which case, the string
75+
// is left unchanged). reserve(0), if successful, will validate an
76+
// invalid string (i.e., "if (s)" will be true afterwards)
77+
unsigned char reserve(unsigned int size);
78+
inline unsigned int length(void) const {
79+
if(buffer()) {
80+
return len();
81+
} else {
82+
return 0;
83+
}
8584
}
86-
}
8785

88-
// creates a copy of the assigned value. if the value is null or
89-
// invalid, or if the memory allocation fails, the string will be
90-
// marked as invalid ("if (s)" will be false).
91-
String & operator =(const String &rhs);
92-
String & operator =(const char *cstr);
93-
String & operator = (const __FlashStringHelper *str);
86+
// creates a copy of the assigned value. if the value is null or
87+
// invalid, or if the memory allocation fails, the string will be
88+
// marked as invalid ("if (s)" will be false).
89+
String & operator =(const String &rhs);
90+
String & operator =(const char *cstr);
91+
String & operator = (const __FlashStringHelper *str);
9492
#ifdef __GXX_EXPERIMENTAL_CXX0X__
95-
String & operator =(String &&rval);
96-
String & operator =(StringSumHelper &&rval);
93+
String & operator =(String &&rval);
94+
String & operator =(StringSumHelper &&rval);
9795
#endif
9896

99-
// concatenate (works w/ built-in types)
97+
// concatenate (works w/ built-in types)
98+
99+
// returns true on success, false on failure (in which case, the string
100+
// is left unchanged). if the argument is null or invalid, the
101+
// concatenation is considered unsucessful.
102+
unsigned char concat(const String &str);
103+
unsigned char concat(const char *cstr);
104+
unsigned char concat(char c);
105+
unsigned char concat(unsigned char c);
106+
unsigned char concat(int num);
107+
unsigned char concat(unsigned int num);
108+
unsigned char concat(long num);
109+
unsigned char concat(unsigned long num);
110+
unsigned char concat(float num);
111+
unsigned char concat(double num);
112+
unsigned char concat(const __FlashStringHelper * str);
113+
114+
// if there's not enough memory for the concatenated value, the string
115+
// will be left unchanged (but this isn't signalled in any way)
116+
String & operator +=(const String &rhs) {
117+
concat(rhs);
118+
return (*this);
119+
}
120+
String & operator +=(const char *cstr) {
121+
concat(cstr);
122+
return (*this);
123+
}
124+
String & operator +=(char c) {
125+
concat(c);
126+
return (*this);
127+
}
128+
String & operator +=(unsigned char num) {
129+
concat(num);
130+
return (*this);
131+
}
132+
String & operator +=(int num) {
133+
concat(num);
134+
return (*this);
135+
}
136+
String & operator +=(unsigned int num) {
137+
concat(num);
138+
return (*this);
139+
}
140+
String & operator +=(long num) {
141+
concat(num);
142+
return (*this);
143+
}
144+
String & operator +=(unsigned long num) {
145+
concat(num);
146+
return (*this);
147+
}
148+
String & operator +=(float num) {
149+
concat(num);
150+
return (*this);
151+
}
152+
String & operator +=(double num) {
153+
concat(num);
154+
return (*this);
155+
}
156+
String & operator += (const __FlashStringHelper *str){
157+
concat(str);
158+
return (*this);
159+
}
100160

101-
// returns true on success, false on failure (in which case, the string
102-
// is left unchanged). if the argument is null or invalid, the
103-
// concatenation is considered unsucessful.
104-
unsigned char concat(const String &str);
105-
unsigned char concat(const char *cstr);
106-
unsigned char concat(char c);
107-
unsigned char concat(unsigned char c);
108-
unsigned char concat(int num);
109-
unsigned char concat(unsigned int num);
110-
unsigned char concat(long num);
111-
unsigned char concat(unsigned long num);
112-
unsigned char concat(float num);
113-
unsigned char concat(double num);
114-
unsigned char concat(const __FlashStringHelper * str);
161+
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
162+
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
163+
friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
164+
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
165+
friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
166+
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
167+
friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
168+
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
169+
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
170+
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
171+
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
115172

116-
// if there's not enough memory for the concatenated value, the string
117-
// will be left unchanged (but this isn't signalled in any way)
118-
String & operator +=(const String &rhs)
119-
{
120-
concat(rhs);
121-
return (*this);
122-
}
123-
String & operator +=(const char *cstr)
124-
{
125-
concat(cstr);
126-
return (*this);
127-
}
128-
String & operator +=(char c)
129-
{
130-
concat(c);
131-
return (*this);
132-
}
133-
String & operator +=(unsigned char num)
134-
{
135-
concat(num);
136-
return (*this);
137-
}
138-
String & operator +=(int num)
139-
{
140-
concat(num);
141-
return (*this);
142-
}
143-
String & operator +=(unsigned int num)
144-
{
145-
concat(num);
146-
return (*this);
147-
}
148-
String & operator +=(long num)
149-
{
150-
concat(num);
151-
return (*this);
152-
}
153-
String & operator +=(unsigned long num)
154-
{
155-
concat(num);
156-
return (*this);
157-
}
158-
String & operator +=(float num)
159-
{
160-
concat(num);
161-
return (*this);
162-
}
163-
String & operator +=(double num)
164-
{
165-
concat(num);
166-
return (*this);
167-
}
168-
String & operator += (const __FlashStringHelper *str)
169-
{
170-
concat(str);
171-
return (*this);
172-
}
173+
// comparison (only works w/ Strings and "strings")
174+
operator StringIfHelperType() const {
175+
return buffer() ? &String::StringIfHelper : 0;
176+
}
177+
int compareTo(const String &s) const;
178+
unsigned char equals(const String &s) const;
179+
unsigned char equals(const char *cstr) const;
180+
unsigned char operator ==(const String &rhs) const {
181+
return equals(rhs);
182+
}
183+
unsigned char operator ==(const char *cstr) const {
184+
return equals(cstr);
185+
}
186+
unsigned char operator !=(const String &rhs) const {
187+
return !equals(rhs);
188+
}
189+
unsigned char operator !=(const char *cstr) const {
190+
return !equals(cstr);
191+
}
192+
unsigned char operator <(const String &rhs) const;
193+
unsigned char operator >(const String &rhs) const;
194+
unsigned char operator <=(const String &rhs) const;
195+
unsigned char operator >=(const String &rhs) const;
196+
unsigned char equalsIgnoreCase(const String &s) const;
197+
unsigned char equalsConstantTime(const String &s) const;
198+
unsigned char startsWith(const String &prefix) const;
199+
unsigned char startsWith(const String &prefix, unsigned int offset) const;
200+
unsigned char endsWith(const String &suffix) const;
173201

174-
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
175-
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
176-
friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
177-
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
178-
friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
179-
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
180-
friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
181-
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
182-
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
183-
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
184-
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
202+
// character acccess
203+
char charAt(unsigned int index) const;
204+
void setCharAt(unsigned int index, char c);
205+
char operator [](unsigned int index) const;
206+
char& operator [](unsigned int index);
207+
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
208+
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const {
209+
getBytes((unsigned char *) buf, bufsize, index);
210+
}
211+
const char* c_str() const { return buffer(); }
212+
char* begin() { return wbuffer(); }
213+
char* end() { return wbuffer() + length(); }
214+
const char* begin() const { return c_str(); }
215+
const char* end() const { return c_str() + length(); }
185216

186-
// comparison (only works w/ Strings and "strings")
187-
operator StringIfHelperType() const
188-
{
189-
return buffer ? &String::StringIfHelper : 0;
190-
}
191-
int compareTo(const String &s) const;
192-
unsigned char equals(const String &s) const;
193-
unsigned char equals(const char *cstr) const;
194-
unsigned char operator ==(const String &rhs) const
195-
{
196-
return equals(rhs);
197-
}
198-
unsigned char operator ==(const char *cstr) const
199-
{
200-
return equals(cstr);
201-
}
202-
unsigned char operator !=(const String &rhs) const
203-
{
204-
return !equals(rhs);
205-
}
206-
unsigned char operator !=(const char *cstr) const
207-
{
208-
return !equals(cstr);
209-
}
210-
unsigned char operator <(const String &rhs) const;
211-
unsigned char operator >(const String &rhs) const;
212-
unsigned char operator <=(const String &rhs) const;
213-
unsigned char operator >=(const String &rhs) const;
214-
unsigned char equalsIgnoreCase(const String &s) const;
215-
unsigned char equalsConstantTime(const String &s) const;
216-
unsigned char startsWith(const String &prefix) const;
217-
unsigned char startsWith(const String &prefix, unsigned int offset) const;
218-
unsigned char endsWith(const String &suffix) const;
217+
// search
218+
int indexOf(char ch) const;
219+
int indexOf(char ch, unsigned int fromIndex) const;
220+
int indexOf(const String &str) const;
221+
int indexOf(const String &str, unsigned int fromIndex) const;
222+
int lastIndexOf(char ch) const;
223+
int lastIndexOf(char ch, unsigned int fromIndex) const;
224+
int lastIndexOf(const String &str) const;
225+
int lastIndexOf(const String &str, unsigned int fromIndex) const;
226+
String substring(unsigned int beginIndex) const {
227+
return substring(beginIndex, len());
228+
}
229+
;
230+
String substring(unsigned int beginIndex, unsigned int endIndex) const;
219231

220-
// character acccess
221-
char charAt(unsigned int index) const;
222-
void setCharAt(unsigned int index, char c);
223-
char operator [](unsigned int index) const;
224-
char& operator [](unsigned int index);
225-
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
226-
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const
227-
{
228-
getBytes((unsigned char *) buf, bufsize, index);
229-
}
230-
const char * c_str() const
231-
{
232-
return buffer;
233-
}
232+
// modification
233+
void replace(char find, char replace);
234+
void replace(const String& find, const String& replace);
235+
void remove(unsigned int index);
236+
void remove(unsigned int index, unsigned int count);
237+
void toLowerCase(void);
238+
void toUpperCase(void);
239+
void trim(void);
234240

235-
// search
236-
int indexOf(char ch) const;
237-
int indexOf(char ch, unsigned int fromIndex) const;
238-
int indexOf(const String &str) const;
239-
int indexOf(const String &str, unsigned int fromIndex) const;
240-
int lastIndexOf(char ch) const;
241-
int lastIndexOf(char ch, unsigned int fromIndex) const;
242-
int lastIndexOf(const String &str) const;
243-
int lastIndexOf(const String &str, unsigned int fromIndex) const;
244-
String substring(unsigned int beginIndex) const
245-
{
246-
return substring(beginIndex, len);
247-
}
248-
;
249-
String substring(unsigned int beginIndex, unsigned int endIndex) const;
241+
// parsing/conversion
242+
long toInt(void) const;
243+
float toFloat(void) const;
244+
double toDouble(void) const;
250245

251-
// modification
252-
void replace(char find, char replace);
253-
void replace(const String& find, const String& replace);
254-
void remove(unsigned int index);
255-
void remove(unsigned int index, unsigned int count);
256-
void toLowerCase(void);
257-
void toUpperCase(void);
258-
void trim(void);
246+
protected:
247+
// Contains the string info when we're not in SSO mode
248+
struct _ptr {
249+
char * buff;
250+
uint16_t cap;
251+
uint16_t len;
252+
};
259253

260-
// parsing/conversion
261-
long toInt(void) const;
262-
float toFloat(void) const;
263-
double toDouble(void) const;
254+
// SSO is handled by checking the last byte of sso_buff.
255+
// When not in SSO mode, that byte is set to 0xff, while when in SSO mode it is always 0x00 (so it can serve as the string terminator as well as a flag)
256+
// This allows strings up up to 12 (11 + \0 termination) without any extra space.
257+
enum { SSOSIZE = sizeof(struct _ptr) + 4 }; // Characters to allocate space for SSO, must be 12 or more
258+
enum { CAPACITY_MAX = 65535 }; // If size of capacity changed, be sure to update this enum
259+
union {
260+
struct _ptr ptr;
261+
char sso_buf[SSOSIZE];
262+
};
263+
// Accessor functions
264+
inline bool sso() const { return sso_buf[SSOSIZE - 1] == 0; }
265+
inline unsigned int len() const { return sso() ? strlen(sso_buf) : ptr.len; }
266+
inline unsigned int capacity() const { return sso() ? SSOSIZE - 1 : ptr.cap; }
267+
inline void setSSO(bool sso) { sso_buf[SSOSIZE - 1] = sso ? 0x00 : 0xff; }
268+
inline void setLen(int len) { if (!sso()) ptr.len = len; }
269+
inline void setCapacity(int cap) { if (!sso()) ptr.cap = cap; }
270+
inline void setBuffer(char *buff) { if (!sso()) ptr.buff = buff; }
271+
// Buffer accessor functions
272+
inline const char *buffer() const { return (const char *)(sso() ? sso_buf : ptr.buff); }
273+
inline char *wbuffer() const { return sso() ? const_cast<char *>(sso_buf) : ptr.buff; } // Writable version of buffer
264274

265-
protected:
266-
char *buffer; // the actual char array
267-
unsigned int capacity; // the array length minus one (for the '\0')
268-
unsigned int len; // the String length (not counting the '\0')
269-
protected:
270-
void init(void);
271-
void invalidate(void);
272-
unsigned char changeBuffer(unsigned int maxStrLen);
273-
unsigned char concat(const char *cstr, unsigned int length);
275+
protected:
276+
void init(void);
277+
void invalidate(void);
278+
unsigned char changeBuffer(unsigned int maxStrLen);
279+
unsigned char concat(const char *cstr, unsigned int length);
274280

275-
// copy and move
276-
String & copy(const char *cstr, unsigned int length);
277-
String & copy(const __FlashStringHelper *pstr, unsigned int length);
281+
// copy and move
282+
String & copy(const char *cstr, unsigned int length);
283+
String & copy(const __FlashStringHelper *pstr, unsigned int length);
278284
#ifdef __GXX_EXPERIMENTAL_CXX0X__
279-
void move(String &rhs);
285+
void move(String &rhs);
280286
#endif
281287
};
282288

283-
class StringSumHelper: public String
284-
{
285-
public:
286-
StringSumHelper(const String &s) :
287-
String(s)
288-
{
289-
}
290-
StringSumHelper(const char *p) :
291-
String(p)
292-
{
293-
}
294-
StringSumHelper(char c) :
295-
String(c)
296-
{
297-
}
298-
StringSumHelper(unsigned char num) :
299-
String(num)
300-
{
301-
}
302-
StringSumHelper(int num) :
303-
String(num)
304-
{
305-
}
306-
StringSumHelper(unsigned int num) :
307-
String(num)
308-
{
309-
}
310-
StringSumHelper(long num) :
311-
String(num)
312-
{
313-
}
314-
StringSumHelper(unsigned long num) :
315-
String(num)
316-
{
317-
}
318-
StringSumHelper(float num) :
319-
String(num)
320-
{
321-
}
322-
StringSumHelper(double num) :
323-
String(num)
324-
{
325-
}
289+
class StringSumHelper: public String {
290+
public:
291+
StringSumHelper(const String &s) :
292+
String(s) {
293+
}
294+
StringSumHelper(const char *p) :
295+
String(p) {
296+
}
297+
StringSumHelper(char c) :
298+
String(c) {
299+
}
300+
StringSumHelper(unsigned char num) :
301+
String(num) {
302+
}
303+
StringSumHelper(int num) :
304+
String(num) {
305+
}
306+
StringSumHelper(unsigned int num) :
307+
String(num) {
308+
}
309+
StringSumHelper(long num) :
310+
String(num) {
311+
}
312+
StringSumHelper(unsigned long num) :
313+
String(num) {
314+
}
315+
StringSumHelper(float num) :
316+
String(num) {
317+
}
318+
StringSumHelper(double num) :
319+
String(num) {
320+
}
326321
};
327322

323+
extern const String emptyString;
324+
328325
#endif // __cplusplus
329326
#endif // String_class_h

0 commit comments

Comments
 (0)
Please sign in to comment.