aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2012-01-06 16:24:56 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2012-01-06 16:24:56 +0100
commitacdb0041b35e27cd03c43b25410eda5151acd2c1 (patch)
tree31fa4b96f803502d869fd9433881af2bc3216513
parent2e55404bcf76f05193803b3d35359c6024f2a5f8 (diff)
downloadbusybox-w32-acdb0041b35e27cd03c43b25410eda5151acd2c1.tar.gz
busybox-w32-acdb0041b35e27cd03c43b25410eda5151acd2c1.tar.bz2
busybox-w32-acdb0041b35e27cd03c43b25410eda5151acd2c1.zip
libpwdgrp/pwd_grp.c: use same static buffer for all getpwXX functions
This should save more than 0.5k of malloced memory in applets which use those functions. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libpwdgrp/pwd_grp.c100
1 files changed, 38 insertions, 62 deletions
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c
index 23abdbec0..edf53f350 100644
--- a/libpwdgrp/pwd_grp.c
+++ b/libpwdgrp/pwd_grp.c
@@ -23,8 +23,6 @@
23/**********************************************************************/ 23/**********************************************************************/
24/* Sizes for statically allocated buffers. */ 24/* Sizes for statically allocated buffers. */
25 25
26/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
27 * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
28#define PWD_BUFFER_SIZE 256 26#define PWD_BUFFER_SIZE 256
29#define GRP_BUFFER_SIZE 256 27#define GRP_BUFFER_SIZE 256
30 28
@@ -49,46 +47,24 @@ static int FAST_FUNC bb__parsespent(void *sp, char *line);
49 47
50struct statics { 48struct statics {
51 /* Smaller things first */ 49 /* Smaller things first */
52 struct passwd getpwuid_resultbuf; 50 /* It's ok to use one buffer for getpwuid and getpwnam. Manpage says:
53 struct group getgrgid_resultbuf; 51 * "The return value may point to a static area, and may be overwritten
54 struct passwd getpwnam_resultbuf; 52 * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
55 struct group getgrnam_resultbuf; 53 */
56 54 struct passwd getpw_resultbuf;
57 char getpwuid_buffer[PWD_BUFFER_SIZE]; 55 struct group getgr_resultbuf;
58 char getgrgid_buffer[GRP_BUFFER_SIZE]; 56
59 char getpwnam_buffer[PWD_BUFFER_SIZE]; 57 char getpw_buffer[PWD_BUFFER_SIZE];
60 char getgrnam_buffer[GRP_BUFFER_SIZE]; 58 char getgr_buffer[GRP_BUFFER_SIZE];
61#if 0
62 struct passwd fgetpwent_resultbuf;
63 struct group fgetgrent_resultbuf;
64 struct spwd fgetspent_resultbuf;
65 char fgetpwent_buffer[PWD_BUFFER_SIZE];
66 char fgetgrent_buffer[GRP_BUFFER_SIZE];
67 char fgetspent_buffer[PWD_BUFFER_SIZE];
68#endif
69#if 0 //ENABLE_USE_BB_SHADOW 59#if 0 //ENABLE_USE_BB_SHADOW
70 struct spwd getspuid_resultbuf; 60 struct spwd getsp_resultbuf;
71 struct spwd getspnam_resultbuf; 61 char getsp_buffer[PWD_BUFFER_SIZE];
72 char getspuid_buffer[PWD_BUFFER_SIZE];
73 char getspnam_buffer[PWD_BUFFER_SIZE];
74#endif 62#endif
75// Not converted - too small to bother 63// Not converted - too small to bother
76//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; 64//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
77//FILE *pwf /*= NULL*/; 65//FILE *pwf /*= NULL*/;
78//FILE *grf /*= NULL*/; 66//FILE *grf /*= NULL*/;
79//FILE *spf /*= NULL*/; 67//FILE *spf /*= NULL*/;
80#if 0
81 struct passwd getpwent_pwd;
82 struct group getgrent_gr;
83 char getpwent_line_buff[PWD_BUFFER_SIZE];
84 char getgrent_line_buff[GRP_BUFFER_SIZE];
85#endif
86#if 0 //ENABLE_USE_BB_SHADOW
87 struct spwd getspent_spwd;
88 struct spwd sgetspent_spwd;
89 char getspent_line_buff[PWD_BUFFER_SIZE];
90 char sgetspent_line_buff[PWD_BUFFER_SIZE];
91#endif
92}; 68};
93 69
94static struct statics *ptr_to_statics; 70static struct statics *ptr_to_statics;
@@ -182,22 +158,22 @@ int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
182struct passwd *fgetpwent(FILE *stream) 158struct passwd *fgetpwent(FILE *stream)
183{ 159{
184 struct statics *S; 160 struct statics *S;
185 struct passwd *resultbuf = RESULTBUF(fgetpwent); 161 struct passwd *resultbuf = RESULTBUF(getpw);
186 char *buffer = BUFFER(fgetpwent); 162 char *buffer = BUFFER(getpw);
187 struct passwd *result; 163 struct passwd *result;
188 164
189 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result); 165 fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
190 return result; 166 return result;
191} 167}
192 168
193struct group *fgetgrent(FILE *stream) 169struct group *fgetgrent(FILE *stream)
194{ 170{
195 struct statics *S; 171 struct statics *S;
196 struct group *resultbuf = RESULTBUF(fgetgrent); 172 struct group *resultbuf = RESULTBUF(getgr);
197 char *buffer = BUFFER(fgetgrent); 173 char *buffer = BUFFER(getgr);
198 struct group *result; 174 struct group *result;
199 175
200 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result); 176 fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
201 return result; 177 return result;
202} 178}
203#endif 179#endif
@@ -207,11 +183,11 @@ struct group *fgetgrent(FILE *stream)
207struct spwd *fgetspent(FILE *stream) 183struct spwd *fgetspent(FILE *stream)
208{ 184{
209 struct statics *S; 185 struct statics *S;
210 struct spwd *resultbuf = RESULTBUF(fgetspent); 186 struct spwd *resultbuf = RESULTBUF(getsp);
211 char *buffer = BUFFER(fgetspent); 187 char *buffer = BUFFER(getsp);
212 struct spwd *result; 188 struct spwd *result;
213 189
214 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result); 190 fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
215 return result; 191 return result;
216} 192}
217#endif 193#endif
@@ -299,11 +275,11 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
299struct passwd *getpwuid(uid_t uid) 275struct passwd *getpwuid(uid_t uid)
300{ 276{
301 struct statics *S; 277 struct statics *S;
302 struct passwd *resultbuf = RESULTBUF(getpwuid); 278 struct passwd *resultbuf = RESULTBUF(getpw);
303 char *buffer = BUFFER(getpwuid); 279 char *buffer = BUFFER(getpw);
304 struct passwd *result; 280 struct passwd *result;
305 281
306 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result); 282 getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
307 return result; 283 return result;
308} 284}
309 285
@@ -311,11 +287,11 @@ struct passwd *getpwuid(uid_t uid)
311struct group *getgrgid(gid_t gid) 287struct group *getgrgid(gid_t gid)
312{ 288{
313 struct statics *S; 289 struct statics *S;
314 struct group *resultbuf = RESULTBUF(getgrgid); 290 struct group *resultbuf = RESULTBUF(getgr);
315 char *buffer = BUFFER(getgrgid); 291 char *buffer = BUFFER(getgr);
316 struct group *result; 292 struct group *result;
317 293
318 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result); 294 getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
319 return result; 295 return result;
320} 296}
321 297
@@ -346,11 +322,11 @@ int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
346struct spwd *getspuid(uid_t uid) 322struct spwd *getspuid(uid_t uid)
347{ 323{
348 struct statics *S; 324 struct statics *S;
349 struct spwd *resultbuf = RESULTBUF(getspuid); 325 struct spwd *resultbuf = RESULTBUF(getsp);
350 char *buffer = BUFFER(getspuid); 326 char *buffer = BUFFER(getsp);
351 struct spwd *result; 327 struct spwd *result;
352 328
353 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result); 329 getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
354 return result; 330 return result;
355} 331}
356#endif 332#endif
@@ -359,11 +335,11 @@ struct spwd *getspuid(uid_t uid)
359struct passwd *getpwnam(const char *name) 335struct passwd *getpwnam(const char *name)
360{ 336{
361 struct statics *S; 337 struct statics *S;
362 struct passwd *resultbuf = RESULTBUF(getpwnam); 338 struct passwd *resultbuf = RESULTBUF(getpw);
363 char *buffer = BUFFER(getpwnam); 339 char *buffer = BUFFER(getpw);
364 struct passwd *result; 340 struct passwd *result;
365 341
366 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result); 342 getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
367 return result; 343 return result;
368} 344}
369 345
@@ -371,11 +347,11 @@ struct passwd *getpwnam(const char *name)
371struct group *getgrnam(const char *name) 347struct group *getgrnam(const char *name)
372{ 348{
373 struct statics *S; 349 struct statics *S;
374 struct group *resultbuf = RESULTBUF(getgrnam); 350 struct group *resultbuf = RESULTBUF(getgr);
375 char *buffer = BUFFER(getgrnam); 351 char *buffer = BUFFER(getgr);
376 struct group *result; 352 struct group *result;
377 353
378 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result); 354 getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
379 return result; 355 return result;
380} 356}
381 357
@@ -383,11 +359,11 @@ struct group *getgrnam(const char *name)
383struct spwd *getspnam(const char *name) 359struct spwd *getspnam(const char *name)
384{ 360{
385 struct statics *S; 361 struct statics *S;
386 struct spwd *resultbuf = RESULTBUF(getspnam); 362 struct spwd *resultbuf = RESULTBUF(getsp);
387 char *buffer = BUFFER(getspnam); 363 char *buffer = BUFFER(getsp);
388 struct spwd *result; 364 struct spwd *result;
389 365
390 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result); 366 getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
391 return result; 367 return result;
392} 368}
393#endif 369#endif