int mod_register_id (int for_mod_type, int id, char * value) { switch(for_mod_type) { case MOD_FOR_SUBJECTS: num_subjects_registered++; subject_ids = (bct *) check_realloc (subject_ids, sizeof(bct) * num_subjects_registered); subject_ids[num_subjects_registered-1] = bct_constructor(id, value); break; case MOD_FOR_CLASSES: num_classes_registered++; class_ids = (bct *) check_realloc (class_ids, sizeof(bct) * num_classes_registered); class_ids[num_classes_registered-1] = bct_constructor(id, value); break; case MOD_FOR_TEACHERS: num_teachers_registered++; teacher_ids = (bct *) check_realloc (teacher_ids, sizeof(bct) * num_teachers_registered); teacher_ids[num_teachers_registered-1] = bct_constructor(id, value); break; case MOD_FOR_ROOMS: num_rooms_registered++; room_ids = (bct *) check_realloc (room_ids, sizeof(bct) * num_rooms_registered); room_ids[num_rooms_registered-1] = bct_constructor(id, value); break; case MOD_FOR_MEETINGS: num_meetingses_registered++; meetings_ids = (bct *) check_realloc (meetings_ids, sizeof(bct) * num_meetingses_registered); meetings_ids[num_meetingses_registered-1] = bct_constructor(id, value); break; } return(1); }
void load_previous_halos(int64_t snap, int64_t chunk, float *bounds) { int64_t rchunk; struct binary_output_header bh; float overlap_region[6]; num_prev_halos = 0; prev_snap = snap-1; if (!phtree) { phtree = fast3tree_init(num_prev_halos, ph); phtree_results = fast3tree_results_init(); } else fast3tree_rebuild(phtree, num_prev_halos, ph); if (snap == STARTING_SNAP || LIGHTCONE || !PARALLEL_IO) return; prev_halo_buffer = check_realloc(prev_halo_buffer, sizeof(struct halo)*PREV_HALO_BUFFER_SIZE, "Allocating previous halo buffer."); for (rchunk = 0; rchunk < NUM_WRITERS; rchunk++) { load_binary_header(snap-1, rchunk, &bh); if (!bounds || bounds_overlap(bh.bounds, bounds, overlap_region, OVERLAP_LENGTH)) load_prev_binary_halos(snap-1, rchunk, bounds, chunk); } prev_halo_buffer = check_realloc(prev_halo_buffer, 0, "Freeing previous halo buffer."); fast3tree_rebuild(phtree, num_prev_halos, ph); }
void load_bgc2(char *filename, struct bgc2_header *hdr, GROUP_DATA_RMPVMAX **groups, int64_t *num_groups, PARTICLE_DATA_PV **pdata, int64_t *num_parts) { FILE *input; int64_t new_group_size, new_part_size; int64_t i, p_start; assert(sizeof(struct bgc2_header) == BGC2_HEADER_SIZE); input = check_fopen(filename, "rb"); fread_fortran(hdr, BGC2_HEADER_SIZE, 1, input, 0); assert(hdr->magic == BGC_MAGIC); assert(hdr->version == 2); assert(hdr->format_group_data == GDATA_FORMAT_RMPVMAX); new_group_size = sizeof(GROUP_DATA_RMPVMAX)*((*num_groups)+hdr->ngroups); *groups = check_realloc(*groups, new_group_size, "Allocating groups."); fread_fortran((*groups) + (*num_groups), sizeof(GROUP_DATA_RMPVMAX), hdr->ngroups, input, 0); *num_groups += hdr->ngroups; new_part_size = sizeof(PARTICLE_DATA_PV)*((*num_parts)+hdr->npart); *pdata = check_realloc(*pdata, new_part_size, "Allocating particles"); p_start = 0; for (i=0; i<hdr->ngroups; i++) { fread_fortran((*pdata) + p_start, sizeof(PARTICLE_DATA_PV), groups[0][i].npart, input, 0); p_start += groups[0][i].npart; } *num_parts += hdr->npart; fclose(input); }
struct fof *return_fullfofs(int64_t *num_f) { struct fof *all_fofs; num_smallfofs = num_alloced_smallfofs = 0; smallfofs = check_realloc(smallfofs, 0, "Freeing SmallFOFs."); particle_smallfofs = check_realloc(particle_smallfofs, 0, "Freeing particle smallfofs."); num_alloced_particles = 0; all_fofs = fofs; *num_f = num_fofs; fofs = NULL; num_alloced_fofs = num_fofs = 0; return all_fofs; }
/** * varray_assign: assign varray entry. * * @param[in] vb #VARRAY structure * @param[in] index index * @param[in] force if entry not found, create it. * @return pointer of the entry * * If specified entry is found then it is returned, else it is allocated * and returned. * This procedure @EMPH{doesn't} operate the contents of the array. */ void * varray_assign(VARRAY *vb, int index, int force) { if (index < 0) die("varray_assign: illegal index value."); if (index >= vb->length) { if (force) vb->length = index + 1; else if (index == 0 && vb->length == 0) return NULL; else die("varray_assign: index(=%d) is out of range.", index); } /* * Expand the area. */ if (index >= vb->alloced) { int old_alloced = vb->alloced; while (index >= vb->alloced) vb->alloced += vb->expand; /* * Old implementations of realloc() may crash * when a null pointer is passed. * Therefore, we cannot use realloc(NULL, ...). */ if (vb->vbuf == NULL) vb->vbuf = (char *)check_malloc(vb->size * vb->alloced); else vb->vbuf = (char *)check_realloc(vb->vbuf, vb->size * vb->alloced); if (debug) fprintf(stderr, "Expanded: from %d to %d.\n", old_alloced, vb->alloced); } return (void *)(vb->vbuf + vb->size * index); }
void read_input_names(char *filename, char ***stringnames, int64_t *num_names) { int64_t i=0, j; char buffer[1024]; FILE *input; char **names = NULL; if (!strlen(filename)) return; input = check_fopen(filename, "r"); while (fgets(buffer, 1024, input)) { while (strlen(buffer) && buffer[strlen(buffer)-1]=='\n') buffer[strlen(buffer)-1] = 0; if (!strlen(buffer)) continue; if (!(i%10)) names = check_realloc(names, sizeof(char *)*(i+10), "Allocating snapshot names."); names[i] = strdup(buffer); i++; } fclose(input); if (*stringnames) { for (j=0; j<*num_names; j++) free((*stringnames)[j]); free(*stringnames); } *num_names = i; *stringnames = names; }
void clear_stats(void) { int64_t i, size; metric_min_mass_bin = (min_mvir>0) ? ((int64_t)(METRIC_BPDEX*log10f(min_mvir))) : 0; metric_max_mass_bin = (max_mvir>0) ? ((int64_t)(METRIC_BPDEX*log10f(max_mvir))) : 0; counts_min_mass_bin = metric_min_mass_bin / METRIC_BPDEX; counts_max_mass_bin = metric_max_mass_bin / METRIC_BPDEX; counts_mass_bins = counts_max_mass_bin - counts_min_mass_bin + 1; metric_mass_bins = metric_max_mass_bin - metric_min_mass_bin + 1; assert(counts_min_mass_bin <= counts_max_mass_bin); assert(metric_min_mass_bin <= metric_max_mass_bin); for (i=0; i<NUM_STATS; i++) { counts[i] = check_realloc(counts[i], sizeof(int64_t)*counts_mass_bins, "consistency statistics"); memset(counts[i], 0, sizeof(int64_t)*counts_mass_bins); } size = sizeof(struct metric_struct)*metric_mass_bins; #define ALLOC_METRIC_STRUCT(x) x = (struct metric_struct *) \ check_realloc(x, size, "allocating metric struct"); \ memset(x, 0, size); ALLOC_METRIC_STRUCT(sigma_x); ALLOC_METRIC_STRUCT(sigma_v); ALLOC_METRIC_STRUCT(sigma_vmax); ALLOC_METRIC_STRUCT(sigma_x_subs); ALLOC_METRIC_STRUCT(sigma_v_subs); ALLOC_METRIC_STRUCT(sigma_vmax_subs); }
void _lh_add_more_buckets(struct litehash *lh) { int64_t i; int64_t old_num_buckets = lh->num_buckets, old_extra_buckets = lh->extra_buckets; int64_t num_extra_buckets_filled = 0; struct litebucket *new_buckets, *lb, *newb; lh->num_buckets *= 2; int64_t new_alloc_size = sizeof(struct litebucket)*(lh->num_buckets + lh->extra_buckets); new_buckets = check_realloc(NULL, new_alloc_size, "Allocating new buckets."); memset(new_buckets, 0, new_alloc_size); for (i=0; i<(lh->num_buckets+lh->extra_buckets); i++) new_buckets[i].next = -1; lh->hashwidth++; for (i=0; i<old_num_buckets+old_extra_buckets; i++) { lb = lh->buckets + i; if (!lb->key) continue; newb = new_buckets + _lh_hash_function(lh, lb->key); if (newb->key) { new_buckets[lh->num_buckets + num_extra_buckets_filled] = *newb; newb->next = lh->num_buckets + num_extra_buckets_filled; num_extra_buckets_filled++; } newb->key = lb->key; newb->data = lb->data; } lh->extra_buckets_alloced = old_extra_buckets; lh->extra_buckets = num_extra_buckets_filled; free(lh->buckets); lh->buckets = new_buckets; }
void find_parents(int64_t ngroups) { int64_t i, j, *halo_order = NULL; struct fast3tree_results *nearest; struct fast3tree *halo_tree; FAST3TREE_TYPE *h1, *h2; float max_dist = BOX_SIZE/2.01, range; halo_order = check_realloc(halo_order, sizeof(int64_t)*ngroups, "Allocating halo order."); halo_tree = fast3tree_init(ngroups, GROUP_LIST); for (i=0; i<ngroups; i++) { GROUP_LIST[i].parent = -1; halo_order[i] = i; } qsort(halo_order, ngroups, sizeof(int64_t), sort_halo_order); nearest = fast3tree_results_init(); for (i=0; i<ngroups; i++) { h1 = &(GROUP_LIST[halo_order[i]]); range = h1->RADIUS*RADIUS_CONVERSION; if (max_dist < range) range = max_dist; fast3tree_find_sphere_periodic(halo_tree, nearest, h1->pos, range); for (j=0; j<nearest->num_points; j++) { h2 = nearest->points[j]; if (h2->RADIUS < h1->RADIUS) h2->parent = h1->id; } } fast3tree_results_free(nearest); fast3tree_free(&halo_tree); free(halo_order); }
void calc_potentials(void) { int64_t i,j=0,count=0,last_id=-1; for (i=0; i<num_p; i++) { if (p[i].hid != last_id) { if (last_id >= 0) h[last_id].num_p = i-h[last_id].p_start; last_id = p[i].hid; h[last_id].p_start = i; } } if (last_id >= 0) h[last_id].num_p = i-h[last_id].p_start; for (i=0; i<num_h; i++) { struct halo *the_h = h+i; if (the_h->id < 0) continue; if (max_num_po < the_h->num_p) { max_num_po = the_h->num_p; po = check_realloc(po, sizeof(struct potential)*max_num_po, "Allocating potentials"); } for (j=0; j<the_h->num_p; j++) { po[j].id = p[the_h->p_start+j].id; memcpy(po[j].pos, p[the_h->p_start+j].pos, sizeof(float)*6); po[j].pe = po[j].ke = 0; } compute_kinetic_energy(po, the_h->num_p, the_h->pos+3, the_h->pos); compute_potential(po, the_h->num_p); count=0; for (j=0; j<the_h->num_p; j++) if (po[j].pe >= po[j].ke) count++; printf("%"PRId64" %"PRId64"\n", the_h->id, count); for (j=0; j<the_h->num_p; j++) if (po[j].pe >= po[j].ke) printf("%"PRId64"\n", po[j].id); } }
int client_parse_icap_header(ci_request_t * req, ci_headers_list_t * h) { int readed = 0, eoh = 0; char *buf, *end; if (req->pstrblock_read_len < 4) /*we need 4 bytes for the end of headers "\r\n\r\n" string */ return CI_NEEDS_MORE; if ((end = strstr(req->pstrblock_read, "\r\n\r\n")) != NULL) { readed = end - req->pstrblock_read + 4; eoh = 1; } else readed = req->pstrblock_read_len - 3; if (check_realloc(&(h->buf), &(h->bufsize), h->bufused, readed) != CI_OK) return CI_ERROR; buf = h->buf + h->bufused; memcpy(buf, req->pstrblock_read, readed); h->bufused += readed; req->pstrblock_read += readed; req->pstrblock_read_len -= readed; if (!eoh) return CI_NEEDS_MORE; h->bufused -= 2; /*We keep the first \r\n of the eohead sequence and the other dropped So stupid but for the time never mind.... */ return CI_OK; }
struct litehash *new_litehash(uint64_t keywidth) { struct litehash *lh = check_realloc(NULL, sizeof(struct litehash), "Allocating litehash."); int64_t i; memset(lh, 0, sizeof(struct litehash)); lh->keywidth = keywidth; lh->hashnum = rand() + (((uint64_t)rand())<<(uint64_t)32) + (uint64_t)(rand()); lh->hashwidth = 8; lh->num_buckets = (uint64_t)1 << lh->hashwidth; lh->buckets = check_realloc(NULL, sizeof(struct litebucket)*lh->num_buckets, "Allocating hash buckets."); memset(lh->buckets, 0, sizeof(struct litebucket)*lh->num_buckets); for (i=0; i<lh->num_buckets; i++) lh->buckets[i].next = -1; if (!(lh->hashnum & 1)) lh->hashnum++; return lh; }
void add_new_halo(void) { int i; if ((num_halos % 1000)==0) { halos = check_realloc(halos, sizeof(struct halo)*(num_halos+1000), "Allocating room for halos."); extra_info = check_realloc(extra_info, sizeof(struct extra_halo_info)*(num_halos+1000), "Allocating room for extra halo info."); memset(halos+num_halos, 0, sizeof(struct halo)*1000); for (i=num_halos; i<num_halos+1000; i++) { extra_info[i].child = extra_info[i].next_cochild = extra_info[i].prev_cochild = extra_info[i].sub_of = -1; extra_info[i].max_metric = 0; halos[i].flags |= GROWING_FLAG; } } num_halos++; }
struct cached_io *cfopen(char *filename, int64_t cache_size) { struct cached_io *cio = check_realloc(NULL, sizeof(struct cached_io), "Allocating new cached IO."); memset(cio, 0, sizeof(struct cached_io)); cio->cachesize = cache_size; cio->filename = check_strdup(filename); return cio; }
static inline void * check_alloc( size_t count, size_t size ) { void *data; data = check_realloc( NULL, count, size ); return data; }
int64_t add_new_smallfof(void) { if (num_smallfofs >= num_alloced_smallfofs) { smallfofs = (struct smallfof *) check_realloc(smallfofs, sizeof(struct smallfof)*(num_smallfofs+1000), "Allocating SmallFOFs.\n"); num_alloced_smallfofs += 1000; } smallfofs[num_smallfofs].root = num_smallfofs; num_smallfofs++; return (num_smallfofs-1); }
int64_t add_new_fof(void) { if (num_fofs >= num_alloced_fofs) { fofs = (struct fof *) check_realloc(fofs, sizeof(struct fof)*(num_fofs+1000), "Allocating FOFs.\n"); memset(fofs + num_fofs, 0, sizeof(struct fof)*1000); num_alloced_fofs = num_fofs + 1000; } num_fofs++; return (num_fofs-1); }
void wstring::alloc(size_t size, bool save) { if (! check_realloc(size)) return; Rep *p = Rep::create(size); if (save) { p->copy(0, data(), length()); p->len = length(); } else p->len = 0; repup(p); }
void cfputs(struct cached_io *cio, char *line) { int64_t linelen = strlen(line); if (!cio->wcache) cio->wcache = check_realloc(NULL, cio->cachesize, "Allocating write cache."); if (linelen + cio->wcache_pos > cio->cachesize) { _flush_wcache(cio, line, linelen); return; } memcpy(cio->wcache + cio->wcache_pos, line, linelen); cio->wcache_pos += linelen; }
void *lh_keylist(struct litehash *lh) { int64_t i, j=0; void *kl = check_realloc(NULL, lh->keywidth*lh->elems, "Allocating key list."); struct litebucket *lb = NULL; for (i=0; i<lh->num_buckets+lh->extra_buckets; i++) { lb = lh->buckets + i; if (!lb->key) continue; memcpy(kl + lh->keywidth*j, lb->key, lh->keywidth); j++; } return kl; }
void init_particle_smallfofs(int64_t num_p, struct particle *particles) { int64_t i; if (num_p > num_alloced_particles) { particle_smallfofs = check_realloc(particle_smallfofs, sizeof(int64_t)*num_p, "Allocating particle smallfof links."); num_alloced_particles = num_p; } for (i=0; i<num_p; i++) particle_smallfofs[i] = -1; root_p = particles; num_particles = num_p; num_boundary_fofs = num_fofs = num_smallfofs = 0; }
inline void add_to_previous_halos(struct halo *h, struct binary_output_header *bh) { if (!(num_prev_halos%1000)) ph = check_realloc(ph, sizeof(struct previous_halo)*(num_prev_halos+1000), "Allocating room for previous halos."); memcpy(ph[num_prev_halos].pos, h->pos, sizeof(float)*6); ph[num_prev_halos].m = h->m; ph[num_prev_halos].r = h->r; ph[num_prev_halos].num_p = h->num_p; ph[num_prev_halos].chunk = bh->chunk; ph[num_prev_halos].p_offset = h->p_start; ph[num_prev_halos].file_offset = sizeof(struct binary_output_header) + sizeof(struct halo)*bh->num_halos + h->p_start*sizeof(int64_t); num_prev_halos++; }
void copy_fullfofs(struct fof **base, int64_t *num_f, int64_t *num_alloced_f) { if ((*num_f)+num_fofs > (*num_alloced_f)) { *base = check_realloc(*base, sizeof(struct fof)*((*num_f)+num_fofs+1000), "Allocating copy space for FOFs."); *num_alloced_f = (*num_f)+num_fofs+1000; } memcpy((*base)+(*num_f), fofs, sizeof(struct fof)*num_fofs); *num_f = (*num_f) + num_fofs; //Make sure to delete last fof, if necessary. if (num_fofs < num_alloced_fofs) num_fofs++; memset(fofs, 0, sizeof(struct fof)*num_fofs); num_boundary_fofs = num_fofs = 0; }
void output_and_free_halos(int64_t id_offset, int64_t snap, int64_t chunk, float *bounds) { if (!strcasecmp(OUTPUT_FORMAT, "BOTH") || !strcasecmp(OUTPUT_FORMAT, "ASCII")) output_ascii(id_offset, snap, chunk, bounds); if (!strcasecmp(OUTPUT_FORMAT, "BOTH")|| !strcasecmp(OUTPUT_FORMAT, "BINARY")) output_binary(id_offset, snap, chunk, bounds); if (chunk<FULL_PARTICLE_CHUNKS) { if (!fork()) { output_full_particles(id_offset, snap, chunk, bounds); exit(0); } } if (DUMP_PARTICLES[0] && (chunk >= DUMP_PARTICLES[1] && chunk <= DUMP_PARTICLES[2])) output_particles_internal(snap, chunk); output_bgc2(id_offset, snap, chunk, bounds); halos = check_realloc(halos, 0, "Freeing halo memory."); num_halos = 0; }
int cfgets(struct cached_io *cio, char *buffer, int64_t maxlen) { int64_t bytes_copied = 0; int64_t to_copy = 0; char *end; if (!cio->rcache) { cio->rcache = check_realloc(NULL, cio->cachesize+1, "Allocating read cache."); _fill_rcache(cio); } if (cio->rcache_pos == cio->rcache_size) { if (cio->rcache_size < cio->cachesize) return 0; _fill_rcache(cio); if (cio->rcache_size == 0) return 0; } while (1) { end = strchr(cio->rcache + cio->rcache_pos, '\n'); if (end) { to_copy = maxlen - bytes_copied - 1; if (to_copy > ((end - (cio->rcache + cio->rcache_pos)) + 1)) to_copy = ((end - (cio->rcache + cio->rcache_pos)) + 1); memcpy(buffer + bytes_copied, cio->rcache + cio->rcache_pos, to_copy); buffer[bytes_copied + to_copy] = 0; cio->rcache_pos = (end - cio->rcache) + 1; return 1; } if (cio->rcache_size == 0) return 1; to_copy = maxlen - bytes_copied - 1; if (to_copy > (cio->rcache_size - cio->rcache_pos)) to_copy = cio->rcache_size - cio->rcache_pos; memcpy(buffer+bytes_copied, cio->rcache + cio->rcache_pos, to_copy); bytes_copied += to_copy; buffer[bytes_copied] = 0; _fill_rcache(cio); } }
wstring& wstring::replace(size_t pos, size_t n1, const wchar_t* s, size_t n2) { const size_t len = length(); if (pos > len) throw out_of_range("pos > len"); if (n1 > len - pos) n1 = len - pos; if (len - n1 > max_size() - n2) throw length_error("len - n1 > max_size() - n2"); size_t newlen = len - n1 + n2; if (check_realloc(newlen)) { Rep *p = Rep::create(newlen); p->copy(0, data(), pos); p->copy(pos + n2, data() + pos + n1, len -(pos + n1)); p->copy(pos, s, n2); repup(p); } else { rep()->move(pos + n2, data() + pos + n1, len -(pos + n1)); rep()->copy(pos, s, n2); } rep()->len = newlen; return *this; }
void lh_setval(struct litehash *lh, void *key, void *data) { struct litebucket *lb; lb = _lh_getval(lh, key); if (!lb) { if (lh->elems >= lh->num_buckets*MAX_LOAD_FACTOR) _lh_add_more_buckets(lh); lb = lh->buckets + _lh_hash_function(lh, key); if (lb->key) { if (lh->extra_buckets >= lh->extra_buckets_alloced) { lh->extra_buckets_alloced += 1000; lh->buckets = check_realloc(lh->buckets, sizeof(struct litebucket)* (lh->extra_buckets_alloced+lh->num_buckets), "Allocating extra buckets."); lb = lh->buckets + _lh_hash_function(lh, key); } lh->buckets[lh->num_buckets+lh->extra_buckets] = *lb; lb->next = lh->num_buckets+lh->extra_buckets; lh->extra_buckets++; } lh->elems++; lb->key = key; } lb->data = data; }
void add_more_output_p(int64_t total) { num_alloced_output_p = total+1000; output_p = check_realloc(output_p, sizeof(struct particle)*num_alloced_output_p, "Allocating bound output particles."); }
char *check_strdup(char *string) { char *newstring = check_realloc(NULL, (strlen(string)+1)*sizeof(char), "Allocating string copy space.\n"); strcpy(newstring, string); return newstring; }
void load_prev_binary_halos(int64_t snap, int64_t chunk, float *bounds, int64_t our_chunk) { FILE *input; char buffer[1024]; struct binary_output_header bh; struct halo h; int64_t remaining = 0, to_read, i,j, h_start = num_prev_halos; double v_to_dx; get_output_filename(buffer, 1024, snap, chunk, "bin"); input = check_fopen(buffer, "rb"); check_fread(&bh, sizeof(struct binary_output_header), 1, input); assert(bh.magic == ROCKSTAR_MAGIC); assert(bh.num_halos >= 0); assert(bh.num_particles >= 0); //Conversion in Comoving Mpc/h / (km/s) //Note that the time units are in 1/H = 1/(h*100 km/s/Mpc) v_to_dx = 0.01*(scale_to_time(SCALE_NOW) - scale_to_time(bh.scale)) / (0.5*(SCALE_NOW + bh.scale)); remaining = bh.num_halos; while (remaining > 0) { to_read = PREV_HALO_BUFFER_SIZE; if (to_read > remaining) to_read = remaining; check_fread(prev_halo_buffer, sizeof(struct halo), to_read, input); remaining -= to_read; for (i=0; i<to_read; i++) { for (j=0; j<3; j++) prev_halo_buffer[i].pos[j] += v_to_dx*prev_halo_buffer[i].pos[j+3]; h = prev_halo_buffer[i]; if (!bounds || _check_bounds(prev_halo_buffer[i].pos, h.pos, bounds) || our_chunk == chunk) add_to_previous_halos(&h, &bh); } } if (chunk == our_chunk) { hid_cache = check_realloc(hid_cache, sizeof(int64_t)*bh.num_particles, "Allocating halo id cache"); check_fread(hid_cache, sizeof(int64_t), bh.num_particles, input); for (i=h_start; i<num_prev_halos; i++) ph[i].file_offset = -1; } fclose(input); }