Browse Source

Follow up to f53916e
* Fixed remaining memory leak.
* Cleaned up and optimized a few more ERS allocations.

aleos89 9 năm trước cách đây
mục cha
commit
9738c825cc
11 tập tin đã thay đổi với 61 bổ sung86 xóa
  1. 2 0
      src/common/core.c
  2. 2 1
      src/common/db.h
  3. 3 17
      src/common/ers.c
  4. 16 18
      src/map/guild.c
  5. 9 13
      src/map/mapreg.c
  6. 1 1
      src/map/mob.c
  7. 0 1
      src/map/npc.c
  8. 7 3
      src/map/pc.c
  9. 16 29
      src/map/script.c
  10. 0 1
      src/map/script.h
  11. 5 2
      src/map/skill.c

+ 2 - 0
src/common/core.c

@@ -7,6 +7,7 @@
 #include "core.h"
 #include "strlib.h"
 #ifndef MINICORE
+#include "ers.h"
 #include "socket.h"
 #include "timer.h"
 #include "thread.h"
@@ -371,6 +372,7 @@ int main (int argc, char **argv)
 	db_final();
 	mempool_final();
 	rathread_final();
+	ers_final();
 #endif
 
 	malloc_final();

+ 2 - 1
src/common/db.h

@@ -43,6 +43,7 @@
 #define _DB_H_
 
 #include "../common/cbasetypes.h"
+
 #include <stdarg.h>
 
 /*****************************************************************************\
@@ -757,7 +758,7 @@ DBComparator db_default_cmp(DBType type);
 DBHasher db_default_hash(DBType type);
 
 /**
- * Returns the default releaser for the specified type of database with the 
+ * Returns the default releaser for the specified type of database with the
  * specified options.
  * NOTE: the options are fixed by {@link #db_fix_options(DBType,DBOptions)}
  * before choosing the releaser

+ 3 - 17
src/common/ers.c

@@ -39,13 +39,13 @@
  * @encoding US-ASCII                                                        *
  * @see common#ers.h                                                         *
 \*****************************************************************************/
-#include <stdlib.h>
+
+#include "ers.h"
 
 #include "../common/cbasetypes.h"
 #include "../common/malloc.h" // CREATE, RECREATE, aMalloc, aFree
 #include "../common/nullpo.h"
 #include "../common/showmsg.h" // ShowMessage, ShowError, ShowFatalError, CL_BOLD, CL_NORMAL
-#include "ers.h"
 
 #include <stdlib.h>
 #include <string.h>
@@ -174,6 +174,7 @@ static void ers_free_cache(ers_cache_t *cache, bool remove)
 		CacheList = cache->Next;
 
 	aFree(cache->Blocks);
+
 	aFree(cache);
 }
 
