summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2014-08-26 23:11:34 -0400
committerMichael Sevakis <jethead71@rockbox.org>2014-08-29 22:06:59 -0400
commit17a007bc60c69d6ea471a96a465e04ba4ac2d00f (patch)
tree2b0ca8b479f48cbd047414a10cb58430faf9ca71
parent77b3625763ae4d5aa6aaa9d44fbc1bfec6b29335 (diff)
downloadrockbox-17a007bc60c69d6ea471a96a465e04ba4ac2d00f.tar.gz
rockbox-17a007bc60c69d6ea471a96a465e04ba4ac2d00f.zip
Add normal alloca() definition and implement a strdupa and friends
Change-Id: I21c9c21fd664fb11bc8496ace4a389f535a030d6
-rw-r--r--firmware/include/string-extra.h34
-rw-r--r--firmware/libc/include/stdlib.h5
-rw-r--r--firmware/target/hosted/system-hosted.h12
3 files changed, 50 insertions, 1 deletions
diff --git a/firmware/include/string-extra.h b/firmware/include/string-extra.h
index 6a9e0c77be..9ab53d8154 100644
--- a/firmware/include/string-extra.h
+++ b/firmware/include/string-extra.h
@@ -34,4 +34,38 @@
34#endif 34#endif
35#endif 35#endif
36 36
37/* copies a buffer of len bytes and null terminates it */
38static inline char * strmemcpy(char *dst, const char *src, size_t len)
39{
40 /* NOTE: for now, assumes valid parameters! */
41 *(char *)mempcpy(dst, src, len) = '\0';
42 return dst;
43}
44
45/* duplicate and null-terminate a memory block on the stack with alloca() */
46#define strmemdupa(s, l) \
47 ({ const char *___s = (s); \
48 size_t ___l = (l); \
49 char *___buf = alloca(___l + 1); \
50 strmemcpy(___buf, ___s, ___l); })
51
52/* strdupa and strndupa may already be provided by a system's string.h */
53
54#ifndef strdupa
55/* duplicate an entire string on the stack with alloca() */
56#define strdupa(s) \
57 ({ const char *__s = (s); \
58 strmemdupa((__s), strlen(__s)); })
59#endif /* strdupa */
60
61#ifndef strndupa
62/* duplicate a string on the stack with alloca(), truncating it if it is too
63 long */
64#define strndupa(s, n) \
65 ({ const char *__s = (s); \
66 size_t __n = (n); \
67 size_t __len = strlen(_s); \
68 strmemdupa(__s, MIN(__n, __len)); })
69#endif /* strndupa */
70
37#endif /* STRING_EXTRA_H */ 71#endif /* STRING_EXTRA_H */
diff --git a/firmware/libc/include/stdlib.h b/firmware/libc/include/stdlib.h
index 57553367c4..e24d6a579f 100644
--- a/firmware/libc/include/stdlib.h
+++ b/firmware/libc/include/stdlib.h
@@ -31,6 +31,11 @@ void free(void *);
31void *realloc(void *, size_t); 31void *realloc(void *, size_t);
32int atexit(void (*)(void)); 32int atexit(void (*)(void));
33 33
34#ifdef __GNUC__
35# undef alloca
36# define alloca(size) __builtin_alloca (size)
37#endif /* GCC. */
38
34#define RAND_MAX INT_MAX 39#define RAND_MAX INT_MAX
35 40
36void srand(unsigned int seed); 41void srand(unsigned int seed);
diff --git a/firmware/target/hosted/system-hosted.h b/firmware/target/hosted/system-hosted.h
index e60803fde0..5e7a7d7d99 100644
--- a/firmware/target/hosted/system-hosted.h
+++ b/firmware/target/hosted/system-hosted.h
@@ -22,7 +22,7 @@
22#ifndef __SYSTEM_HOSTED_H__ 22#ifndef __SYSTEM_HOSTED_H__
23#define __SYSTEM_HOSTED_H__ 23#define __SYSTEM_HOSTED_H__
24 24
25#include "system.h" 25#ifndef __PCTOOL__
26 26
27static inline void commit_dcache(void) {} 27static inline void commit_dcache(void) {}
28static inline void commit_discard_dcache(void) {} 28static inline void commit_discard_dcache(void) {}
@@ -34,4 +34,14 @@ static inline void core_sleep(void)
34 wait_for_interrupt(); 34 wait_for_interrupt();
35} 35}
36 36
37#endif /* __PCTOOL__ */
38
39#if defined(WIN32) || defined(__PCTOOL__)
40
41#ifndef alloca
42#define alloca __builtin_alloca
43#endif
44
45#endif /* WIN32 || __PCTOOL__ */
46
37#endif 47#endif