1
0
mirror of https://github.com/moparisthebest/pacman synced 2025-01-08 12:28:00 -05:00

Improve depends string parsing

Remove the need for an unconditional string duplication by using pointer
arithmetic instead, and strndup() instead of an unspecified-length strdup().
This should reduce memory churn a fair amount as this is called pretty
frequently during database loads.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-01-19 12:00:39 -06:00
parent aff3e63c45
commit 01c8f39ab8

View File

@ -322,8 +322,6 @@ static int dep_vercmp(const char *version1, pmdepmod_t mod,
int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep) int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep)
{ {
alpm_list_t *i; alpm_list_t *i;
const char *pkgname = pkg->name;
const char *pkgversion = pkg->version;
int satisfy = 0; int satisfy = 0;
/* check (pkg->name, pkg->version) */ /* check (pkg->name, pkg->version) */
@ -331,8 +329,8 @@ int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep)
&& pkg->name_hash != dep->name_hash) { && pkg->name_hash != dep->name_hash) {
/* skip more expensive checks */ /* skip more expensive checks */
} else { } else {
satisfy = (strcmp(pkgname, dep->name) == 0 satisfy = (strcmp(pkg->name, dep->name) == 0
&& dep_vercmp(pkgversion, dep->mod, dep->version)); && dep_vercmp(pkg->version, dep->mod, dep->version));
if(satisfy) { if(satisfy) {
return(satisfy); return(satisfy);
} }
@ -366,55 +364,44 @@ int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep)
pmdepend_t *_alpm_splitdep(const char *depstring) pmdepend_t *_alpm_splitdep(const char *depstring)
{ {
pmdepend_t *depend; pmdepend_t *depend;
char *ptr = NULL; const char *ptr, *version = NULL;
char *newstr = NULL;
if(depstring == NULL) { if(depstring == NULL) {
return(NULL); return(NULL);
} }
STRDUP(newstr, depstring, RET_ERR(PM_ERR_MEMORY, NULL));
CALLOC(depend, 1, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL)); CALLOC(depend, 1, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL));
/* Find a version comparator if one exists. If it does, set the type and /* Find a version comparator if one exists. If it does, set the type and
* increment the ptr accordingly so we can copy the right strings. */ * increment the ptr accordingly so we can copy the right strings. */
if((ptr = strstr(newstr, ">="))) { if((ptr = strstr(depstring, ">="))) {
depend->mod = PM_DEP_MOD_GE; depend->mod = PM_DEP_MOD_GE;
*ptr = '\0'; version = ptr + 2;
ptr += 2; } else if((ptr = strstr(depstring, "<="))) {
} else if((ptr = strstr(newstr, "<="))) {
depend->mod = PM_DEP_MOD_LE; depend->mod = PM_DEP_MOD_LE;
*ptr = '\0'; version = ptr + 2;
ptr += 2; } else if((ptr = strstr(depstring, "="))) {
} else if((ptr = strstr(newstr, "="))) {
/* Note: we must do =,<,> checks after <=, >= checks */ /* Note: we must do =,<,> checks after <=, >= checks */
depend->mod = PM_DEP_MOD_EQ; depend->mod = PM_DEP_MOD_EQ;
*ptr = '\0'; version = ptr + 1;
ptr += 1; } else if((ptr = strstr(depstring, "<"))) {
} else if((ptr = strstr(newstr, "<"))) {
depend->mod = PM_DEP_MOD_LT; depend->mod = PM_DEP_MOD_LT;
*ptr = '\0'; version = ptr + 1;
ptr += 1; } else if((ptr = strstr(depstring, ">"))) {
} else if((ptr = strstr(newstr, ">"))) {
depend->mod = PM_DEP_MOD_GT; depend->mod = PM_DEP_MOD_GT;
*ptr = '\0'; version = ptr + 1;
ptr += 1;
} else { } else {
/* no version specified - copy the name and return it */ /* no version specified, leave version and ptr NULL */
depend->mod = PM_DEP_MOD_ANY; depend->mod = PM_DEP_MOD_ANY;
STRDUP(depend->name, newstr, RET_ERR(PM_ERR_MEMORY, NULL));
depend->name_hash = _alpm_hash_sdbm(depend->name);
depend->version = NULL;
free(newstr);
return(depend);
} }
/* if we get here, we have a version comparator, copy the right parts /* copy the right parts to the right places */
* to the right places */ STRNDUP(depend->name, depstring, ptr - depstring,
STRDUP(depend->name, newstr, RET_ERR(PM_ERR_MEMORY, NULL)); RET_ERR(PM_ERR_MEMORY, NULL));
depend->name_hash = _alpm_hash_sdbm(depend->name); depend->name_hash = _alpm_hash_sdbm(depend->name);
STRDUP(depend->version, ptr, RET_ERR(PM_ERR_MEMORY, NULL)); if(version) {
free(newstr); STRDUP(depend->version, version, RET_ERR(PM_ERR_MEMORY, NULL));
}
return(depend); return(depend);
} }