Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
719abca4
You need to sign in or sign up before continuing.
Commit
719abca4
authored
Jul 20, 2015
by
Johny Mattsson
Browse files
Stop cjson from killing the node on out-of-mem.
parent
00e1e6bc
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/cjson/cjson_mem.c
0 → 100644
View file @
719abca4
#include "cjson_mem.h"
#include "../lua/lauxlib.h"
#include <c_stdlib.h>
static
lua_State
*
gL
;
static
const
char
errfmt
[]
=
"cjson %salloc: out of mem (%d bytes)"
;
void
cjson_mem_setlua
(
lua_State
*
L
)
{
gL
=
L
;
}
void
*
cjson_mem_malloc
(
uint32_t
sz
)
{
void
*
p
=
(
void
*
)
c_malloc
(
sz
);
if
(
!
p
&&
gL
)
luaL_error
(
gL
,
errfmt
,
"m"
,
sz
);
return
p
;
}
void
*
cjson_mem_realloc
(
void
*
o
,
uint32_t
sz
)
{
void
*
p
=
(
void
*
)
c_realloc
(
o
,
sz
);
if
(
!
p
&&
gL
)
luaL_error
(
gL
,
errfmt
,
"re"
,
sz
);
return
p
;
}
app/cjson/cjson_mem.h
0 → 100644
View file @
719abca4
#ifndef _CJSON_MEM_H_
#define _CJSON_MEM_H_
#include "../lua/lua.h"
void
cjson_mem_setlua
(
lua_State
*
L
);
void
*
cjson_mem_malloc
(
uint32_t
sz
);
void
*
cjson_mem_realloc
(
void
*
p
,
uint32_t
sz
);
#endif
app/cjson/strbuf.c
View file @
719abca4
...
@@ -28,6 +28,7 @@
...
@@ -28,6 +28,7 @@
#include "c_string.h"
#include "c_string.h"
#include "strbuf.h"
#include "strbuf.h"
#include "cjson_mem.h"
int
strbuf_init
(
strbuf_t
*
s
,
int
len
)
int
strbuf_init
(
strbuf_t
*
s
,
int
len
)
{
{
...
@@ -46,7 +47,7 @@ int strbuf_init(strbuf_t *s, int len)
...
@@ -46,7 +47,7 @@ int strbuf_init(strbuf_t *s, int len)
s
->
reallocs
=
0
;
s
->
reallocs
=
0
;
s
->
debug
=
0
;
s
->
debug
=
0
;
s
->
buf
=
(
char
*
)
c_malloc
(
size
);
s
->
buf
=
(
char
*
)
c
json_mem
_malloc
(
size
);
if
(
!
s
->
buf
){
if
(
!
s
->
buf
){
NODE_ERR
(
"not enough memory
\n
"
);
NODE_ERR
(
"not enough memory
\n
"
);
return
-
1
;
return
-
1
;
...
@@ -60,7 +61,7 @@ strbuf_t *strbuf_new(int len)
...
@@ -60,7 +61,7 @@ strbuf_t *strbuf_new(int len)
{
{
strbuf_t
*
s
;
strbuf_t
*
s
;
s
=
(
strbuf_t
*
)
c_malloc
(
sizeof
(
strbuf_t
));
s
=
(
strbuf_t
*
)
c
json_mem
_malloc
(
sizeof
(
strbuf_t
));
if
(
!
s
){
if
(
!
s
){
NODE_ERR
(
"not enough memory
\n
"
);
NODE_ERR
(
"not enough memory
\n
"
);
return
NULL
;
return
NULL
;
...
@@ -170,7 +171,7 @@ int strbuf_resize(strbuf_t *s, int len)
...
@@ -170,7 +171,7 @@ int strbuf_resize(strbuf_t *s, int len)
(
long
)
s
,
s
->
size
,
newsize
);
(
long
)
s
,
s
->
size
,
newsize
);
}
}
s
->
buf
=
(
char
*
)
c_realloc
(
s
->
buf
,
newsize
);
s
->
buf
=
(
char
*
)
c
json_mem
_realloc
(
s
->
buf
,
newsize
);
if
(
!
s
->
buf
){
if
(
!
s
->
buf
){
NODE_ERR
(
"not enough memory"
);
NODE_ERR
(
"not enough memory"
);
return
-
1
;
return
-
1
;
...
...
app/modules/cjson.c
View file @
719abca4
...
@@ -45,6 +45,7 @@
...
@@ -45,6 +45,7 @@
#include "flash_api.h"
#include "flash_api.h"
#include "strbuf.h"
#include "strbuf.h"
#include "cjson_mem.h"
#define FPCONV_G_FMT_BUFSIZE 32
#define FPCONV_G_FMT_BUFSIZE 32
#define fpconv_strtod c_strtod
#define fpconv_strtod c_strtod
...
@@ -1461,7 +1462,7 @@ static int json_decode(lua_State *l)
...
@@ -1461,7 +1462,7 @@ static int json_decode(lua_State *l)
* string must be smaller than the entire json string */
* string must be smaller than the entire json string */
json
.
tmp
=
strbuf_new
(
json_len
);
json
.
tmp
=
strbuf_new
(
json_len
);
if
(
json
.
tmp
==
NULL
){
if
(
json
.
tmp
==
NULL
){
return
luaL_error
(
l
,
"not enough
t
memory"
);
return
luaL_error
(
l
,
"not enough memory"
);
}
}
json_next_token
(
&
json
,
&
token
);
json_next_token
(
&
json
,
&
token
);
...
@@ -1552,6 +1553,8 @@ const LUA_REG_TYPE cjson_map[] =
...
@@ -1552,6 +1553,8 @@ const LUA_REG_TYPE cjson_map[] =
LUALIB_API
int
luaopen_cjson
(
lua_State
*
L
)
LUALIB_API
int
luaopen_cjson
(
lua_State
*
L
)
{
{
cjson_mem_setlua
(
L
);
/* Initialise number conversions */
/* Initialise number conversions */
// fpconv_init(); // not needed for a specific cpu.
// fpconv_init(); // not needed for a specific cpu.
if
(
-
1
==
cfg_init
(
&
_cfg
)){
if
(
-
1
==
cfg_init
(
&
_cfg
)){
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment