Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ PB_NS_BEGIN


#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>


Expand Down Expand Up @@ -541,6 +540,8 @@ static void lpb_readtype(lua_State *L, lpb_State *LS, int type, pb_Slice *s) {
# define setmode(a,b) ((void)0)
#endif

#if BP_IO

static int io_read(lua_State *L) {
FILE *fp = (FILE*)lua_touserdata(L, 1);
size_t nr;
Expand Down Expand Up @@ -615,6 +616,7 @@ LUALIB_API int luaopen_pb_io(lua_State *L) {
return 1;
}

#endif /* BP_IO */

/* protobuf integer conversion */

Expand Down Expand Up @@ -910,7 +912,7 @@ typedef struct lpb_Slice {

static void lpb_resetslice(lua_State *L, lpb_Slice *s, size_t size) {
if (size == sizeof(lpb_Slice)) {
if (s->buff != s->init_buff) free(s->buff);
if (s->buff != s->init_buff) PB_FREE(s->buff);
memset(s, 0, sizeof(lpb_Slice));
s->buff = s->init_buff;
s->size = LPB_INITSTACKLEN;
Expand All @@ -934,7 +936,7 @@ static void lpb_enterview(lua_State *L, lpb_Slice *s, pb_Slice view) {
if (s->used >= s->size) {
size_t newsize = s->size * 2;
pb_Slice *oldp = s->buff != s->init_buff ? s->buff : NULL;
pb_Slice *newp = (pb_Slice*)realloc(oldp, newsize*sizeof(pb_Slice));
pb_Slice *newp = (pb_Slice*)PB_REALLOC(oldp, newsize*sizeof(pb_Slice));
if (newp == NULL) { luaL_error(L, "out of memory"); return; }
if (oldp == NULL) memcpy(newp, s->buff, s->used*sizeof(pb_Slice));
s->buff = newp;
Expand Down Expand Up @@ -1249,6 +1251,7 @@ static int Lpb_load_unsafe(lua_State *L) {
return 2;
}

#if BP_IO
static int Lpb_loadfile(lua_State *L) {
lpb_State *LS = lpb_lstate(L);
const char *filename = luaL_checkstring(L, 1);
Expand All @@ -1275,6 +1278,7 @@ static int Lpb_loadfile(lua_State *L) {
lua_pushinteger(L, pb_pos(s)+1);
return 2;
}
#endif

static int lpb_pushtype(lua_State *L, const pb_Type *t) {
if (t == NULL) return 0;
Expand Down Expand Up @@ -2082,7 +2086,9 @@ LUALIB_API int luaopen_pb(lua_State *L) {
#define ENTRY(name) { #name, Lpb_##name }
ENTRY(clear),
ENTRY(load),
#if BP_IO
ENTRY(loadfile),
#endif
ENTRY(encode),
ENTRY(decode),
ENTRY(types),
Expand Down
60 changes: 42 additions & 18 deletions pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@
# endif
#endif

/* Compile with -DBP_IO=0 to exclude IO lib */
#ifndef BP_IO
#define BP_IO 1
#endif

/* Compile with -DUSE_DLMALLOC if using Doug Lea's memory allocator */
#ifdef USE_DLMALLOC
#define PB_REALLOC dlrealloc
#define PB_MALLOC dlmalloc
#define PB_FREE dlfree
#endif

/* Fine tune what allocator functions to use */
#ifndef PB_REALLOC
#define PB_REALLOC realloc
#endif
#ifndef PB_MALLOC
#define PB_MALLOC malloc
#endif
#ifndef PB_FREE
#define PB_FREE free
#endif

#ifdef PB_STATIC_API
# ifndef PB_IMPLEMENTATION
# define PB_IMPLEMENTATION
Expand Down Expand Up @@ -73,6 +96,7 @@ typedef signed long long int64_t;

#include <stddef.h>
#include <limits.h>
#include <lua.h>

PB_NS_BEGIN

Expand Down Expand Up @@ -685,7 +709,7 @@ PB_API void pb_initbuffer(pb_Buffer *b)
{ memset(b, 0, sizeof(pb_Buffer)); }

PB_API void pb_resetbuffer(pb_Buffer *b)
{ if (pb_onheap(b)) free(b->u.h.buff); pb_initbuffer(b); }
{ if (pb_onheap(b)) PB_FREE(b->u.h.buff); pb_initbuffer(b); }

static int pb_write32(char *buff, uint32_t n) {
int p, c = 0;
Expand Down Expand Up @@ -725,7 +749,7 @@ PB_API char *pb_prepbuffsize(pb_Buffer *b, size_t len) {
while (newsize < PB_MAX_SIZET/2 && newsize < expected)
newsize += newsize >> 1;
if (newsize < expected) return NULL;
if ((newp = (char*)realloc(oldp, newsize)) == NULL) return NULL;
if ((newp = (char*)PB_REALLOC(oldp, newsize)) == NULL) return NULL;
if (!pb_onheap(b)) memcpy(newp, pb_buffer(b), b->size);
b->heap = 1;
b->u.h.buff = newp;
Expand Down Expand Up @@ -823,7 +847,7 @@ PB_API void pb_freepool(pb_Pool *pool) {
void *page = pool->pages;
while (page) {
void *next = *(void**)((char*)page + PB_POOLSIZE - sizeof(void*));
free(page);
PB_FREE(page);
page = next;
}
pb_initpool(pool, pool->obj_size);
Expand All @@ -833,7 +857,7 @@ PB_API void *pb_poolalloc(pb_Pool *pool) {
void *obj = pool->freed;
if (obj == NULL) {
size_t objsize = pool->obj_size, offset;
void *newpage = malloc(PB_POOLSIZE);
void *newpage = PB_MALLOC(PB_POOLSIZE);
if (newpage == NULL) return NULL;
offset = ((PB_POOLSIZE - sizeof(void*)) / objsize - 1) * objsize;
for (; offset > 0; offset -= objsize) {
Expand Down Expand Up @@ -861,7 +885,7 @@ PB_API void pb_inittable(pb_Table *t, size_t entrysize)
{ memset(t, 0, sizeof(pb_Table)), t->entry_size = (unsigned)entrysize; }

PB_API void pb_freetable(pb_Table *t)
{ free(t->hash); pb_inittable(t, t->entry_size); }
{ PB_FREE(t->hash); pb_inittable(t, t->entry_size); }

static pb_Entry *pbT_hash(const pb_Table *t, pb_Key key) {
size_t h = ((size_t)key*2654435761U)&(t->size-1);
Expand Down Expand Up @@ -909,7 +933,7 @@ PB_API size_t pb_resizetable(pb_Table *t, size_t size) {
if (newsize < size) return 0;
nt.size = newsize;
nt.lastfree = nt.entry_size * newsize;
nt.hash = (pb_Entry*)malloc(nt.lastfree);
nt.hash = (pb_Entry*)PB_MALLOC(nt.lastfree);
if (nt.hash == NULL) return 0;
memset(nt.hash, 0, nt.lastfree);
for (i = 0; i < rawsize; i += t->entry_size) {
Expand All @@ -918,7 +942,7 @@ PB_API size_t pb_resizetable(pb_Table *t, size_t size) {
if (nt.entry_size > sizeof(pb_Entry))
memcpy(newe+1, olde+1, nt.entry_size - sizeof(pb_Entry));
}
free(t->hash);
PB_FREE(t->hash);
*t = nt;
return newsize;
}
Expand Down Expand Up @@ -977,11 +1001,11 @@ static void pbN_free(pb_State *S) {
pb_NameEntry *ne = nt->hash[i];
while (ne != NULL) {
pb_NameEntry *next = ne->next;
free(ne);
PB_FREE(ne);
ne = next;
}
}
free(nt->hash);
PB_FREE(nt->hash);
pbN_init(S);
}

Expand All @@ -1001,7 +1025,7 @@ static size_t pbN_resize(pb_State *S, size_t size) {
while (newsize < PB_MAX_HASHSIZE/sizeof(pb_NameEntry*) && newsize < size)
newsize <<= 1;
if (newsize < size) return 0;
hash = (pb_NameEntry**)malloc(newsize * sizeof(pb_NameEntry*));
hash = (pb_NameEntry**)PB_MALLOC(newsize * sizeof(pb_NameEntry*));
if (hash == NULL) return 0;
memset(hash, 0, newsize * sizeof(pb_NameEntry*));
for (i = 0; i < nt->size; ++i) {
Expand All @@ -1013,7 +1037,7 @@ static size_t pbN_resize(pb_State *S, size_t size) {
entry = next;
}
}
free(nt->hash);
PB_FREE(nt->hash);
nt->hash = hash;
nt->size = newsize;
return newsize;
Expand All @@ -1025,7 +1049,7 @@ static pb_NameEntry *pbN_newname(pb_State *S, pb_Slice s, unsigned hash) {
size_t len = pb_len(s);
if (nt->count >= nt->size && !pbN_resize(S, nt->size * 2)) return NULL;
list = &nt->hash[hash & (nt->size - 1)];
newobj = (pb_NameEntry*)malloc(sizeof(pb_NameEntry) + len + 1);
newobj = (pb_NameEntry*)PB_MALLOC(sizeof(pb_NameEntry) + len + 1);
if (newobj == NULL) return NULL;
newobj->next = *list;
newobj->length = (unsigned)len;
Expand All @@ -1047,7 +1071,7 @@ static void pbN_delname(pb_State *S, pb_NameEntry *name) {
else {
*list = (*list)->next;
--nt->count;
free(name);
PB_FREE(name);
break;
}
}
Expand Down Expand Up @@ -1094,7 +1118,7 @@ PB_API pb_Name *pb_newname(pb_State *S, pb_Slice s, pb_Cache *cache) {
PB_API const pb_Name *pb_name(const pb_State *S, pb_Slice s, pb_Cache *cache) {
pb_NameEntry *entry = NULL;
pb_CacheSlot *slot;
if (S == NULL || s.p == NULL) return NULL;
if (s.p == NULL) return NULL;
if (cache == NULL)
entry = pbN_getname(S, s, pbN_calchash(s));
else {
Expand Down Expand Up @@ -1175,7 +1199,7 @@ PB_API pb_Field** pb_sortfield(pb_Type* t) {
int index = 0;
unsigned int i = 0;
const pb_Field* f = NULL;
pb_Field** list = malloc(sizeof(pb_Field*) * t->field_count);
pb_Field** list = PB_MALLOC(sizeof(pb_Field*) * t->field_count);

assert(list);
while (pb_nextfield(t, &f)) {
Expand Down Expand Up @@ -1263,7 +1287,7 @@ PB_API pb_Type *pb_newtype(pb_State *S, pb_Name *tname) {

PB_API void pb_delsort(pb_Type *t) {
if (t->field_sort) {
free(t->field_sort);
PB_FREE(t->field_sort);
t->field_sort = NULL;
}
}
Expand Down Expand Up @@ -1360,7 +1384,7 @@ typedef struct pb_ArrayHeader {
} pb_ArrayHeader;

#define pbL_rawh(A) ((pb_ArrayHeader*)(A) - 1)
#define pbL_delete(A) ((A) ? (void)free(pbL_rawh(A)) : (void)0)
#define pbL_delete(A) ((A) ? (void)PB_FREE(pbL_rawh(A)) : (void)0)
#define pbL_count(A) ((A) ? pbL_rawh(A)->count : 0)
#define pbL_add(A) (pbL_grow((void**)&(A),sizeof(*(A)))==PB_OK ?\
&(A)[pbL_rawh(A)->count++] : NULL)
Expand Down Expand Up @@ -1428,7 +1452,7 @@ static int pbL_grow(void **pp, size_t objs) {
size_t used = (h ? h->count : 0);
size_t size = used + 4, nsize = size + (size >> 1);
nh = nsize < size ? NULL :
(pb_ArrayHeader*)realloc(h, sizeof(pb_ArrayHeader)+nsize*objs);
(pb_ArrayHeader*)PB_REALLOC(h, sizeof(pb_ArrayHeader)+nsize*objs);
if (nh == NULL) return PB_ENOMEM;
nh->count = (unsigned)used;
nh->capacity = (unsigned)nsize;
Expand Down