ini_file_reader.c realloc change to malloc and memcpy

pull/4/head
yuqing 2015-07-14 09:50:35 +08:00
parent d884069622
commit f9936ec4ba
3 changed files with 27 additions and 11 deletions

View File

@ -1,6 +1,7 @@
Version 1.17 2015-07-13 Version 1.17 2015-07-14
* ini_file_reader.c change PJWHash to Time33Hash and increase capacity * ini_file_reader.c change PJWHash to Time33Hash and increase capacity
* ini_file_reader.c realloc change to malloc and memcpy
Version 1.16 2015-07-01 Version 1.16 2015-07-01
* fast_mblock add fast_mblock_delay_free * fast_mblock add fast_mblock_delay_free

View File

@ -36,7 +36,7 @@ all: $(ALL_OBJS) $(ALL_PRGS) $(ALL_LIBS)
libfastcommon.so: libfastcommon.so:
$(COMPILE) -o $@ $< -shared $(FAST_SHARED_OBJS) $(COMPILE) -o $@ $< -shared $(FAST_SHARED_OBJS)
libfastcommon.a: $(FAST_STATIC_OBJS) libfastcommon.a: $(FAST_STATIC_OBJS)
ar rcs $@ $< ar rcs $@ $(FAST_STATIC_OBJS)
.o: .o:
$(COMPILE) -o $@ $< $(STATIC_OBJS) $(LIB_PATH) $(INC_PATH) $(COMPILE) -o $@ $< $(STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
.c: .c:

View File

@ -20,7 +20,7 @@
#include "ini_file_reader.h" #include "ini_file_reader.h"
#define _LINE_BUFFER_SIZE 512 #define _LINE_BUFFER_SIZE 512
#define _ALLOC_ITEMS_ONCE 32 #define _INIT_ALLOC_ITEM_COUNT 32
static int iniDoLoadFromFile(const char *szFilename, \ static int iniDoLoadFromFile(const char *szFilename, \
IniContext *pContext); IniContext *pContext);
@ -416,19 +416,34 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
if (pSection->count >= pSection->alloc_count) if (pSection->count >= pSection->alloc_count)
{ {
pSection->alloc_count += _ALLOC_ITEMS_ONCE; int bytes;
pSection->items=(IniItem *)realloc(pSection->items, IniItem *pNew;
sizeof(IniItem) * pSection->alloc_count); if (pSection->alloc_count == 0)
if (pSection->items == NULL) {
pSection->alloc_count = _INIT_ALLOC_ITEM_COUNT;
}
else
{
pSection->alloc_count *= 2;
}
bytes = sizeof(IniItem) * pSection->alloc_count;
pNew = (IniItem *)malloc(bytes);
if (pNew == NULL)
{ {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
"realloc %d bytes fail", __LINE__, \ "malloc %d bytes fail", __LINE__, bytes);
(int)sizeof(IniItem) * \
pSection->alloc_count);
result = errno != 0 ? errno : ENOMEM; result = errno != 0 ? errno : ENOMEM;
break; break;
} }
if (pSection->count > 0)
{
memcpy(pNew, pSection->items,
sizeof(IniItem) * pSection->count);
free(pSection->items);
}
pSection->items = pNew;
pItem = pSection->items + pSection->count; pItem = pSection->items + pSection->count;
memset(pItem, 0, sizeof(IniItem) * \ memset(pItem, 0, sizeof(IniItem) * \
(pSection->alloc_count - pSection->count)); (pSection->alloc_count - pSection->count));