libbb: shrink recursive_action() by reducing memory pressure
function old new delta recursive_action1 - 316 +316 file_action_grep 161 164 +3 add_to_prg_cache_if_socket 202 205 +3 depmod_main 509 511 +2 writeFileToTarball 488 489 +1 parse_module 281 282 +1 fileAction 207 208 +1 act 189 190 +1 add_to_dirlist 65 64 -1 writeTarFile 196 194 -2 uuidcache_init 47 45 -2 uuidcache_check_device 109 107 -2 true_action 8 6 -2 run_parts_main 310 308 -2 netstat_main 534 532 -2 lsusb_main 29 27 -2 lspci_main 45 43 -2 initial_scan 138 136 -2 grep_main 845 843 -2 find_main 482 480 -2 config_file_action 437 435 -2 chmod_main 142 140 -2 dirAction 14 10 -4 diff_main 1544 1540 -4 chown_main 154 148 -6 skip_dir 136 129 -7 dir_act 191 184 -7 recursive_action 453 69 -384 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 7/20 up/down: 328/-439) Total: -111 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
@@ -21,10 +21,9 @@
|
||||
* is so stinking huge.
|
||||
*/
|
||||
|
||||
static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
|
||||
struct stat *statbuf UNUSED_PARAM,
|
||||
void* userData UNUSED_PARAM,
|
||||
int depth UNUSED_PARAM)
|
||||
static int FAST_FUNC true_action(struct recursive_state *state UNUSED_PARAM,
|
||||
const char *fileName UNUSED_PARAM,
|
||||
struct stat *statbuf UNUSED_PARAM)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
@@ -65,12 +64,7 @@ static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
|
||||
* 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
|
||||
*/
|
||||
|
||||
int FAST_FUNC recursive_action(const char *fileName,
|
||||
unsigned flags,
|
||||
int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
|
||||
int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
|
||||
void* userData,
|
||||
unsigned depth)
|
||||
static int recursive_action1(recursive_state_t *state, const char *fileName)
|
||||
{
|
||||
struct stat statbuf;
|
||||
unsigned follow;
|
||||
@@ -78,24 +72,21 @@ int FAST_FUNC recursive_action(const char *fileName,
|
||||
DIR *dir;
|
||||
struct dirent *next;
|
||||
|
||||
if (!fileAction) fileAction = true_action;
|
||||
if (!dirAction) dirAction = true_action;
|
||||
|
||||
follow = ACTION_FOLLOWLINKS;
|
||||
if (depth == 0)
|
||||
if (state->depth == 0)
|
||||
follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
|
||||
follow &= flags;
|
||||
follow &= state->flags;
|
||||
status = (follow ? stat : lstat)(fileName, &statbuf);
|
||||
if (status < 0) {
|
||||
#ifdef DEBUG_RECURS_ACTION
|
||||
bb_error_msg("status=%d flags=%x", status, flags);
|
||||
bb_error_msg("status=%d flags=%x", status, state->flags);
|
||||
#endif
|
||||
if ((flags & ACTION_DANGLING_OK)
|
||||
if ((state->flags & ACTION_DANGLING_OK)
|
||||
&& errno == ENOENT
|
||||
&& lstat(fileName, &statbuf) == 0
|
||||
) {
|
||||
/* Dangling link */
|
||||
return fileAction(fileName, &statbuf, userData, depth);
|
||||
return state->fileAction(state, fileName, &statbuf);
|
||||
}
|
||||
goto done_nak_warn;
|
||||
}
|
||||
@@ -103,20 +94,20 @@ int FAST_FUNC recursive_action(const char *fileName,
|
||||
/* If S_ISLNK(m), then we know that !S_ISDIR(m).
|
||||
* Then we can skip checking first part: if it is true, then
|
||||
* (!dir) is also true! */
|
||||
if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
|
||||
if ( /* (!(state->flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
|
||||
!S_ISDIR(statbuf.st_mode)
|
||||
) {
|
||||
return fileAction(fileName, &statbuf, userData, depth);
|
||||
return state->fileAction(state, fileName, &statbuf);
|
||||
}
|
||||
|
||||
/* It's a directory (or a link to one, and followLinks is set) */
|
||||
|
||||
if (!(flags & ACTION_RECURSE)) {
|
||||
return dirAction(fileName, &statbuf, userData, depth);
|
||||
if (!(state->flags & ACTION_RECURSE)) {
|
||||
return state->dirAction(state, fileName, &statbuf);
|
||||
}
|
||||
|
||||
if (!(flags & ACTION_DEPTHFIRST)) {
|
||||
status = dirAction(fileName, &statbuf, userData, depth);
|
||||
if (!(state->flags & ACTION_DEPTHFIRST)) {
|
||||
status = state->dirAction(state, fileName, &statbuf);
|
||||
if (status == FALSE)
|
||||
goto done_nak_warn;
|
||||
if (status == SKIP)
|
||||
@@ -140,11 +131,13 @@ int FAST_FUNC recursive_action(const char *fileName,
|
||||
continue;
|
||||
|
||||
/* process every file (NB: ACTION_RECURSE is set in flags) */
|
||||
s = recursive_action(nextFile, flags, fileAction, dirAction,
|
||||
userData, depth + 1);
|
||||
state->depth++;
|
||||
s = recursive_action1(state, nextFile);
|
||||
if (s == FALSE)
|
||||
status = FALSE;
|
||||
free(nextFile);
|
||||
state->depth--;
|
||||
|
||||
//#define RECURSE_RESULT_ABORT -1
|
||||
// if (s == RECURSE_RESULT_ABORT) {
|
||||
// closedir(dir);
|
||||
@@ -153,15 +146,36 @@ int FAST_FUNC recursive_action(const char *fileName,
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
if (flags & ACTION_DEPTHFIRST) {
|
||||
if (!dirAction(fileName, &statbuf, userData, depth))
|
||||
if (state->flags & ACTION_DEPTHFIRST) {
|
||||
if (!state->dirAction(state, fileName, &statbuf))
|
||||
goto done_nak_warn;
|
||||
}
|
||||
|
||||
return status;
|
||||
|
||||
done_nak_warn:
|
||||
if (!(flags & ACTION_QUIET))
|
||||
if (!(state->flags & ACTION_QUIET))
|
||||
bb_simple_perror_msg(fileName);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int FAST_FUNC recursive_action(const char *fileName,
|
||||
unsigned flags,
|
||||
int FAST_FUNC (*fileAction)(struct recursive_state *state, const char *fileName, struct stat* statbuf),
|
||||
int FAST_FUNC (*dirAction)(struct recursive_state *state, const char *fileName, struct stat* statbuf),
|
||||
void *userData)
|
||||
{
|
||||
/* Keeping a part of variables of recusive descent in a "state structure"
|
||||
* instead of passing ALL of them down as parameters of recursive_action1()
|
||||
* relieves register pressure, both in recursive_action1()
|
||||
* and in every file/dirAction().
|
||||
*/
|
||||
recursive_state_t state;
|
||||
state.flags = flags;
|
||||
state.depth = 0;
|
||||
state.userData = userData;
|
||||
state.fileAction = fileAction ? fileAction : true_action;
|
||||
state.dirAction = dirAction ? dirAction : true_action;
|
||||
|
||||
return recursive_action1(&state, fileName);
|
||||
}
|
||||
|
Reference in New Issue
Block a user