--- deliantra/server/common/object.C 2006/08/31 17:54:14 1.10 +++ deliantra/server/common/object.C 2006/09/03 00:18:40 1.11 @@ -1,6 +1,6 @@ /* * static char *rcsid_object_c = - * "$Id: object.C,v 1.10 2006/08/31 17:54:14 root Exp $"; + * "$Id: object.C,v 1.11 2006/09/03 00:18:40 root Exp $"; */ /* @@ -52,7 +52,7 @@ object *objects; /* Pointer to the list of used objects */ object *free_objects; /* Pointer to the list of unused objects */ -object *active_objects; /* List of active objects that need to be processed */ +object *active_objects; /* List of active objects that need to be processed */ short freearr_x[SIZEOFFREE]= {0,0,1,1,1,0,-1,-1,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1, @@ -278,7 +278,7 @@ /* * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump. - * Some error messages. + * Some error messages. * The result of the dump is stored in the static global errmsg array. */ @@ -381,12 +381,12 @@ */ object *find_object_name(const char *str) { - const char *name=add_string(str); + const char *name = shstr::find (str); object *op; for(op=objects;op!=NULL;op=op->next) - if(op->name==name) + if(&op->name == name) break; - free_string(name); + return op; } @@ -519,98 +519,69 @@ /* Zero the key_values on op, decrementing the shared-string * refcounts and freeing the links. */ -static void free_key_values(object * op) { - key_value * i; - key_value * next = NULL; - - if (op->key_values == NULL) return; - - for (i = op->key_values; i != NULL; i = next) { - /* Store next *first*. */ - next = i->next; - - if (i->key) FREE_AND_CLEAR_STR(i->key); - if (i->value) FREE_AND_CLEAR_STR(i->value); - i->next = NULL; - free(i); +static void free_key_values(object * op) +{ + for (key_value *i = op->key_values; i != 0; ) + { + key_value *next = i->next; + delete i; + i = next; } - - op->key_values = NULL; + + op->key_values = 0; } - /* * clear_object() frees everything allocated by an object, and also * clears all variables and flags to default settings. */ -void clear_object(object *op) { - op->clear (); - - event *evt; - event *evt2; - - /* redo this to be simpler/more efficient. Was also seeing - * crashes in the old code. Move this to the top - am - * seeing periodic crashes in this code, and would like to have - * as much info available as possible (eg, object name). - */ - for (evt = op->events; evt; evt=evt2) { - evt2 = evt->next; - - if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook); - if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin); - if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options); - - free(evt); - } - op->events = NULL; - - free_key_values(op); - - /* the memset will clear all these values for us, but we need - * to reduce the refcount on them. - */ - if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name); - if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl); - if (op->title != NULL) FREE_AND_CLEAR_STR(op->title); - if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race); - if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying); - if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill); - if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg); - if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore); - if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname); - +void clear_object(object *op) +{ + op->clear (); - memset((void*)op, 0, sizeof (object_special)); - /* Below here, we clear things that are not done by the memset, - * or set default values that are not zero. - */ - /* This is more or less true */ - SET_FLAG(op, FLAG_REMOVED); + free_key_values (op); + /* the memset will clear all these values for us, but we need + * to reduce the refcount on them. + */ + op->name = 0; + op->name_pl = 0; + op->title = 0; + op->race = 0; + op->slaying = 0; + op->skill = 0; + op->msg = 0; + op->lore = 0; + op->materialname = 0; + + memset((void*)op, 0, sizeof (object_special)); + /* Below here, we clear things that are not done by the memset, + * or set default values that are not zero. + */ + /* This is more or less true */ + SET_FLAG (op, FLAG_REMOVED); - op->contr = NULL; - op->below=NULL; - op->above=NULL; - op->inv=NULL; - op->events=NULL; - op->container=NULL; - op->env=NULL; - op->more=NULL; - op->head=NULL; - op->map=NULL; - op->refcount=0; - op->active_next = NULL; - op->active_prev = NULL; - /* What is not cleared is next, prev, and count */ + op->contr = NULL; + op->below = NULL; + op->above = NULL; + op->inv = NULL; + op->container=NULL; + op->env=NULL; + op->more=NULL; + op->head=NULL; + op->map=NULL; + op->refcount=0; + op->active_next = NULL; + op->active_prev = NULL; + /* What is not cleared is next, prev, and count */ - op->expmul=1.0; - op->face = blank_face; - op->attacked_by_count = (tag_t) -1; - if (settings.casting_time) - op->casting_time = -1; + op->expmul=1.0; + op->face = blank_face; + op->attacked_by_count = (tag_t) -1; + if (settings.casting_time) + op->casting_time = -1; } /* @@ -622,108 +593,53 @@ * will point at garbage. */ -void copy_object(object *op2, object *op) { - int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); - event *evt, *evt2, *evt_new; - - op->clear (); - - /* Decrement the refcounts, but don't bother zeroing the fields; - they'll be overwritten by memcpy. */ - if(op->name!=NULL) free_string(op->name); - if(op->name_pl!=NULL) free_string(op->name_pl); - if(op->title!=NULL) free_string(op->title); - if(op->race!=NULL) free_string(op->race); - if(op->slaying!=NULL) free_string(op->slaying); - if(op->skill!=NULL) free_string(op->skill); - if(op->msg!=NULL) free_string(op->msg); - if(op->lore!=NULL) free_string(op->lore); - if(op->materialname != NULL) free_string(op->materialname); - if(op->custom_name != NULL) free_string(op->custom_name); - - /* Basically, same code as from clear_object() */ - for (evt = op->events; evt; evt=evt2) { - evt2 = evt->next; - - if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook); - if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin); - if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options); - - free(evt); - } - op->events = NULL; - - free_key_values(op); - - *(object_special *)op = *(object_special *)op2; - op2->clone (op); - - if(is_freed) SET_FLAG(op,FLAG_FREED); - if(is_removed) SET_FLAG(op,FLAG_REMOVED); - if(op->name!=NULL) add_refcount(op->name); - if(op->name_pl!=NULL) add_refcount(op->name_pl); - if(op->title!=NULL) add_refcount(op->title); - if(op->race!=NULL) add_refcount(op->race); - if(op->slaying!=NULL) add_refcount(op->slaying); - if(op->skill!=NULL) add_refcount(op->skill); - if(op->lore!=NULL) add_refcount(op->lore); - if(op->msg!=NULL) add_refcount(op->msg); - if(op->custom_name!=NULL) add_refcount(op->custom_name); - if (op->materialname != NULL) add_refcount(op->materialname); - - if((op2->speed<0) && !editor) - op->speed_left=op2->speed_left-RANDOM()%200/100.0; - - /* Copy over event information */ - evt2 = NULL; - for (evt = op2->events; evt; evt=evt->next) { - evt_new = (event *) malloc(sizeof(event)); - memcpy(evt_new, evt, sizeof(event)); - if (evt_new->hook) add_refcount(evt_new->hook); - if (evt_new->plugin) add_refcount(evt_new->plugin); - if (evt_new->options) add_refcount(evt_new->options); - evt_new->next = NULL; - - /* Try to be a little clever here, and store away the - * last event we copied, so that its simpler to update the - * pointer. - */ - if (evt2) - evt2->next = evt_new; - else - op->events = evt_new; +void copy_object(object *op2, object *op) +{ + int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); - evt2 = evt_new; - } - /* Copy over key_values, if any. */ - if (op2->key_values != NULL) { - key_value * tail = NULL; - key_value * i; + op->clear (); - op->key_values = NULL; + free_key_values (op); - for (i = op2->key_values; i != NULL; i = i->next) { - key_value * new_link = (key_value *) malloc(sizeof(key_value)); + *(object_special *)op = *(object_special *)op2; + op2->clone (op); - new_link->next = NULL; - new_link->key = add_refcount(i->key); - if (i->value) - new_link->value = add_refcount(i->value); - else - new_link->value = NULL; + if (is_freed) SET_FLAG(op,FLAG_FREED); + if (is_removed) SET_FLAG(op,FLAG_REMOVED); + + if (op2->speed < 0) + op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0; + + /* Copy over key_values, if any. */ + if (op2->key_values != NULL) { + key_value *tail = NULL; + key_value *i; + + op->key_values = NULL; + + for (i = op2->key_values; i != NULL; i = i->next) + { + key_value *new_link = new key_value; - /* Try and be clever here, too. */ - if (op->key_values == NULL) { - op->key_values = new_link; - tail = new_link; - } else { - tail->next = new_link; - tail = new_link; + new_link->next = NULL; + new_link->key = i->key; + new_link->value = i->value; + + /* Try and be clever here, too. */ + if (op->key_values == NULL) + { + op->key_values = new_link; + tail = new_link; } - } - } + else + { + tail->next = new_link; + tail = new_link; + } + } + } - update_ob_speed(op); + update_ob_speed (op); } /* @@ -734,10 +650,8 @@ void expand_objects(void) { int i; object *obj; - obj = (object *) CALLOC(OBJ_EXPAND,sizeof(object)); + obj = new object [OBJ_EXPAND]; - if(obj==NULL) - fatal(OUT_OF_MEMORY); free_objects=obj; obj[0].prev=NULL; obj[0].next= &obj[1], @@ -765,29 +679,23 @@ * If there are no free objects, expand_objects() is called to get more. */ -object *get_object(void) { +object *get_object(void) +{ object *op; - if(free_objects==NULL) { + if(free_objects==NULL) expand_objects(); - } - op=free_objects; -#ifdef MEMORY_DEBUG - /* The idea is hopefully by doing a realloc, the memory - * debugging program will now use the current stack trace to - * report leaks. - */ - op = realloc(op, sizeof(object)); - SET_FLAG(op, FLAG_REMOVED); - SET_FLAG(op, FLAG_FREED); -#endif - if(!QUERY_FLAG(op,FLAG_FREED)) { + op = free_objects; + + if (!QUERY_FLAG (op, FLAG_FREED)) LOG(llevError,"Fatal: Getting busy object.\n"); - } - free_objects=op->next; - if(free_objects!=NULL) - free_objects->prev=NULL; + + free_objects = op->next; + + if (free_objects != NULL) + free_objects->prev = NULL; + op->count= ++ob_count; op->name=NULL; op->name_pl=NULL; @@ -838,7 +746,7 @@ */ if (QUERY_FLAG(op, FLAG_FREED) && op->speed) { - LOG(llevError,"Object %s is freed but has speed.\n", op->name); + LOG(llevError,"Object %s is freed but has speed.\n", &op->name); #ifdef MANY_CORES abort(); #else @@ -932,7 +840,7 @@ void update_object(object *op, int action) { int update_now=0, flags; - MoveType move_on, move_off, move_block, move_slow; + MoveType move_on, move_off, move_block, move_slow; if (op == NULL) { /* this should never happen */ @@ -1069,7 +977,7 @@ op=tmp; } } - else { /* Put objects in inventory onto this space */ + else { /* Put objects in inventory onto this space */ op=ob->inv; while(op!=NULL) { tmp=op->below; @@ -1105,49 +1013,35 @@ ob->next->prev=ob->prev; } - if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name); - if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl); - if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title); - if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race); - if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying); - if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill); - if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore); - if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg); - if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname); + ob->name = 0; + ob->name_pl = 0; + ob->title = 0; + ob->race = 0; + ob->slaying = 0; + ob->skill = 0; + ob->lore = 0; + ob->msg = 0; + ob->materialname = 0; - - /* Why aren't events freed? */ - free_key_values(ob); - -#if 0 /* MEMORY_DEBUG*/ - /* This is a nice idea. Unfortunately, a lot of the code in crossfire - * presumes the freed_object will stick around for at least a little - * bit - */ - /* this is necessary so that memory debugging programs will - * be able to accurately report source of malloc. If we recycle - * objects, then some other area may be doing the get_object - * and not freeing it, but the original one that malloc'd the - * object will get the blame. - */ - free(ob); -#else + free_key_values (ob); /* Now link it with the free_objects list: */ ob->prev=NULL; ob->next=free_objects; + if(free_objects!=NULL) free_objects->prev=ob; + free_objects=ob; nroffreeobjects++; -#endif } /* * count_free() returns the number of objects on the list of free objects. */ -int count_free(void) { +int count_free () +{ int i=0; object *tmp=free_objects; while(tmp!=NULL) @@ -1159,7 +1053,8 @@ * count_used() returns the number of objects on the list of used objects. */ -int count_used(void) { +int count_used() +{ int i=0; object *tmp=objects; while(tmp!=NULL) @@ -1171,7 +1066,8 @@ * count_active() returns the number of objects on the list of active objects. */ -int count_active(void) { +int count_active() +{ int i=0; object *tmp=active_objects; while(tmp!=NULL) @@ -1349,7 +1245,7 @@ move_apply(tmp, op, NULL); if (was_destroyed (op, tag)) { LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed " - "leaving object\n", tmp->name, tmp->arch->name); + "leaving object\n", &tmp->name, &tmp->arch->name); } } @@ -1584,7 +1480,7 @@ */ /* Have object 'fall below' other objects that block view. - * Unless those objects are exits, type 66 + * Unless those objects are exits, type 66 * If INS_ON_TOP is used, don't do this processing * Need to find the object that in fact blocks view, otherwise * stacking is a bit odd. @@ -1593,7 +1489,7 @@ (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) && (op->face && !op->face->visibility)) { for (last=top; last != floor; last=last->below) - if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; + if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break; /* Check to see if we found the object that blocks view, * and make sure we have a below pointer for it so that * we can get inserted below this one, which requires we @@ -1632,10 +1528,10 @@ * it, so save a few ticks and start from there. */ if (!(flag & INS_MAP_LOAD)) - for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { + for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) { if (tmp->type == PLAYER) - tmp->contr->socket.update_look=1; - } + tmp->contr->socket.update_look=1; + } /* If this object glows, it may affect lighting conditions that are * visible to others on this map. But update_all_los is really @@ -1717,7 +1613,7 @@ if(orig_ob->nrofnrof?orig_ob->nrof:1, orig_ob->name); + orig_ob->nrof?orig_ob->nrof:1, &orig_ob->name); return NULL; } newob = object_create_clone(orig_ob); @@ -1874,7 +1770,7 @@ } if (op->more) { LOG(llevError, "Tried to insert multipart object %s (%d)\n", - op->name, op->count); + &op->name, op->count); return op; } CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL); @@ -1970,7 +1866,7 @@ tag_t tag; mapstruct *m=op->map; int x=op->x, y=op->y; - MoveType move_on, move_slow, move_block; + MoveType move_on, move_slow, move_block; if(QUERY_FLAG(op,FLAG_NO_APPLY)) return 0; @@ -2298,7 +2194,7 @@ sint16 nx, ny; object *tmp; mapstruct *mp; - MoveType blocked, move_type; + MoveType blocked, move_type; if (exclude && exclude->head) { exclude = exclude->head; @@ -2602,7 +2498,7 @@ object_thawer thawer (filename); if (thawer) load_object(thawer,op,LO_NEWFILE,0); - LOG(llevDebug," load str completed, object=%s\n",op->name); + LOG(llevDebug," load str completed, object=%s\n", &op->name); CLEAR_FLAG(op,FLAG_REMOVED); return op; @@ -2651,7 +2547,7 @@ key_value * link; const char * canonical_key; - canonical_key = find_string(key); + canonical_key = shstr::find (key); if (canonical_key == NULL) { /* 1. There being a field named key on any object @@ -2684,7 +2580,7 @@ * * Returns TRUE on success. */ -int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) { +int set_ob_key_value_s(object * op, const shstr &canonical_key, const char * value, int add_key) { key_value * field = NULL, *last=NULL; for (field=op->key_values; field != NULL; field=field->next) { @@ -2693,25 +2589,23 @@ continue; } - if (field->value) FREE_AND_CLEAR_STR(field->value); if (value) - field->value = add_string(value); + field->value = value; else { /* Basically, if the archetype has this key set, * we need to store the null value so when we save * it, we save the empty value so that when we load, * we get this value back again. */ - if (get_ob_key_link(&op->arch->clone, canonical_key)) - field->value = NULL; - else { - /* Delete this link */ - if (field->key) FREE_AND_CLEAR_STR(field->key); - if (field->value) FREE_AND_CLEAR_STR(field->value); + if (get_ob_key_link (&op->arch->clone, canonical_key)) + field->value = 0; + else + { if (last) last->next = field->next; else op->key_values = field->next; - free(field); - } + + delete field; + } } return TRUE; } @@ -2730,10 +2624,10 @@ */ if (value == NULL) return TRUE; - field = (key_value *) malloc(sizeof(key_value)); + field = new key_value; - field->key = add_refcount(canonical_key); - field->value = add_string(value); + field->key = canonical_key; + field->value = value; /* Usual prepend-addition. */ field->next = op->key_values; op->key_values = field; @@ -2750,26 +2644,8 @@ * * Returns TRUE on success. */ -int set_ob_key_value(object * op, const char * key, const char * value, int add_key) { - const char * canonical_key = NULL; - int floating_ref = FALSE; - int ret; - - /* HACK This mess is to make sure set_ob_value() passes a shared string - * to get_ob_key_link(), without leaving a leaked refcount. - */ - - canonical_key = find_string(key); - if (canonical_key == NULL) { - canonical_key = add_string(key); - floating_ref = TRUE; - } - - ret = set_ob_key_value_s(op, canonical_key, value, add_key); - - if (floating_ref) { - free_string(canonical_key); - } - - return ret; +int set_ob_key_value(object * op, const char *key, const char * value, int add_key) +{ + shstr key_ (key); + return set_ob_key_value_s (op, key_, value, add_key); }