summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLance <Lance@54d463be-8e91-2dee-dedb-b68131a5f0ec>2006-04-29 04:00:19 +0000
committerLance <Lance@54d463be-8e91-2dee-dedb-b68131a5f0ec>2006-04-29 04:00:19 +0000
commit4db583138b9556f72b13df7f66eeeace6a67ce3e (patch)
tree72b6589e9f7a0e46e2aa459cd6272bb90bfe7e96
parent525aa7d56ec5d74eac548d948782330d02df8f3e (diff)
downloadhercules-4db583138b9556f72b13df7f66eeeace6a67ce3e.tar.gz
hercules-4db583138b9556f72b13df7f66eeeace6a67ce3e.tar.bz2
hercules-4db583138b9556f72b13df7f66eeeace6a67ce3e.tar.xz
hercules-4db583138b9556f72b13df7f66eeeace6a67ce3e.zip
* Using malloc for memory allocations that doesn't need to be pre-initialized.
* Eliminated initializations for pointers returned by calloc. * Moved grfio_final to do_init since it's not used after server boot. * Optimized scripts to return CONSTSTR instead of allocating memory just to return a constant string. Some buildin functions still need work. * Disabled graph since it doesn't work yet. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@6355 54d463be-8e91-2dee-dedb-b68131a5f0ec
-rw-r--r--Changelog-Trunk.txt9
-rw-r--r--src/common/graph.c2
-rw-r--r--src/common/grfio.c14
-rw-r--r--src/map/atcommand.c6
-rw-r--r--src/map/charsave.c2
-rw-r--r--src/map/chat.c4
-rw-r--r--src/map/chrif.c6
-rw-r--r--src/map/clif.c23
-rw-r--r--src/map/guild.c6
-rw-r--r--src/map/irc.c4
-rw-r--r--src/map/map.c42
-rw-r--r--src/map/npc.c35
-rw-r--r--src/map/pc.c4
-rw-r--r--src/map/script.c89
-rw-r--r--src/map/skill.c7
15 files changed, 119 insertions, 134 deletions
diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt
index c1fd8e207..976f66852 100644
--- a/Changelog-Trunk.txt
+++ b/Changelog-Trunk.txt
@@ -2,6 +2,15 @@ Date Added
AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO INTO TRUNK.
IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
+
+2006/04/29
+ * Using malloc for memory allocations that doesn't need to be pre-initialized.
+ * Eliminated initializations for pointers returned by calloc.
+ * Moved grfio_final to do_init since it's not used after server boot.
+ * Optimized scripts to return CONSTSTR instead of allocating memory just to return
+ a constant string. Some buildin functions still need work.
+ * Disabled graph since it doesn't work yet. [Lance]
+
2006/04/28
* Modified the mob_db structure. The mvp related columns were moved to
before the drops, amount of drops increased from 10 to 15. [Skotlex]
diff --git a/src/common/graph.c b/src/common/graph.c
index a68d39ce0..0a49fa709 100644
--- a/src/common/graph.c
+++ b/src/common/graph.c
@@ -2,7 +2,7 @@
// For more information, see LICENCE in the main folder
// graph creation is enabled
-#define ENABLE_GRAPH
+// #define ENABLE_GRAPH
#ifdef ENABLE_GRAPH
diff --git a/src/common/grfio.c b/src/common/grfio.c
index eae06eada..d0178d3e4 100644
--- a/src/common/grfio.c
+++ b/src/common/grfio.c
@@ -591,7 +591,7 @@ void* grfio_reads(char *fname, int *size)
lentry.declen = ftell(in);
}
fseek(in,0,0); // SEEK_SET
- buf2 = (unsigned char *)aCallocA(lentry.declen + 1024, 1);
+ buf2 = (unsigned char *)aMallocA(lentry.declen + 1024);
fread(buf2, 1, lentry.declen, in);
fclose(in);
strncpy(lentry.fn, fname, sizeof(lentry.fn) - 1);
@@ -611,11 +611,11 @@ void* grfio_reads(char *fname, int *size)
char *gfname = gentry_table[entry->gentry - 1];
in = fopen(gfname, "rb");
if(in != NULL) {
- unsigned char *buf = (unsigned char *)aCallocA(entry->srclen_aligned + 1024, 1);
+ unsigned char *buf = (unsigned char *)aMallocA(entry->srclen_aligned + 1024);
fseek(in, entry->srcpos, 0);
fread(buf, 1, entry->srclen_aligned, in);
fclose(in);
- buf2 = (unsigned char *)aCallocA(entry->declen + 1024, 1);
+ buf2 = (unsigned char *)aMallocA(entry->declen + 1024);
if (entry->type == 1 || entry->type == 3 || entry->type == 5) {
uLongf len;
if (entry->cycle >= 0)
@@ -694,7 +694,7 @@ static int grfio_entryread(char *gfname,int gentry)
if (grf_version == 0x01) { //****** Grf version 01xx ******
list_size = grf_size - ftell(fp);
- grf_filelist = (unsigned char *) aCallocA(list_size, 1);
+ grf_filelist = (unsigned char *) aMallocA(list_size);
/*if (grf_filelist == NULL){
fclose(fp);
ShowError("out of memory : grf_filelist\n");
@@ -770,13 +770,13 @@ static int grfio_entryread(char *gfname,int gentry)
return 4;
}
- rBuf = (unsigned char *)aCallocA(rSize , 1); // Get a Read Size
+ rBuf = (unsigned char *)aMallocA(rSize); // Get a Read Size
/*if (rBuf==NULL) {
fclose(fp);
ShowError("out of memory : grf compress entry table buffer\n");
return 3;
}*/
- grf_filelist = (unsigned char *)aCallocA(eSize , 1); // Get a Extend Size
+ grf_filelist = (unsigned char *)aMallocA(eSize); // Get a Extend Size
/*if (grf_filelist==NULL) {
aFree(rBuf);
fclose(fp);
@@ -952,7 +952,7 @@ char *grfio_alloc_ptr(char *fname)
memset(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS);
}
len = strlen( fname );
- buf = (char*)aCallocA(len + 1, 1);
+ buf = (char*)aMallocA(len + 1);
strcpy(buf, fname);
gentry_table[gentry_entrys++] = buf;
diff --git a/src/map/atcommand.c b/src/map/atcommand.c
index 90069f409..686d03c87 100644
--- a/src/map/atcommand.c
+++ b/src/map/atcommand.c
@@ -889,7 +889,7 @@ int msg_config_read(const char *cfgName) {
if (msg_number >= 0 && msg_number < MAX_MSG) {
if (msg_table[msg_number] != NULL)
aFree(msg_table[msg_number]);
- msg_table[msg_number] = (char *)aCalloc(strlen(w2) + 1, sizeof (char));
+ msg_table[msg_number] = (char *)aMalloc((strlen(w2) + 1)*sizeof (char));
strcpy(msg_table[msg_number],w2);
// printf("message #%d: '%s'.\n", msg_number, msg_table[msg_number]);
}
@@ -1951,8 +1951,8 @@ int atcommand_whozeny(
clif_displaymessage(fd, msg_table[28]); // No player found.
return 0;
}
- zeny = (int *)aCallocA(users, sizeof(int));
- counted = (int *)aCallocA(users, sizeof(int));
+ zeny = (int *)aMallocA(users*sizeof(int));
+ counted = (int *)aMallocA(users*sizeof(int));
for (i = 0; i < users; i++) {
if ((pl_sd = pl_allsd[i])) {
memcpy(player_name, pl_sd->status.name, NAME_LENGTH);
diff --git a/src/map/charsave.c b/src/map/charsave.c
index 01d6afc07..3f5241900 100644
--- a/src/map/charsave.c
+++ b/src/map/charsave.c
@@ -26,7 +26,7 @@ struct mmo_charstatus *charsave_loadchar(int charid){
char *str_p;
friends = 0;
- c = (struct mmo_charstatus *)aMalloc(sizeof(struct mmo_charstatus));
+ c = (struct mmo_charstatus *)aCalloc(1,sizeof(struct mmo_charstatus));
if(charid <= 0){
ShowError("charsave_loadchar() charid <= 0! (%d)", charid);
diff --git a/src/map/chat.c b/src/map/chat.c
index 3ea00e2ab..0936b48ac 100644
--- a/src/map/chat.c
+++ b/src/map/chat.c
@@ -28,7 +28,7 @@ int chat_createchat(struct map_session_data *sd,int limit,int pub,char* pass,cha
if (sd->chatID)
return 0; //Prevent people abusing the chat system by creating multiple chats, as pointed out by End of Exam. [Skotlex]
pc_stop_walking(sd,1);
- cd = (struct chat_data *) aCalloc(1,sizeof(struct chat_data));
+ cd = (struct chat_data *) aMalloc(sizeof(struct chat_data));
cd->limit = limit;
cd->pub = pub;
@@ -266,7 +266,7 @@ int chat_createnpcchat(struct npc_data *nd,int limit,int pub,int trigger,char* t
nullpo_retr(1, nd);
- cd = (struct chat_data *) aCalloc(1,sizeof(struct chat_data));
+ cd = (struct chat_data *) aMalloc(sizeof(struct chat_data));
cd->limit = cd->trigger = limit;
if(trigger>0)
diff --git a/src/map/chrif.c b/src/map/chrif.c
index 373808cc7..939208e8e 100644
--- a/src/map/chrif.c
+++ b/src/map/chrif.c
@@ -465,7 +465,7 @@ void chrif_authreq(struct map_session_data *sd)
aFree(auth_data->char_dat);
idb_remove(auth_db, sd->bl.id);
} else { //data from char server has not arrived yet.
- auth_data = aCalloc(1, sizeof(struct auth_node));
+ auth_data = aMalloc(sizeof(struct auth_node));
auth_data->sd = sd;
auth_data->fd = sd->fd;
auth_data->account_id = sd->bl.id;
@@ -511,8 +511,8 @@ void chrif_authok(int fd) {
return;
}
// Awaiting for client to connect.
- auth_data = (struct auth_node *)aCalloc(1, sizeof(struct auth_node));
- auth_data->char_dat = (struct mmo_charstatus *) aCalloc(1, sizeof(struct mmo_charstatus));
+ auth_data = (struct auth_node *)aMalloc(sizeof(struct auth_node));
+ auth_data->char_dat = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
auth_data->account_id=RFIFOL(fd, 4);
auth_data->login_id1=RFIFOL(fd, 8);
diff --git a/src/map/clif.c b/src/map/clif.c
index 387839a1c..849db4008 100644
--- a/src/map/clif.c
+++ b/src/map/clif.c
@@ -751,7 +751,7 @@ static int clif_clearchar_delay_sub(int tid, unsigned int tick, int id, int data
int clif_clearchar_delay(unsigned int tick, struct block_list *bl, int type) {
struct block_list *tbl;
- tbl = aCalloc(1, sizeof (struct block_list));
+ tbl = aMalloc(sizeof (struct block_list));
memcpy (tbl, bl, sizeof (struct block_list));
add_timer(tick, clif_clearchar_delay_sub, (int)tbl, type);
@@ -4811,7 +4811,7 @@ int clif_GMmessage(struct block_list *bl, char* mes, int len, int flag)
int lp;
lp = (flag & 0x10) ? 8 : 4;
- buf = (unsigned char*)aCallocA(len + lp + 8, sizeof(unsigned char));
+ buf = (unsigned char*)aMallocA((len + lp + 8)*sizeof(unsigned char));
WBUFW(buf,0) = 0x9a;
WBUFW(buf,2) = len + lp;
@@ -4885,7 +4885,7 @@ void clif_MainChatMessage(char* message) {
int clif_announce(struct block_list *bl, char* mes, int len, unsigned long color, int flag)
{
unsigned char *buf;
- buf = (unsigned char*)aCallocA(len + 16, sizeof(unsigned char));
+ buf = (unsigned char*)aMallocA((len + 16)*sizeof(unsigned char));
WBUFW(buf,0) = 0x1c3;
WBUFW(buf,2) = len + 16;
WBUFL(buf,4) = color;
@@ -7062,7 +7062,7 @@ int clif_guild_message(struct guild *g,int account_id,const char *mes,int len)
struct map_session_data *sd;
unsigned char *buf;
- buf = (unsigned char*)aCallocA(len + 4, sizeof(unsigned char));
+ buf = (unsigned char*)aMallocA((len + 4)*sizeof(unsigned char));
WBUFW(buf, 0) = 0x17f;
WBUFW(buf, 2) = len + 4;
@@ -7368,7 +7368,7 @@ int clif_disp_onlyself(struct map_session_data *sd, char *mes, int len)
nullpo_retr(0, sd);
- buf = (unsigned char*)aCallocA(len + 5, sizeof(unsigned char));
+ buf = (unsigned char*)aMallocA((len + 5)*sizeof(unsigned char));
WBUFW(buf, 0) = 0x17f;
WBUFW(buf, 2) = len + 5;
@@ -8458,7 +8458,7 @@ void clif_parse_GlobalMessage(int fd, struct map_session_data *sd) { // S 008c <
if (RFIFOW(fd,2)+4 < 128)
buf = buf2; //Use a static buffer.
else
- buf = (unsigned char*)aCallocA(RFIFOW(fd,2) + 4, sizeof(char));
+ buf = (unsigned char*)aMallocA((RFIFOW(fd,2) + 4)*sizeof(char));
// send message to others
WBUFW(buf,0) = 0x8d;
@@ -8759,7 +8759,7 @@ void clif_parse_Wis(int fd, struct map_session_data *sd) { // S 0096 <len>.w <ni
//printf("clif_parse_Wis: message: '%s'.\n", RFIFOP(fd,28));
- gm_command = (char*)aCallocA(strlen((const char*)RFIFOP(fd,28)) + 28, sizeof(char)); // 24+3+(RFIFOW(fd,2)-28)+1 or 24+3+(strlen(RFIFOP(fd,28))+1 (size can be wrong with hacker)
+ gm_command = (char*)aMallocA((strlen((const char*)RFIFOP(fd,28)) + 28)*sizeof(char)); // 24+3+(RFIFOW(fd,2)-28)+1 or 24+3+(strlen(RFIFOP(fd,28))+1 (size can be wrong with hacker)
sprintf(gm_command, "%s : %s", sd->status.name, RFIFOP(fd,28));
if ((is_charcommand(fd, sd, gm_command, 0) != CharCommand_None) ||
@@ -8787,8 +8787,7 @@ void clif_parse_Wis(int fd, struct map_session_data *sd) { // S 0096 <len>.w <ni
whisper_tmp += 4; //Skip the NPC: string part.
if ((npc = npc_name2id(whisper_tmp)))
{
- whisper_tmp=(char *)aCallocA(strlen((char *)(RFIFOP(fd,28)))+1,sizeof(char));
- whisper_tmp[0]=0;
+ whisper_tmp=(char *)aMallocA((strlen((char *)(RFIFOP(fd,28)))+1)*sizeof(char));
sprintf(whisper_tmp, "%s", (const char*)RFIFOP(fd,28));
for( j=0;whisper_tmp[j]!='\0';j++)
@@ -8807,8 +8806,7 @@ void clif_parse_Wis(int fd, struct map_session_data *sd) { // S 0096 <len>.w <ni
split_data[i][j-k]='\0';
aFree(whisper_tmp);
- whisper_tmp=(char *)aCallocA(15,sizeof(char));
- whisper_tmp[0]=0;
+ whisper_tmp=(char *)aMallocA(15*sizeof(char));
for (j=0;j<=10;j++)
{
@@ -8817,8 +8815,7 @@ void clif_parse_Wis(int fd, struct map_session_data *sd) { // S 0096 <len>.w <ni
}//You don't need to zero them, just reset them [Kevin]
aFree(whisper_tmp);
- whisper_tmp=(char *)aCallocA(strlen(npc->name)+18,sizeof(char));
- whisper_tmp[0]=0;
+ whisper_tmp=(char *)aMallocA((strlen(npc->name)+18)*sizeof(char));
sprintf(whisper_tmp, "%s::OnWhisperGlobal", npc->name);
npc_event(sd,whisper_tmp,0); // Calls the NPC label
diff --git a/src/map/guild.c b/src/map/guild.c
index dc059bb40..0c71ee293 100644
--- a/src/map/guild.c
+++ b/src/map/guild.c
@@ -482,7 +482,7 @@ int guild_npc_request_info(int guild_id,const char *event)
if(event==NULL || *event==0)
return guild_request_info(guild_id);
- ev=(struct eventlist *)aCalloc(1,sizeof(struct eventlist));
+ ev=(struct eventlist *)aMalloc(sizeof(struct eventlist));
memcpy(ev->name,event,strlen(event));
//The one in the db becomes the next event from this.
ev->next=idb_put(guild_infoevent_db,guild_id,ev);
@@ -1680,7 +1680,7 @@ int guild_addcastleinfoevent(int castle_id,int index,const char *name)
if( name==NULL || *name==0 )
return 0;
- ev=(struct eventlist *)aCalloc(1,sizeof(struct eventlist));
+ ev=(struct eventlist *)aMalloc(sizeof(struct eventlist));
memcpy(ev->name,name,sizeof(ev->name));
//The next event becomes whatever was currently stored.
ev->next= idb_put(guild_castleinfoevent_db,code,ev);
@@ -1904,7 +1904,7 @@ int guild_agit_break(struct mob_data *md)
nullpo_retr(0, md);
- evname=(char *)aCallocA(strlen(md->npc_event) + 1, sizeof(char));
+ evname=(char *)aMallocA((strlen(md->npc_event) + 1)*sizeof(char));
strcpy(evname,md->npc_event);
// Now By User to Run [OnAgitBreak] NPC Event...
diff --git a/src/map/irc.c b/src/map/irc.c
index 3c04505e2..2ddc70c5e 100644
--- a/src/map/irc.c
+++ b/src/map/irc.c
@@ -148,7 +148,7 @@ int irc_parse(int fd)
return 0;
}
if (session[fd]->session_data == NULL){
- irc_si = (struct IRC_Session_Info*)aCalloc(1, sizeof(struct IRC_Session_Info));
+ irc_si = (struct IRC_Session_Info*)aMalloc(sizeof(struct IRC_Session_Info));
irc_si->fd = fd;
irc_si->state = 0;
session[fd]->session_data = irc_si;
@@ -157,7 +157,7 @@ int irc_parse(int fd)
irc_si->fd = fd;
}
if(RFIFOREST(fd) > 0){
- char *incoming_string=aCalloc(RFIFOREST(fd),sizeof(char));
+ char *incoming_string=aMalloc(RFIFOREST(fd)*sizeof(char));
memcpy(incoming_string,RFIFOP(fd,0),RFIFOREST(fd));
send_to_parser(fd,incoming_string,"\n");
RFIFOSKIP(fd,RFIFOREST(fd));
diff --git a/src/map/map.c b/src/map/map.c
index 9a97d7446..950eb6e03 100644
--- a/src/map/map.c
+++ b/src/map/map.c
@@ -2738,7 +2738,7 @@ static int map_loadafm (struct map_data *m, char *fn)
ys = m->ys = afm_size[1];
m->water_height = map_waterheight(m->name);
// check this, unsigned where it might not need to be
- m->gat = (unsigned char*)aCallocA(xs * ys, 1);
+ m->gat = (unsigned char*)aMallocA(xs * ys);
for (y = 0; y < ys; y++) {
str = fgets(afm_line, sizeof(afm_line)-1, afm_file);
@@ -2834,7 +2834,7 @@ int map_readaf2 (struct map_data *m)
*/
int map_readgat (struct map_data *m)
{
- char fn[256], *pt;
+ char fn[256];
char *gat;
int wh,x,y,xs,ys;
struct gat_1cell {float high[4]; int type;} *p = NULL;
@@ -2842,13 +2842,6 @@ int map_readgat (struct map_data *m)
if (strstr(m->name,".gat") == NULL)
return 0;
- if ((pt = strstr(m->name,"<")) != NULL) { // [MouseJstr]
- char buf[64];
- *pt++ = '\0';
- sprintf(buf,"data\\%s", pt);
- m->alias = aStrdup(buf);
- }
-
sprintf(fn,"data\\%s",m->name);
// read & convert fn
@@ -2859,7 +2852,7 @@ int map_readgat (struct map_data *m)
xs = m->xs = *(int*)(gat+6);
ys = m->ys = *(int*)(gat+10);
- m->gat = (unsigned char *)aCallocA(m->xs * m->ys, sizeof(unsigned char));
+ m->gat = (unsigned char *)aMallocA((m->xs * m->ys)*sizeof(unsigned char));
m->water_height = wh = map_waterheight(m->name);
for (y = 0; y < ys; y++) {
@@ -3068,10 +3061,12 @@ int map_readallmaps (void)
size = map[i].bxs * map[i].bys * sizeof(int);
map[i].block_count = (int*)aCallocA(size, 1);
- memset(map[i].block_count, 0, size);
+ // Already initialized in aCallocA
+ //memset(map[i].block_count, 0, size);
map[i].block_mob_count = (int*)aCallocA(size, 1);
- memset(map[i].block_mob_count, 0, size);
+ // Already initialized in aCallocA
+ //memset(map[i].block_mob_count, 0, size);
uidb_put(map_db, (unsigned int)map[i].index, &map[i]);
@@ -3129,20 +3124,15 @@ int parse_console(char *buf) {
int m, n;
struct map_session_data *sd;
- sd = (struct map_session_data*)aCalloc(sizeof(*sd), 1);
+ sd = (struct map_session_data*)aCalloc(sizeof(struct map_session_data), 1);
sd->fd = 0;
strcpy( sd->status.name , "console");
- type = (char *)aMallocA(64);
- command = (char *)aMallocA(64);
- map = (char *)aMallocA(64);
- buf2 = (char *)aMallocA(72);
-
- memset(type,0,64);
- memset(command,0,64);
- memset(map,0,64);
- memset(buf2,0,72);
+ type = (char *)aCallocA(64,1);
+ command = (char *)aCallocA(64,1);
+ map = (char *)aCallocA(64,1);
+ buf2 = (char *)aCallocA(72,1);
if ( ( n = sscanf(buf, "%[^:]:%[^:]:%99s %d %d[^\n]", type , command , map , &x , &y )) < 5 )
if ( ( n = sscanf(buf, "%[^:]:%[^\n]", type , command )) < 2 )
@@ -3593,9 +3583,11 @@ void do_final(void) {
ShowStatus("Terminating...\n");
- // we probably don't need the cache open at all times 'yet', so this is closed by mapsource_final [celest]
+ //we probably don't need the cache open at all times 'yet', so this is closed by mapsource_final [celest]
//map_cache_close();
- grfio_final();
+
+ // We probably don't need the grfio after server bootup 'yet' too. So this is closed near the end of do_init [Lance]
+ //grfio_final();
for (i = 0; i < map_num; i++)
if (map[i].m >= 0)
@@ -3887,6 +3879,8 @@ int do_init(int argc, char *argv[]) {
if (battle_config.pk_mode == 1)
ShowNotice("Server is running on '"CL_WHITE"PK Mode"CL_RESET"'.\n");
+ grfio_final(); // Unused after reading all maps.
+
ShowStatus("Server is '"CL_GREEN"ready"CL_RESET"' and listening on port '"CL_WHITE"%d"CL_RESET"'.\n\n", map_port);
return 0;
diff --git a/src/map/npc.c b/src/map/npc.c
index 857c5963d..2d890f466 100644
--- a/src/map/npc.c
+++ b/src/map/npc.c
@@ -284,7 +284,7 @@ int npc_event_export(char *lname,void *data,va_list ap)
unsigned char buf[51];
char *p=strchr(lname,':');
// エクスポートされる
- ev=(struct event_data *) aCalloc(sizeof(struct event_data), 1);
+ ev=(struct event_data *) aMalloc(sizeof(struct event_data));
if (ev==NULL) {
ShowFatalError("npc_event_export: out of memory !\n");
exit(1);
@@ -459,11 +459,12 @@ int npc_addeventtimer(struct npc_data *nd,int tick,const char *name)
ShowError("npc_addeventimer: Event %s does not exists.\n", name);
return 1; //Event does not exists!
}
- evname =(unsigned char *) aCallocA(NAME_LENGTH, sizeof(char));
+ evname =(unsigned char *) aMallocA(NAME_LENGTH*sizeof(char));
if(evname==NULL){
ShowFatalError("npc_addeventtimer: out of memory !\n");exit(1);
}
memcpy(evname,name,NAME_LENGTH-1);
+ evname[NAME_LENGTH-1] = '\0';
nd->eventtimer[i]=add_timer(gettick()+tick,
npc_event_timer,nd->bl.id,(int)evname);
}else
@@ -1397,7 +1398,7 @@ void npc_addsrcfile (char *name)
nsl = nsl->next;
}
- nsl = (struct npc_src_list *) aCalloc (1, sizeof(*nsl) + strlen(name));
+ nsl = (struct npc_src_list *) aMalloc (sizeof(*nsl) + strlen(name));
nsl->next = NULL;
strncpy(nsl->name, name, strlen(name) + 1);
if (npc_src_first == NULL)
@@ -1704,7 +1705,7 @@ static int npc_skip_script (char *w1,char *w2,char *w3,char *w4,char *first_line
unsigned char line[1024];
int curly_count = 0;
- srcbuf = (unsigned char *)aCallocA(srcsize, sizeof(char));
+ srcbuf = (unsigned char *)aMallocA(srcsize*sizeof(char));
if (strchr(first_line, '{')) {
strcpy((char *)srcbuf, strchr(first_line, '{'));
startline = *lines;
@@ -1746,7 +1747,6 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
unsigned char line[1024];
int i;
struct npc_data *nd;
- int evflag = 0;
struct dbt *label_db;
char *p;
struct npc_label_list *label_dup = NULL;
@@ -1768,7 +1768,7 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
if (strcmp(w2, "script") == 0){
// parsing script with curly
int curly_count = 0;
- srcbuf = (unsigned char *)aCallocA(srcsize, sizeof(char));
+ srcbuf = (unsigned char *)aMallocA(srcsize*sizeof(char));
if (strchr(first_line, '{')) {
strcpy((char *)srcbuf, strchr(first_line, '{'));
startline = *lines;
@@ -1834,7 +1834,7 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
if (xs >= 0) xs = xs * 2 + 1;
if (ys >= 0) ys = ys * 2 + 1;
- if (class_ >= -1) { // -1 NPCs use OnTouch [Lance]
+ if (m >= 0) {
for (i = 0; i < ys; i++) {
for (j = 0; j < xs; j++) {
if (map_getcell(m, x - xs/2 + j, y - ys/2 + i, CELL_CHKNOPASS))
@@ -1852,10 +1852,6 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
nd->u.scr.ys = 0;
}
- if (class_ < 0 && m >= 0) { // イベント型NPC
- evflag = 1;
- }
-
while ((p = strchr(w3,':'))) {
if (p[1] == ':') break;
}
@@ -1886,19 +1882,22 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
nd->eventtimer[i] = -1;
if (m >= 0) {
nd->n = map_addnpc(m, nd);
- if (class_ >= 0)
- status_set_viewdata(&nd->bl, nd->class_);
status_change_init(&nd->bl);
unit_dataset(&nd->bl);
nd->ud.dir = dir;
map_addblock(&nd->bl);
- if (evflag) { // イベント型
+ // Unused. You can always use On::XXXX events. Have this removed to improve perfomance.
+ /*if (evflag) { // イベント型
struct event_data *ev = (struct event_data *)aCalloc(1, sizeof(struct event_data));
ev->nd = nd;
ev->pos = 0;
strdb_put(ev_db, nd->exname, ev);
} else {
clif_spawn(&nd->bl);
+ }*/
+ if (class_ >= 0){
+ status_set_viewdata(&nd->bl, nd->class_);
+ clif_spawn(&nd->bl);
}
} else {
// we skip map_addnpc, but still add it to the list of ID's
@@ -1943,7 +1942,7 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
// remember the label is max 50 chars + eos; see the strdb_init below
// generate the data and insert it
- ev=(struct event_data *)aCalloc(1,sizeof(struct event_data));
+ ev=(struct event_data *)aMalloc(sizeof(struct event_data));
ev->nd=nd;
ev->pos=pos;
if (strdb_put(ev_db,buf,ev) != NULL) //There was already another event of the same name?
@@ -1963,7 +1962,7 @@ static int npc_parse_script (char *w1,char *w2,char *w3,char *w4,char *first_lin
struct npc_timerevent_list *te = nd->u.scr.timer_event;
int j, k = nd->u.scr.timeramount;
if (te == NULL)
- te = (struct npc_timerevent_list *)aCallocA(1,sizeof(struct npc_timerevent_list));
+ te = (struct npc_timerevent_list *)aMallocA(sizeof(struct npc_timerevent_list));
else
te = (struct npc_timerevent_list *)aRealloc( te, sizeof(struct npc_timerevent_list) * (k+1) );
for (j = 0; j < k; j++){
@@ -1997,7 +1996,7 @@ static int npc_parse_function (char *w1, char *w2, char *w3, char *w4, char *fir
struct dbt *user_db;
// スクリプトの解析
- srcbuf = (unsigned char *) aCallocA (srcsize, sizeof(char));
+ srcbuf = (unsigned char *) aMallocA (srcsize*sizeof(char));
if (strchr(first_line,'{')) {
strcpy(srcbuf, strchr(first_line,'{'));
startline = *lines;
@@ -2036,7 +2035,7 @@ static int npc_parse_function (char *w1, char *w2, char *w3, char *w4, char *fir
return 1;
}
- p = (char *) aCallocA (50, sizeof(char));
+ p = (char *) aMallocA (50*sizeof(char));
strncpy(p, w3, 50);
user_db = script_get_userfunc_db();
diff --git a/src/map/pc.c b/src/map/pc.c
index 036771b74..ed8dfbf62 100644
--- a/src/map/pc.c
+++ b/src/map/pc.c
@@ -7106,8 +7106,8 @@ int pc_read_gm_account(int fd)
if (gm_account != NULL)
aFree(gm_account);
GM_num = 0;
- gm_account = (struct gm_account *) aCallocA(((RFIFOW(fd,2) - 4) / 5), sizeof(struct gm_account));
- for (i = 4; i < RFIFOW(fd,2); i = i + 5) {
+ gm_account = (struct gm_account *) aMallocA(((RFIFOW(fd,2) - 4) / 5)*sizeof(struct gm_account));
+ for (i = 4; i < RFIFOW(fd,2); i += 5) {
gm_account[GM_num].account_id = RFIFOL(fd,i);
gm_account[GM_num].level = (int)RFIFOB(fd,i+4);
//printf("GM account: %d -> level %d\n", gm_account[GM_num].account_id, gm_account[GM_num].level);
diff --git a/src/map/script.c b/src/map/script.c
index c1cedb9b4..71993f33e 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -2436,7 +2436,7 @@ char* conv_str(struct script_state *st,struct script_data *data)
get_val(st,data);
if(data->type==C_INT){
char *buf;
- buf=(char *)aCallocA(ITEM_NAME_LENGTH,sizeof(char));
+ buf=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
snprintf(buf,ITEM_NAME_LENGTH, "%d",data->u.num);
data->type=C_STR;
data->u.str=buf;
@@ -2783,7 +2783,7 @@ int buildin_menu(struct script_state *st)
conv_str(st,& (st->stack->stack_data[i]));
len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
}
- buf=(char *)aCallocA(len+1,sizeof(char));
+ buf=(char *)aMallocA((len+1)*sizeof(char));
buf[0]=0;
for(i=st->start+2,len=0;i<st->end;i+=2){
strcat(buf,st->stack->stack_data[i].u.str);
@@ -4275,8 +4275,9 @@ char *buildin_getpartyname_sub(int party_id)
if(p!=NULL){
char *buf;
- buf=(char *)aCallocA(NAME_LENGTH,sizeof(char));
+ buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
memcpy(buf, p->name, NAME_LENGTH-1);
+ buf[NAME_LENGTH-1] = '\0';
return buf;
}
@@ -4343,8 +4344,9 @@ char *buildin_getguildname_sub(int guild_id)
if(g!=NULL){
char *buf;
- buf=(char *)aCallocA(NAME_LENGTH,sizeof(char));
+ buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
memcpy(buf, g->name, NAME_LENGTH-1);
+ buf[NAME_LENGTH-1] = '\0';
return buf;
}
return NULL;
@@ -4372,8 +4374,9 @@ char *buildin_getguildmaster_sub(int guild_id)
if(g!=NULL){
char *buf;
- buf=(char *)aCallocA(NAME_LENGTH,sizeof(char));
+ buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
memcpy(buf, g->master, NAME_LENGTH-1);
+ buf[NAME_LENGTH-1] = '\0';
return buf;
}
@@ -4416,8 +4419,8 @@ int buildin_getguildmasterid(struct script_state *st)
int buildin_strcharinfo(struct script_state *st)
{
struct map_session_data *sd;
- char *buf;
int num;
+ char *buf;
sd=script_rid2sd(st);
if (!sd) { //Avoid crashing....
@@ -4427,9 +4430,7 @@ int buildin_strcharinfo(struct script_state *st)
num=conv_num(st,& (st->stack->stack_data[st->start+2]));
switch(num){
case 0:
- buf=(char *)aCallocA(NAME_LENGTH,sizeof(char));
- memcpy(buf, sd->status.name, NAME_LENGTH-1);
- push_str(st->stack,C_STR,(unsigned char *) buf);
+ push_str(st->stack,C_CONSTSTR,(unsigned char *) sd->status.name);
break;
case 1:
buf=buildin_getpartyname_sub(sd->status.party_id);
@@ -4497,7 +4498,7 @@ int buildin_getequipname(struct script_state *st)
struct item_data* item;
char *buf;
- buf=(char *)aCallocA(64,sizeof(char));
+ buf=(char *)aMallocA(64*sizeof(char));
sd=script_rid2sd(st);
num=conv_num(st,& (st->stack->stack_data[st->start+2]));
i=pc_checkequip(sd,equip[num-1]);
@@ -5312,7 +5313,7 @@ int buildin_gettimestr(struct script_state *st)
fmtstr=conv_str(st,& (st->stack->stack_data[st->start+2]));
maxlen=conv_num(st,& (st->stack->stack_data[st->start+3]));
- tmpstr=(char *)aCallocA(maxlen+1,sizeof(char));
+ tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
strftime(tmpstr,maxlen,fmtstr,localtime(&now));
tmpstr[maxlen]='\0';
@@ -7146,18 +7147,15 @@ int buildin_getcastlename(struct script_state *st)
char *mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
struct guild_castle *gc;
int i;
- char *buf=NULL;
for(i=0;i<MAX_GUILDCASTLE;i++){
if( (gc=guild_castle_search(i)) != NULL ){
if(strcmp(mapname,gc->map_name)==0){
- buf=(char *)aCallocA(NAME_LENGTH,sizeof(char));
- memcpy(buf, gc->castle_name, NAME_LENGTH-1);
break;
}
}
}
- if(buf)
- push_str(st->stack,C_STR,(unsigned char *) buf);
+ if(gc)
+ push_str(st->stack,C_CONSTSTR,(unsigned char *) gc->castle_name);
else
push_str(st->stack,C_CONSTSTR,(unsigned char *) "");
return 0;
@@ -7709,22 +7707,12 @@ int buildin_strmobinfo(struct script_state *st)
switch (num) {
case 1:
{
- char *buf;
- buf=(char *) aCallocA(NAME_LENGTH, sizeof(char));
-// buf=mob_db(class_)->name;
-// for string assignments you would need to go for c++ [Shinomori]
- memcpy(buf, mob_db(class_)->name, NAME_LENGTH-1);
- push_str(st->stack,C_STR,(unsigned char *) buf);
+ push_str(st->stack,C_CONSTSTR,(unsigned char *) mob_db(class_)->name);
break;
}
case 2:
{
- char *buf;
- buf=(char *) aCallocA(NAME_LENGTH, sizeof(char));
-// buf=mob_db(class_).jname;
-// for string assignments you would need to go for c++ [Shinomori]
- memcpy(buf,mob_db(class_)->jname, NAME_LENGTH-1);
- push_str(st->stack,C_STR,(unsigned char *) buf);
+ push_str(st->stack,C_CONSTSTR,(unsigned char *) mob_db(class_)->jname);
break;
}
case 3:
@@ -7820,9 +7808,9 @@ int buildin_getitemname(struct script_state *st)
push_str(st->stack,C_CONSTSTR,(unsigned char *) "null");
return 0;
}
- item_name=(char *)aCallocA(ITEM_NAME_LENGTH,sizeof(char));
+ item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
- memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH-1);
+ memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
push_str(st->stack,C_STR,(unsigned char *) item_name);
return 0;
}
@@ -7936,7 +7924,7 @@ int buildin_petskillbonus(struct script_state *st)
if (pd->bonus->timer != -1)
delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
} else //init
- pd->bonus = (struct pet_bonus *) aCalloc(1, sizeof(struct pet_bonus));
+ pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
pd->bonus->type=conv_num(st,& (st->stack->stack_data[st->start+2]));
pd->bonus->val=conv_num(st,& (st->stack->stack_data[st->start+3]));
@@ -7982,10 +7970,10 @@ int buildin_petloot(struct script_state *st)
aFree(pd->loot->item);
}
else
- pd->loot = (struct pet_loot *)aCalloc(1, sizeof(struct pet_loot));
+ pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
- memset(pd->loot->item,0,max * sizeof(struct item));
+ //memset(pd->loot->item,0,max * sizeof(struct item));
pd->loot->max=max;
pd->loot->count = 0;
@@ -8230,7 +8218,7 @@ int buildin_petrecovery(struct script_state *st)
if (pd->recovery->timer != -1)
delete_timer(pd->recovery->timer, pet_recovery_timer);
} else //Init
- pd->recovery = (struct pet_recovery *)aCalloc(1, sizeof(struct pet_recovery));
+ pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
pd->recovery->type=conv_num(st,& (st->stack->stack_data[st->start+2]));
pd->recovery->delay=conv_num(st,& (st->stack->stack_data[st->start+3]));
@@ -8263,7 +8251,7 @@ int buildin_petheal(struct script_state *st)
delete_timer(pd->s_skill->timer, pet_heal_timer);
}
} else //init memory
- pd->s_skill = (struct pet_skill_support *) aCalloc(1, sizeof(struct pet_skill_support));
+ pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
//Use the lv as the amount to heal
@@ -8295,7 +8283,7 @@ int buildin_petskillattack(struct script_state *st)
pd=sd->pd;
if (pd->a_skill == NULL)
- pd->a_skill = (struct pet_skill_attack *)aCalloc(1, sizeof(struct pet_skill_attack));
+ pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
pd->a_skill->id=conv_num(st,& (st->stack->stack_data[st->start+2]));
pd->a_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
@@ -8320,7 +8308,7 @@ int buildin_petskillattack2(struct script_state *st)
pd=sd->pd;
if (pd->a_skill == NULL)
- pd->a_skill = (struct pet_skill_attack *)aCalloc(1, sizeof(struct pet_skill_attack));
+ pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
pd->a_skill->id=conv_num(st,& (st->stack->stack_data[st->start+2]));
pd->a_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
@@ -8354,7 +8342,7 @@ int buildin_petskillsupport(struct script_state *st)
delete_timer(pd->s_skill->timer, pet_heal_timer);
}
} else //init memory
- pd->s_skill = (struct pet_skill_support *) aCalloc(1, sizeof(struct pet_skill_support));
+ pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
pd->s_skill->id=conv_num(st,& (st->stack->stack_data[st->start+2]));
pd->s_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
@@ -8584,11 +8572,8 @@ int buildin_getpetinfo(struct script_state *st)
break;
case 2:
if(sd->pet.name)
- { //Shamelessly copied from strcharinfo() [Skotlex]
- char *buf;
- buf=(char *)aCallocA(NAME_LENGTH,sizeof(char));
- memcpy(buf, sd->pet.name, NAME_LENGTH-1);
- push_str(st->stack,C_STR,(unsigned char *) buf);
+ {
+ push_str(st->stack,C_CONSTSTR,(unsigned char *) sd->pet.name);
}
else
push_str(st->stack,C_CONSTSTR, (unsigned char *) "null");
@@ -8674,7 +8659,7 @@ int buildin_select(struct script_state *st)
conv_str(st,& (st->stack->stack_data[i]));
len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
}
- buf=(char *)aCalloc(len+1,sizeof(char));
+ buf=(char *)aMalloc((len+1)*sizeof(char));
buf[0]=0;
for(i=st->start+2,len=0;i<st->end;i++){
strcat(buf,st->stack->stack_data[i].u.str);
@@ -8937,7 +8922,7 @@ int buildin_getsavepoint(struct script_state *st)
y=sd->status.save_point.y;
switch(type){
case 0:
- mapname=(char *) aCallocA(MAP_NAME_LENGTH+1, sizeof(char));
+ mapname=(char *) aMallocA((MAP_NAME_LENGTH+1)*sizeof(char));
memcpy(mapname, mapindex_id2name(sd->status.save_point.map), MAP_NAME_LENGTH);
mapname[MAP_NAME_LENGTH]='\0';
push_str(st->stack,C_STR,(unsigned char *) mapname);
@@ -9761,7 +9746,7 @@ int buildin_escape_sql(struct script_state *st) {
char *t_query, *query;
query = conv_str(st,& (st->stack->stack_data[st->start+2]));
- t_query = aCallocA(strlen(query)*2+1,sizeof(char));
+ t_query = aMallocA((strlen(query)*2+1)*sizeof(char));
jstrescapecpy(t_query,query);
push_str(st->stack,C_STR,(unsigned char *)t_query);
return 0;
@@ -10592,8 +10577,8 @@ void op_add(struct script_state* st)
st->stack->stack_data[st->stack->sp-1].u.num += st->stack->stack_data[st->stack->sp].u.num;
} else { // ssの予定
char *buf;
- buf=(char *)aCallocA(strlen(st->stack->stack_data[st->stack->sp-1].u.str)+
- strlen(st->stack->stack_data[st->stack->sp].u.str)+1,sizeof(char));
+ buf=(char *)aMallocA((strlen(st->stack->stack_data[st->stack->sp-1].u.str)+
+ strlen(st->stack->stack_data[st->stack->sp].u.str)+1)*sizeof(char));
strcpy(buf,st->stack->stack_data[st->stack->sp-1].u.str);
strcat(buf,st->stack->stack_data[st->stack->sp].u.str);
if(st->stack->stack_data[st->stack->sp-1].type==C_STR)
@@ -11072,7 +11057,7 @@ int run_script(unsigned char *script,int pos,int rid,int oid)
sd->npc_scriptstate = 0;
} else {
// the script is different, make new script_state and stack
- st.stack = aCalloc (1, sizeof(struct script_stack));
+ st.stack = aMalloc (sizeof(struct script_stack));
st.stack->sp = 0;
st.stack->sp_max = 64;
st.stack->stack_data = (struct script_data *) aCalloc (st.stack->sp_max,sizeof(st.stack->stack_data[0]));
@@ -11194,7 +11179,7 @@ int mapreg_setregstr(int num,const char *str)
mapreg_dirty=1;
return 0;
}
- p=(char *)aCallocA(strlen(str)+1, sizeof(char));
+ p=(char *)aMallocA((strlen(str)+1)*sizeof(char));
strcpy(p,str);
if (idb_put(mapregstr_db,num,p))
@@ -11236,7 +11221,7 @@ static int script_load_mapreg(void)
ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
continue;
}
- p=(char *)aCallocA(strlen(buf2) + 1,sizeof(char));
+ p=(char *)aMallocA((strlen(buf2) + 1)*sizeof(char));
strcpy(p,buf2);
s= add_str((unsigned char *) buf1);
idb_put(mapregstr_db,(i<<24)|s,p);
@@ -11277,7 +11262,7 @@ static int script_load_mapreg(void)
strcpy(buf1,sql_row[0]);
if( buf1[strlen(buf1)-1]=='$' ){
i = atoi(sql_row[1]);
- p=(char *)aCallocA(strlen(sql_row[2]) + 1,sizeof(char));
+ p=(char *)aMallocA((strlen(sql_row[2]) + 1)*sizeof(char));
strcpy(p,sql_row[2]);
s= add_str((unsigned char *) buf1);
idb_put(mapregstr_db,(i<<24)|s,p);
diff --git a/src/map/skill.c b/src/map/skill.c
index 21140d8c0..d70e6c262 100644
--- a/src/map/skill.c
+++ b/src/map/skill.c
@@ -6499,12 +6499,13 @@ struct skill_unit_group *skill_unitsetting( struct block_list *src, int skillid,
group->bl_flag= skill_get_unit_bl_target(skillid);
if(skillid==HT_TALKIEBOX ||
skillid==RG_GRAFFITI){
- group->valstr=(char *) aCallocA(MESSAGE_SIZE, sizeof(char));
+ group->valstr=(char *) aMallocA(MESSAGE_SIZE*sizeof(char));
if(group->valstr==NULL){
ShowFatalError("skill_castend_map: out of memory !\n");
exit(1);
}
memcpy(group->valstr,talkie_mes,MESSAGE_SIZE-1);
+ group->valstr[MESSAGE_SIZE-1] = '\0';
}
//Why redefine local variables when the ones of the function can be reused? [Skotlex]
@@ -9374,9 +9375,9 @@ struct skill_unit_group *skill_initunitgroup(struct block_list *src,
group->group_id=skill_unit_group_newid++;
if(skill_unit_group_newid<=0)
skill_unit_group_newid = MAX_SKILL_DB;
- group->unit=(struct skill_unit *)aCalloc(count,sizeof(struct skill_unit));
+ group->unit=(struct skill_unit *)aMalloc(count*sizeof(struct skill_unit));
group->unit_count=count;
- group->val1=group->val2=0;
+ group->val1=group->val2=group->val3=0;
group->skill_id=skillid;
group->skill_lv=skilllv;
group->unit_id=unit_id;