Stop cjson from killing the node on out-of-mem.

This commit is contained in:
Johny Mattsson 2015-07-20 18:02:16 +10:00
parent 00e1e6bcbe
commit 719abca418
4 changed files with 47 additions and 4 deletions

28
app/cjson/cjson_mem.c Normal file
View File

@ -0,0 +1,28 @@
#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;
}

11
app/cjson/cjson_mem.h Normal file
View File

@ -0,0 +1,11 @@
#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

View File

@ -28,6 +28,7 @@
#include "c_string.h"
#include "strbuf.h"
#include "cjson_mem.h"
int strbuf_init(strbuf_t *s, int len)
{
@ -46,7 +47,7 @@ int strbuf_init(strbuf_t *s, int len)
s->reallocs = 0;
s->debug = 0;
s->buf = (char *)c_malloc(size);
s->buf = (char *)cjson_mem_malloc(size);
if (!s->buf){
NODE_ERR("not enough memory\n");
return -1;
@ -60,7 +61,7 @@ strbuf_t *strbuf_new(int len)
{
strbuf_t *s;
s = (strbuf_t *)c_malloc(sizeof(strbuf_t));
s = (strbuf_t *)cjson_mem_malloc(sizeof(strbuf_t));
if (!s){
NODE_ERR("not enough memory\n");
return NULL;
@ -170,7 +171,7 @@ int strbuf_resize(strbuf_t *s, int len)
(long)s, s->size, newsize);
}
s->buf = (char *)c_realloc(s->buf, newsize);
s->buf = (char *)cjson_mem_realloc(s->buf, newsize);
if (!s->buf){
NODE_ERR("not enough memory");
return -1;

View File

@ -45,6 +45,7 @@
#include "flash_api.h"
#include "strbuf.h"
#include "cjson_mem.h"
#define FPCONV_G_FMT_BUFSIZE 32
#define fpconv_strtod c_strtod
@ -1461,7 +1462,7 @@ static int json_decode(lua_State *l)
* string must be smaller than the entire json string */
json.tmp = strbuf_new(json_len);
if(json.tmp == NULL){
return luaL_error(l, "not enought memory");
return luaL_error(l, "not enough memory");
}
json_next_token(&json, &token);
@ -1552,6 +1553,8 @@ const LUA_REG_TYPE cjson_map[] =
LUALIB_API int luaopen_cjson( lua_State *L )
{
cjson_mem_setlua (L);
/* Initialise number conversions */
// fpconv_init(); // not needed for a specific cpu.
if(-1==cfg_init(&_cfg)){