summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/HPM.c69
-rw-r--r--src/common/HPM.h11
-rw-r--r--src/common/HPMi.h11
-rw-r--r--src/common/db.c6
-rw-r--r--src/common/ers.c4
-rw-r--r--src/common/ers.h9
-rw-r--r--src/common/malloc.c33
-rw-r--r--src/common/malloc.h4
8 files changed, 138 insertions, 9 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c
index 005bc2ddc..426fac94a 100644
--- a/src/common/HPM.c
+++ b/src/common/HPM.c
@@ -215,6 +215,7 @@ struct hplugin *hplugin_load(const char* filename) {
plugin->hpi->HookStop = HPM->import_symbol("HookStop",plugin->idx);
plugin->hpi->HookStopped = HPM->import_symbol("HookStopped",plugin->idx);
plugin->hpi->addArg = HPM->import_symbol("addArg",plugin->idx);
+ plugin->hpi->addConf = HPM->import_symbol("addConf",plugin->idx);
/* server specific */
if( HPM->load_sub )
HPM->load_sub(plugin);
@@ -532,6 +533,9 @@ void* HPM_calloc(size_t num, size_t size, const char *file, int line, const char
void* HPM_realloc(void *p, size_t size, const char *file, int line, const char *func) {
return iMalloc->realloc(p,size,HPM_file2ptr(file),line,func);
}
+void* HPM_reallocz(void *p, size_t size, const char *file, int line, const char *func) {
+ return iMalloc->reallocz(p,size,HPM_file2ptr(file),line,func);
+}
char* HPM_astrdup(const char *p, const char *file, int line, const char *func) {
return iMalloc->astrdup(p,HPM_file2ptr(file),line,func);
}
@@ -602,6 +606,48 @@ bool hpm_add_arg(unsigned int pluginID, char *name, bool has_param, void (*func)
return true;
}
+bool hplugins_addconf(unsigned int pluginID, enum HPluginConfType type, char *name, void (*func) (const char *val)) {
+ struct HPConfListenStorage *conf;
+ unsigned int i;
+
+ if( type >= HPCT_MAX ) {
+ ShowError("HPM->addConf:%s: unknown point '%u' specified for config '%s'\n",HPM->pid2name(pluginID),type,name);
+ return false;
+ }
+
+ for(i = 0; i < HPM->confsc[type]; i++) {
+ if( !strcmpi(name,HPM->confs[type][i].key) ) {
+ ShowError("HPM->addConf:%s: duplicate '%s', already in use by '%s'!",HPM->pid2name(pluginID),name,HPM->pid2name(HPM->confs[type][i].pluginID));
+ return false;
+ }
+ }
+
+ RECREATE(HPM->confs[type], struct HPConfListenStorage, ++HPM->confsc[type]);
+ conf = &HPM->confs[type][HPM->confsc[type] - 1];
+
+ conf->pluginID = pluginID;
+ safestrncpy(conf->key, name, HPM_ADDCONF_LENGTH);
+ conf->func = func;
+
+ return true;
+}
+bool hplugins_parse_conf(const char *w1, const char *w2, enum HPluginConfType point) {
+ unsigned int i;
+
+ /* exists? */
+ for(i = 0; i < HPM->confsc[point]; i++) {
+ if( !strcmpi(w1,HPM->confs[point][i].key) )
+ break;
+ }
+
+ /* trigger and we're set! */
+ if( i != HPM->confsc[point] ) {
+ HPM->confs[point][i].func(w2);
+ return true;
+ }
+
+ return false;
+}
void hplugins_share_defaults(void) {
/* console */
@@ -617,6 +663,7 @@ void hplugins_share_defaults(void) {
HPM->share(HPM_HookStop,"HookStop");
HPM->share(HPM_HookStopped,"HookStopped");
HPM->share(hpm_add_arg,"addArg");
+ HPM->share(hplugins_addconf,"addConf");
/* core */
HPM->share(&runflag,"runflag");
HPM->share(arg_v,"arg_v");
@@ -661,6 +708,7 @@ void hpm_init(void) {
HPMiMalloc->malloc = HPM_mmalloc;
HPMiMalloc->calloc = HPM_calloc;
HPMiMalloc->realloc = HPM_realloc;
+ HPMiMalloc->reallocz = HPM_reallocz;
HPMiMalloc->astrdup = HPM_astrdup;
sscanf(HPM_VERSION, "%u.%u", &HPM->version[0], &HPM->version[1]);
@@ -728,6 +776,11 @@ void hpm_final(void) {
aFree(HPM->packets[i]);
}
+ for( i = 0; i < HPCT_MAX; i++ ) {
+ if( HPM->confsc[i] )
+ aFree(HPM->confs[i]);
+ }
+
HPM->arg_db->destroy(HPM->arg_db,HPM->arg_db_clear_sub);
/* HPM->fnames is cleared after the memory manager goes down */
@@ -736,13 +789,26 @@ void hpm_final(void) {
return;
}
void hpm_defaults(void) {
+ unsigned int i;
HPM = &HPM_s;
HPM->fnames = NULL;
HPM->fnamec = 0;
HPM->force_return = false;
HPM->hooking = false;
-
+ /* */
+ HPM->fnames = NULL;
+ HPM->fnamec = 0;
+ for(i = 0; i < hpPHP_MAX; i++) {
+ HPM->packets[i] = NULL;
+ HPM->packetsc[i] = 0;
+ }
+ for(i = 0; i < HPCT_MAX; i++) {
+ HPM->confs[i] = NULL;
+ HPM->confsc[i] = 0;
+ }
+ HPM->arg_db = NULL;
+ /* */
HPM->init = hpm_init;
HPM->final = hpm_final;
@@ -767,4 +833,5 @@ void hpm_defaults(void) {
HPM->arg_help = hpm_arg_help;
HPM->grabHPData = hplugins_grabHPData;
HPM->grabHPDataSub = NULL;
+ HPM->parseConf = hplugins_parse_conf;
}
diff --git a/src/common/HPM.h b/src/common/HPM.h
index d5a5cbb6a..1f2ba4648 100644
--- a/src/common/HPM.h
+++ b/src/common/HPM.h
@@ -88,6 +88,12 @@ struct HPDataOperationStorage {
void **HPDataSRCPtr;
unsigned int *hdatac;
};
+/* */
+struct HPConfListenStorage {
+ unsigned int pluginID;
+ char key[HPM_ADDCONF_LENGTH];
+ void (*func) (const char *val);
+};
/* Hercules Plugin Manager Interface */
struct HPM_interface {
@@ -108,6 +114,9 @@ struct HPM_interface {
/* plugin file ptr caching */
struct HPMFileNameCache *fnames;
unsigned int fnamec;
+ /* config listen */
+ struct HPConfListenStorage *confs[HPCT_MAX];
+ unsigned int confsc[HPCT_MAX];
/* --command-line */
DBMap *arg_db;
/* funcs */
@@ -135,6 +144,8 @@ struct HPM_interface {
void (*grabHPData) (struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr);
/* for server-specific HPData e.g. map_session_data */
void (*grabHPDataSub) (struct HPDataOperationStorage *ret, enum HPluginDataTypes type, void *ptr);
+ /* for custom config parsing */
+ bool (*parseConf) (const char *w1, const char *w2, enum HPluginConfType point);
} HPM_s;
struct HPM_interface *HPM;
diff --git a/src/common/HPMi.h b/src/common/HPMi.h
index 2cd1075c4..742132cde 100644
--- a/src/common/HPMi.h
+++ b/src/common/HPMi.h
@@ -36,6 +36,7 @@ struct map_session_data;
#include "../common/showmsg.h"
#define HPM_VERSION "1.0"
+#define HPM_ADDCONF_LENGTH 40
struct hplugin_info {
char* name;
@@ -83,6 +84,12 @@ enum HPluginDataTypes {
HPDT_NPCD,
};
+/* used in macros and conf storage */
+enum HPluginConfType {
+ HPCT_BATTLE, /* battle-conf (map-server */
+ HPCT_MAX,
+};
+
#define addHookPre(tname,hook) (HPMi->AddHook(HOOK_TYPE_PRE,(tname),(hook),HPMi->pid))
#define addHookPost(tname,hook) (HPMi->AddHook(HOOK_TYPE_POST,(tname),(hook),HPMi->pid))
/* need better names ;/ */
@@ -128,6 +135,8 @@ enum HPluginDataTypes {
}
/* HPMi->addPacket */
#define addPacket(cmd,len,receive,point) HPMi->addPacket(cmd,len,receive,point,HPMi->pid)
+/* HPMi->addBattleConf */
+#define addBattleConf(bcname,funcname) HPMi->addConf(HPMi->pid,HPCT_BATTLE,bcname,funcname)
/* Hercules Plugin Mananger Include Interface */
HPExport struct HPMi_interface {
@@ -150,6 +159,8 @@ HPExport struct HPMi_interface {
bool (*HookStopped) (void);
/* program --arg/-a */
bool (*addArg) (unsigned int pluginID, char *name, bool has_param,void (*func) (char *param),void (*help) (void));
+ /* battle-config recv param */
+ bool (*addConf) (unsigned int pluginID, enum HPluginConfType type, char *name, void (*func) (const char *val));
} HPMi_s;
#ifndef _HPM_H_
HPExport struct HPMi_interface *HPMi;
diff --git a/src/common/db.c b/src/common/db.c
index ba3fef97b..efe7ca8b2 100644
--- a/src/common/db.c
+++ b/src/common/db.c
@@ -2446,7 +2446,7 @@ DBMap* db_alloc(const char *file, const char *func, int line, DBType type, DBOpt
db->free_lock = 0;
/* Other */
snprintf(ers_name, 50, "db_alloc:nodes:%s:%s:%d",func,file,line);
- db->nodes = ers_new(sizeof(struct dbn),ers_name,ERS_OPT_WAIT|ERS_OPT_FREE_NAME);
+ db->nodes = ers_new(sizeof(struct dbn),ers_name,ERS_OPT_WAIT|ERS_OPT_FREE_NAME|ERS_OPT_CLEAN);
db->cmp = DB->default_cmp(type);
db->hash = DB->default_hash(type);
db->release = DB->default_release(type, options);
@@ -2609,8 +2609,8 @@ void* db_data2ptr(DBData *data)
* @see #db_final(void)
*/
void db_init(void) {
- db_iterator_ers = ers_new(sizeof(struct DBIterator_impl),"db.c::db_iterator_ers",ERS_OPT_NONE);
- db_alloc_ers = ers_new(sizeof(struct DBMap_impl),"db.c::db_alloc_ers",ERS_OPT_NONE);
+ db_iterator_ers = ers_new(sizeof(struct DBIterator_impl),"db.c::db_iterator_ers",ERS_OPT_CLEAN);
+ db_alloc_ers = ers_new(sizeof(struct DBMap_impl),"db.c::db_alloc_ers",ERS_OPT_CLEAN);
ers_chunk_size(db_alloc_ers, 50);
DB_COUNTSTAT(db_init);
}
diff --git a/src/common/ers.c b/src/common/ers.c
index 22269a51f..a7dad49ab 100644
--- a/src/common/ers.c
+++ b/src/common/ers.c
@@ -40,6 +40,7 @@
* @see common#ers.h *
\*****************************************************************************/
#include <stdlib.h>
+#include <string.h>
#include "../common/cbasetypes.h"
#include "../common/malloc.h" // CREATE, RECREATE, aMalloc, aFree
@@ -238,6 +239,9 @@ static void ers_obj_free_entry(ERS self, void *entry)
return;
}
+ if( instance->Options & ERS_OPT_CLEAN )
+ memset((unsigned char*)reuse + sizeof(struct ers_list), 0, instance->Cache->ObjectSize - sizeof(struct ers_list));
+
reuse->Next = instance->Cache->ReuseList;
instance->Cache->ReuseList = reuse;
instance->Count--;
diff --git a/src/common/ers.h b/src/common/ers.h
index 4ff2a6230..e63711b81 100644
--- a/src/common/ers.h
+++ b/src/common/ers.h
@@ -71,10 +71,11 @@
#endif /* not ERS_ALIGN_ENTRY */
enum ERSOptions {
- ERS_OPT_NONE = 0x0,
- ERS_OPT_CLEAR = 0x1,/* silently clears any entries left in the manager upon destruction */
- ERS_OPT_WAIT = 0x2,/* wait for entries to come in order to list! */
- ERS_OPT_FREE_NAME = 0x4,/* name is dynamic memory, and should be freed */
+ ERS_OPT_NONE = 0x0,
+ ERS_OPT_CLEAR = 0x1,/* silently clears any entries left in the manager upon destruction */
+ ERS_OPT_WAIT = 0x2,/* wait for entries to come in order to list! */
+ ERS_OPT_FREE_NAME = 0x4,/* name is dynamic memory, and should be freed */
+ ERS_OPT_CLEAN = 0x8,/* clears used memory upon ers_free so that its all new to be reused on the next alloc */
};
/**
diff --git a/src/common/malloc.c b/src/common/malloc.c
index 23e28a65f..875b87a15 100644
--- a/src/common/malloc.c
+++ b/src/common/malloc.c
@@ -369,6 +369,37 @@ void* _mrealloc(void *memblock, size_t size, const char *file, int line, const c
}
}
+/* a _mrealloc clone with the difference it 'z'eroes the newly created memory */
+void* _mreallocz(void *memblock, size_t size, const char *file, int line, const char *func ) {
+ size_t old_size;
+ void *p = NULL;
+
+ if(memblock == NULL) {
+ p = iMalloc->malloc(size,file,line,func);
+ memset(p,0,size);
+ return p;
+ }
+
+ old_size = ((struct unit_head *)((char *)memblock - sizeof(struct unit_head) + sizeof(long)))->size;
+ if( old_size == 0 ) {
+ old_size = ((struct unit_head_large *)((char *)memblock - sizeof(struct unit_head_large) + sizeof(long)))->size;
+ }
+ if(old_size > size) {
+ // Size reduction - return> as it is (negligence)
+ return memblock;
+ } else {
+ // Size Large
+ p = iMalloc->malloc(size,file,line,func);
+ if(p != NULL) {
+ memcpy(p,memblock,old_size);
+ memset(p+old_size,0,size-old_size);
+ }
+ iMalloc->free(memblock,file,line,func);
+ return p;
+ }
+}
+
+
char* _mstrdup(const char *p, const char *file, int line, const char *func )
{
if(p == NULL) {
@@ -822,12 +853,14 @@ void malloc_defaults(void) {
iMalloc->malloc = _mmalloc;
iMalloc->calloc = _mcalloc;
iMalloc->realloc = _mrealloc;
+ iMalloc->reallocz= _mreallocz;
iMalloc->astrdup = _mstrdup;
iMalloc->free = _mfree;
#else
iMalloc->malloc = aMalloc_;
iMalloc->calloc = aCalloc_;
iMalloc->realloc = aRealloc_;
+ iMalloc->reallocz= aRealloc_;/* not using memory manager huhum o.o perhaps we could still do something about */
iMalloc->astrdup = aStrdup_;
iMalloc->free = aFree_;
#endif
diff --git a/src/common/malloc.h b/src/common/malloc.h
index cd0ef238b..19b5213bb 100644
--- a/src/common/malloc.h
+++ b/src/common/malloc.h
@@ -33,6 +33,7 @@
# define aMalloc(n) (iMalloc->malloc((n),ALC_MARK))
# define aCalloc(m,n) (iMalloc->calloc((m),(n),ALC_MARK))
# define aRealloc(p,n) (iMalloc->realloc((p),(n),ALC_MARK))
+# define aReallocz(p,n) (iMalloc->reallocz((p),(n),ALC_MARK))
# define aStrdup(p) (iMalloc->astrdup((p),ALC_MARK))
# define aFree(p) (iMalloc->free((p),ALC_MARK))
@@ -54,7 +55,7 @@
////////////// Others //////////////////////////
// should be merged with any of above later
#define CREATE(result, type, number) ((result) = (type *) aCalloc((number), sizeof(type)))
-#define RECREATE(result, type, number) ((result) = (type *) aRealloc((result), sizeof(type) * (number)))
+#define RECREATE(result, type, number) ((result) = (type *) aReallocz((result), sizeof(type) * (number)))
////////////////////////////////////////////////
@@ -73,6 +74,7 @@ struct malloc_interface {
void* (*malloc )(size_t size, const char *file, int line, const char *func);
void* (*calloc )(size_t num, size_t size, const char *file, int line, const char *func);
void* (*realloc )(void *p, size_t size, const char *file, int line, const char *func);
+ void* (*reallocz)(void *p, size_t size, const char *file, int line, const char *func);
char* (*astrdup )(const char *p, const char *file, int line, const char *func);
void (*free )(void *p, const char *file, int line, const char *func);
/* */