- Cleanups or minor changes.

- Now addtick_timer invokes settick_timer, so keep an eye for whatever timer issues it's supposed to have.
- Removed the flush_fifo from clif_parse_TickSend until the socket problems are fixed.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@9521 54d463be-8e91-2dee-dedb-b68131a5f0ec
This commit is contained in:
FlavioJS 2006-12-19 09:15:00 +00:00
parent 2afbde751d
commit 43256aa77e
6 changed files with 199 additions and 163 deletions

View File

@ -3,6 +3,12 @@ Date Added
AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO INTO TRUNK. 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. IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
2006/12/19
* Cleanups or minor changes.
* Now addtick_timer invokes settick_timer, so keep an eye for whatever
timer issues it's supposed to have.
* Removed the flush_fifo from clif_parse_TickSend until the socket
problems are fixed. [FlavioJS]
2006/12/18 2006/12/18
* Updated POS2 macros. The 6th byte is subx0 and suby0. [FlavioJS] * Updated POS2 macros. The 6th byte is subx0 and suby0. [FlavioJS]
* Added hom_setting to specify which homunculus 'quirks' are in effect. The * Added hom_setting to specify which homunculus 'quirks' are in effect. The

View File

@ -101,30 +101,30 @@ typedef unsigned int uint32;
#undef UINT8_MIN #undef UINT8_MIN
#undef UINT16_MIN #undef UINT16_MIN
#undef UINT32_MIN #undef UINT32_MIN
#define UINT8_MIN (uint8) 0 #define UINT8_MIN ((uint8) 0)
#define UINT16_MIN (uint16)0 #define UINT16_MIN ((uint16)0)
#define UINT32_MIN (uint32)0 #define UINT32_MIN ((uint32)0)
#undef UINT8_MAX #undef UINT8_MAX
#undef UINT16_MAX #undef UINT16_MAX
#undef UINT32_MAX #undef UINT32_MAX
#define UINT8_MAX (uint8) 0xFF #define UINT8_MAX ((uint8) 0xFF)
#define UINT16_MAX (uint16)0xFFFF #define UINT16_MAX ((uint16)0xFFFF)
#define UINT32_MAX (uint32)0xFFFFFFFF #define UINT32_MAX ((uint32)0xFFFFFFFF)
#undef SINT8_MIN #undef SINT8_MIN
#undef SINT16_MIN #undef SINT16_MIN
#undef SINT32_MIN #undef SINT32_MIN
#define SINT8_MIN (sint8) 0x80 #define SINT8_MIN ((sint8) 0x80)
#define SINT16_MIN (sint16)0x8000 #define SINT16_MIN ((sint16)0x8000)
#define SINT32_MIN (sint32)0x80000000 #define SINT32_MIN ((sint32)0x80000000)
#undef SINT8_MAX #undef SINT8_MAX
#undef SINT16_MAX #undef SINT16_MAX
#undef SINT32_MAX #undef SINT32_MAX
#define SINT8_MAX (sint8) 0x7F #define SINT8_MAX ((sint8) 0x7F)
#define SINT16_MAX (sint16)0x7FFF #define SINT16_MAX ((sint16)0x7FFF)
#define SINT32_MAX (sint32)0x7FFFFFFF #define SINT32_MAX ((sint32)0x7FFFFFFF)
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////

View File

