void* gt_realloc_mem(void *ptr, size_t size, const char *src_file, int src_line) { MAInfo *mainfo; void *mem; gt_assert(ma); if (ma->bookkeeping) { gt_mutex_lock(bookkeeping_lock); ma->mallocevents++; if (ptr) { mainfo = gt_hashmap_get(ma->allocated_pointer, ptr); gt_assert(mainfo); subtract_size(ma, mainfo->size); gt_hashmap_remove(ma->allocated_pointer, ptr); } mainfo = xmalloc(sizeof *mainfo, ma->current_size, src_file, src_line); mainfo->size = size; mainfo->src_file = src_file; mainfo->src_line = src_line; mem = xrealloc(ptr, size, ma->current_size, src_file, src_line); gt_hashmap_add(ma->allocated_pointer, mem, mainfo); add_size(ma, size); gt_mutex_unlock(bookkeeping_lock); return mem; } return xrealloc(ptr, size, ma->current_size, src_file, src_line); }
void gt_free_mem(void *ptr, GT_UNUSED const char *src_file, GT_UNUSED int src_line) { MAInfo *mainfo; gt_assert(ma); if (ptr == NULL) return; if (ma->bookkeeping) { gt_mutex_lock(bookkeeping_lock); #ifndef NDEBUG if (!gt_hashmap_get(ma->allocated_pointer, ptr)) { fprintf(stderr, "bug: double free() attempted on line %d in file " "\"%s\"\n", src_line, src_file); exit(GT_EXIT_PROGRAMMING_ERROR); } #endif mainfo = gt_hashmap_get(ma->allocated_pointer, ptr); gt_assert(mainfo); subtract_size(ma, mainfo->size); gt_hashmap_remove(ma->allocated_pointer, ptr); free(ptr); gt_mutex_unlock(bookkeeping_lock); } else { free(ptr); } }
static GtHpolProcessorAddToHashResult gt_hpol_processor_add_segment_to_hashmap( GtHpolProcessor *hpp, GtAlignedSegment *as) { GtAlignedSegment *stored_as; if ((stored_as = gt_hashmap_get(hpp->processed_segments, gt_aligned_segment_description(as))) != NULL) { hpp->nof_multihits++; if (!gt_aligned_segment_seq_edited(stored_as) && gt_aligned_segment_seq_edited(as)) { hpp->nof_replaced++; /* change with newly edited as */ gt_hashmap_remove(hpp->processed_segments, (void*)gt_aligned_segment_description(as)); gt_hashmap_add(hpp->processed_segments, (void*)gt_aligned_segment_description(as), as); return GT_HPOL_PROCESSOR_REPLACED; } /* otherwise discard (todo: implement combination of edits) */ else { return GT_HPOL_PROCESSOR_NOT_REPLACED; } } else { gt_hashmap_add(hpp->processed_segments, (void*)gt_aligned_segment_description(as), as); return GT_HPOL_PROCESSOR_NEW_RECORD; } }
static void remove_elem(GtDlistelem *elem, GtDlist *trees, GtHashmap *target_to_elem, GtStr *key) { GtGenomeNode *node = gt_dlistelem_get_data(elem); gt_genome_node_delete(node); gt_dlist_remove(trees, elem); gt_hashmap_remove(target_to_elem, gt_str_get(key)); }
void gt_feature_info_replace_pseudo_parent(GtFeatureInfo *fi, GtFeatureNode *child, GtFeatureNode *new_pseudo_parent) { const char *id; gt_assert(fi && child && new_pseudo_parent); gt_assert(gt_feature_node_is_pseudo((GtFeatureNode*) new_pseudo_parent)); id = gt_feature_node_get_attribute(child, GT_GFF_ID); gt_assert(id); gt_hashmap_remove(fi->id_to_pseudo_parent, id); gt_feature_info_add_pseudo_parent(fi, id, new_pseudo_parent); }
static void xfclose_generic(void *stream, GtFileMode genfilemode, FA *fa) { FAFileInfo *fileinfo; gt_assert(stream && fa); fileinfo = gt_hashmap_get(fa->file_pointer, stream); gt_assert(fileinfo); gt_hashmap_remove(fa->file_pointer, stream); switch (genfilemode) { case GFM_UNCOMPRESSED: gt_xfclose(stream); break; case GFM_GZIP: gt_xgzclose(stream); break; case GFM_BZIP2: BZ2_bzclose(stream); break; default: gt_assert(0); } }
static int gt_hashmap_test(GtHashType hash_type) { char *s1 = "foo", *s2 = "bar"; GT_UNUSED unsigned long ul1 = 1UL, ul2 = 2UL; GT_UNUSED unsigned long long ull1 = 3ULL, ull2 = 4ULL, *sptr = NULL, *tptr = NULL; GtHashmap *hm; GtHashtable *ht; int had_err = 0; do { /* empty hash */ hm = gt_hashmap_new(hash_type, NULL, NULL); gt_hashmap_delete(hm); /* empty hash with reset */ hm = gt_hashmap_new(hash_type, NULL, NULL); gt_hashmap_reset(hm); gt_hashmap_delete(hm); /* hashes containing one element */ hm = gt_hashmap_new(hash_type, NULL, NULL); gt_hashmap_add(hm, s1, s2); my_ensure(had_err, gt_hashmap_get(hm, s1) == s2); my_ensure(had_err, !gt_hashmap_get(hm, s2)); gt_hashmap_delete(hm); /* hashes containing two elements */ hm = gt_hashmap_new(hash_type, NULL, NULL); gt_hashmap_add(hm, s1, s2); gt_hashmap_add(hm, s2, s1); my_ensure(had_err, gt_hashmap_get(hm, s1) == s2); my_ensure(had_err, gt_hashmap_get(hm, s2) == s1); /* remove element A and ensure it's no longer present */ gt_hashmap_remove(hm, s1); my_ensure(had_err, !gt_hashmap_get(hm, s1)); my_ensure(had_err, gt_hashmap_get(hm, s2) == s1); gt_hashmap_delete(hm); /* hashes containing two elements (store key and value in * hashmap) where simple free is the correct way to get rid of them */ if (hash_type == GT_HASH_STRING) { hm = gt_hashmap_new(hash_type, gt_free_func, gt_free_func); gt_hashmap_add(hm, gt_cstr_dup(s1), gt_cstr_dup(s2)); gt_hashmap_add(hm, gt_cstr_dup(s2), gt_cstr_dup(s1)); my_ensure(had_err, !strcmp(gt_hashmap_get(hm, s1), s2)); my_ensure(had_err, !strcmp(gt_hashmap_get(hm, s2), s1)); gt_hashmap_remove(hm, s1); /* remove first element */ my_ensure(had_err, !gt_hashmap_get(hm, s1)); my_ensure(had_err, !strcmp(gt_hashmap_get(hm, s2), s1)); gt_hashmap_delete(hm); } /* test direct modification of hash contents */ ht = testul_testull_gt_hashmap_new(); my_ensure(had_err, !sptr); sptr = testul_testull_gt_hashmap_add_and_return_storage(ht, ul1, ull1); my_ensure(had_err, *sptr == ull1); sptr = testul_testull_gt_hashmap_add_and_return_storage(ht, ul1, ull2); my_ensure(had_err, *sptr == ull2); (*sptr)++; tptr = testul_testull_gt_hashmap_get(ht, ul1); my_ensure(had_err, tptr == sptr); my_ensure(had_err, *tptr == ull2+1); gt_hashtable_delete(ht); } while (0); return had_err; }