Re: [Modulos-devel] Gotos?
Status: Pre-Alpha
Brought to you by:
luizshigunov
From: Luiz H. S. <shi...@in...> - 2002-01-17 11:49:41
|
On Fri, 18 Jan 2002, Stephen Caldwell wrote: > Do you think it's wise to use gotos so much? 99.99% C/C++ of the programming community would say that gotos are bad. You're right. But I use gotos to generate better code. Usually I use gotos when an error occurs and something must be done to undo what was done. Like this: if (MemManager_AllocObj(mapCache, (void**)&map)) return NULL; TaskManager_P(&task->mapSem); /* get address if not FIXED */ if (!(prop & UserModManager_FIXED_ADDR)) { start = GetUnmappedArea(task, start, len, align); if (!start) goto errorFreeMap; } else { /* free old mappings */ if (DestroyMapping(task, start, len)) { start = NULL; errorFreeMap: MemManager_FreeObj(mapCache, map); goto errorV; } } ... ... errorV: TaskManager_V(&task->mapSem, 0); return start; } Without gotos: if (MemManager_AllocObj(mapCache, (void**)&map)) return NULL; TaskManager_P(&task->mapSem); /* get address if not FIXED */ if (!(prop & UserModManager_FIXED_ADDR)) { start = GetUnmappedArea(task, start, len, align); if (!start) { MemManager_FreeObj(mapCache, map); TaskManager_V(&task->mapSem, 0); return NULL; } } else { /* free old mappings */ if (DestroyMapping(task, start, len)) { MemManager_FreeObj(mapCache, map); TaskManager_V(&task->mapSem, 0); return NULL; } } ... ... TaskManager_V(&task->mapSem, 0); return start; } You can see a lot of duplicate code. But the compiler can't! So I have to use gotos to generate smaller code. That's a common practice. Look Linux, FreeBSD, etc, etc. If you see some code that can be changed to eliminate a goto without making the code bigger. Please, tell me. I'll be glad to change it. See you, Luiz Henrique Shigunov (shi...@in...) ICQ: 103562572 Visit ModulOS: http://modulos.sourceforge.net/ Personal Page: http://www.inf.ufsc.br/~shigunov/ |