aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-10-01 21:52:16 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2020-10-01 21:52:16 +0200
commit689d0650ab63425adaea26afe347015a204958ee (patch)
tree9c2e10b952cad6804359bc10ccc963ff138fc3d4 /libbb
parent3c3928fc658bddacd4e540589868bc28a8e4ca0a (diff)
downloadbusybox-w32-689d0650ab63425adaea26afe347015a204958ee.tar.gz
busybox-w32-689d0650ab63425adaea26afe347015a204958ee.tar.bz2
busybox-w32-689d0650ab63425adaea26afe347015a204958ee.zip
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>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/recursive_action.c72
1 files changed, 43 insertions, 29 deletions
diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c
index 0831ecc3a..b1c4bfad7 100644
--- a/libbb/recursive_action.c
+++ b/libbb/recursive_action.c
@@ -21,10 +21,9 @@
21 * is so stinking huge. 21 * is so stinking huge.
22 */ 22 */
23 23
24static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM, 24static int FAST_FUNC true_action(struct recursive_state *state UNUSED_PARAM,
25 struct stat *statbuf UNUSED_PARAM, 25 const char *fileName UNUSED_PARAM,
26 void* userData UNUSED_PARAM, 26 struct stat *statbuf UNUSED_PARAM)
27 int depth UNUSED_PARAM)
28{ 27{
29 return TRUE; 28 return TRUE;
30} 29}
@@ -65,12 +64,7 @@ static int FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
65 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir. 64 * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
66 */ 65 */
67 66
68int FAST_FUNC recursive_action(const char *fileName, 67static int recursive_action1(recursive_state_t *state, const char *fileName)
69 unsigned flags,
70 int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
71 int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
72 void* userData,
73 unsigned depth)
74{ 68{
75 struct stat statbuf; 69 struct stat statbuf;
76 unsigned follow; 70 unsigned follow;
@@ -78,24 +72,21 @@ int FAST_FUNC recursive_action(const char *fileName,
78 DIR *dir; 72 DIR *dir;
79 struct dirent *next; 73 struct dirent *next;
80 74
81 if (!fileAction) fileAction = true_action;
82 if (!dirAction) dirAction = true_action;
83
84 follow = ACTION_FOLLOWLINKS; 75 follow = ACTION_FOLLOWLINKS;
85 if (depth == 0) 76 if (state->depth == 0)
86 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0; 77 follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
87 follow &= flags; 78 follow &= state->flags;
88 status = (follow ? stat : lstat)(fileName, &statbuf); 79 status = (follow ? stat : lstat)(fileName, &statbuf);
89 if (status < 0) { 80 if (status < 0) {
90#ifdef DEBUG_RECURS_ACTION 81#ifdef DEBUG_RECURS_ACTION
91 bb_error_msg("status=%d flags=%x", status, flags); 82 bb_error_msg("status=%d flags=%x", status, state->flags);
92#endif 83#endif
93 if ((flags & ACTION_DANGLING_OK) 84 if ((state->flags & ACTION_DANGLING_OK)
94 && errno == ENOENT 85 && errno == ENOENT
95 && lstat(fileName, &statbuf) == 0 86 && lstat(fileName, &statbuf) == 0
96 ) { 87 ) {
97 /* Dangling link */ 88 /* Dangling link */
98 return fileAction(fileName, &statbuf, userData, depth); 89 return state->fileAction(state, fileName, &statbuf);
99 } 90 }
100 goto done_nak_warn; 91 goto done_nak_warn;
101 } 92 }
@@ -103,20 +94,20 @@ int FAST_FUNC recursive_action(const char *fileName,
103 /* If S_ISLNK(m), then we know that !S_ISDIR(m). 94 /* If S_ISLNK(m), then we know that !S_ISDIR(m).
104 * Then we can skip checking first part: if it is true, then 95 * Then we can skip checking first part: if it is true, then
105 * (!dir) is also true! */ 96 * (!dir) is also true! */
106 if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */ 97 if ( /* (!(state->flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
107 !S_ISDIR(statbuf.st_mode) 98 !S_ISDIR(statbuf.st_mode)
108 ) { 99 ) {
109 return fileAction(fileName, &statbuf, userData, depth); 100 return state->fileAction(state, fileName, &statbuf);
110 } 101 }
111 102
112 /* It's a directory (or a link to one, and followLinks is set) */ 103 /* It's a directory (or a link to one, and followLinks is set) */
113 104
114 if (!(flags & ACTION_RECURSE)) { 105 if (!(state->flags & ACTION_RECURSE)) {
115 return dirAction(fileName, &statbuf, userData, depth); 106 return state->dirAction(state, fileName, &statbuf);
116 } 107 }
117 108
118 if (!(flags & ACTION_DEPTHFIRST)) { 109 if (!(state->flags & ACTION_DEPTHFIRST)) {
119 status = dirAction(fileName, &statbuf, userData, depth); 110 status = state->dirAction(state, fileName, &statbuf);
120 if (status == FALSE) 111 if (status == FALSE)
121 goto done_nak_warn; 112 goto done_nak_warn;
122 if (status == SKIP) 113 if (status == SKIP)
@@ -140,11 +131,13 @@ int FAST_FUNC recursive_action(const char *fileName,
140 continue; 131 continue;
141 132
142 /* process every file (NB: ACTION_RECURSE is set in flags) */ 133 /* process every file (NB: ACTION_RECURSE is set in flags) */
143 s = recursive_action(nextFile, flags, fileAction, dirAction, 134 state->depth++;
144 userData, depth + 1); 135 s = recursive_action1(state, nextFile);
145 if (s == FALSE) 136 if (s == FALSE)
146 status = FALSE; 137 status = FALSE;
147 free(nextFile); 138 free(nextFile);
139 state->depth--;
140
148//#define RECURSE_RESULT_ABORT -1 141//#define RECURSE_RESULT_ABORT -1
149// if (s == RECURSE_RESULT_ABORT) { 142// if (s == RECURSE_RESULT_ABORT) {
150// closedir(dir); 143// closedir(dir);
@@ -153,15 +146,36 @@ int FAST_FUNC recursive_action(const char *fileName,
153 } 146 }
154 closedir(dir); 147 closedir(dir);
155 148
156 if (flags & ACTION_DEPTHFIRST) { 149 if (state->flags & ACTION_DEPTHFIRST) {
157 if (!dirAction(fileName, &statbuf, userData, depth)) 150 if (!state->dirAction(state, fileName, &statbuf))
158 goto done_nak_warn; 151 goto done_nak_warn;
159 } 152 }
160 153
161 return status; 154 return status;
162 155
163 done_nak_warn: 156 done_nak_warn:
164 if (!(flags & ACTION_QUIET)) 157 if (!(state->flags & ACTION_QUIET))
165 bb_simple_perror_msg(fileName); 158 bb_simple_perror_msg(fileName);
166 return FALSE; 159 return FALSE;
167} 160}
161
162int FAST_FUNC recursive_action(const char *fileName,
163 unsigned flags,
164 int FAST_FUNC (*fileAction)(struct recursive_state *state, const char *fileName, struct stat* statbuf),
165 int FAST_FUNC (*dirAction)(struct recursive_state *state, const char *fileName, struct stat* statbuf),
166 void *userData)
167{
168 /* Keeping a part of variables of recusive descent in a "state structure"
169 * instead of passing ALL of them down as parameters of recursive_action1()
170 * relieves register pressure, both in recursive_action1()
171 * and in every file/dirAction().
172 */
173 recursive_state_t state;
174 state.flags = flags;
175 state.depth = 0;
176 state.userData = userData;
177 state.fileAction = fileAction ? fileAction : true_action;
178 state.dirAction = dirAction ? dirAction : true_action;
179
180 return recursive_action1(&state, fileName);
181}