From f9936ec4bad8294ec1e92a4c20782dfaa75e7762 Mon Sep 17 00:00:00 2001 From: yuqing Date: Tue, 14 Jul 2015 09:50:35 +0800 Subject: [PATCH] ini_file_reader.c realloc change to malloc and memcpy --- HISTORY | 3 ++- src/Makefile.in | 2 +- src/ini_file_reader.c | 33 ++++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/HISTORY b/HISTORY index b52a53f..1b07d8d 100644 --- a/HISTORY +++ b/HISTORY @@ -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 realloc change to malloc and memcpy Version 1.16 2015-07-01 * fast_mblock add fast_mblock_delay_free diff --git a/src/Makefile.in b/src/Makefile.in index df33213..7222bb3 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -36,7 +36,7 @@ all: $(ALL_OBJS) $(ALL_PRGS) $(ALL_LIBS) libfastcommon.so: $(COMPILE) -o $@ $< -shared $(FAST_SHARED_OBJS) libfastcommon.a: $(FAST_STATIC_OBJS) - ar rcs $@ $< + ar rcs $@ $(FAST_STATIC_OBJS) .o: $(COMPILE) -o $@ $< $(STATIC_OBJS) $(LIB_PATH) $(INC_PATH) .c: diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 98c07f0..a55e0f2 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -19,8 +19,8 @@ #include "http_func.h" #include "ini_file_reader.h" -#define _LINE_BUFFER_SIZE 512 -#define _ALLOC_ITEMS_ONCE 32 +#define _LINE_BUFFER_SIZE 512 +#define _INIT_ALLOC_ITEM_COUNT 32 static int iniDoLoadFromFile(const char *szFilename, \ IniContext *pContext); @@ -416,19 +416,34 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext) if (pSection->count >= pSection->alloc_count) { - pSection->alloc_count += _ALLOC_ITEMS_ONCE; - pSection->items=(IniItem *)realloc(pSection->items, - sizeof(IniItem) * pSection->alloc_count); - if (pSection->items == NULL) + int bytes; + IniItem *pNew; + if (pSection->alloc_count == 0) + { + 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, " \ - "realloc %d bytes fail", __LINE__, \ - (int)sizeof(IniItem) * \ - pSection->alloc_count); + "malloc %d bytes fail", __LINE__, bytes); result = errno != 0 ? errno : ENOMEM; break; } + if (pSection->count > 0) + { + memcpy(pNew, pSection->items, + sizeof(IniItem) * pSection->count); + free(pSection->items); + } + + pSection->items = pNew; pItem = pSection->items + pSection->count; memset(pItem, 0, sizeof(IniItem) * \ (pSection->alloc_count - pSection->count));