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); } }
void gt_spacepeak_free(GtUword size) { gt_assert(peaklogger && size <= peaklogger->current); gt_mutex_lock(peaklogger->mutex); peaklogger->current -= size; gt_mutex_unlock(peaklogger->mutex); }
GtAlphabet* gt_alphabet_ref(GtAlphabet *alphabet) { gt_assert(alphabet); gt_mutex_lock(alphabet->refmutex); alphabet->reference_count++; gt_mutex_unlock(alphabet->refmutex); return alphabet; }
void gt_alphabet_delete(GtAlphabet *alphabet) { if (!alphabet) return; gt_mutex_lock(alphabet->refmutex); if (alphabet->reference_count) { alphabet->reference_count--; gt_mutex_unlock(alphabet->refmutex); return; } gt_mutex_unlock(alphabet->refmutex); gt_free(alphabet->mapdomain); gt_free(alphabet->characters); if (alphabet->alphadef != NULL) gt_str_delete(alphabet->alphadef); gt_mutex_delete(alphabet->refmutex); gt_free(alphabet); }
void gt_spacepeak_add(GtUword size) { gt_assert(peaklogger); gt_mutex_lock(peaklogger->mutex); peaklogger->current += size; if (peaklogger->current > peaklogger->max) peaklogger->max = peaklogger->current; gt_mutex_unlock(peaklogger->mutex); }
void gt_ma_show_allocations(FILE *outfp) { GT_UNUSED int had_err; gt_assert(ma); gt_mutex_lock(bookkeeping_lock); had_err = gt_hashmap_foreach(ma->allocated_pointer, print_allocation, outfp, NULL); gt_mutex_unlock(bookkeeping_lock); gt_assert(!had_err); /* cannot happen, print_allocation() is sane */ }
static void* gt_feature_index_unit_test_add(void *data) { GtFeatureNode *feature; GtFeatureIndexTestShared *shm = (GtFeatureIndexTestShared*) data; while (true) { gt_mutex_lock(shm->mutex); if (shm->next_node_idx == GT_FI_TEST_FEATURES_PER_THREAD * gt_jobs) { gt_mutex_unlock(shm->mutex); return NULL; } gt_assert(shm->next_node_idx < gt_array_size(shm->nodes)); feature = *(GtFeatureNode**) gt_array_get(shm->nodes, shm->next_node_idx++); gt_mutex_unlock(shm->mutex); gt_feature_index_add_feature_node(shm->fi, feature, shm->err); gt_genome_node_delete((GtGenomeNode*) feature); } return NULL; }
void gt_ma_clean(void) { gt_assert(ma); gt_mutex_lock(bookkeeping_lock); ma->bookkeeping = false; gt_hashmap_delete(ma->allocated_pointer); gt_mutex_unlock(bookkeeping_lock); gt_mutex_delete(bookkeeping_lock); free(ma); ma = NULL; }
unsigned int gt_ya_random (void) { register int ret; gt_mutex_lock(mutex); ret = a[i1] + a[i2]; a[i1] = ret; if (++i1 >= VectorSize) i1 = 0; if (++i2 >= VectorSize) i2 = 0; gt_mutex_unlock(mutex); return ret; }
const char* gt_symbol(const char *cstr) { const char *symbol; if (!cstr) return NULL; gt_mutex_lock(symbol_mutex); if (!(symbol = gt_cstr_table_get(symbols, cstr))) { gt_cstr_table_add(symbols, cstr); symbol = gt_cstr_table_get(symbols, cstr); } gt_mutex_unlock(symbol_mutex); return symbol; }
int gt_ma_check_space_leak(void) { CheckSpaceLeakInfo info; GT_UNUSED int had_err; gt_assert(ma); gt_mutex_lock(bookkeeping_lock); info.has_leak = false; had_err = gt_hashmap_foreach(ma->allocated_pointer, check_space_leak, &info, NULL); gt_assert(!had_err); /* cannot happen, check_space_leak() is sane */ gt_mutex_unlock(bookkeeping_lock); if (info.has_leak) return -1; return 0; }
void* gt_calloc_mem(size_t nmemb, 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++; mainfo = xmalloc(sizeof *mainfo, ma->current_size, src_file, src_line); mainfo->size = nmemb * size; mainfo->src_file = src_file; mainfo->src_line = src_line; mem = xcalloc(nmemb, size, ma->current_size, src_file, src_line); gt_hashmap_add(ma->allocated_pointer, mem, mainfo); add_size(ma, nmemb * size); gt_mutex_unlock(bookkeeping_lock); return mem; } return xcalloc(nmemb, size, ma->current_size, src_file, src_line); }
static void* gt_feature_index_unit_test_query(void *data) { GtFeatureIndexTestShared *shm = (GtFeatureIndexTestShared*) data; GtRange rng; GtError *err = shm->err; GtUword i; int had_err = 0; GtArray *arr, *arr_ref; gt_mutex_lock(shm->mutex); if (gt_error_is_set(shm->err)) { gt_mutex_unlock(shm->mutex); return NULL; } gt_mutex_unlock(shm->mutex); arr = gt_array_new(sizeof (GtFeatureNode*)); arr_ref = gt_array_new(sizeof (GtFeatureNode*)); rng.start = random() % (GT_FI_TEST_END - GT_FI_TEST_QUERY_WIDTH); rng.end = rng.start + random() % (GT_FI_TEST_QUERY_WIDTH); /* get reference set by linear search */ gt_mutex_lock(shm->mutex); for (i=0; i<GT_FI_TEST_FEATURES_PER_THREAD * gt_jobs; i++) { GtRange rng2; GtFeatureNode *fn; fn = *(GtFeatureNode**) gt_array_get(shm->nodes, i); rng2 = gt_genome_node_get_range((GtGenomeNode*) fn); if (gt_range_overlap(&rng, &rng2)) { gt_array_add(arr_ref, fn); } } gt_mutex_unlock(shm->mutex); /* query feature index */ gt_feature_index_get_features_for_range(shm->fi, arr, GT_FI_TEST_SEQID, &rng, err); /* result size must be equal */ if (gt_array_size(arr) != gt_array_size(arr_ref)) had_err = -1; /* nodes must be the same (note that we should not rely on ptr equality) */ if (!had_err) { gt_array_sort(arr_ref, cmp_range_start); gt_array_sort(arr , cmp_range_start); for (i=0;i<gt_array_size(arr);i++) { if (had_err) break; if (!gt_feature_node_is_similar(*(GtFeatureNode**) gt_array_get(arr, i), *(GtFeatureNode**) gt_array_get(arr_ref, i))) { had_err = -1; } } } if (had_err) { gt_mutex_lock(shm->mutex); shm->error_count++; gt_mutex_unlock(shm->mutex); } gt_array_delete(arr); gt_array_delete(arr_ref); return NULL; }