Skip to content

Commit

Permalink
Merge pull request esp8266#15 from esp8266/master
Browse files Browse the repository at this point in the history
memmove_P
  • Loading branch information
Jason2866 authored Sep 14, 2019
2 parents 30627b7 + 8dd068e commit b1837bf
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 40 deletions.
1 change: 1 addition & 0 deletions cores/esp8266/StreamString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ size_t StreamString::write(const uint8_t *data, size_t size) {
*(wbuffer() + newlen) = 0x00; // add null for string end
return size;
}
DEBUGV(":stream2string: OOM (%d->%d)\n", length(), newlen+1);
}
return 0;
}
Expand Down
33 changes: 14 additions & 19 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
size_t oldSize = capacity() + 1; // include NULL.
if (isSSO()) {
// Copy the SSO buffer into allocated space
memmove(newbuffer, sso.buff, sizeof(sso.buff));
memmove_P(newbuffer, sso.buff, sizeof(sso.buff));
}
if (newSize > oldSize)
{
Expand All @@ -210,7 +210,7 @@ String & String::copy(const char *cstr, unsigned int length) {
return *this;
}
setLen(length);
memmove(wbuffer(), cstr, length + 1);
memmove_P(wbuffer(), cstr, length + 1);
return *this;
}

Expand All @@ -228,7 +228,7 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
void String::move(String &rhs) {
if(buffer()) {
if(capacity() >= rhs.len()) {
memmove(wbuffer(), rhs.buffer(), rhs.length() + 1);
memmove_P(wbuffer(), rhs.buffer(), rhs.length() + 1);
setLen(rhs.len());
rhs.invalidate();
return;
Expand All @@ -241,7 +241,7 @@ void String::move(String &rhs) {
}
if (rhs.isSSO()) {
setSSO(true);
memmove(sso.buff, rhs.sso.buff, sizeof(sso.buff));
memmove_P(sso.buff, rhs.sso.buff, sizeof(sso.buff));
} else {
setSSO(false);
setBuffer(rhs.wbuffer());
Expand Down Expand Up @@ -313,7 +313,7 @@ unsigned char String::concat(const String &s) {
return 1;
if (!reserve(newlen))
return 0;
memmove(wbuffer() + len(), buffer(), len());
memmove_P(wbuffer() + len(), buffer(), len());
setLen(newlen);
wbuffer()[len()] = 0;
return 1;
Expand All @@ -330,12 +330,7 @@ unsigned char String::concat(const char *cstr, unsigned int length) {
return 1;
if(!reserve(newlen))
return 0;
if (cstr >= wbuffer() && cstr < wbuffer() + len())
// compatible with SSO in ram #6155 (case "x += x.c_str()")
memmove(wbuffer() + len(), cstr, length + 1);
else
// compatible with source in flash #6367
memcpy_P(wbuffer() + len(), cstr, length + 1);
memmove_P(wbuffer() + len(), cstr, length + 1);
setLen(newlen);
return 1;
}
Expand Down Expand Up @@ -739,21 +734,21 @@ void String::replace(const String& find, const String& replace) {
char *foundAt;
if(diff == 0) {
while((foundAt = strstr(readFrom, find.buffer())) != NULL) {
memmove(foundAt, replace.buffer(), replace.len());
memmove_P(foundAt, replace.buffer(), replace.len());
readFrom = foundAt + replace.len();
}
} else if(diff < 0) {
char *writeTo = wbuffer();
while((foundAt = strstr(readFrom, find.buffer())) != NULL) {
unsigned int n = foundAt - readFrom;
memmove(writeTo, readFrom, n);
memmove_P(writeTo, readFrom, n);
writeTo += n;
memmove(writeTo, replace.buffer(), replace.len());
memmove_P(writeTo, replace.buffer(), replace.len());
writeTo += replace.len();
readFrom = foundAt + find.len();
setLen(len() + diff);
}
memmove(writeTo, readFrom, strlen(readFrom)+1);
memmove_P(writeTo, readFrom, strlen(readFrom)+1);
} else {
unsigned int size = len(); // compute size needed for result
while((foundAt = strstr(readFrom, find.buffer())) != NULL) {
Expand All @@ -767,9 +762,9 @@ void String::replace(const String& find, const String& replace) {
int index = len() - 1;
while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
readFrom = wbuffer() + index + find.len();
memmove(readFrom + diff, readFrom, len() - (readFrom - buffer()));
memmove_P(readFrom + diff, readFrom, len() - (readFrom - buffer()));
int newLen = len() + diff;
memmove(wbuffer() + index, replace.buffer(), replace.len());
memmove_P(wbuffer() + index, replace.buffer(), replace.len());
setLen(newLen);
wbuffer()[newLen] = 0;
index--;
Expand Down Expand Up @@ -797,7 +792,7 @@ void String::remove(unsigned int index, unsigned int count) {
char *writeTo = wbuffer() + index;
unsigned int newlen = len() - count;
setLen(newlen);
memmove(writeTo, wbuffer() + index + count, newlen - index);
memmove_P(writeTo, wbuffer() + index + count, newlen - index);
wbuffer()[newlen] = 0;
}

Expand Down Expand Up @@ -829,7 +824,7 @@ void String::trim(void) {
unsigned int newlen = end + 1 - begin;
setLen(newlen);
if(begin > buffer())
memmove(wbuffer(), begin, newlen);
memmove_P(wbuffer(), begin, newlen);
wbuffer()[newlen] = 0;
}

Expand Down
6 changes: 3 additions & 3 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ const String& HTTPClient::getString(void)

_payload.reset(new StreamString());

if(_size) {
if(_size > 0) {
// try to reserve needed memmory
if(!_payload->reserve((_size + 1))) {
DEBUG_HTTPCLIENT("[HTTP-Client][getString] not enough memory to reserve a string! need: %d\n", (_size + 1));
Expand Down Expand Up @@ -1456,10 +1456,10 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)

free(buff);

DEBUG_HTTPCLIENT("[HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: %d).\n", bytesWritten);
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStreamDataBlock] end of chunk or data (transferred: %d).\n", bytesWritten);

if((size > 0) && (size != bytesWritten)) {
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStreamDataBlock] bytesWritten %d and size %d mismatch!.\n", bytesWritten, size);
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStreamDataBlock] transferred size %d and request size %d mismatch!.\n", bytesWritten, size);
return HTTPC_ERROR_STREAM_WRITE;
}

Expand Down
2 changes: 1 addition & 1 deletion package/package_esp8266com_index.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"tools": [
{
"version": "3.7.2-post1",
"name": "python",
"name": "python3",
"systems": [
{
"host": "x86_64-mingw32",
Expand Down
3 changes: 2 additions & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ compiler.elf2hex.extra_flags=
## generate file with git version number
## needs git
recipe.hooks.sketch.prebuild.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.signing}" --mode header --publickey "{build.source.path}/public.key" --out "{build.path}/core/Updater_Signing.h"
recipe.hooks.core.prebuild.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.makecorever}" --build_path "{build.path}" --platform_path "{runtime.platform.path}" --version "unix-{version}"
# This is quite a working hack. This form of prebuild hook, while intuitive, is not explicitly documented.
recipe.hooks.prebuild.10.pattern="{runtime.tools.python3.path}/python3" "{runtime.tools.makecorever}" --build_path "{build.path}" --platform_path "{runtime.platform.path}" --version "unix-{version}"

## Build the app.ld linker file
recipe.hooks.linking.prelink.1.pattern="{compiler.path}{compiler.c.cmd}" -CC -E -P {build.vtable_flags} "{runtime.platform.path}/tools/sdk/ld/eagle.app.v6.common.ld.h" -o "{build.path}/local.eagle.app.v6.common.ld"
Expand Down
31 changes: 18 additions & 13 deletions tests/host/common/ArduinoMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,26 @@ void help (const char* argv0, int exitcode)
" -b - blocking tty/mocked-uart (default: not blocking tty)\n"
" -S - spiffs size in KBytes (default: %zd)\n"
" -L - littlefs size in KBytes (default: %zd)\n"
" -v - mock verbose\n"
" (negative value will force mismatched size)\n"
"\t (spiffs, littlefs: negative value will force mismatched size)\n"
" -T - show timestamp on output\n"
" -v - verbose\n"
, argv0, MOCK_PORT_SHIFTER, spiffs_kb, littlefs_kb);
exit(exitcode);
}

static struct option options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "fast", no_argument, NULL, 'f' },
{ "local", no_argument, NULL, 'l' },
{ "sigint", no_argument, NULL, 'c' },
{ "blockinguart", no_argument, NULL, 'b' },
{ "verbose", no_argument, NULL, 'v' },
{ "interface", required_argument, NULL, 'i' },
{ "spiffskb", required_argument, NULL, 'S' },
{ "littlefskb", required_argument, NULL, 'L' },
{ "portshifter", required_argument, NULL, 's' },
{ "help", no_argument, NULL, 'h' },
{ "fast", no_argument, NULL, 'f' },
{ "local", no_argument, NULL, 'l' },
{ "sigint", no_argument, NULL, 'c' },
{ "blockinguart", no_argument, NULL, 'b' },
{ "verbose", no_argument, NULL, 'v' },
{ "timestamp", no_argument, NULL, 'T' },
{ "interface", required_argument, NULL, 'i' },
{ "spiffskb", required_argument, NULL, 'S' },
{ "littlefskb", required_argument, NULL, 'L' },
{ "portshifter", required_argument, NULL, 's' },
};

void cleanup ()
Expand Down Expand Up @@ -182,7 +184,7 @@ int main (int argc, char* const argv [])

for (;;)
{
int n = getopt_long(argc, argv, "hlcfbvi:S:s:L:", options, NULL);
int n = getopt_long(argc, argv, "hlcfbvTi:S:s:L:", options, NULL);
if (n < 0)
break;
switch (n)
Expand Down Expand Up @@ -217,6 +219,9 @@ int main (int argc, char* const argv [])
case 'v':
mockdebug = true;
break;
case 'T':
serial_timestamp = true;
break;
default:
help(argv[0], EXIT_FAILURE);
}
Expand Down
28 changes: 26 additions & 2 deletions tests/host/common/MockUART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/

#include <unistd.h> // write
#include <sys/time.h> // gettimeofday
#include <time.h> // localtime

#include "Arduino.h"
#include "uart.h"
Expand Down Expand Up @@ -59,13 +61,35 @@ struct uart_
struct uart_rx_buffer_ * rx_buffer;
};

bool serial_timestamp = false;

// write one byte to the emulated UART
static void
uart_do_write_char(const int uart_nr, char c)
{
static bool w = false;

if (uart_nr >= UART0 && uart_nr <= UART1)
if (1 != write(uart_nr + 1, &c, 1))
fprintf(stderr, "Unable to write character to emulated UART stream: %d\n", c);
{
if (serial_timestamp && (c == '\n' || c == '\r'))
{
if (w)
{
FILE* out = uart_nr == UART0? stdout: stderr;
timeval tv;
gettimeofday(&tv, nullptr);
const tm* tm = localtime(&tv.tv_sec);
fprintf(out, "\r\n%d:%02d:%02d.%06d: ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int)tv.tv_usec);
fflush(out);
w = false;
}
}
else
{
write(uart_nr + 1, &c, 1);
w = true;
}
}
}

// write a new byte into the RX FIFO buffer
Expand Down
2 changes: 1 addition & 1 deletion tests/host/common/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int ets_printf (const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
int mockverbose (const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));

extern const char* host_interface; // cmdline parameter

extern bool serial_timestamp;
extern int mock_port_shifter;

#define NO_GLOBAL_BINDING 0xffffffff
Expand Down
1 change: 1 addition & 0 deletions tests/host/sys/pgmspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ inline size_t strlen_P(const char *s) { return strlen(s); }
inline int vsnprintf_P(char *str, size_t size, const char *format, va_list ap) { return vsnprintf(str, size, format, ap); }

#define memcpy_P memcpy
#define memmove_P memmove
#define strncpy_P strncpy
#define strcmp_P strcmp
#define memccpy_P memccpy
Expand Down
1 change: 1 addition & 0 deletions tools/sdk/libc/xtensa-lx106-elf/include/sys/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
int _EXFUN(memcmp_P,(const _PTR, const _PTR, size_t));
_PTR _EXFUN(memmem_P, (const _PTR, size_t, const _PTR, size_t));
_PTR _EXFUN(memcpy_P,(_PTR __restrict, const _PTR __restrict, size_t));
_PTR _EXFUN(memmove_P,(_PTR __restrict, const _PTR __restrict, size_t));
_PTR _EXFUN(memccpy_P,(_PTR __restrict, const _PTR __restrict, int, size_t));
_PTR _EXFUN(memchr_P,(const _PTR, int, size_t));

Expand Down
Binary file modified tools/sdk/libc/xtensa-lx106-elf/lib/libc.a
Binary file not shown.

0 comments on commit b1837bf

Please sign in to comment.