@ -34,47 +34,50 @@ extern time_t stall_time;
#define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) #define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos))
#endif #endif
// use function instead of macro. // use function instead of macro.
#define RFIFOB(fd,pos) (*(unsigned char*)RFIFOP(fd,pos)) #define RFIFOB(fd,pos) (*(uint8*)RFIFOP(fd,pos))
#define RFIFOW(fd,pos) (*(unsigned short*)RFIFOP(fd,pos)) #define RFIFOW(fd,pos) (*(uint16*)RFIFOP(fd,pos))
#define RFIFOL(fd,pos) (*(unsigned long*)RFIFOP(fd,pos)) #define RFIFOL(fd,pos) (*(uint32*)RFIFOP(fd,pos))
#define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) #define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos)
#define RFIFOFLUSH(fd) \ #define RFIFOFLUSH(fd) \
if(session[fd]->rdata_size == session[fd]->rdata_pos) \ do { \
{ session[fd]->rdata_size = session[fd]->rdata_pos = 0; } else { \ if(session[fd]->rdata_size == session[fd]->rdata_pos){ \
session[fd]->rdata_size = session[fd]->rdata_pos = 0; \
} else { \
session[fd]->rdata_size -= session[fd]->rdata_pos; \ session[fd]->rdata_size -= session[fd]->rdata_pos; \
memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \ memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \
session[fd]->rdata_pos=0; \ session[fd]->rdata_pos=0; \
} } \
} while(0)
//#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size-session[fd]->rdata_pos-(len)<0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos+=(len))) //#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size-session[fd]->rdata_pos-(len)<0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos+=(len)))
#define RBUFP(p,pos) (((unsigned char*)(p))+(pos)) #define RBUFP(p,pos) (((uint8*)(p))+(pos))
#define RBUFB(p,pos) (*(unsigned char*)RBUFP((p),(pos))) #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos)))
#define RBUFW(p,pos) (*(unsigned short*)RBUFP((p),(pos))) #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos)))
#define RBUFL(p,pos) (*(unsigned long*)RBUFP((p),(pos))) #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos)))
#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) #define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size)
#ifdef TURBO #ifdef TURBO
#define WFIFOHEAD(fd, x) char *wbPtr ## fd = fd?(session[fd]->wdata+session[fd]->wdata_size):0; #define WFIFOHEAD(fd, x) uint8 *wbPtr ## fd = fd?(session[fd]->wdata+session[fd]->wdata_size):0;
#define WFIFOP(fd,pos) (&wbPtr ## fd[pos]) #define WFIFOP(fd,pos) (&wbPtr ## fd[pos])
#else #else
#define WFIFOHEAD(fd, size) { if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); } #define WFIFOHEAD(fd, size) do{ if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); }while(0)
#define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) #define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos))
#endif #endif
#define WFIFOB(fd,pos) (*(unsigned char*)WFIFOP(fd,pos)) #define WFIFOB(fd,pos) (*(uint8*)WFIFOP(fd,pos))
#define WFIFOW(fd,pos) (*(unsigned short*)WFIFOP(fd,pos)) #define WFIFOW(fd,pos) (*(uint16*)WFIFOP(fd,pos))
#define WFIFOL(fd,pos) (*(unsigned long*)WFIFOP(fd,pos)) #define WFIFOL(fd,pos) (*(uint32*)WFIFOP(fd,pos))
// use function instead of macro. // use function instead of macro.
//#define WFIFOSET(fd,len) (session[fd]->wdata_size = (session[fd]->wdata_size + (len) + 2048 < session[fd]->max_wdata) ? session[fd]->wdata_size + len : session[fd]->wdata_size) //#define WFIFOSET(fd,len) (session[fd]->wdata_size = (session[fd]->wdata_size + (len) + 2048 < session[fd]->max_wdata) ? session[fd]->wdata_size + len : session[fd]->wdata_size)
#define WBUFP(p,pos) (((unsigned char*)(p)) + (pos)) #define WBUFP(p,pos) (((uint8*)(p)) + (pos))
#define WBUFB(p,pos) (*(unsigned char*)((p) + (pos))) #define WBUFB(p,pos) (*(uint8*)((p) + (pos)))
#define WBUFW(p,pos) (*(unsigned short*)((p) + (pos))) #define WBUFW(p,pos) (*(uint16*)((p) + (pos)))
#define WBUFL(p,pos) (*(unsigned long*)((p) + (pos))) #define WBUFL(p,pos) (*(uint32*)((p) + (pos)))
#define TOB(n) ((unsigned char)(n)) #define TOB(n) ((uint8)((n)&UINT8_MAX))
#define TOW(n) ((unsigned short)(n)) #define TOW(n) ((uint16)((n)&UINT16_MAX))
#define TOL(n) ((unsigned long)(n)) #define TOL(n) ((uint32)((n)&UINT32_MAX))
//FD_SETSIZE must be modified on the project files/Makefile, since a change here won't affect //FD_SETSIZE must be modified on the project files/Makefile, since a change here won't affect
// dependant windows libraries. // dependant windows libraries.

