Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15712 Modified Files: array.h lex.l list.c list.h lpc.h main.c net.c net.h object.c object.h sys.c sys.h vars.c Log Message: Cleanups. Index: net.h =================================================================== RCS file: /cvsroot/agd/server/src/net.h,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- net.h 1 Apr 2004 19:10:53 -0000 1.11 +++ net.h 8 Jun 2004 20:44:53 -0000 1.12 @@ -24,11 +24,10 @@ int socket; struct sockaddr_in addr; char recvbuf[NET_RECEIVEBUF]; -/* enum { DISCONNECTED = -1, DEFAULT, CONNECTED } state;*/ time_t last_active; } player_connection_t; -typedef struct player_t { +typedef struct player { player_connection_t conn; object_t *ob; @@ -36,8 +35,6 @@ int input_to_called; } player_t; -/*void net_accept(player_t *p);*/ - void net_loop(void); void net_listen(unsigned short port); void net_send(char *buf, int siz, player_t *p); Index: sys.c =================================================================== RCS file: /cvsroot/agd/server/src/sys.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- sys.c 7 Jun 2004 15:45:51 -0000 1.14 +++ sys.c 8 Jun 2004 20:44:53 -0000 1.15 @@ -17,13 +17,10 @@ #endif #include <dirent.h> -#include "sys.h" #include "compile_options.h" +#include "sys.h" config_t conf; -/* -static mstats_t mstats; -*/ #define CONFCHECK(x, y) if((x) == 0) { \ fprintf(stderr, "\""y": \" directive not found in config file.\n"); \ @@ -44,9 +41,6 @@ fprintf(stderr, "Can't open configuration file! (%s)\n", strerror(errno)); exit(5); } -/* - conf.exit_stats = 0; -*/ while(fscanf(f, "%s ", buf) != EOF) { line++; @@ -71,12 +65,7 @@ conf.error_log = stringdup(buf); continue; } -/* - if(strncmp(buf, "exitstats:", 11) == 0) { - fscanf(f, "%d\n", &conf.exit_stats); - continue; - } -*/ + if(strncmp(buf, "master:", 8) == 0) { fscanf(f, "%s\n", buf); conf.master = stringdup(buf); @@ -101,64 +90,16 @@ CONFCHECK(conf.master, "master"); } -#if 0 -char *bytes_string(int bytes) -{ - static char buf[256]; - int scale = 0; - float result = bytes; - - char c = 0; - int prec = 3; - - for(scale = 0; scale < 2; scale++) { - if(result >= 1024.0) { - result /= 1024.0; - } else break; - } - - if(result != 1.0) c = 's'; - if(floor(result) == result) prec = 0; - - switch(scale) { - case 0: sprintf(buf, "%d byte%c", bytes, c); break; - case 1: sprintf(buf, "%.*f kilobyte%c", prec, result, c); break; - case 2: sprintf(buf, "%.*f megabyte%c", prec, result, c); break; - } - - return buf; -} - -void mstats_summary(void) -{ - int avg; - - printf("Memory allocation statistics:\n"); - printf("----------------------------------------------------------\n"); - printf(" Total malloc() calls: %d\n", mstats.alloc); - printf(" Total free() calls: %d (%d on nullpointers)\n", mstats.free, mstats.free_on_null); - printf(" Total bytes allocated: %s\n", bytes_string(mstats.total_alloced)); - if(mstats.alloc != 0) - avg = mstats.total_alloced / mstats.alloc; - else - avg = 0; - printf(" Average bytes per call: %s\n", bytes_string(avg)); - printf("----------------------------------------------------------\n"); -} -#endif - void *xmalloc(size_t bytes) { void *p; p = malloc(bytes); -/* mstats.alloc++;*/ if(!p) { fprintf(stderr, "Out of memory!\n"); exit(1); /* Or crash()? */ } -/* mstats.total_alloced += bytes;*/ return p; } @@ -176,19 +117,16 @@ } #ifdef DEBUG + /* Quite useless. */ void xfree(void *p) { -/* mstats.free++;*/ if(!p) { fprintf(stderr, "free() called on a nullpointer!\n"); -/* mstats.free_on_null++;*/ return; } free(p); } -#else -#define xfree(p) free(p) #endif int legal_path(char *fn) @@ -229,4 +167,3 @@ s = strcat(s, path); return s; } - Index: array.h =================================================================== RCS file: /cvsroot/agd/server/src/array.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- array.h 7 Jun 2004 15:35:52 -0000 1.7 +++ array.h 8 Jun 2004 20:44:53 -0000 1.8 @@ -1,7 +1,7 @@ #ifndef _ARRAY_H #define _ARRAY_H -typedef struct array_t { +typedef struct array { int length; int ref; variable_t **data; Index: list.c =================================================================== RCS file: /cvsroot/agd/server/src/list.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- list.c 7 Jun 2004 15:42:56 -0000 1.7 +++ list.c 8 Jun 2004 20:44:53 -0000 1.8 @@ -2,6 +2,13 @@ list.c - a generic linked list data structure Based on (the old) array.c -solicit 06.12.03 + + XXX - this could be a lot faster if we wouldn't + start from the beginning of the chain to + find an empty slot or then push it into the end, + but just set new element as root, and former + root as ->next in chain - i.e. reverse the order. + Look into it. */ #include <stdio.h> #include "sys.h" Index: sys.h =================================================================== RCS file: /cvsroot/agd/server/src/sys.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- sys.h 7 Jun 2004 15:45:51 -0000 1.9 +++ sys.h 8 Jun 2004 20:44:53 -0000 1.10 @@ -2,28 +2,18 @@ #define _SYS_H /* memory allocation */ -/*#define type_xmalloc(type) (type *) xmalloc(sizeof(type))*/ void *xmalloc(size_t bytes); void *xrealloc(void *ptr, size_t bytes); +#ifdef DEBUG void xfree(void *p); - -#if 0 -/* memory stats */ -void mstats_summary(void); - -typedef struct { - int alloc; - int free, free_on_null; - int total_alloced; -} mstats_t; +#else +#define xfree free #endif + /* configuration */ typedef struct { int port, heartbeat; -/* - int exit_stats; -*/ char *lib_root, *error_log; char *master; int debuglevel; Index: net.c =================================================================== RCS file: /cvsroot/agd/server/src/net.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- net.c 7 Jun 2004 15:44:17 -0000 1.15 +++ net.c 8 Jun 2004 20:44:53 -0000 1.16 @@ -7,8 +7,9 @@ #include <string.h> #include <errno.h> -#include "sys.h" +#include "config.h" #include "compile_options.h" +#include "sys.h" #include "list.h" #include "lpc.h" #include "object.h" @@ -26,7 +27,7 @@ static int greatest_sockfd, listenfd; static fd_set readfds, writefds; -void do_write(char *s); +extern void do_write(char *s); void net_listen(unsigned short port) { @@ -99,7 +100,7 @@ this_player = p; player_ob = xmalloc(sizeof(object_t)); player_ob->iaob = p; - player_ob->name = NULL; + player_ob->name = "NONE"; /* So find_object() won't barf. */ player_ob->prog = NULL; this_ob = p->ob = player_ob; @@ -115,12 +116,11 @@ return; } - p->ob = login_ob->u.ob; - this_ob = p->ob /*login_ob->u.ob*/; + this_ob = p->ob = login_ob->u.ob; p->ob->iaob = p; apply(login_ob->u.ob, "logon", NULL); - if(this_player == p && p->ob == player_ob) + if(this_player == p && p->ob == player_ob) /* wasn't changed. */ return; debug("net_accept", "player object was changed to %p; freeing player_ob %p\n", p->ob, player_ob); @@ -203,7 +203,6 @@ /* the login object will have to restore this object's iaob */ pl->ob->iaob = NULL; } -/* pl->conn.state = DISCONNECTED;*/ break; default: net_read(pl); @@ -220,7 +219,7 @@ if(!to_be_dested.data) return; - for(p = /*&all_objects*/ &to_be_dested; p; p=p->next) { + for(p = &to_be_dested; p; p=p->next) { object_t *ob = p->data; if(ob->flags & O_DESTRUCTED) actual_destruct(ob); @@ -241,7 +240,7 @@ FD_SET(listenfd, &readfds); LOOP_PLAYERS() - if(/*pl->conn.state != DISCONNECTED*/pl->ob) { + if(pl->ob) { FD_SET(pl->conn.socket, &readfds); /*FD_SET(pl->conn.socket, &writefds);*/ } Index: list.h =================================================================== RCS file: /cvsroot/agd/server/src/list.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- list.h 1 Feb 2004 13:12:30 -0000 1.4 +++ list.h 8 Jun 2004 20:44:53 -0000 1.5 @@ -1,9 +1,9 @@ #ifndef _LIST_H #define _LIST_H -typedef struct list_s { +typedef struct list { void *data; - struct list_s *next; + struct list *next; } list_t; void list_push(list_t *l, void *data); Index: object.c =================================================================== RCS file: /cvsroot/agd/server/src/object.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- object.c 7 Jun 2004 15:45:02 -0000 1.14 +++ object.c 8 Jun 2004 20:44:53 -0000 1.15 @@ -2,10 +2,10 @@ #include "config.h" */ #include <stdlib.h> -/*#include "arch.h"*/ -#include "sys.h" #include "compile_options.h" +#include "sys.h" +/*#include "arch.h"*/ #include "list.h" #include "lpc.h" #include "object.h" @@ -13,7 +13,7 @@ #include "vars.h" list_t all_objects, - to_be_dested; /* Objects which have the destructed flag + to_be_dested; /* Objects which have the destructed flag set, but are still in memory. This is so that destruct_all() in net.c has less objects to traverse. */ @@ -23,7 +23,7 @@ player_t *this_player; object_t *this_ob, *previous_ob; -program_t *compile_prog(char *path); +extern program_t *compile_prog(char *path); void free_fun(void *p) { @@ -77,11 +77,20 @@ void actual_destruct(object_t *ob) { + int i; + /* list_remove(&all_objects, ob);*/ unref_prog(&ob->prog); - free_array(&ob->globals, (void (*)(void*))free_var); + for(i=0;i<ob->numglobals;i++) { + free_var(&ob->globals[i]); + } + if(ob->globals) + xfree(ob->globals); + +/* free_array(&ob->globals, (void (*)(void*)) free_var); */ /* WTF? */ + if(ob->name) xfree(ob->name); @@ -116,14 +125,29 @@ return; p->ref--; + if(p->ref < 0) + printf("p[%p]->ref < 0!\n", p); if(p->ref <= 0) { int i; for(i=0;i<p->num_str;i++) xfree(p->str_tbl[i]); xfree(p->str_tbl); + for(i=0;i<p->numfunc;i++) { + free_fun(p->functions[i]); + xfree(p->fun_table[i]); + } + if(p->numfunc) { + xfree(p->functions); + xfree(p->fun_table); + } + +/* Again: WTF? */ +/* free_array(&p->functions, free_fun); free_array(&p->fun_table, NULL); +*/ + xfree(p); *prog = NULL; } @@ -138,10 +162,12 @@ for(p = &all_objects; p; p = p->next) { object_t *ob = p->data; - /* I'd rather not have the check for ob->name here. - * It's only here because of net.c, which creates a temporary - * ob with NULL name. */ - if(ob->name && strcmp(path, ob->name) == 0) { +#ifdef DEBUG + if(!ob->name) { + printf("Warning! Object[%p] doesn't have a name!\n", ob); + } else +#endif + if(strcmp(path, ob->name) == 0) { return ob; } } Index: lpc.h =================================================================== RCS file: /cvsroot/agd/server/src/lpc.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- lpc.h 7 Jun 2004 15:43:11 -0000 1.9 +++ lpc.h 8 Jun 2004 20:44:53 -0000 1.10 @@ -21,7 +21,7 @@ #define ST_MALLOC 1 #define ST_STATIC 2 -typedef struct variable_t { +typedef struct variable { unsigned char type; char *name; char string_type; @@ -32,9 +32,9 @@ unsigned char c; #endif char *s; - struct array_t *a; - struct object_t *ob; - struct variable_t *v; /* T_LVALUE */ + struct array *a; + struct object *ob; + struct variable *v; /* T_LVALUE */ } u; } variable_t; Index: lex.l =================================================================== RCS file: /cvsroot/agd/server/src/lex.l,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- lex.l 7 Jun 2004 15:42:38 -0000 1.15 +++ lex.l 8 Jun 2004 20:44:53 -0000 1.16 @@ -21,12 +21,11 @@ #ifdef DEBUG #define RET(type, value, tok) \ - pos += yyleng; \ if(conf.debuglevel > 3) \ printf("yylex(): %"#type"\n", value); \ return tok #else -#define RET(type, value, tok) pos += yyleng; return tok +#define RET(type, value, tok) return tok #endif #define NEWLINE() pos = 1; line++ @@ -246,7 +245,7 @@ yylval.str = str; RET(s, str, L_IDENTIFIER); } -" " pos++; +" " /*pos++;*/ \t pos += 8; \r { if(!warned_newlines) { Index: object.h =================================================================== RCS file: /cvsroot/agd/server/src/object.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- object.h 7 Jun 2004 15:45:20 -0000 1.9 +++ object.h 8 Jun 2004 20:44:53 -0000 1.10 @@ -5,9 +5,9 @@ int type; int *args; - int numarg; + int num_arg; - long int *code; + int *code; int codelen; unsigned int lineno; @@ -16,7 +16,7 @@ typedef struct { function_t **functions; /* This shouldn't be an array to pointers. */ int numfunc; - char **fun_table; /* This should be a hash table. */ + char **fun_table; /* This could be a hash table. */ int num_str; char **str_tbl; @@ -24,7 +24,7 @@ int ref; } program_t; -typedef struct object_t { +typedef struct object { char *name; program_t *prog; @@ -35,11 +35,11 @@ list_t refs; int flags; - struct object_t *environment, *parent; - struct object_t **inv; + struct object *environment, *parent; + struct object **inv; /* The interactive player associated with this object. */ - struct player_t *iaob; + struct player *iaob; } object_t; #define O_DESTRUCTED 0x1 Index: main.c =================================================================== RCS file: /cvsroot/agd/server/src/main.c,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- main.c 7 Jun 2004 15:43:32 -0000 1.19 +++ main.c 8 Jun 2004 20:44:53 -0000 1.20 @@ -181,7 +181,7 @@ parse_config(conf_file); check_logfile(); dfuns_init(); - init_interpreter(); +/* init_interpreter();*/ master = load_object(conf.master); if(!master) @@ -189,7 +189,7 @@ master->numref++; net_listen(conf.port); - + time(&startup_time); printf("Started at %s", ctime(&startup_time)); srand(startup_time); Index: vars.c =================================================================== RCS file: /cvsroot/agd/server/src/vars.c,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- vars.c 7 Jun 2004 15:46:15 -0000 1.13 +++ vars.c 8 Jun 2004 20:44:53 -0000 1.14 @@ -135,9 +135,9 @@ ret->u.i = v1->u.i + v2->u.i; return ret; case T_STRING: - buf = xmalloc(256); /* legit */ + buf = xmalloc(256); sprintf(buf, "%d", v1->u.i); - buf2 = xmalloc(strlen(v2->u.s) + strlen(buf) + 1); /* legit */ + buf2 = xmalloc(strlen(v2->u.s) + strlen(buf) + 1); xfree(buf); sprintf(buf2, "%d%s", v1->u.i, v2->u.s); ret->type = T_STRING; @@ -157,7 +157,7 @@ } else if(!v2->u.s) buf = stringdup(v1->u.s); else { - buf = xmalloc(strlen(v1->u.s) + strlen(v2->u.s) + 1); /* legit */ + buf = xmalloc(strlen(v1->u.s) + strlen(v2->u.s) + 1); sprintf(buf, "%s%s", v1->u.s, v2->u.s); } ret->type = T_STRING; @@ -165,9 +165,9 @@ ret->string_type = ST_MALLOC; return ret; case T_INT: - buf = xmalloc(256); /* legit */ + buf = xmalloc(256); sprintf(buf, "%d", v2->u.i); - buf2 = xmalloc(strlen(v1->u.s) + strlen(buf) + 1); /* legit */ + buf2 = xmalloc(strlen(v1->u.s) + strlen(buf) + 1); xfree(buf); sprintf(buf2, "%s%d", v1->u.s, v2->u.i); ret->type = T_STRING; @@ -177,7 +177,7 @@ case T_OBJECT: /* check for v2->u.ob */ ret->type = T_STRING; - ret->u.s = xmalloc(strlen(v1->u.s) + strlen(v2->u.ob->name) + 1); /* legit */ + ret->u.s = xmalloc(strlen(v1->u.s) + strlen(v2->u.ob->name) + 1); sprintf(ret->u.s, "%s%s", v1->u.s, v2->u.ob->name); ret->string_type = ST_MALLOC; return ret; @@ -193,7 +193,7 @@ ret->u.s = "<NULL>"; ret->string_type = ST_STATIC; } else { - ret->u.s = xmalloc(strlen(v1->u.ob->name) + strlen(v2->u.s) + 1); /* legit */ + ret->u.s = xmalloc(strlen(v1->u.ob->name) + strlen(v2->u.s) + 1); sprintf(ret->u.s, "%s%s", v1->u.ob->name, v2->u.s); ret->string_type = ST_MALLOC; } @@ -276,4 +276,3 @@ return "unknown"; } } - |