improved _alpm_list_free handling

This commit is contained in:
Aurelien Foret 2006-02-22 20:49:33 +00:00
parent 78ffd98701
commit 16ff7cfa8e
13 changed files with 33 additions and 39 deletions

View file

@ -64,6 +64,7 @@ pmdb_t *_alpm_db_open(char *dbpath, char *treename, int mode)
if(db->dir == NULL) {
if(mode & DB_O_CREATE) {
_alpm_log(PM_LOG_WARNING, "could not open database '%s' -- try creating it", treename);
_alpm_log(PM_LOG_DEBUG, "creating database '%s'", treename);
if(mkdir(db->path, 0755) == 0) {
db->dir = opendir(db->path);
}
@ -83,8 +84,10 @@ pmdb_t *_alpm_db_open(char *dbpath, char *treename, int mode)
return(db);
}
void _alpm_db_close(pmdb_t *db)
void _alpm_db_close(void *data)
{
pmdb_t *db = data;
if(db == NULL) {
return;
}

View file

@ -48,7 +48,7 @@ typedef struct __pmdb_t {
} pmdb_t;
pmdb_t *_alpm_db_open(char *path, char *treename, int mode);
void _alpm_db_close(pmdb_t *db);
void _alpm_db_close(void *data);
void _alpm_db_rewind(pmdb_t *db);
pmpkg_t *_alpm_db_scan(pmdb_t *db, char *target, unsigned int inforeq);
int _alpm_db_read(pmdb_t *db, char *name, unsigned int inforeq, pmpkg_t *info);

View file

@ -42,8 +42,10 @@ pmgrp_t *_alpm_grp_new()
return(grp);
}
void _alpm_grp_free(pmgrp_t *grp)
void _alpm_grp_free(void *data)
{
pmgrp_t *grp = data;
if(grp == NULL) {
return;
}

View file

@ -33,16 +33,10 @@ typedef struct __pmgrp_t {
#define FREEGRP(p) do { if(p) { _alpm_grp_free(p); p = NULL; } } while(0)
#define FREELISTGRPS(p) do { \
PMList *i; \
for(i = p; i; i = i->next) { \
FREEGRP(i->data); \
} \
FREELIST(p); \
} while(0)
#define FREELISTGRPS(p) _FREELIST(p, _alpm_grp_free)
pmgrp_t *_alpm_grp_new(void);
void _alpm_grp_free(pmgrp_t *grp);
void _alpm_grp_free(void *data);
int _alpm_grp_cmp(const void *g1, const void *g2);
#endif /* _ALPM_GROUP_H */

View file

@ -42,13 +42,15 @@ PMList *_alpm_list_new()
return(list);
}
void _alpm_list_free(PMList *list)
void _alpm_list_free(PMList *list, _alpm_fn_free fn)
{
PMList *ptr, *it = list;
while(it) {
ptr = it->next;
free(it->data);
if(fn) {
fn(it->data);
}
free(it);
it = ptr;
}

View file

@ -31,20 +31,16 @@ typedef struct __pmlist_t {
typedef struct __pmlist_t PMList;
#define FREELIST(p) do { if(p) { _alpm_list_free(p); p = NULL; } } while(0)
#define FREELISTPTR(p) do { \
PMList *i; \
for(i = p; i; i = i->next) { \
i->data = NULL; \
} \
FREELIST(p); \
} while(0)
#define _FREELIST(p, f) do { if(p) { _alpm_list_free(p, f); p = NULL; } } while(0)
#define FREELIST(p) _FREELIST(p, free)
#define FREELISTPTR(p) _FREELIST(p, NULL)
typedef void (*_alpm_fn_free) (void *);
/* Sort comparison callback function declaration */
typedef int (*_alpm_fn_cmp) (const void *, const void *);
PMList *_alpm_list_new(void);
void _alpm_list_free(PMList *list);
void _alpm_list_free(PMList *list, _alpm_fn_free fn);
PMList *_alpm_list_add(PMList *list, void *data);
PMList *_alpm_list_add_sorted(PMList *list, void *data, _alpm_fn_cmp fn);
PMList *_alpm_list_remove(PMList *haystack, void *needle, _alpm_fn_cmp fn, void **data);

View file

@ -121,8 +121,10 @@ pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg)
return(newpkg);
}
void _alpm_pkg_free(pmpkg_t *pkg)
void _alpm_pkg_free(void *data)
{
pmpkg_t *pkg = data;
if(pkg == NULL) {
return;
}

View file

@ -75,20 +75,11 @@ do { \
} \
} while(0)
#define FREELISTPKGS(p) \
do { \
if(p) { \
PMList *i; \
for(i = p; i; i = i->next) { \
FREEPKG(i->data); \
}\
FREELIST(p);\
} \
} while(0)
#define FREELISTPKGS(p) _FREELIST(p, _alpm_pkg_free)
pmpkg_t* _alpm_pkg_new(const char *name, const char *version);
pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg);
void _alpm_pkg_free(pmpkg_t *pkg);
void _alpm_pkg_free(void *data);
pmpkg_t *_alpm_pkg_load(char *pkgfile);
pmpkg_t *_alpm_pkg_isin(char *needle, PMList *haystack);
int _alpm_pkg_splitname(char *target, char *name, char *version);

View file

@ -63,8 +63,10 @@ pmsyncpkg_t *_alpm_sync_new(int type, pmpkg_t *spkg, void *data)
return(sync);
}
void _alpm_sync_free(pmsyncpkg_t *sync)
void _alpm_sync_free(void *data)
{
pmsyncpkg_t *sync = data;
if(sync == NULL) {
return;
}

View file

@ -34,7 +34,7 @@ typedef struct __pmsyncpkg_t {
#define FREESYNC(p) do { if(p) { _alpm_sync_free(p); p = NULL; } } while(0)
pmsyncpkg_t *_alpm_sync_new(int type, pmpkg_t *spkg, void *data);
void _alpm_sync_free(pmsyncpkg_t *sync);
void _alpm_sync_free(void *data);
PMList *_alpm_sync_load_dbarchive(char *archive);

View file

@ -56,8 +56,10 @@ pmtrans_t *_alpm_trans_new()
return(trans);
}
void _alpm_trans_free(pmtrans_t *trans)
void _alpm_trans_free(void *data)
{
pmtrans_t *trans = data;
if(trans == NULL) {
return;
}

View file

@ -62,7 +62,7 @@ do { \
} while(0)
pmtrans_t *_alpm_trans_new(void);
void _alpm_trans_free(pmtrans_t *trans);
void _alpm_trans_free(void *data);
int _alpm_trans_init(pmtrans_t *trans, unsigned char type, unsigned char flags, alpm_trans_cb_event event, alpm_trans_cb_conv conv);
int _alpm_trans_sysupgrade(pmtrans_t *trans);
int _alpm_trans_addtarget(pmtrans_t *trans, char *target);

View file

@ -134,7 +134,7 @@ int main(int argc, char* argv[])
snprintf(line, PATH_MAX, "/bin/cp %s %s/install", path, topdir);
system(line);
}
_alpm_list_free(backup);
_alpm_list_free(backup, free);
}
umask(oldumask);
return(0);