From 01c8f39ab8db36ad7ac1c2078a8c1a5b7504192a Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 19 Jan 2011 12:00:39 -0600 Subject: 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 --- lib/libalpm/deps.c | 53 ++++++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) (limited to 'lib/libalpm/deps.c') diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index 3d4b1df4..a7203b4b 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -322,8 +322,6 @@ static int dep_vercmp(const char *version1, pmdepmod_t mod, int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep) { alpm_list_t *i; - const char *pkgname = pkg->name; - const char *pkgversion = pkg->version; int satisfy = 0; /* check (pkg->name, pkg->version) */ @@ -331,8 +329,8 @@ int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep) && pkg->name_hash != dep->name_hash) { /* skip more expensive checks */ } else { - satisfy = (strcmp(pkgname, dep->name) == 0 - && dep_vercmp(pkgversion, dep->mod, dep->version)); + satisfy = (strcmp(pkg->name, dep->name) == 0 + && dep_vercmp(pkg->version, dep->mod, dep->version)); if(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 *depend; - char *ptr = NULL; - char *newstr = NULL; + const char *ptr, *version = NULL; if(depstring == NULL) { return(NULL); } - STRDUP(newstr, depstring, 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 * 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; - *ptr = '\0'; - ptr += 2; - } else if((ptr = strstr(newstr, "<="))) { + version = ptr + 2; + } else if((ptr = strstr(depstring, "<="))) { depend->mod = PM_DEP_MOD_LE; - *ptr = '\0'; - ptr += 2; - } else if((ptr = strstr(newstr, "="))) { + version = ptr + 2; + } else if((ptr = strstr(depstring, "="))) { /* Note: we must do =,<,> checks after <=, >= checks */ depend->mod = PM_DEP_MOD_EQ; - *ptr = '\0'; - ptr += 1; - } else if((ptr = strstr(newstr, "<"))) { + version = ptr + 1; + } else if((ptr = strstr(depstring, "<"))) { depend->mod = PM_DEP_MOD_LT; - *ptr = '\0'; - ptr += 1; - } else if((ptr = strstr(newstr, ">"))) { + version = ptr + 1; + } else if((ptr = strstr(depstring, ">"))) { depend->mod = PM_DEP_MOD_GT; - *ptr = '\0'; - ptr += 1; + version = ptr + 1; } else { - /* no version specified - copy the name and return it */ + /* no version specified, leave version and ptr NULL */ 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 - * to the right places */ - STRDUP(depend->name, newstr, RET_ERR(PM_ERR_MEMORY, NULL)); + /* copy the right parts to the right places */ + STRNDUP(depend->name, depstring, ptr - depstring, + RET_ERR(PM_ERR_MEMORY, NULL)); depend->name_hash = _alpm_hash_sdbm(depend->name); - STRDUP(depend->version, ptr, RET_ERR(PM_ERR_MEMORY, NULL)); - free(newstr); + if(version) { + STRDUP(depend->version, version, RET_ERR(PM_ERR_MEMORY, NULL)); + } return(depend); } -- cgit v1.2.3-24-g4f1b