@@ -331,21 +332,6 @@ ERS *ers_new(uint32 size, char *name, enum ERSOptions options)
 void ers_report(void) {
 	ers_cache_t *cache;
 	unsigned int cache_c = 0, blocks_u = 0, blocks_a = 0, memory_b = 0, memory_t = 0;
-#ifdef DEBUG
-	struct ers_instance_t *instance;
-	unsigned int instance_c = 0, instance_c_d = 0;
-
-	for (instance = InstanceList; instance; instance = instance->Next) {
-		instance_c++;
-		if( (instance->Options & ERS_OPT_WAIT) && !instance->Count )
-			continue;
-		instance_c_d++;
-		ShowMessage(CL_BOLD"[ERS Instance "CL_NORMAL""CL_WHITE"%s"CL_NORMAL""CL_BOLD" report]\n"CL_NORMAL, instance->Name);
-		ShowMessage("\tblock size        : %u\n", instance->Cache->ObjectSize);
-		ShowMessage("\tblocks being used : %u\n", instance->Count);
-		ShowMessage("\tmemory in use     : %.2f MB\n", instance->Count == 0 ? 0. : (double)((instance->Count * instance->Cache->ObjectSize)/1024)/1024);
-	}
-#endif
 
 	for (cache = CacheList; cache; cache = cache->Next) {
 		cache_c++;

+ 16 - 18
src/map/guild.c

@@ -96,15 +96,14 @@ static TBL_PC* guild_sd_check(int guild_id, uint32 account_id, uint32 char_id) {
 	return sd;
 }
 
- // Modified [Komurka]
+// Modified [Komurka]
 int guild_skill_get_max (int id) {
 	if ((id = guild_skill_get_index(id)) < 0)
 		return 0;
 	return guild_skill_tree[id].max;
 }
 
-// Retrive skill_lv learned by guild
-
+// Retrieve skill_lv learned by guild
 int guild_checkskill(struct guild *g, int id) {
 	if ((id = guild_skill_get_index(id)) < 0)
 		return 0;
@@ -187,7 +186,7 @@ struct guild* guild_searchname(char* str) {
 	struct guild* g;
 	DBIterator *iter = db_iterator(guild_db);
 
-	for( g = dbi_first(iter); dbi_exists(iter); g = dbi_next(iter) ) 	{
+	for( g = dbi_first(iter); dbi_exists(iter); g = dbi_next(iter) ) {
 		if( strcmpi(g->name, str) == 0 )
 			break;
 	}
@@ -206,7 +205,7 @@ struct guild_castle* guild_mapindex2gc(short mapindex) {
 	struct guild_castle* gc;
 	DBIterator *iter = db_iterator(castle_db);
 
-	for( gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter) ) 	{
+	for( gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter) ) {
 		if( gc->mapindex == mapindex )
 			break;
 	}
@@ -256,17 +255,17 @@ void guild_makemember(struct guild_member *m,struct map_session_data *sd) {
 	nullpo_retv(sd);
 
 	memset(m,0,sizeof(struct guild_member));
-	m->account_id	=sd->status.account_id;
-	m->char_id		=sd->status.char_id;
-	m->hair			=sd->status.hair;
-	m->hair_color	=sd->status.hair_color;
-	m->gender		=sd->status.sex;
-	m->class_		=sd->status.class_;
-	m->lv			=sd->status.base_level;
-//	m->exp			=0;
-//	m->exp_payper	=0;
-	m->online		=1;
-	m->position		=MAX_GUILDPOSITION-1;
+	m->account_id	= sd->status.account_id;
+	m->char_id		= sd->status.char_id;
+	m->hair			= sd->status.hair;
+	m->hair_color	= sd->status.hair_color;
+	m->gender		= sd->status.sex;
+	m->class_		= sd->status.class_;
+	m->lv			= sd->status.base_level;
+//	m->exp			= 0;
+//	m->exp_payper	= 0;
+	m->online		= 1;
+	m->position		= MAX_GUILDPOSITION-1;
 	memcpy(m->name,sd->status.name,NAME_LENGTH);
 	return;
 }
@@ -1960,12 +1959,11 @@ int guild_castledatasave(int castle_id, int index, int value) {
 	return 0;
 }
 
-int guild_castle_reconnect_sub(void *key, void *data, va_list ap) {
+void guild_castle_reconnect_sub(void *key, void *data, va_list ap) {
 	int castle_id = GetWord((int)__64BPRTSIZE(key), 0);
 	int index = GetWord((int)__64BPRTSIZE(key), 1);
 	intif_guild_castle_datasave(castle_id, index, *(int *)data);
 	aFree(data);
-	return 1;
 }
 
 /**

+ 9 - 13
src/map/mapreg.c

@@ -204,19 +204,13 @@ static void script_load_mapreg(void)
 		int s = add_str(varname);
 		int i = index;
 
+		if( i64db_exists(regs.vars, reference_uid(s, i)) ) {
+			ShowWarning("load_mapreg: duplicate! '%s' => '%s' skipping...\n",varname,value);
+			continue;
+		}
 		if( varname[length-1] == '$' ) {
-			if( i64db_exists(regs.vars, reference_uid(s, i)) ) {
-				ShowWarning("load_mapreg: Duplicate! '%s' => '%s' skipping...\n",varname,value);
-				continue;
-			}
-
 			mapreg_setregstr(reference_uid(s, i), value);
 		} else {
-			if( i64db_exists(regs.vars, reference_uid(s, i)) ) {
-				ShowWarning("load_mapreg: Duplicate! '%s' => '%s' skipping...\n",varname,value);
-				continue;
-			}
-
 			mapreg_setreg(reference_uid(s, i), atoi(value));
 		}
 	}
@@ -301,8 +295,10 @@ void mapreg_reload(void)
 
 	regs.vars->clear(regs.vars, mapreg_destroyreg);
 
-	if (regs.arrays)
+	if (regs.arrays) {
 		regs.arrays->destroy(regs.arrays, script_free_array_db);
+		regs.arrays = NULL;
+	}
 
 	script_load_mapreg();
 }
@@ -314,7 +310,7 @@ void mapreg_final(void)
 {
 	script_save_mapreg();
 
-	regs.vars->clear(regs.vars, mapreg_destroyreg);
+	regs.vars->destroy(regs.vars, mapreg_destroyreg);
 
 	ers_destroy(mapreg_ers);
 
@@ -328,7 +324,7 @@ void mapreg_final(void)
 void mapreg_init(void)
 {
 	regs.vars = i64db_alloc(DB_OPT_BASE);
-	mapreg_ers = ers_new(sizeof(struct mapreg_save), "mapreg.c:mapreg_ers", ERS_OPT_NONE);
+	mapreg_ers = ers_new(sizeof(struct mapreg_save), "mapreg.c:mapreg_ers", ERS_OPT_CLEAN);
 
 	skip_insert = false;
 	regs.arrays = NULL;

+ 1 - 1
src/map/mob.c

@@ -4886,7 +4886,7 @@ void do_init_mob(void){
 	memset(mob_db_data,0,sizeof(mob_db_data)); //Clear the array
 	mob_db_data[0] = (struct mob_db*)aCalloc(1, sizeof (struct mob_db));	//This mob is used for random spawns
 	mob_makedummymobdb(0); //The first time this is invoked, it creates the dummy mob
-	item_drop_ers = ers_new(sizeof(struct item_drop),"mob.c::item_drop_ers",ERS_OPT_NONE);
+	item_drop_ers = ers_new(sizeof(struct item_drop),"mob.c::item_drop_ers",ERS_OPT_CLEAN);
 	item_drop_list_ers = ers_new(sizeof(struct item_drop_list),"mob.c::item_drop_list_ers",ERS_OPT_NONE);
 	mob_item_drop_ratio = idb_alloc(DB_OPT_BASE);
 	mob_skill_db = idb_alloc(DB_OPT_BASE);

+ 0 - 1
src/map/npc.c

@@ -2035,7 +2035,6 @@ int npc_unload(struct npc_data* nd, bool single) {
 			aFree(nd->u.scr.timer_event);
 		if (nd->src_id == 0) {
 			if(nd->u.scr.script) {
-				script_stop_instances(nd->bl.id);
 				script_free_code(nd->u.scr.script);
 				nd->u.scr.script = NULL;
 			}

+ 7 - 3
src/map/pc.c

@@ -11627,8 +11627,12 @@ void do_init_pc(void) {
 
 	do_init_pc_groups();
 
-	pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_NONE);
+	pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
 	pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.c:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
-	num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_NONE);
-	str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_NONE);
+	num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c:num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
+	str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c:str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
+
+	ers_chunk_size(pc_sc_display_ers, 150);
+	ers_chunk_size(num_reg_ers, 300);
+	ers_chunk_size(str_reg_ers, 50);
 }

+ 16 - 29
src/map/script.c

@@ -164,11 +164,11 @@ int str_hash[SCRIPT_HASH_SIZE];
 //#define SCRIPT_HASH_SDBM
 #define SCRIPT_HASH_ELF
 
-static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
-static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
-static int parse_options=0;
-DBMap* script_get_label_db(void){ return scriptlabel_db; }
-DBMap* script_get_userfunc_db(void){ return userfunc_db; }
+static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
+static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
+static int parse_options = 0;
+DBMap* script_get_label_db(void) { return scriptlabel_db; }
+DBMap* script_get_userfunc_db(void) { return userfunc_db; }
 
 // important buildin function references for usage in scripts
 static int buildin_set_ref = 0;
@@ -178,7 +178,7 @@ static int buildin_getelementofarray_ref = 0;
 
 // Caches compiled autoscript item code.
 // Note: This is not cleared when reloading itemdb.
-static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
+static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
 
 struct Script_Config script_config = {
 	1, // warn_func_mismatch_argtypes
@@ -3942,24 +3942,6 @@ void run_script(struct script_code *rootscript, int pos, int rid, int oid)
 	run_script_main(st);
 }
 
-void script_stop_instances(int id)
-{
-	DBIterator *iter;
-	struct script_state* st;
-
-	if (!active_scripts)
-		return; // Don't even bother.
-
-	iter = db_iterator(st_db);
-
-	for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter)) {
-		if (st->oid == id)
-			script_free_state(st);
-	}
-
-	dbi_destroy(iter);
-}
-
 /*==========================================
  * Timer function for sleep
  *------------------------------------------*/
@@ -4640,11 +4622,12 @@ void do_final_script() {
 	mapreg_final();
 
 	db_destroy(scriptlabel_db);
+	userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
+	autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
+
 	ers_destroy(array_ers);
 	if (generic_ui_array)
 		aFree(generic_ui_array);
-	userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
-	autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
 
 	iter = db_iterator(st_db);
 	for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
@@ -4699,13 +4682,17 @@ void do_init_script(void) {
 	userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
 	scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
 	autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
-	array_ers = ers_new(sizeof(struct script_array), "script.c:array_ers", ERS_OPT_NONE);
-	st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_OPT_NONE);
-	stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_NONE);
+
+	st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
+	stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_NONE|ERS_OPT_FLEX_CHUNK);
+	array_ers = ers_new(sizeof(struct script_array), "script.c:array_ers", ERS_OPT_CLEAN|ERS_OPT_CLEAR);
 
 	ers_chunk_size(st_ers, 10);
 	ers_chunk_size(stack_ers, 10);
 
+	active_scripts = 0;
+	next_id = 0;
+
 	mapreg_init();
 #ifdef BETA_THREAD_TEST
 	CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);

+ 0 - 1
src/map/script.h

@@ -319,7 +319,6 @@ void pop_stack(struct script_state* st, int start, int end);
 int run_script_timer(int tid, unsigned int tick, int id, intptr_t data);
 void run_script_main(struct script_state *st);
 
-void script_stop_instances(int id);
 struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n);
 void script_free_code(struct script_code* code);
 void script_free_vars(struct DBMap *storage);

+ 5 - 2
src/map/skill.c

@@ -20780,8 +20780,11 @@ void do_init_skill(void)
 	skillunit_db = idb_alloc(DB_OPT_BASE);
 	skillusave_db = idb_alloc(DB_OPT_RELEASE_DATA);
 	bowling_db = idb_alloc(DB_OPT_BASE);
-	skill_unit_ers = ers_new(sizeof(struct skill_unit_group),"skill.c::skill_unit_ers",ERS_OPT_NONE);
-	skill_timer_ers  = ers_new(sizeof(struct skill_timerskill),"skill.c::skill_timer_ers",ERS_OPT_NONE);
+	skill_unit_ers = ers_new(sizeof(struct skill_unit_group),"skill.c::skill_unit_ers",ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
+	skill_timer_ers  = ers_new(sizeof(struct skill_timerskill),"skill.c::skill_timer_ers",ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
+
+	ers_chunk_size(skill_unit_ers, 150);
+	ers_chunk_size(skill_timer_ers, 150);
 
 	add_timer_func_list(skill_unit_timer,"skill_unit_timer");
 	add_timer_func_list(skill_castend_id,"skill_castend_id");