diff options
author | ticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358> | 2008-12-15 00:30:29 (EST) |
---|---|---|
committer | ticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358> | 2008-12-15 00:30:29 (EST) |
commit | 733d8409723a397b4ced39ea7aaab8bc8b4af5d9 (patch) | |
tree | 7138fbfc11577cf7d0fbae0ae7fa98a02ef63a8e | |
parent | 1cefade73444d4670d9ae7c06e8f9cc55492fd79 (diff) |
opkg: adding the hash_table_remove API, not using yet.
Just complete the API for future usage.
Clean all the entry at initial time. This reduces planty of unnecessary check.
In order to prevent this kind of bug, using calloc to replace most malloc
git-svn-id: http://opkg.googlecode.com/svn/trunk@160 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358
-rw-r--r-- | libopkg/file_util.c | 2 | ||||
-rw-r--r-- | libopkg/hash_table.c | 33 | ||||
-rw-r--r-- | libopkg/hash_table.h | 1 | ||||
-rw-r--r-- | libopkg/nv_pair_list.c | 2 | ||||
-rw-r--r-- | libopkg/opkg.c | 9 | ||||
-rw-r--r-- | libopkg/opkg_cmd.c | 6 | ||||
-rw-r--r-- | libopkg/opkg_conf.c | 4 | ||||
-rw-r--r-- | libopkg/opkg_install.c | 2 | ||||
-rw-r--r-- | libopkg/opkg_remove.c | 4 | ||||
-rw-r--r-- | libopkg/opkg_utils.c | 6 | ||||
-rw-r--r-- | libopkg/pkg.c | 12 | ||||
-rw-r--r-- | libopkg/pkg_depends.c | 13 | ||||
-rw-r--r-- | libopkg/pkg_dest_list.c | 2 | ||||
-rw-r--r-- | libopkg/pkg_parse.c | 6 | ||||
-rw-r--r-- | libopkg/pkg_src_list.c | 2 | ||||
-rw-r--r-- | libopkg/pkg_vec.c | 9 | ||||
-rw-r--r-- | libopkg/sprintf_alloc.c | 2 | ||||
-rw-r--r-- | libopkg/str_list.c | 2 | ||||
-rw-r--r-- | libopkg/void_list.c | 4 | ||||
-rw-r--r-- | libopkg/xregex.c | 2 |
20 files changed, 75 insertions, 48 deletions
diff --git a/libopkg/file_util.c b/libopkg/file_util.c index 6f249a2..fad4178 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -143,7 +143,7 @@ char *file_md5sum_alloc(const char *file_name) char *md5sum_hex; unsigned char md5sum_bin[md5sum_bin_len]; - md5sum_hex = malloc(md5sum_hex_len + 1); + md5sum_hex = calloc(1, md5sum_hex_len + 1); if (md5sum_hex == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return strdup(""); diff --git a/libopkg/hash_table.c b/libopkg/hash_table.c index e7f5a92..713ecff 100644 --- a/libopkg/hash_table.c +++ b/libopkg/hash_table.c @@ -146,7 +146,7 @@ int hash_table_insert(hash_table_t *hash, const char *key, void *value) return 0; } } - hash_entry->next = (hash_entry_t *)malloc(sizeof(hash_entry_t)); + hash_entry->next = (hash_entry_t *)calloc(1, sizeof(hash_entry_t)); if (!hash_entry->next) { return -ENOMEM; } @@ -161,6 +161,37 @@ int hash_table_insert(hash_table_t *hash, const char *key, void *value) return 0; } +int hash_table_remove(hash_table_t *hash, const char *key) +{ + int ndx= hash_index(hash, key); + hash_entry_t *hash_entry = hash->entries + ndx; + hash_entry_t *next_entry=NULL, *last_entry=NULL; + while (hash_entry) + { + if (hash_entry->key) + { + if (strcmp(key, hash_entry->key) == 0) { + free(hash_entry->key); + if (last_entry) { + last_entry->next = hash_entry->next; + free(hash_entry); + } else { + next_entry = hash_entry->next; + if (next_entry) { + memmove(hash_entry, next_entry, sizeof(hash_entry_t)); + free(next_entry); + } else { + memset(hash_entry, 0 , sizeof(hash_entry_t)); + } + } + return 1; + } + } + last_entry = hash_entry; + hash_entry = hash_entry->next; + } + return 0; +} void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data) { diff --git a/libopkg/hash_table.h b/libopkg/hash_table.h index d4e8a3d..c065609 100644 --- a/libopkg/hash_table.h +++ b/libopkg/hash_table.h @@ -39,6 +39,7 @@ int hash_table_init(const char *name, hash_table_t *hash, int len); void hash_table_deinit(hash_table_t *hash); void *hash_table_get(hash_table_t *hash, const char *key); int hash_table_insert(hash_table_t *hash, const char *key, void *value); +int hash_table_remove(hash_table_t *has, const char *key); void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data); #endif /* _HASH_TABLE_H_ */ diff --git a/libopkg/nv_pair_list.c b/libopkg/nv_pair_list.c index a3010e2..544d1e0 100644 --- a/libopkg/nv_pair_list.c +++ b/libopkg/nv_pair_list.c @@ -57,7 +57,7 @@ nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const cha int err; /* freed in nv_pair_list_deinit */ - nv_pair_t *nv_pair = malloc(sizeof(nv_pair_t)); + nv_pair_t *nv_pair = calloc(1, sizeof(nv_pair_t)); if (nv_pair == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); diff --git a/libopkg/opkg.c b/libopkg/opkg.c index 7e67755..35ddb89 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -163,8 +163,7 @@ opkg_package_new () opkg_package_t *p; - p = malloc (sizeof (opkg_package_t)); - memset (p, 0, sizeof (opkg_package_t)); + p = calloc (1, sizeof (opkg_package_t)); return p; } @@ -189,9 +188,9 @@ opkg_new () opkg_t *opkg; int err; - opkg = malloc (sizeof (opkg_t)); + opkg = calloc (1, sizeof (opkg_t)); - opkg->args = malloc (sizeof (args_t)); + opkg->args = calloc (1, sizeof (args_t)); err = args_init (opkg->args); if (err) { @@ -200,7 +199,7 @@ opkg_new () return NULL; } - opkg->conf = malloc (sizeof (opkg_conf_t)); + opkg->conf = calloc (1, sizeof (opkg_conf_t)); err = opkg_conf_init (opkg->conf, opkg->args); if (err) { diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index 8386fce..cf1cc5d 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -316,7 +316,7 @@ static opkg_intercept_t opkg_prep_intercepts(opkg_conf_t *conf) char *newpath; int gen; - ctx = malloc (sizeof (*ctx)); + ctx = calloc (1, sizeof (*ctx)); oldpath = getenv ("PATH"); if (oldpath) { ctx->oldpath = strdup (oldpath); @@ -925,7 +925,7 @@ static int opkg_remove_cmd(opkg_conf_t *conf, int argc, char **argv) available = pkg_vec_alloc(); pkg_hash_fetch_all_installed(&conf->pkg_hash, available); for (i=0; i < argc; i++) { - pkg_name = malloc(strlen(argv[i])+2); + pkg_name = calloc(1, strlen(argv[i])+2); strcpy(pkg_name,argv[i]); for (a=0; a < available->len; a++) { pkg = available->pkgs[a]; @@ -1086,7 +1086,7 @@ static int opkg_files_cmd(opkg_conf_t *conf, int argc, char **argv) size_t used_len; char *buff ; - buff = (char *)malloc(buff_len); + buff = (char *)calloc(1, buff_len); if ( buff == NULL ) { fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__); return ENOMEM; diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c index 99db505..c29e4c2 100644 --- a/libopkg/opkg_conf.c +++ b/libopkg/opkg_conf.c @@ -73,7 +73,7 @@ int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options) { NULL } }; - *options = (opkg_option_t *)malloc(sizeof(tmp)); + *options = (opkg_option_t *)calloc(1, sizeof(tmp)); if ( options == NULL ){ fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__); return -1; @@ -182,7 +182,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args) lists_dir = tmp; } - pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5); + pending_dir = calloc(1, strlen(lists_dir)+strlen("/pending")+5); snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending"); conf->lists_dir = strdup(lists_dir); diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c index dc014d3..01d5875 100644 --- a/libopkg/opkg_install.c +++ b/libopkg/opkg_install.c @@ -597,7 +597,7 @@ static int pkg_remove_orphan_dependent(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old if (found) continue; d_str = old_pkg->depends_str[i]; - buf = malloc (strlen (d_str) + 1); + buf = calloc (1, strlen (d_str) + 1); j=0; while (d_str[j] != '\0' && d_str[j] != ' ') { buf[j]=d_str[j]; diff --git a/libopkg/opkg_remove.c b/libopkg/opkg_remove.c index 1debf21..3edb6de 100644 --- a/libopkg/opkg_remove.c +++ b/libopkg/opkg_remove.c @@ -54,7 +54,7 @@ int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, /* if caller requested the set of installed dependents */ if (pdependents) { int p = 0; - abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *)); + abstract_pkg_t **dependents = (abstract_pkg_t **)calloc((n_installed_dependents+1), sizeof(abstract_pkg_t *)); if ( dependents == NULL ){ fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__); @@ -181,7 +181,7 @@ static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg) int x = 0; pkg_t *p; d_str = pkg->depends_str[i]; - buffer = malloc (strlen (d_str) + 1); + buffer = calloc (1, strlen (d_str) + 1); if (!buffer) { fprintf(stderr,"%s Unable to allocate memory.\n", __FUNCTION__); diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index de7712e..6a827e0 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -61,7 +61,7 @@ char **read_raw_pkgs_from_stream(FILE *fp) int count = 0; size_t size = 512; - buf = malloc (size); + buf = calloc (1, size); while (fgets(buf, size, fp)) { while (strlen (buf) == (size - 1) @@ -96,7 +96,7 @@ char *trim_alloc(char *line) char *new; char *dest, *src, *end; - new = malloc(strlen(line) + 1); + new = calloc(1, strlen(line) + 1); if ( new == NULL ){ fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__); return NULL; @@ -142,7 +142,7 @@ void push_error_list(struct errlist ** errors, char * msg){ struct errlist *err_lst_tmp; - err_lst_tmp = malloc ( sizeof (err_lst_tmp) ); + err_lst_tmp = calloc (1, sizeof (err_lst_tmp) ); err_lst_tmp->errmsg=strdup(msg) ; err_lst_tmp->next = *errors; *errors = err_lst_tmp; diff --git a/libopkg/pkg.c b/libopkg/pkg.c index cc15c9b..7ec3498 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -77,7 +77,7 @@ pkg_t *pkg_new(void) { pkg_t *pkg; - pkg = malloc(sizeof(pkg_t)); + pkg = calloc(1, sizeof(pkg_t)); if (pkg == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; @@ -429,7 +429,7 @@ abstract_pkg_t *abstract_pkg_new(void) { abstract_pkg_t * ab_pkg; - ab_pkg = malloc(sizeof(abstract_pkg_t)); + ab_pkg = calloc(1, sizeof(abstract_pkg_t)); if (ab_pkg == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); @@ -461,7 +461,7 @@ void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){ char **raw =NULL; char **raw_start=NULL; - temp_str = (char *) malloc (strlen(pkg->dest->info_dir)+strlen(pkg->name)+12); + temp_str = (char *) calloc (1, strlen(pkg->dest->info_dir)+strlen(pkg->name)+12); if (temp_str == NULL ){ opkg_message(conf, OPKG_INFO, "Out of memory in %s\n", __FUNCTION__); return; @@ -497,7 +497,7 @@ char * pkg_formatted_info(pkg_t *pkg ) char *line; char * buff; - buff = malloc(8192); + buff = calloc(1, 8192); if (buff == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; @@ -1398,9 +1398,9 @@ int pkg_free_installed_files(pkg_t *pkg) str_list_elt_t *iter; pkg->installed_files_ref_cnt--; - if (pkg->installed_files_ref_cnt > 0) { + + if (pkg->installed_files_ref_cnt > 0) return 0; - } if (pkg->installed_files) { diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c index 49962ba..bbe7868 100644 --- a/libopkg/pkg_depends.c +++ b/libopkg/pkg_depends.c @@ -653,7 +653,7 @@ int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) if (pkg->provides) return 0; - pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1)); + pkg->provides = (abstract_pkg_t **)calloc((pkg->provides_count + 1), sizeof(abstract_pkg_t *)); if (pkg->provides == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return -1 ; @@ -683,8 +683,7 @@ int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) if (!pkg->conflicts_count) return 0; - conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) * - pkg->conflicts_count); + conflicts = pkg->conflicts = calloc(pkg->conflicts_count, sizeof(compound_depend_t)); if (conflicts == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return -1; @@ -705,7 +704,7 @@ int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg) if (!pkg->replaces_count) return 0; - pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count); + pkg->replaces = (abstract_pkg_t **)calloc(pkg->replaces_count, sizeof(abstract_pkg_t *)); if (pkg->replaces == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return -1; @@ -743,7 +742,7 @@ int buildDepends(hash_table_t * hash, pkg_t * pkg) if (0 && pkg->pre_depends_count) fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", pkg->name, pkg->pre_depends_count, pkg->depends_count); - depends = pkg->depends = malloc(sizeof(compound_depend_t) * count); + depends = pkg->depends = calloc(count, sizeof(compound_depend_t)); if (depends == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return -1; @@ -878,7 +877,7 @@ void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg) static depend_t * depend_init(void) { - depend_t * d = (depend_t *)malloc(sizeof(depend_t)); + depend_t * d = (depend_t *)calloc(1, sizeof(depend_t)); if ( d==NULL ){ fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; @@ -913,7 +912,7 @@ static int parseDepends(compound_depend_t *compound_depend, compound_depend->type = DEPEND; compound_depend->possibility_count = num_of_ors + 1; - possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1)); + possibilities = (depend_t **)calloc((num_of_ors + 1), sizeof(depend_t *) ); if (!possibilities) return -ENOMEM; compound_depend->possibilities = possibilities; diff --git a/libopkg/pkg_dest_list.c b/libopkg/pkg_dest_list.c index ac69312..48ca07f 100644 --- a/libopkg/pkg_dest_list.c +++ b/libopkg/pkg_dest_list.c @@ -59,7 +59,7 @@ pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name, pkg_dest_t *pkg_dest; /* freed in plg_dest_list_deinit */ - pkg_dest = malloc(sizeof(pkg_dest_t)); + pkg_dest = calloc(1, sizeof(pkg_dest_t)); if (pkg_dest == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return NULL; diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c index af5ead7..76cd648 100644 --- a/libopkg/pkg_parse.c +++ b/libopkg/pkg_parse.c @@ -141,7 +141,7 @@ int parseVersion(pkg_t *pkg, char *raw) if (!pkg->version) { - pkg->version= malloc(strlen(raw)+1); + pkg->version= calloc(1, strlen(raw)+1); if ( pkg->version == NULL ) { fprintf(stderr, "%s: out of memory \n", __FUNCTION__); return ENOMEM; @@ -230,7 +230,7 @@ int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest) pkg->priority = parseGenericFieldType("Priority", *lines); else if(isGenericFieldType("Provides", *lines)){ /* Here we add the internal_use to align the off by one problem between provides_str and provides */ - provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */ + provide = (char * ) calloc(1, strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */ if ( alterProvidesLine(*lines,provide) ){ return EINVAL; } @@ -373,7 +373,7 @@ out:; if ( pkg_false_provides==1) { pkg->provides_count = 1; - pkg->provides_str = malloc (sizeof (char*)); + pkg->provides_str = calloc (1, sizeof (char*)); pkg->provides_str[0] = strdup ("opkg_internal_use_only"); } diff --git a/libopkg/pkg_src_list.c b/libopkg/pkg_src_list.c index 4f07ca4..c9f02f1 100644 --- a/libopkg/pkg_src_list.c +++ b/libopkg/pkg_src_list.c @@ -48,7 +48,7 @@ pkg_src_t *pkg_src_list_append(pkg_src_list_t *list, int err; /* freed in pkg_src_list_deinit */ - pkg_src_t *pkg_src = malloc(sizeof(pkg_src_t)); + pkg_src_t *pkg_src = calloc(1, sizeof(pkg_src_t)); if (pkg_src == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); diff --git a/libopkg/pkg_vec.c b/libopkg/pkg_vec.c index 2c1bcf9..56e29bc 100644 --- a/libopkg/pkg_vec.c +++ b/libopkg/pkg_vec.c @@ -185,12 +185,9 @@ void abstract_pkg_vec_free(abstract_pkg_vec_t *vec) */ void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg) { - - vec->pkgs = - (abstract_pkg_t **) - realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *)); - vec->pkgs[vec->len] = pkg; - vec->len++; + vec->pkgs = (abstract_pkg_t **) realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *)); + vec->pkgs[vec->len] = pkg; + vec->len++; } abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i) diff --git a/libopkg/sprintf_alloc.c b/libopkg/sprintf_alloc.c index 8c66994..30ab033 100644 --- a/libopkg/sprintf_alloc.c +++ b/libopkg/sprintf_alloc.c @@ -44,7 +44,7 @@ int sprintf_alloc(char **str, const char *fmt, ...) /* ripped more or less straight out of PRINTF(3) */ - if ((new_str = malloc(size)) == NULL) + if ((new_str = calloc(1, size)) == NULL) return -1; *str = new_str; diff --git a/libopkg/str_list.c b/libopkg/str_list.c index d6f9209..1d21746 100644 --- a/libopkg/str_list.c +++ b/libopkg/str_list.c @@ -31,7 +31,7 @@ void str_list_elt_deinit(str_list_elt_t *elt) str_list_t *str_list_alloc() { - str_list_t *list = (str_list_t *)malloc(sizeof(str_list_t)); + str_list_t *list = (str_list_t *)calloc(1, sizeof(str_list_t)); if (list) str_list_init(list); return list; diff --git a/libopkg/void_list.c b/libopkg/void_list.c index 8d61fbb..3d9d611 100644 --- a/libopkg/void_list.c +++ b/libopkg/void_list.c @@ -60,7 +60,7 @@ int void_list_append(void_list_t *list, void *data) void_list_elt_t *elt; /* freed in void_list_deinit */ - elt = malloc(sizeof(void_list_elt_t)); + elt = calloc(1, sizeof(void_list_elt_t)); if (elt == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return ENOMEM; @@ -84,7 +84,7 @@ int void_list_push(void_list_t *list, void *data) { void_list_elt_t *elt; - elt = malloc(sizeof(void_list_elt_t)); + elt = calloc(1, sizeof(void_list_elt_t)); if (elt == NULL) { fprintf(stderr, "%s: out of memory\n", __FUNCTION__); return ENOMEM; diff --git a/libopkg/xregex.c b/libopkg/xregex.c index 0ae54e7..b0cd8b9 100644 --- a/libopkg/xregex.c +++ b/libopkg/xregex.c @@ -39,7 +39,7 @@ static void print_regcomp_err(const regex_t *preg, int err) fprintf(stderr, "%s: Error compiling regex:", __FUNCTION__); size = regerror(err, preg, 0, 0); - error = malloc(size); + error = calloc(1, size); if (error) { regerror(err, preg, error, size); fprintf(stderr, "%s\n", error); |