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

Allow non-aligned PSTR() (#7275)

* Allow non-aligned PSTR()

* Add PSTR4() macro to first 4-bytes aligned PSTR
This commit is contained in:
s-hadinger 2020-05-18 21:21:50 +02:00 committed by GitHub
parent 1b20cd6263
commit 3e4d7c76c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -48,7 +48,8 @@ void ICACHE_FLASH_ATTR print_stats(int force);
int ICACHE_FLASH_ATTR umm_info_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2))); int ICACHE_FLASH_ATTR umm_info_safe_printf_P(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
#define UMM_INFO_PRINTF(fmt, ...) umm_info_safe_printf_P(PSTR(fmt), ##__VA_ARGS__) #define UMM_INFO_PRINTF(fmt, ...) umm_info_safe_printf_P(PSTR4(fmt), ##__VA_ARGS__)
// use PSTR4() instead of PSTR() to ensure 4-bytes alignment in Flash, whatever the default alignment of PSTR_ALIGN
#endif #endif

View File

@ -31,12 +31,26 @@ extern "C" {
#define PGM_VOID_P const void * #define PGM_VOID_P const void *
#endif #endif
// PSTR() macro modified to start on a 32-bit boundary. This adds on average #ifndef PSTR_ALIGN
// 1.5 bytes/string, but in return memcpy_P and strcpy_P will work 4~8x faster // PSTR() macro starts by default on a 32-bit boundary. This adds on average
#ifndef PSTR // 1.5 bytes/string, but in return memcpy_P and strcpy_P will work 4~8x faster
// Allow users to override the alignment with PSTR_ALIGN
#define PSTR_ALIGN 4
#endif
#ifndef PSTRN
// Multi-alignment variant of PSTR, n controls the alignment and should typically be 1 or 4
// Adapted from AVR-specific code at https://forum.arduino.cc/index.php?topic=194603.0 // Adapted from AVR-specific code at https://forum.arduino.cc/index.php?topic=194603.0
// Uses C attribute section instead of ASM block to allow for C language string concatenation ("x" "y" === "xy") // Uses C attribute section instead of ASM block to allow for C language string concatenation ("x" "y" === "xy")
#define PSTR(s) (__extension__({static const char __c[] __attribute__((__aligned__(4))) __attribute__((section( "\".irom0.pstr." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\", \"aSM\", @progbits, 1 #"))) = (s); &__c[0];})) #define PSTRN(s,n) (__extension__({static const char __c[] __attribute__((__aligned__(n))) __attribute__((section( "\".irom0.pstr." __FILE__ "." __STRINGIZE(__LINE__) "." __STRINGIZE(__COUNTER__) "\", \"aSM\", @progbits, 1 #"))) = (s); &__c[0];}))
#endif
#ifndef PSTR
// PSTR() uses the default alignment defined by PSTR_ALIGN
#define PSTR(s) PSTRN(s,PSTR_ALIGN)
#endif
#ifndef PSTR4
// PSTR4() enforces 4-bytes alignment whatever the value of PSTR_ALIGN
// as required by functions like ets_strlen() or ets_memcpy()
#define PSTR4(s) PSTRN(s,4)
#endif #endif
// Flash memory must be read using 32 bit aligned addresses else a processor // Flash memory must be read using 32 bit aligned addresses else a processor