IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New An example of production code.
This little gem is currently in the shipping version of our product.
Its purpose is to clean out and remove one element in the array. It may be called as many times as the array has elements. Enjoy.

(I mangled it just a little to make the kind of work we do less obvious. I may be disclosing trade secrets here :) )



void
clear_poll_objects(int iObjToClear)
{

int i, ret_code;
struct pollObject *poll, *tmp_object, *sub_object;

Log(LOG_INFO, "MISC/DEL_POLLOBJ", "d", iObjToClear);

/* ------------------------------------------------------- */
/* walk through each target list to delete all pollObjects */
/* ------------------------------------------------------- */
for(i=iObjToClear; i<= iObjToClear; i++) {

\tpoll= listHeads[i].nextObject;

\twhile(poll!= (struct pollObject *) NULL) {


\t /* --------------------------- */
\t /* loop through all subObjects */
\t /* --------------------------- */
\t sub_object= poll;


\t poll= poll->nextObject; /* for the next iteration. Do not use
\t\t\t\t poll until the end of outer wile()! */


\t while(sub_object!= NULL) {

\t /* ----------------------------------------------- */
\t /* delete the shared part if there are no other references */
\t /* ----------------------------------------------- */
\t if(--sub_object->object->useCount == 0) {

\t\tif((ret_code= rm_shared(sub_object->object)) < 0) {

\t\t listHeads[i].stats.localErr++;
\t\t Log(LOG_ERROR, "ERRORS/FREE_FAIL", "s", "clear_poll_objects");
\t\t};
\t } else
\t\tNLog(LOG_DETAIL, "MISC/DEL_POLLOBJ_SKIP");

\t /* maybelas would delete the malloc'd values here if I did that*/

\t if(sub_object->subObject== NULL) {

\t /* clear out the lastRaw value */\t
\t clearUnivalue(sub_object->lastValue.raw,
\t\t\t sub_object->object->objectType);
\t }

\t /* delete this sub_object */
\t tmp_object= sub_object;
\t sub_object= sub_object->subObject;
\t (void) free(tmp_object);

\t }; /* end loop through sub objects */


};

};\t/* end loop through listHeads */

/* free the listHead struct */
/*free((char *)listHeads);*/


/* ----------------------------------------- */
/* reinitialize the various flags and counts */
/* except those that the cmd line overrides */
/* ----------------------------------------- */
/* for pertarget reconfig just subtract one from the list_count var */
if(list_count >= 0)list_count--;\t/* no valid lists */
/*clear and reallocate the array */
for(i=iObjToClear;i<max_poll_lists;i++)
{
memcpy(&listHeads[i],&listHeads[i+1],sizeof(listHeads[i+1]));
/* free(&listHeads[i+1]);*/
}

if(max_poll_lists > 0)max_poll_lists--;
listHeads = (pollListHead *) realloc(listHeads,sizeof(listHeads[0]) * max_poll_lists);
if(listHeads != NULL)
{
/* sort the new list */
qsort((void*)listHeads,list_count+1,sizeof(listHeads[0]),comparePollHeads);
/* do I really have to do this? there should be no changes at this point */
for(i=0;i<=list_count;i++){
iFoundInArray = AddToArray(listHeads[i].target.inSockAddr.sin_addr.s_addr);
listHeads[i].reqIdMask=(0x1000+ iFoundInArray)<<16;
}
}
else {
NLog(LOG_FATAL, "DSM/BORED");
exit(0);
/* printf("dsm: could not reallocate the listHeads array\\n");*/
}
}


New Maybe I'm Amazed
Or not. The *average* programmer is an imbecile.

I particularly like the loop over the index to get that single iteration. Very clever.
New I love the comment...
/* do I really have to do this? there should be no changes at this point */


Guilty as charged for writing similar. Then again, I was working on 25-year-old COBOL programs that had comments like "This bit doesn't really do anything, but it'll break if you remove it", so I felt I was just sticking to the established coding 'style' :-)

On and on and on and on,
and on and on and on goes John.
New Viva Tables, F arrays and linked lists
________________
oop.ismad.com
     An example of production code. - (Arkadiy) - (3)
         Maybe I'm Amazed - (tuberculosis) - (1)
             I love the comment... - (Meerkat)
         Viva Tables, F arrays and linked lists -NT - (tablizer)

If a tree falls in the forest and no one hears it, maybe that's where your kid should be practicing the trumpet.
43 ms