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>
This commit is contained in:
Denys Vlasenko 2012-01-06 16:24:56 +01:00
parent 2e55404bcf
commit acdb0041b3

View File

@ -23,8 +23,6 @@
/**********************************************************************/ /**********************************************************************/
/* Sizes for statically allocated buffers. */ /* Sizes for statically allocated buffers. */
/* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
* _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
#define PWD_BUFFER_SIZE 256 #define PWD_BUFFER_SIZE 256
#define GRP_BUFFER_SIZE 256 #define GRP_BUFFER_SIZE 256
@ -49,46 +47,24 @@ static int FAST_FUNC bb__parsespent(void *sp, char *line);
struct statics { struct statics {
/* Smaller things first */ /* Smaller things first */
struct passwd getpwuid_resultbuf; /* It's ok to use one buffer for getpwuid and getpwnam. Manpage says:
struct group getgrgid_resultbuf; * "The return value may point to a static area, and may be overwritten
struct passwd getpwnam_resultbuf; * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
struct group getgrnam_resultbuf; */
struct passwd getpw_resultbuf;
struct group getgr_resultbuf;
char getpwuid_buffer[PWD_BUFFER_SIZE]; char getpw_buffer[PWD_BUFFER_SIZE];
char getgrgid_buffer[GRP_BUFFER_SIZE]; char getgr_buffer[GRP_BUFFER_SIZE];
char getpwnam_buffer[PWD_BUFFER_SIZE];
char getgrnam_buffer[GRP_BUFFER_SIZE];
#if 0
struct passwd fgetpwent_resultbuf;
struct group fgetgrent_resultbuf;
struct spwd fgetspent_resultbuf;
char fgetpwent_buffer[PWD_BUFFER_SIZE];
char fgetgrent_buffer[GRP_BUFFER_SIZE];
char fgetspent_buffer[PWD_BUFFER_SIZE];
#endif
#if 0 //ENABLE_USE_BB_SHADOW #if 0 //ENABLE_USE_BB_SHADOW
struct spwd getspuid_resultbuf; struct spwd getsp_resultbuf;
struct spwd getspnam_resultbuf; char getsp_buffer[PWD_BUFFER_SIZE];
char getspuid_buffer[PWD_BUFFER_SIZE];
char getspnam_buffer[PWD_BUFFER_SIZE];
#endif #endif
// Not converted - too small to bother // Not converted - too small to bother
//pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
//FILE *pwf /*= NULL*/; //FILE *pwf /*= NULL*/;
//FILE *grf /*= NULL*/; //FILE *grf /*= NULL*/;
//FILE *spf /*= NULL*/; //FILE *spf /*= NULL*/;
#if 0
struct passwd getpwent_pwd;
struct group getgrent_gr;
char getpwent_line_buff[PWD_BUFFER_SIZE];
char getgrent_line_buff[GRP_BUFFER_SIZE];
#endif
#if 0 //ENABLE_USE_BB_SHADOW
struct spwd getspent_spwd;
struct spwd sgetspent_spwd;
char getspent_line_buff[PWD_BUFFER_SIZE];
char sgetspent_line_buff[PWD_BUFFER_SIZE];
#endif
}; };
static struct statics *ptr_to_statics; static struct statics *ptr_to_statics;
@ -182,22 +158,22 @@ int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
struct passwd *fgetpwent(FILE *stream) struct passwd *fgetpwent(FILE *stream)
{ {
struct statics *S; struct statics *S;
struct passwd *resultbuf = RESULTBUF(fgetpwent); struct passwd *resultbuf = RESULTBUF(getpw);
char *buffer = BUFFER(fgetpwent); char *buffer = BUFFER(getpw);
struct passwd *result; struct passwd *result;
fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result); fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
return result; return result;
} }
struct group *fgetgrent(FILE *stream) struct group *fgetgrent(FILE *stream)
{ {
struct statics *S; struct statics *S;
struct group *resultbuf = RESULTBUF(fgetgrent); struct group *resultbuf = RESULTBUF(getgr);
char *buffer = BUFFER(fgetgrent); char *buffer = BUFFER(getgr);
struct group *result; struct group *result;
fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result); fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
return result; return result;
} }
#endif #endif
@ -207,11 +183,11 @@ struct group *fgetgrent(FILE *stream)
struct spwd *fgetspent(FILE *stream) struct spwd *fgetspent(FILE *stream)
{ {
struct statics *S; struct statics *S;
struct spwd *resultbuf = RESULTBUF(fgetspent); struct spwd *resultbuf = RESULTBUF(getsp);
char *buffer = BUFFER(fgetspent); char *buffer = BUFFER(getsp);
struct spwd *result; struct spwd *result;
fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result); fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
return result; return result;
} }
#endif #endif
@ -299,11 +275,11 @@ int sgetspent_r(const char *string, struct spwd *result_buf,
struct passwd *getpwuid(uid_t uid) struct passwd *getpwuid(uid_t uid)
{ {
struct statics *S; struct statics *S;
struct passwd *resultbuf = RESULTBUF(getpwuid); struct passwd *resultbuf = RESULTBUF(getpw);
char *buffer = BUFFER(getpwuid); char *buffer = BUFFER(getpw);
struct passwd *result; struct passwd *result;
getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result); getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
return result; return result;
} }
@ -311,11 +287,11 @@ struct passwd *getpwuid(uid_t uid)
struct group *getgrgid(gid_t gid) struct group *getgrgid(gid_t gid)
{ {
struct statics *S; struct statics *S;
struct group *resultbuf = RESULTBUF(getgrgid); struct group *resultbuf = RESULTBUF(getgr);
char *buffer = BUFFER(getgrgid); char *buffer = BUFFER(getgr);
struct group *result; struct group *result;
getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result); getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
return result; return result;
} }
@ -346,11 +322,11 @@ int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
struct spwd *getspuid(uid_t uid) struct spwd *getspuid(uid_t uid)
{ {
struct statics *S; struct statics *S;
struct spwd *resultbuf = RESULTBUF(getspuid); struct spwd *resultbuf = RESULTBUF(getsp);
char *buffer = BUFFER(getspuid); char *buffer = BUFFER(getsp);
struct spwd *result; struct spwd *result;
getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result); getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
return result; return result;
} }
#endif #endif
@ -359,11 +335,11 @@ struct spwd *getspuid(uid_t uid)
struct passwd *getpwnam(const char *name) struct passwd *getpwnam(const char *name)
{ {
struct statics *S; struct statics *S;
struct passwd *resultbuf = RESULTBUF(getpwnam); struct passwd *resultbuf = RESULTBUF(getpw);
char *buffer = BUFFER(getpwnam); char *buffer = BUFFER(getpw);
struct passwd *result; struct passwd *result;
getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result); getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
return result; return result;
} }
@ -371,11 +347,11 @@ struct passwd *getpwnam(const char *name)
struct group *getgrnam(const char *name) struct group *getgrnam(const char *name)
{ {
struct statics *S; struct statics *S;
struct group *resultbuf = RESULTBUF(getgrnam); struct group *resultbuf = RESULTBUF(getgr);
char *buffer = BUFFER(getgrnam); char *buffer = BUFFER(getgr);
struct group *result; struct group *result;
getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result); getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
return result; return result;
} }
@ -383,11 +359,11 @@ struct group *getgrnam(const char *name)
struct spwd *getspnam(const char *name) struct spwd *getspnam(const char *name)
{ {
struct statics *S; struct statics *S;
struct spwd *resultbuf = RESULTBUF(getspnam); struct spwd *resultbuf = RESULTBUF(getsp);
char *buffer = BUFFER(getspnam); char *buffer = BUFFER(getsp);
struct spwd *result; struct spwd *result;
getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result); getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
return result; return result;
} }
#endif #endif