View File

@ -31,66 +31,76 @@
#define TIMER_MIN_INTERVAL 50 #define TIMER_MIN_INTERVAL 50
// timers
static struct TimerData* timer_data = NULL; static struct TimerData* timer_data = NULL;
static int timer_data_max = 0; static int timer_data_max = 0;
static int timer_data_num = 0; static int timer_data_num = 0;
// free timers
static int* free_timer_list = NULL; static int* free_timer_list = NULL;
static int free_timer_list_max = 0; static int free_timer_list_max = 0;
static int free_timer_list_pos = 0; static int free_timer_list_pos = 0;
//NOTE: using a binary heap should improve performance [FlavioJS]
// timer heap (ordered array of tid's)
static int timer_heap_num = 0; static int timer_heap_num = 0;
static int timer_heap_max = 0; static int timer_heap_max = 0;
static int* timer_heap = NULL; static int* timer_heap = NULL;
// searches for the target tick's position and stores it in pos (binary search)
static int fix_heap_flag =0; //Flag for fixing the stack only once per tick loop. May not be the best way, but it's all I can think of currently :X [Skotlex] #define HEAP_SEARCH(target,from,to,pos) \
do { \
int max,pivot; \
pos = from; \
max = to; \
while (pos < max) { \
pivot = (pos + max) / 2; \
if (DIFF_TICK(target, timer_data[timer_heap[pivot]].tick) < 0) \
pos = pivot + 1; \
else \
max = pivot; \
} \
} while(0)
// for debug // for debug
struct timer_func_list { struct timer_func_list {
int (*func)(int,unsigned int,int,int);
struct timer_func_list* next; struct timer_func_list* next;
TimerFunc func;
char* name; char* name;
}; };
static struct timer_func_list* tfl_root; static struct timer_func_list* tfl_root = NULL;
time_t start_time; time_t start_time;
#ifdef __WIN32 /// Sets the name of a timer function.
/* Modified struct timezone to void - we pass NULL anyway */ int add_timer_func_list(TimerFunc func, char* name)
void gettimeofday (struct timeval *t, void *dummy)
{
DWORD millisec = GetTickCount();
t->tv_sec = (int) (millisec / 1000);
t->tv_usec = (millisec % 1000) * 1000;
}
#endif
//
int add_timer_func_list(int (*func)(int,unsigned int,int,int), char* name)
{ {
struct timer_func_list* tfl; struct timer_func_list* tfl;
if (name) { if (name) {
tfl = (struct timer_func_list*) aCalloc (sizeof(struct timer_func_list), 1); for( tfl=tfl_root; tfl != NULL; tfl=tfl->next )
tfl->name = (char *) aMalloc (strlen(name) + 1); {// check suspicious cases
if( func == tfl->func )
ShowWarning("add_timer_func_list: duplicating function %08x(%s) as %s.\n",(int)tfl->func,tfl->name,name);
else if( strcmp(name,tfl->name) == 0 )
ShowWarning("add_timer_func_list: function %08X has the same name as %08X(%s)\n",(int)func,(int)tfl->func,tfl->name);
}
CREATE(tfl,struct timer_func_list,1);
tfl->next = tfl_root; tfl->next = tfl_root;
tfl->func = func; tfl->func = func;
strcpy(tfl->name, name); tfl->name = aStrdup(name);
tfl_root = tfl; tfl_root = tfl;
} }
return 0; return 0;
} }
char* search_timer_func_list(int (*func)(int,unsigned int,int,int)) /// Returns the name of the timer function.
char* search_timer_func_list(TimerFunc func)
{ {
struct timer_func_list* tfl = tfl_root; struct timer_func_list* tfl;
while (tfl) {
for( tfl=tfl_root; tfl != NULL; tfl=tfl->next )
if (func == tfl->func) if (func == tfl->func)
return tfl->name; return tfl->name;
tfl = tfl->next;
}
return "unknown timer function"; return "unknown timer function";
} }
@ -103,18 +113,22 @@ static int gettick_count;
unsigned int gettick_nocache(void) unsigned int gettick_nocache(void)
{ {
#ifdef _WIN32
gettick_count = 256;
return gettick_cache = GetTickCount();
#else
struct timeval tval; struct timeval tval;
gettimeofday(&tval, NULL); gettimeofday(&tval, NULL);
gettick_count = 256; gettick_count = 256;
return gettick_cache = tval.tv_sec * 1000 + tval.tv_usec / 1000; return gettick_cache = tval.tv_sec * 1000 + tval.tv_usec / 1000;
#endif
} }
unsigned int gettick(void) unsigned int gettick(void)
{ {
gettick_count--; if (--gettick_count < 0)
if (gettick_count < 0)
return gettick_nocache(); return gettick_nocache();
return gettick_cache; return gettick_cache;
@ -124,61 +138,56 @@ unsigned int gettick(void)
* CORE : Timer Heap * CORE : Timer Heap
*-------------------------------------- *--------------------------------------
*/ */
static void push_timer_heap(int index) /// Adds a timer to the timer_heap
static void push_timer_heap(int tid)
{ {
int i, j; unsigned int tick;
int min, max, pivot; // for sorting int pos;
int i;
// check number of element // check number of element
if (timer_heap_num >= timer_heap_max) { if (timer_heap_num >= timer_heap_max) {
if (timer_heap_max == 0) { if (timer_heap_max == 0) {
timer_heap_max = 256; timer_heap_max = 256;
timer_heap = (int *) aCalloc( sizeof(int) , 256); CREATE(timer_heap, int, 256);
} else { } else {
timer_heap_max += 256; timer_heap_max += 256;
timer_heap = (int *) aRealloc( timer_heap, sizeof(int) * timer_heap_max); RECREATE(timer_heap, int, timer_heap_max);
malloc_tsetdword(timer_heap + (timer_heap_max - 256), 0, sizeof(int) * 256); malloc_tsetdword(timer_heap + (timer_heap_max - 256), 0, sizeof(int) * 256);
} }
} }
// do a sorting from higher to lower // do a sorting from higher to lower
j = timer_data[index].tick; // speed up tick = timer_data[tid].tick; // speed up
// with less than 4 values, it's speeder to use simple loop // with less than 4 values, it's speeder to use simple loop
if (timer_heap_num < 4) { if (timer_heap_num < 4) {
for(i = timer_heap_num; i > 0; i--) for(i = timer_heap_num; i > 0; i--)
{
// if (j < timer_data[timer_heap[i - 1]].tick) //Plain comparisons break on bound looping timers. [Skotlex] // if (j < timer_data[timer_heap[i - 1]].tick) //Plain comparisons break on bound looping timers. [Skotlex]
if (DIFF_TICK(j, timer_data[timer_heap[i - 1]].tick) < 0) if (DIFF_TICK(tick, timer_data[timer_heap[i - 1]].tick) < 0)
break; break;
else else
timer_heap[i] = timer_heap[i - 1]; timer_heap[i] = timer_heap[i - 1];
timer_heap[i] = index; }
// searching by dichotomie timer_heap[i] = tid;
// searching by dichotomy (binary search)
} else { } else {
// if lower actual item is higher than new // if lower actual item is higher than new
// if (j < timer_data[timer_heap[timer_heap_num - 1]].tick) //Plain comparisons break on bound looping timers. [Skotlex] // if (j < timer_data[timer_heap[timer_heap_num - 1]].tick) //Plain comparisons break on bound looping timers. [Skotlex]
if (DIFF_TICK(j, timer_data[timer_heap[timer_heap_num - 1]].tick) < 0) if (DIFF_TICK(tick, timer_data[timer_heap[timer_heap_num - 1]].tick) < 0)
timer_heap[timer_heap_num] = index; timer_heap[timer_heap_num] = tid;
else { else {
// searching position // searching position
min = 0; HEAP_SEARCH(tick,0,timer_heap_num-1,pos);
max = timer_heap_num - 1;
while (min < max) {
pivot = (min + max) / 2;
// if (j < timer_data[timer_heap[pivot]].tick) //Plain comparisons break on bound looping timers. [Skotlex]
if (DIFF_TICK(j, timer_data[timer_heap[pivot]].tick) < 0)
min = pivot + 1;
else
max = pivot;
}
// move elements - do loop if there are a little number of elements to move // move elements - do loop if there are a little number of elements to move
if (timer_heap_num - min < 5) { if (timer_heap_num - pos < 5) {
for(i = timer_heap_num; i > min; i--) for(i = timer_heap_num; i > pos; i--)
timer_heap[i] = timer_heap[i - 1]; timer_heap[i] = timer_heap[i - 1];
// move elements - else use memmove (speeder for a lot of elements) // move elements - else use memmove (speeder for a lot of elements)
} else } else
memmove(&timer_heap[min + 1], &timer_heap[min], sizeof(int) * (timer_heap_num - min)); memmove(&timer_heap[pos + 1], &timer_heap[pos], sizeof(int) * (timer_heap_num - pos));
// save new element // save new element
timer_heap[min] = index; timer_heap[pos] = tid;
} }
} }
@ -190,34 +199,41 @@ static void push_timer_heap(int index)
*-------------------------- *--------------------------
*/ */
int acquire_timer (void) /// Returns a free timer id.
static int acquire_timer(void)
{ {
int i; int tid;
if (free_timer_list_pos) { if (free_timer_list_pos) {
do { do {
i = free_timer_list[--free_timer_list_pos]; tid = free_timer_list[--free_timer_list_pos];
} while(i >= timer_data_num && free_timer_list_pos > 0); } while(tid >= timer_data_num && free_timer_list_pos > 0);
} else } else
i = timer_data_num; tid = timer_data_num;
if (i >= timer_data_num) if (tid >= timer_data_num)
for (i = timer_data_num; i < timer_data_max && timer_data[i].type; i++); for (tid = timer_data_num; tid < timer_data_max && timer_data[tid].type; tid++);
if (i >= timer_data_num && i >= timer_data_max) { if (tid >= timer_data_num && tid >= timer_data_max)
if (timer_data_max == 0) { {// expand timer array
if (timer_data_max == 0)
{// create timer data (1st time)
timer_data_max = 256; timer_data_max = 256;
timer_data = (struct TimerData*) aCalloc( sizeof(struct TimerData) , timer_data_max); CREATE(timer_data, struct TimerData, timer_data_max);
} else { } else
{// add more timers
timer_data_max += 256; timer_data_max += 256;
timer_data = (struct TimerData *) aRealloc( timer_data, sizeof(struct TimerData) * timer_data_max); RECREATE(timer_data, struct TimerData, timer_data_max);
malloc_tsetdword(timer_data + (timer_data_max - 256), 0, sizeof(struct TimerData) * 256); malloc_tsetdword(timer_data + (timer_data_max - 256), 0, sizeof(struct TimerData) * 256);
} }
} }
return i; if (tid >= timer_data_num)
timer_data_num = tid + 1;
return tid;
} }
int add_timer(unsigned int tick,int (*func)(int,unsigned int,int,int), int id, int data) int add_timer(unsigned int tick,TimerFunc func, int id, int data)
{ {
int tid = acquire_timer(); int tid = acquire_timer();
@ -229,13 +245,10 @@ int add_timer(unsigned int tick,int (*func)(int,unsigned int,int,int), int id, i
timer_data[tid].interval = 1000; timer_data[tid].interval = 1000;
push_timer_heap(tid); push_timer_heap(tid);
if (tid >= timer_data_num)
timer_data_num = tid + 1;
return tid; return tid;
} }
int add_timer_interval(unsigned int tick, int (*func)(int,unsigned int,int,int), int id, int data, int interval) int add_timer_interval(unsigned int tick, TimerFunc func, int id, int data, int interval)
{ {
int tid; int tid;
@ -254,13 +267,10 @@ int add_timer_interval(unsigned int tick, int (*func)(int,unsigned int,int,int),
timer_data[tid].interval = interval; timer_data[tid].interval = interval;
push_timer_heap(tid); push_timer_heap(tid);
if (tid >= timer_data_num)
timer_data_num = tid + 1;
return tid; return tid;
} }
int delete_timer(int id, int (*func)(int,unsigned int,int,int)) int delete_timer(int id, TimerFunc func)
{ {
if (id <= 0 || id >= timer_data_num) { if (id <= 0 || id >= timer_data_num) {
ShowError("delete_timer error : no such timer %d (%08x(%s))\n", id, (int)func, search_timer_func_list(func)); ShowError("delete_timer error : no such timer %d (%08x(%s))\n", id, (int)func, search_timer_func_list(func));
@ -281,41 +291,59 @@ int delete_timer(int id, int (*func)(int,unsigned int,int,int))
int addtick_timer(int tid, unsigned int tick) int addtick_timer(int tid, unsigned int tick)
{ {
return timer_data[tid].tick += tick; // Doesn't adjust the timer position. Might be the root of the FIXME in settick_timer. [FlavioJS]
//return timer_data[tid].tick += tick;
return settick_timer(tid, timer_data[tid].tick+tick);
} }
//Sets the tick at which the timer triggers directly (meant as a replacement of delete_timer + add_timer) [Skotlex] //Sets the tick at which the timer triggers directly (meant as a replacement of delete_timer + add_timer) [Skotlex]
//FIXME: DON'T use this function yet, it is not correctly reorganizing the timer stack causing unexpected problems later on! //FIXME: DON'T use this function yet, it is not correctly reorganizing the timer stack causing unexpected problems later on!
int settick_timer(int tid, unsigned int tick) int settick_timer(int tid, unsigned int tick)
{ {
int i,j; int old_pos,pos;
if (timer_data[tid].tick == tick) unsigned int old_tick;
old_tick = timer_data[tid].tick;
if( old_tick == tick )
return tick; return tick;
//FIXME: This search is not all that effective... there doesn't seems to be a better way to locate an element in the heap. //FIXME: This search is not all that effective... there doesn't seems to be a better way to locate an element in the heap.
for(i = timer_heap_num-1; i >= 0 && timer_heap[i] != tid; i--); //for(i = timer_heap_num-1; i >= 0 && timer_heap[i] != tid; i--);
if (i < 0) // search old_tick position
return -1; //Sort of impossible, isn't it? HEAP_SEARCH(old_tick,0,timer_heap_num-1,old_pos);
if (DIFF_TICK(timer_data[tid].tick, tick) > 0) while( timer_heap[old_pos] != tid )
{// skip timers with the same tick
if( DIFF_TICK(old_tick,timer_data[timer_heap[old_pos]].tick) != 0 )
{
ShowError("settick_timer: no such timer %d (%08x(%s))\n", tid, (int)timer_data[tid].func, search_timer_func_list(timer_data[tid].func));
return -1;
}
++old_pos;
}
if( DIFF_TICK(tick,timer_data[tid].tick) < 0 )
{// Timer is accelerated, shift timer near the end of the heap. {// Timer is accelerated, shift timer near the end of the heap.
if (i == timer_heap_num-1) //Nothing to shift. if (old_pos == timer_heap_num-1) //Nothing to shift.
j = timer_heap_num-1; pos = old_pos;
else { else {
for (j = i+1; j < timer_heap_num && DIFF_TICK(timer_data[j].tick, tick) > 0; j++); HEAP_SEARCH(tick,old_pos+1,timer_heap_num-1,pos);
j--; --pos;
memmove(&timer_heap[i], &timer_heap[i+1], (j-i)*sizeof(int)); if (pos != old_pos)
memmove(&timer_heap[old_pos], &timer_heap[old_pos+1], (pos-old_pos)*sizeof(int));
} }
} else { //Timer is delayed, shift timer near the beginning of the heap. } else
if (i == 0) //Nothing to shift. {// Timer is delayed, shift timer near the beginning of the heap.
j = 0; if (old_pos == 0) //Nothing to shift.
pos = old_pos;
else { else {
for (j = i-1; j >= 0 && DIFF_TICK(timer_data[j].tick, tick) < 0; j--); HEAP_SEARCH(tick,0,old_pos-1,pos);
j++; ++pos;
memmove(&timer_heap[j+1], &timer_heap[j], (i-j)*sizeof(int)); if (pos != old_pos)
memmove(&timer_heap[pos+1], &timer_heap[pos], (old_pos-pos)*sizeof(int));
} }
} }
timer_heap[j] = tid; timer_heap[pos] = tid;
timer_data[tid].tick = tick; timer_data[tid].tick = tick;
return tick; return tick;
} }
@ -341,15 +369,16 @@ static void fix_timer_heap(unsigned int tick)
} }
//Move elements to readjust the heap. //Move elements to readjust the heap.
tmp_heap = aCalloc(sizeof(int), i); tmp_heap = aCalloc(sizeof(int), i);
memmove(&tmp_heap[0], &timer_heap[0], i*sizeof(int)); memcpy(tmp_heap, timer_heap, i*sizeof(int));
memmove(&timer_heap[0], &timer_heap[i], (timer_heap_num-i)*sizeof(int)); memmove(timer_heap, &timer_heap[i], (timer_heap_num-i)*sizeof(int));
memmove(&timer_heap[timer_heap_num-i], &tmp_heap[0], i*sizeof(int)); memmove(&timer_heap[timer_heap_num-i], tmp_heap, i*sizeof(int));
aFree(tmp_heap); aFree(tmp_heap);
} }
} }
int do_timer(unsigned int tick) int do_timer(unsigned int tick)
{ {
static int fix_heap_flag = 0; //Flag for fixing the stack only once per tick loop. May not be the best way, but it's all I can think of currently :X [Skotlex]
int i, nextmin = 1000; int i, nextmin = 1000;
if (tick < 0x010000 && fix_heap_flag) if (tick < 0x010000 && fix_heap_flag)
@ -362,8 +391,8 @@ int do_timer(unsigned int tick)
i = timer_heap[timer_heap_num - 1]; // next shorter element i = timer_heap[timer_heap_num - 1]; // next shorter element
if ((nextmin = DIFF_TICK(timer_data[i].tick, tick)) > 0) if ((nextmin = DIFF_TICK(timer_data[i].tick, tick)) > 0)
break; break;
if (timer_heap_num > 0) // suppress the actual element from the table
timer_heap_num--; --timer_heap_num; // suppress the actual element from the table
timer_data[i].type |= TIMER_REMOVE_HEAP; timer_data[i].type |= TIMER_REMOVE_HEAP;
if (timer_data[i].func) { if (timer_data[i].func) {
if (nextmin < -1000) { if (nextmin < -1000) {
@ -382,7 +411,7 @@ int do_timer(unsigned int tick)
timer_data[i].type = 0; timer_data[i].type = 0;
if (free_timer_list_pos >= free_timer_list_max) { if (free_timer_list_pos >= free_timer_list_max) {
free_timer_list_max += 256; free_timer_list_max += 256;
free_timer_list = (int *) aRealloc(free_timer_list, sizeof(int) * free_timer_list_max); RECREATE(free_timer_list,int,free_timer_list_max);
malloc_tsetdword(free_timer_list + (free_timer_list_max - 256), 0, 256 * sizeof(int)); malloc_tsetdword(free_timer_list + (free_timer_list_max - 256), 0, 256 * sizeof(int));
} }
free_timer_list[free_timer_list_pos++] = i; free_timer_list[free_timer_list_pos++] = i;
@ -403,7 +432,7 @@ int do_timer(unsigned int tick)
if (nextmin < TIMER_MIN_INTERVAL) if (nextmin < TIMER_MIN_INTERVAL)
nextmin = TIMER_MIN_INTERVAL; nextmin = TIMER_MIN_INTERVAL;
if ((unsigned int)(tick + nextmin) < tick) //Tick will loop, rearrange the heap on the next iteration. if (UINT_MAX - nextmin < tick) //Tick will loop, rearrange the heap on the next iteration.
fix_heap_flag = 1; fix_heap_flag = 1;
return nextmin; return nextmin;
} }
@ -420,17 +449,16 @@ void timer_init(void)
void timer_final(void) void timer_final(void)
{ {
struct timer_func_list* tfl = tfl_root, *tfl2; struct timer_func_list *tfl;
struct timer_func_list *next;
while (tfl) { for( tfl=tfl_root; tfl != NULL; tfl = next )
tfl2 = tfl->next; // copy next pointer next = tfl->next; // copy next pointer
aFree(tfl->name); // free structures aFree(tfl->name); // free structures
aFree(tfl); aFree(tfl);
tfl = tfl2; // use copied pointer for next cycle
} }
if (timer_data) aFree(timer_data); if (timer_data) aFree(timer_data);
if (timer_heap) aFree(timer_heap); if (timer_heap) aFree(timer_heap);
if (free_timer_list) aFree(free_timer_list); if (free_timer_list) aFree(free_timer_list);
} }

View File

@ -12,17 +12,19 @@
#define BASE_TICK 5 #define BASE_TICK 5
#define TIMER_ONCE_AUTODEL 1 #define TIMER_ONCE_AUTODEL 0x1
#define TIMER_INTERVAL 2 #define TIMER_INTERVAL 0x2
#define TIMER_REMOVE_HEAP 16 #define TIMER_REMOVE_HEAP 0x10
#define DIFF_TICK(a,b) ((int)((a)-(b))) #define DIFF_TICK(a,b) ((int)((a)-(b)))
// Struct declaration // Struct declaration
typedef int (*TimerFunc)(int,unsigned int,int,int);
struct TimerData { struct TimerData {
unsigned int tick; unsigned int tick;
int (*func)(int,unsigned int,int,int); TimerFunc func;
int id; int id;
int data; int data;
int type; int type;
@ -32,16 +34,12 @@ struct TimerData {
// Function prototype declaration // Function prototype declaration
#ifdef __WIN32
void gettimeofday(struct timeval *t, void *dummy);
#endif
unsigned int gettick_nocache(void); unsigned int gettick_nocache(void);
unsigned int gettick(void); unsigned int gettick(void);
int add_timer(unsigned int,int (*)(int,unsigned int,int,int),int,int); int add_timer(unsigned int,TimerFunc f,int,int);
int add_timer_interval(unsigned int,int (*)(int,unsigned int,int,int),int,int,int); int add_timer_interval(unsigned int,TimerFunc f,int,int,int);
int delete_timer(int,int (*)(int,unsigned int,int,int)); int delete_timer(int,TimerFunc f);
int addtick_timer(int tid,unsigned int tick); int addtick_timer(int tid,unsigned int tick);
int settick_timer(int tid,unsigned int tick); int settick_timer(int tid,unsigned int tick);
@ -49,8 +47,8 @@ struct TimerData *get_timer(int tid);
int do_timer(unsigned int tick); int do_timer(unsigned int tick);
int add_timer_func_list(int (*)(int,unsigned int,int,int),char*); int add_timer_func_list(TimerFunc func, char* name);
char* search_timer_func_list(int (*)(int,unsigned int,int,int)); char* search_timer_func_list(TimerFunc f);
unsigned long get_uptime(void); unsigned long get_uptime(void);

View File

@ -8450,10 +8450,10 @@ void clif_parse_LoadEndAck(int fd,struct map_session_data *sd)
// If player is dead, and is spawned (such as @refresh) send death packet. [Valaris] // If player is dead, and is spawned (such as @refresh) send death packet. [Valaris]
if(pc_isdead(sd)) if(pc_isdead(sd))
clif_clearchar_area(&sd->bl,1); clif_clearchar_area(&sd->bl,1);
// Uncomment if you want to make player face in the same direction he was facing right before warping. [Skotlex] // Uncomment if you want to make player face in the same direction he was facing right before warping. [Skotlex]
// else // else
// clif_changed_dir(&sd->bl, SELF); // clif_changed_dir(&sd->bl, SELF);
// Trigger skill effects if you appear standing on them // Trigger skill effects if you appear standing on them
if(!battle_config.pc_invincible_time) if(!battle_config.pc_invincible_time)
skill_unit_move(&sd->bl,gettick(),1); skill_unit_move(&sd->bl,gettick(),1);
@ -8472,7 +8472,8 @@ void clif_parse_TickSend(int fd, struct map_session_data *sd) {
WFIFOW(fd,0)=0x7f; WFIFOW(fd,0)=0x7f;
WFIFOL(fd,2)=gettick(); WFIFOL(fd,2)=gettick();
WFIFOSET(fd,packet_len(0x7f)); WFIFOSET(fd,packet_len(0x7f));
flush_fifo(fd,0); // send immediatly so the client gets accurate "pings" // removed until the socket problems are fixed. [FlavioJS]
//flush_fifo(fd,0); // send immediatly so the client gets accurate "pings"
return; return;
} }