1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-04 18:03:20 +03:00

Move some core functions back into RAM

This commit is contained in:
Ivan Grokhotkov 2015-10-06 16:11:40 +03:00
parent 4c8a29a74d
commit 4f35207951
2 changed files with 148 additions and 148 deletions

View File

@ -25,20 +25,20 @@
#define CONT_STACKGUARD 0xfeefeffe
void cont_init(cont_t* cont) {
void ICACHE_RAM_ATTR cont_init(cont_t* cont) {
cont->stack_guard1 = CONT_STACKGUARD;
cont->stack_guard2 = CONT_STACKGUARD;
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4);
cont->struct_start = (unsigned*) cont;
}
int cont_check(cont_t* cont) {
int ICACHE_RAM_ATTR cont_check(cont_t* cont) {
if(cont->stack_guard1 != CONT_STACKGUARD || cont->stack_guard2 != CONT_STACKGUARD) return 1;
return 0;
}
bool cont_can_yield(cont_t* cont) {
bool ICACHE_RAM_ATTR cont_can_yield(cont_t* cont) {
return !ETS_INTR_WITHINISR() &&
cont->pc_ret != 0 && cont->pc_yield == 0;
}

View File

@ -39,25 +39,25 @@
#include "user_interface.h"
#include "debug.h"
void* malloc(size_t size) {
void* ICACHE_RAM_ATTR malloc(size_t size) {
size = ((size + 3) & ~((size_t)0x3));
return os_malloc(size);
}
void free(void* ptr) {
void ICACHE_RAM_ATTR free(void* ptr) {
os_free(ptr);
}
void* realloc(void* ptr, size_t size) {
void* ICACHE_RAM_ATTR realloc(void* ptr, size_t size) {
size = ((size + 3) & ~((size_t)0x3));
return os_realloc(ptr, size);
}
int puts(const char * str) {
int ICACHE_RAM_ATTR puts(const char * str) {
return os_printf("%s", str);
}
int printf(const char* format, ...) {
int ICACHE_RAM_ATTR printf(const char* format, ...) {
int ret;
va_list arglist;
va_start(arglist, format);
@ -66,7 +66,7 @@ int printf(const char* format, ...) {
return ret;
}
int sprintf(char* buffer, const char* format, ...) {
int ICACHE_RAM_ATTR sprintf(char* buffer, const char* format, ...) {
int ret;
va_list arglist;
va_start(arglist, format);
@ -75,7 +75,7 @@ int sprintf(char* buffer, const char* format, ...) {
return ret;
}
int snprintf(char* buffer, size_t size, const char* format, ...) {
int ICACHE_RAM_ATTR snprintf(char* buffer, size_t size, const char* format, ...) {
int ret;
va_list arglist;
va_start(arglist, format);
@ -84,22 +84,22 @@ int snprintf(char* buffer, size_t size, const char* format, ...) {
return ret;
}
int vprintf(const char * format, va_list arg) {
int ICACHE_RAM_ATTR vprintf(const char * format, va_list arg) {
return ets_vprintf(format, arg);
}
int vsnprintf(char * buffer, size_t size, const char * format, va_list arg) {
int ICACHE_RAM_ATTR vsnprintf(char * buffer, size_t size, const char * format, va_list arg) {
return ets_vsnprintf(buffer, size, format, arg);
}
size_t ICACHE_FLASH_ATTR strnlen(const char *s, size_t len) {
size_t strnlen(const char *s, size_t len) {
// there is no ets_strnlen
const char *cp;
for (cp = s; len != 0 && *cp != '\0'; cp++, len--);
return (size_t)(cp - s);
}
char* ICACHE_FLASH_ATTR strchr(const char * str, int character) {
char* strchr(const char * str, int character) {
while(1) {
if(*str == 0x00) {
return NULL;
@ -111,7 +111,7 @@ char* ICACHE_FLASH_ATTR strchr(const char * str, int character) {
}
}
char * ICACHE_FLASH_ATTR strrchr(const char * str, int character) {
char* strrchr(const char * str, int character) {
char * ret = NULL;
while(1) {
if(*str == 0x00) {
@ -124,11 +124,11 @@ char * ICACHE_FLASH_ATTR strrchr(const char * str, int character) {
}
}
char* ICACHE_FLASH_ATTR strcat(char * dest, const char * src) {
char* strcat(char * dest, const char * src) {
return strncat(dest, src, strlen(src));
}
char* ICACHE_FLASH_ATTR strncat(char * dest, const char * src, size_t n) {
char* strncat(char * dest, const char * src, size_t n) {
size_t i;
size_t offset = strlen(dest);
for(i = 0; i < n && src[i]; i++) {
@ -138,7 +138,7 @@ char* ICACHE_FLASH_ATTR strncat(char * dest, const char * src, size_t n) {
return dest;
}
char* ICACHE_FLASH_ATTR strtok_r(char* s, const char* delim, char** last) {
char* strtok_r(char* s, const char* delim, char** last) {
const char* spanp;
char* tok;
char c;
@ -193,13 +193,13 @@ cont:
// NOTREACHED EVER
}
char* ICACHE_FLASH_ATTR strtok(char* s, const char* delim) {
char* strtok(char* s, const char* delim) {
static char* last;
return (strtok_r(s, delim, &last));
}
int ICACHE_FLASH_ATTR strcasecmp(const char * str1, const char * str2) {
int strcasecmp(const char * str1, const char * str2) {
int d = 0;
while(1) {
int c1 = tolower(*str1++);
@ -211,7 +211,7 @@ int ICACHE_FLASH_ATTR strcasecmp(const char * str1, const char * str2) {
return d;
}
char* ICACHE_FLASH_ATTR strdup(const char *str) {
char* strdup(const char *str) {
size_t len = strlen(str) + 1;
char *cstr = malloc(len);
if(cstr) {
@ -222,7 +222,7 @@ char* ICACHE_FLASH_ATTR strdup(const char *str) {
// based on Source:
// https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93
double ICACHE_FLASH_ATTR strtod(const char* str, char** endptr) {
double strtod(const char* str, char** endptr) {
double result = 0.0;
double factor = 1.0;
bool decimals = false;
@ -388,7 +388,7 @@ int isblank(int c) {
static int errno_var = 0;
int* ICACHE_FLASH_ATTR __errno(void) {
int* __errno(void) {
// DEBUGV("__errno is called last error: %d (not current)\n", errno_var);
return &errno_var;
}
@ -440,7 +440,7 @@ time_t mktime(struct tm *timp) {
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
size_t ICACHE_FLASH_ATTR strlcpy(char* dst, const char* src, size_t size) {
size_t strlcpy(char* dst, const char* src, size_t size) {
const char *s = src;
size_t n = size;
@ -486,8 +486,8 @@ size_t ICACHE_FLASH_ATTR strlcpy(char* dst, const char* src, size_t size) {
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
@ -505,134 +505,134 @@ size_t ICACHE_FLASH_ATTR strlcpy(char* dst, const char* src, size_t size) {
* SUCH DAMAGE.
*/
long ICACHE_FLASH_ATTR strtol(const char *nptr, char **endptr, int base) {
const unsigned char *s = (const unsigned char *)nptr;
unsigned long acc;
int c;
unsigned long cutoff;
int neg = 0, any, cutlim;
long strtol(const char *nptr, char **endptr, int base) {
const unsigned char *s = (const unsigned char *)nptr;
unsigned long acc;
int c;
unsigned long cutoff;
int neg = 0, any, cutlim;
/*
* Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x.
*/
do {
c = *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else if (c == '+')
c = *s++;
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
/*
* Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x.
*/
do {
c = *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else if (c == '+')
c = *s++;
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
/*
* Compute the cutoff value between legal numbers and illegal
* numbers. That is the largest legal value, divided by the
* base. An input number that is greater than this value, if
* followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit. For instance, if the range for longs is
* [-2147483648..2147483647] and the input base is 10,
* cutoff will be set to 214748364 and cutlim to either
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
* a value > 214748364, or equal but the next digit is > 7 (or 8),
* the number is too big, and we will return a range error.
*
* Set any if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
cutlim = cutoff % (unsigned long)base;
cutoff /= (unsigned long)base;
for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
/*
* Compute the cutoff value between legal numbers and illegal
* numbers. That is the largest legal value, divided by the
* base. An input number that is greater than this value, if
* followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit. For instance, if the range for longs is
* [-2147483648..2147483647] and the input base is 10,
* cutoff will be set to 214748364 and cutlim to either
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
* a value > 214748364, or equal but the next digit is > 7 (or 8),
* the number is too big, and we will return a range error.
*
* Set any if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
cutlim = cutoff % (unsigned long)base;
cutoff /= (unsigned long)base;
for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = neg ? LONG_MIN : LONG_MAX;
errno = ERANGE;
} else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = (char *) (any ? (char *)s - 1 : nptr);
return (acc);
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = neg ? LONG_MIN : LONG_MAX;
errno = ERANGE;
} else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = (char *) (any ? (char *)s - 1 : nptr);
return (acc);
}
unsigned long ICACHE_FLASH_ATTR strtoul(const char *nptr, char **endptr, int base)
unsigned long strtoul(const char *nptr, char **endptr, int base)
{
const unsigned char *s = (const unsigned char *)nptr;
unsigned long acc;
int c;
unsigned long cutoff;
int neg = 0, any, cutlim;
const unsigned char *s = (const unsigned char *)nptr;
unsigned long acc;
int c;
unsigned long cutoff;
int neg = 0, any, cutlim;
/*
* See strtol for comments as to the logic used.
*/
do {
c = *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else if (c == '+')
c = *s++;
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
/*
* See strtol for comments as to the logic used.
*/
do {
c = *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else if (c == '+')
c = *s++;
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0;; c = *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = ULONG_MAX;
errno = ERANGE;
} else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = (char *) (any ? (char *)s - 1 : nptr);
return (acc);
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = ULONG_MAX;
errno = ERANGE;
} else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = (char *) (any ? (char *)s - 1 : nptr);
return (acc);
}