diff options
author | Dan McGee <dan@archlinux.org> | 2011-01-19 19:20:32 +0100 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-01-21 16:14:47 +0100 |
commit | c86ff120c870a2d0e0427567ae7449e23ad42954 (patch) | |
tree | f1d4d4e178f65854192efd4698bffc5c5627428a /lib/libalpm/util.c | |
parent | 01c8f39ab8db36ad7ac1c2078a8c1a5b7504192a (diff) | |
download | pacman-c86ff120c870a2d0e0427567ae7449e23ad42954.tar.gz pacman-c86ff120c870a2d0e0427567ae7449e23ad42954.tar.xz |
Improve splitname memory allocation
We don't need to create a temporary copy of the string if we are smart with
our pointer manipulation and string copying. This saves a bunch of string
duplication during database parsing, both local and sync.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/util.c')
-rw-r--r-- | lib/libalpm/util.c | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 81d950e8..089b37cb 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -864,25 +864,23 @@ int _alpm_splitname(const char *target, pmpkg_t *pkg) * package name can contain hyphens, so parse from the back- go back * two hyphens and we have split the version from the name. */ - char *tmp, *p, *q; + const char *version, *end; if(target == NULL || pkg == NULL) { return(-1); } - STRDUP(tmp, target, RET_ERR(PM_ERR_MEMORY, -1)); - p = tmp + strlen(tmp); + end = target + strlen(target); /* remove any trailing '/' */ - while (*(p - 1) == '/') { - --p; - *p = '\0'; + while (*(end - 1) == '/') { + --end; } /* do the magic parsing- find the beginning of the version string * by doing two iterations of same loop to lop off two hyphens */ - for(q = --p; *q && *q != '-'; q--); - for(p = --q; *p && *p != '-'; p--); - if(*p != '-' || p == tmp) { + for(version = end - 1; *version && *version != '-'; version--); + for(version = version - 1; *version && *version != '-'; version--); + if(*version != '-' || version == target) { return(-1); } @@ -890,16 +888,17 @@ int _alpm_splitname(const char *target, pmpkg_t *pkg) if(pkg->version) { FREE(pkg->version); } - STRDUP(pkg->version, p+1, RET_ERR(PM_ERR_MEMORY, -1)); - /* insert a terminator at the end of the name (on hyphen)- then copy it */ - *p = '\0'; + /* version actually points to the dash, so need to increment 1 and account + * for potential end character */ + STRNDUP(pkg->version, version + 1, end - version - 1, + RET_ERR(PM_ERR_MEMORY, -1)); + if(pkg->name) { FREE(pkg->name); } - STRDUP(pkg->name, tmp, RET_ERR(PM_ERR_MEMORY, -1)); + STRNDUP(pkg->name, target, version - target, RET_ERR(PM_ERR_MEMORY, -1)); pkg->name_hash = _alpm_hash_sdbm(pkg->name); - free(tmp); return(0); } |