/* coalesce a pointer and free it */ void coalesce_free_ptr(void *ptr, unsigned size) { // check next ptr if (!GET_ALLOC(HDRP(NEXT_BLKP(ptr)))) { unsigned nextSize = GET_SIZE(HDRP(NEXT_BLKP(ptr))); void *nextPtr = NEXT_BLKP(ptr); // check prev ptr if (!GET_ALLOC(HDRP(PREV_BLKP(ptr)))) { // coalesce with both adjacent blocks unsigned prevSize = GET_SIZE(HDRP(PREV_BLKP(ptr))); void *prevPtr = ADD_PTR(ptr, -prevSize); // prevPtr->next->prev = prevPtr->prev; PUT(PREV_PTR(GET(NEXT_PTR(prevPtr))), GET(PREV_PTR(prevPtr))); // prevPtr->prev->next = prevPtr->next; PUT(NEXT_PTR(GET(PREV_PTR(prevPtr))), GET(NEXT_PTR(prevPtr))); // nextPtr->next->prev = nextPtr->prev; PUT(PREV_PTR(GET(NEXT_PTR(nextPtr))), GET(PREV_PTR(nextPtr))); // nextPtr->prev->next = nextPtr->next; PUT(NEXT_PTR(GET(PREV_PTR(nextPtr))), GET(NEXT_PTR(nextPtr))); // create coalesced free block, stick block at end of free_list free_ptr(prevPtr, (void *)GET(PREV_PTR(free_list)), free_list, prevSize + size + nextSize); // tail->prev->next = prevPtr; PUT(NEXT_PTR(GET(PREV_PTR(free_list))), prevPtr); // tail->prev = prevPtr; PUT(PREV_PTR(free_list), prevPtr); return; } // coalesce with next block // nextPtr->prev->next = ptr; PUT(NEXT_PTR(GET(PREV_PTR(nextPtr))), ptr); // nextPtr->next->prev = ptr; PUT(PREV_PTR(GET(NEXT_PTR(nextPtr))), ptr); // create coalesced free block free_ptr(ptr, (void *)GET(PREV_PTR(nextPtr)), (void *)GET(NEXT_PTR(nextPtr)), size + nextSize); return; } if (!GET_ALLOC(HDRP(PREV_BLKP(ptr)))) { // coalesce with previous block unsigned prevSize = GET_SIZE(HDRP(PREV_BLKP(ptr))); void *prevPtr = ADD_PTR(ptr, -prevSize); // create coalesced free block free_ptr(prevPtr, (void *)GET(PREV_PTR(prevPtr)), (void *)GET(NEXT_PTR(prevPtr)), prevSize + size); } else { // don't coalesce // tail->prev->next = ptr; PUT(NEXT_PTR(GET(PREV_PTR(free_list))), ptr); // create free block free_ptr(ptr, (void *)GET(PREV_PTR(free_list)), free_list, size); // tail->prev = ptr; PUT(PREV_PTR(free_list), ptr); } }
/* * Execute development-time test */ bool_t rcmd_test_1_svc(rcmd_str_listentry_ptr arg1, rcmd_status *result, struct svc_req *rqstp) { int argc = 0; char** argv = NULL; rcmd_str_listentry* p; int k; for (p = arg1; p; p = p->next) argc++; argv = (char**) xmalloc(sizeof(char*) * (argc + 1)); for (k = 0, p = arg1; p; p = p->next) argv[k++] = p->value; argv[k] = NULL; result->status = execute_test(argc, argv, &result->message); /* XDR does not tolerate NULL string pointers */ if (result->message == NULL) result->message = xstrdup(""); free_ptr(argv); return true; }
void mem_alloc( void **ptr, int req ) { free_ptr( ptr ); *ptr = malloc( req ); if( *ptr == NULL ) abort_on_error( -4 ); } /* End of void mem_alloc() */
// Estimate site MAF from (logscaled) GL through an EM // If indF == NULL, assumes uniform prior // Else (0 < indF < 1), assumes HWE with specified level of inbreeding double est_maf(uint64_t n_ind, double **pdg, double F){ double maf; double *indF = init_ptr(n_ind, F); maf = est_maf(n_ind, pdg, indF); free_ptr((void*) indF); return maf; }
void DynArray::clean_up(int *stringOffset) { if ( stringOffset ) { int i; for ( i = 0 ; i<last_ele ; i++ ) free_ptr( body_buf+i*ele_size, stringOffset ); } last_ele=0; cur_pos =0; resize( block_num ); }
int mem_Close(void) { printf("mem chunks allocated:%d\n",mem_chunks_num); printf("mem allocated size:%d\n",mem_allocated_size); printf("mem max allocated size:%d\n",mem_max_allocated_size); printf("mem max chunk size:%d\n",mem_max_chunk_size); int r = mem_DebugHeapWalk(1); if(mem_first_chunk) { mem_chunk *mi = mem_first_chunk; while(mi) { if(!mi->is_freed) { free_ptr(mi->ptr); } mem_chunk *next = mi->next; free_ptr(mi); mi = next; } } return(r); }
void priority_queue_free(priority_queue_t *queue, void (*free_ptr)(void *a)) { size_t i; for (i = 0; i < queue->alloc_size; i++) { if (queue->pointers[i] != NULL) { if (free_ptr == NULL) { ckd_free(queue->pointers[i]); } else { free_ptr(queue->pointers[i]); } } } ckd_free(queue->pointers); ckd_free(queue); }
static int join_lines(struct COOR *p, struct COOR *q) { p->row = row; p->col = col - 1; switch (data_type) { case CELL_TYPE: p->val = ((CELL *) middle)[col]; break; case FCELL_TYPE: p->dval = ((FCELL *) middle)[col]; break; case DCELL_TYPE: p->dval = ((DCELL *) middle)[col]; break; default: break; } G_debug(3, "join_lines: p: row:%d, col:%d; q: row:%d, col:%d", p->row, p->col, q->row, q->col); if (p->fptr != NULL) { G_warning(_("join_lines: p front pointer not NULL!")); /* exit(EXIT_FAILURE); */ } p->fptr = q->bptr; if (q->fptr != NULL) { G_warning(_("join_lines: q front pointer not NULL!")); /* exit(EXIT_FAILURE); */ } if (q->bptr->fptr == q) q->bptr->fptr = p; else q->bptr->bptr = p; free_ptr(q); write_line(p); return 0; }
void operator()( int /*id*/ ) const { void* (*malloc_ptr)(size_t); void (*free_ptr)(void*); const char* actual_name; LIBRARY_HANDLE lib = LOAD_LIBRARY(actual_name = MALLOCLIB_NAME1); if (!lib) lib = LOAD_LIBRARY(actual_name = MALLOCLIB_NAME2); if (!lib) { REPORT("Can't load " MALLOCLIB_NAME1 " or " MALLOCLIB_NAME2 "\n"); exit(1); } #if _WIN32 || _WIN64 (void *&)malloc_ptr = GetProcAddress(lib, "scalable_malloc"); (void *&)free_ptr = GetProcAddress(lib, "scalable_free"); #else (void *&)malloc_ptr = dlsym(lib, "scalable_malloc"); (void *&)free_ptr = dlsym(lib, "scalable_free"); #endif if (!malloc_ptr || !free_ptr) { REPORT("Can't find scalable_(malloc|free) in %s \n", actual_name); exit(1); } void *p = malloc_ptr(100); memset(p, 1, 100); free_ptr(p); #if _WIN32 || _WIN64 BOOL ret = FreeLibrary(lib); ASSERT(ret, "FreeLibrary must be successful"); ASSERT(GetModuleHandle(actual_name), "allocator library must not be unloaded"); #else int ret = dlclose(lib); ASSERT(ret == 0, "dlclose must be successful"); ASSERT(dlsym(RTLD_DEFAULT, "scalable_malloc"), "allocator library must not be unloaded"); #endif }
void free_ptr(void ****ptr, uint64_t A, uint64_t B, uint64_t C){ for(uint64_t a = 0; a < A; a++) free_ptr(ptr[a], B, C); free_ptr(ptr); }
void free_ptr(void **ptr, uint64_t A){ for(uint64_t a = 0; a < A; a++) free_ptr(ptr[a]); free_ptr(ptr); }
gboolean readgm( char *gm, int *i1, int *i2, double *x1, double *y1, double *z1, double *x2, double *y2, double *z2, double *rad ) { char *line_buf = NULL, *startptr = NULL, *endptr = NULL; int len, i, idx; int nint = 2, nflt = 7; int iarr[2] = { 0, 0 }; double rarr[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; int eof; /* EOF error flag */ /* Clear return values */ *i1 = *i2 = 0; *x1 = *y1 = *z1 = *x2 = *y2 = *z2 = *rad = 0.0; /* read a line from input file */ mem_alloc((void **)&line_buf, LINE_LEN, "in readgm()"); if( line_buf == NULL ) return( FALSE ); startptr = line_buf; eof = Load_Line( line_buf, input_fp ); if( eof == EOF ) { Strlcpy( gm, "GE", 3 ); fprintf( stderr, "xnec2c: readgm(): geometry data card error\n" "Unexpected EOF while reading input file - appending GE card\n" ); stop( _("readgm(): Geometry data card error\n"\ "Unexpected EOF while reading input file\n"\ "Uppending a default GE card"), ERR_OK ); free_ptr( (void **)&startptr ); return( FALSE ); } /* get line length */ len = (int)strlen( line_buf ); /* abort if card's mnemonic too short or missing */ if( len < 2 ) { Strlcpy( gm, "XX", 3 ); fprintf( stderr, "xnec2c: readgm(): geometry data card error\n" "card's mnemonic code too short or missing\n" ); stop( _("readgm(): Geometry data card error\n"\ "Card's mnemonic code too short or missing"), ERR_OK ); free_ptr( (void **)&startptr ); return( FALSE ); } /* extract card's mnemonic code */ Strlcpy( gm, line_buf, 3 ); /* Return if only mnemonic on card */ if( len == 2 ) { free_ptr( (void **)&startptr ); return( TRUE ); } /* Compatibility with NEC4, * comments between data cards */ if( strcmp(gm, "CM") == 0 ) { free_ptr( (void **)&startptr ); return( TRUE ); } /* check line for spurious characters */ for( idx = 2; idx < len; idx++ ) { if( ((line_buf[idx] >= '0') && (line_buf[idx] <= '9')) || (line_buf[idx] == ' ') || (line_buf[idx] == '.') || (line_buf[idx] == ',') || (line_buf[idx] == '+') || (line_buf[idx] == '-') || (line_buf[idx] == 'E') || (line_buf[idx] == 'e') || (line_buf[idx] == '\t') || (line_buf[idx] == '\0') ) continue; else break; } if( idx < len ) { fprintf( stderr, "xnec2c: readgm(): geometry data card \"%s\" error\n" "Spurious character '%c' at column %d\n", gm, line_buf[idx], idx+1 ); stop( _("readmn(): Geometry data card error\n"\ "Spurious character in command card"), ERR_OK ); free_ptr( (void **)&startptr ); return( FALSE ); } /* read integers from line */ line_buf += 2; for( i = 0; i < nint; i++ ) { /* read an integer from line, reject spurious chars */ iarr[i] = (int)strtol( line_buf, &endptr, 10 ); if( *endptr == '\0' ) break; line_buf = endptr + 1; } /* for( i = 0; i < nint; i++ ) */ /* Return if no floats are specified in the card */ if( *endptr == '\0' ) { *i1 = iarr[0]; *i2 = iarr[1]; *x1 = rarr[0]; *y1 = rarr[1]; *z1 = rarr[2]; *x2 = rarr[3]; *y2 = rarr[4]; *z2 = rarr[5]; *rad= rarr[6]; free_ptr( (void **)&startptr ); return( TRUE ); } /* read doubles from line */ for( i = 0; i < nflt; i++ ) { /* read a double from line */ rarr[i] = Strtod( line_buf, &endptr ); if( *endptr == '\0' ) break; line_buf = endptr + 1; } /* for( i = 0; i < nflt; i++ ) */ /* Return values on normal exit */ *i1 = iarr[0]; *i2 = iarr[1]; *x1 = rarr[0]; *y1 = rarr[1]; *z1 = rarr[2]; *x2 = rarr[3]; *y2 = rarr[4]; *z2 = rarr[5]; *rad = rarr[6]; free_ptr( (void **)&startptr ); return( TRUE ); } /* readgm() */
/* just returns if line is not completed yet */ int write_boundary(struct COOR *seed) { struct COOR *point, *line_begin, *line_end, *last; int dir, line_type, n, n1, i; point = seed; if ((dir = at_end(point))) { /* already have one end of line */ line_begin = point; line_end = find_end(point, dir, &line_type, &n); if (line_type == OPEN) return (-1); /* unfinished line */ direction = dir; } else { /* in middle of a line */ line_end = find_end(point, FORWARD, &line_type, &n); if (line_type == OPEN) /* line not finished */ return (-1); if (line_type == END) { /* found one end at least *//* look for other one */ line_begin = find_end(point, BACKWARD, &line_type, &n1); if (line_type == OPEN) /* line not finished */ return (-1); if (line_type == LOOP) { /* this should NEVER be the case */ return (-1); } direction = at_end(line_begin); /* found both ends now; total length */ n += n1; /* is sum of distances to each end */ } else { /* line_type = LOOP by default */ /* already have correct length */ line_begin = line_end; /* end and beginning are the same */ direction = FORWARD; /* direction is arbitrary */ } } dir = direction; if (smooth_flag == SMOOTH) write_smooth_bnd(line_begin, line_end, n); else write_bnd(line_begin, line_end, n); /* now free all the pointers */ direction = dir; point = line_begin; last = NULPTR; n1 = 0; /* skip first and last point */ while ((point = move(point)) == line_begin); while (point && point != line_end) { last = point; n1++; point = move(point); if (point == last) { /* should not happen */ G_warning("loop during free ptrs, ptr %d of %d", n1, n); point = move(point); } if (last->fptr != NULPTR) if (last->fptr->fptr == last) last->fptr->fptr = NULPTR; /* it can be NULL after the previous line, even though before it wasn't */ if (last->fptr != NULPTR) if (last->fptr->bptr == last) last->fptr->bptr = NULPTR; if (last->bptr != NULPTR) if (last->bptr->fptr == last) last->bptr->fptr = NULPTR; if (last->bptr != NULPTR) if (last->bptr->bptr == last) last->bptr->bptr = NULPTR; free_ptr(last); } if (point != line_end) { /* should not happen */ G_warning("Line end not reached, possible memory leak"); } /* free first and last point */ free_ptr(line_begin); if (line_end != line_begin) free_ptr(line_end); return (0); }
void mem_free(void *ptr) { mem_chunk *chunk = mem_get_chunk(ptr); if(!chunk) { printf("chunk not found\ndouble freeing:%x\n",ptr); return; } if(!chunk->is_freed) { if(mem_verbosity>=1) { printf("freeing ptr:%x\n",ptr); } if(mem_verbosity>=2) { mem_Dump(ptr); } free_ptr(ptr); mem_allocated_size-=chunk->size; if(mem_last_chunk == chunk) { if(chunk->prev) { //printf("removing last chunk in chain\n"); mem_last_chunk = chunk->prev; } else { //printf("removed all chunks setting last chunk to NULL\n"); mem_last_chunk = NULL; } } if(mem_first_chunk == chunk) { if(chunk->next) { //printf("removing first chunk in chain\n"); mem_first_chunk = chunk->next; } else { //printf("removed all chunks setting first chunk to NULL\n"); mem_first_chunk = NULL; } } if(chunk->next) { //printf("setting next chunk prev\n"); chunk->next->prev = chunk->prev; } if(chunk->prev) { //printf("setting prev chunk next\n"); chunk->prev->next = chunk->next; } } else { printf("double freeing:%x\n",ptr); return; //int x = 1/0;//my breakpoint style :-) } }
double* read_pos(char *in_pos, uint64_t n_sites){ uint64_t n_fields; char *buf = init_ptr(BUFF_LEN, (const char*) '\0'); char *prev_chr = init_ptr(BUFF_LEN, (const char*) '\0'); uint64_t prev_pos = 0; // Allocate memory double *pos_dist = init_ptr(n_sites+1, (double) INFINITY); // Open file gzFile in_pos_fh = open_gzfile(in_pos, "r"); if(in_pos_fh == NULL) error(__FUNCTION__, "cannot open POS file!"); for(uint64_t s = 1; s <= n_sites; s++){ char **tmp; if( gzgets(in_pos_fh, buf, BUFF_LEN) == NULL) error(__FUNCTION__, "cannot read next site from POS file!"); // Remove trailing newline chomp(buf); // Check if empty if(strlen(buf) == 0) continue; // Parse input line into array n_fields = split(buf, (const char*) " \t", &tmp); // Check if header and skip if(!n_fields || strtod(tmp[1], NULL)==0){ fprintf(stderr, "> Header found! Skipping line...\n"); if(s != 1){ warn(__FUNCTION__, " header found but not on first line. Is this an error?"); fprintf(stderr, "%s/n", buf); } s--; continue; } if(n_fields < 2) error(__FUNCTION__, "wrong POS file format!"); // If first chr to be parsed if(strlen(prev_chr) == 0) strcpy(prev_chr, tmp[0]); if(strcmp(prev_chr, tmp[0]) == 0){ pos_dist[s] = strtod(tmp[1], NULL) - prev_pos; if(pos_dist[s] < 1) error(__FUNCTION__, "invalid distance between adjacent sites!"); }else{ pos_dist[s] = INFINITY; strcpy(prev_chr, tmp[0]); } prev_pos = strtoul(tmp[1], NULL, 0); // Free memory free_ptr((void**) tmp, n_fields); } // Check if file is at EOF gzread(in_pos_fh, buf, 1); if(!gzeof(in_pos_fh)) error(__FUNCTION__, "POS file not at EOF. Check POS file and number of sites!"); gzclose(in_pos_fh); delete [] buf; delete [] prev_chr; return pos_dist; }
int write_line(struct COOR *seed) { struct COOR *point, *begin, *end, *last; int dir, line_type, n, n1; point = seed; if ((dir = at_end(point))) { /* already have one end of line */ begin = point; end = find_end(point, dir, &line_type, &n); if (line_type == OPEN) { return (-1); /* unfinished line */ } direction = dir; } else { /* in middle of a line */ end = find_end(point, FORWARD, &line_type, &n); if (line_type == OPEN) { /* line not finished */ return (-1); } if (line_type == END) { /* found one end at least *//* look for other one */ begin = find_end(point, BACKWARD, &line_type, &n1); if (line_type == OPEN) { /* line not finished */ return (-1); } if (line_type == LOOP) { /* this should NEVER be the case */ G_warning(_("write_line: found half a loop!")); return (-1); } direction = at_end(begin); /* found both ends now; total length */ n += n1; /* is sum of distances to each end */ } else { /* line_type = LOOP by default */ /* already have correct length */ begin = end; /* end and beginning are the same */ direction = FORWARD; /* direction is arbitrary */ } } dir = direction; /* if (n > 2) */ write_ln(begin, end, n); /* now free all the pointers */ direction = dir; point = begin; /* skip first and last point */ while ((point = move(point)) == begin); while (point && point != end) { last = point; point = move(point); if (point == last) { /* should not happen */ G_warning("loop during free ptrs, ptr %d of %d", n1, n); point = move(point); } if (last->fptr != NULL) if (last->fptr->fptr == last) last->fptr->fptr = NULL; /* now it can already ne NULL */ if (last->fptr != NULL) if (last->fptr->bptr == last) last->fptr->bptr = NULL; if (last->bptr != NULL) if (last->bptr->fptr == last) last->bptr->fptr = NULL; if (last->bptr != NULL) if (last->bptr->bptr == last) last->bptr->bptr = NULL; free_ptr(last); } /* end of for i */ if (point != end) { /* should not happen */ G_warning("Line end not reached, possible memory leak"); } /* free first and last point */ free_ptr(begin); if (end != begin) free_ptr(end); return (0); }