Skip to content

Commit 0b087ac

Browse files
committed
Provide replacements for malloc, free and other libc functions
#11
1 parent 12c7623 commit 0b087ac

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
libc_replacements.c - replaces libc functions with functions
3+
from Espressif SDK
4+
5+
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
6+
This file is part of the esp8266 core for Arduino environment.
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation; either
11+
version 2.1 of the License, or (at your option) any later version.
12+
13+
This library is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public
19+
License along with this library; if not, write to the Free Software
20+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
23+
#include <stddef.h>
24+
#include <stdarg.h>
25+
26+
#include "ets_sys.h"
27+
#include "os_type.h"
28+
#include "osapi.h"
29+
#include "mem.h"
30+
#include "user_interface.h"
31+
32+
33+
void* malloc(size_t size) {
34+
return os_malloc(size);
35+
}
36+
37+
void free(void* ptr) {
38+
os_free(ptr);
39+
}
40+
41+
void* realloc(void* ptr, size_t size) {
42+
return os_realloc(ptr, size);
43+
}
44+
45+
int printf(const char* format, ...) {
46+
va_list arglist;
47+
va_start(arglist, format);
48+
ets_vprintf(format, arglist);
49+
va_end(arglist);
50+
}
51+
52+
int sprintf(char* buffer, const char* format, ...) {
53+
va_list arglist;
54+
va_start(arglist, format);
55+
ets_vsprintf(buffer, format, arglist);
56+
va_end(arglist);
57+
}
58+
59+
int snprintf(char* buffer, size_t size, const char* format, ...) {
60+
va_list arglist;
61+
va_start(arglist, format);
62+
ets_vsnprintf(buffer, size, format, arglist);
63+
va_end(arglist);
64+
}
65+
66+
int memcmp(const void *s1, const void *s2, size_t n) {
67+
return ets_memcmp(s1, s2, n);
68+
}
69+
70+
void* memcpy(void *dest, const void *src, size_t n) {
71+
return ets_memcpy(dest, src, n);
72+
}
73+
74+
void* memset(void *s, int c, size_t n) {
75+
return ets_memset(s, c, n);
76+
}
77+
78+
int strcmp(const char *s1, const char *s2) {
79+
return ets_strcmp(s1, s2);
80+
}
81+
82+
char* strcpy(char *dest, const char *src) {
83+
return ets_strcpy(dest, src);
84+
}
85+
86+
size_t strlen(const char *s) {
87+
return ets_strlen(s);
88+
}
89+
90+
int strncmp(const char *s1, const char *s2, size_t len) {
91+
return ets_strncmp(s1, s2, len);
92+
}
93+
94+
char *strncpy(char *dest, const char *src, size_t n) {
95+
return ets_strncpy(dest, src, n);
96+
}
97+
98+
char *ets_strstr(const char *haystack, const char *needle) {
99+
return strstr(haystack, needle);
100+
}
101+

hardware/esp8266com/esp8266/platform.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ compiler.S.flags=-c -g -x assembler-with-cpp -MMD
2323
compiler.c.elf.ldscript=eagle.app.v6.ld
2424
compiler.c.elf.flags=-nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{compiler.c.elf.ldscript}"
2525
compiler.c.elf.cmd=xtensa-lx106-elf-gcc
26-
compiler.c.elf.libs=-lc -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp
26+
compiler.c.elf.libs=-lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp
2727

2828
compiler.cpp.cmd=xtensa-lx106-elf-g++
2929
compiler.cpp.flags=-c -Os -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -std=c++11 -MMD
@@ -66,7 +66,7 @@ recipe.S.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.cpreprocessor.fla
6666
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{build.path}/{archive_file}" "{object_file}"
6767

6868
## Combine gc-sections, archives, and objects
69-
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{build.path}/{archive_file}" {compiler.c.elf.libs} -Wl,--end-group "-L{build.path}"
69+
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{build.path}/{archive_file}" {compiler.c.elf.libs} -Wl,--end-group -lc "-L{build.path}"
7070

7171
## Create eeprom
7272
recipe.objcopy.eep.pattern=

0 commit comments

Comments
 (0)