static void record_delete(char *buf, long len, bool move_after) { struct change *change = buffer->cur_change; BUG_ON(!len); BUG_ON(!buf); if (change_merge == prev_change_merge) { if (change_merge == CHANGE_MERGE_DELETE) { xrenew(change->buf, change->del_count + len); memcpy(change->buf + change->del_count, buf, len); change->del_count += len; free(buf); return; } if (change_merge == CHANGE_MERGE_ERASE) { xrenew(buf, len + change->del_count); memcpy(buf + len, change->buf, change->del_count); change->del_count += len; free(change->buf); change->buf = buf; change->offset -= len; return; } } change = new_change(); change->offset = buffer_offset(); change->del_count = len; change->move_after = move_after; change->buf = buf; }
/* add backbone index-set from moltype imt */ static bb_t *addbackbone(bb_t *bb, int *rescnt, gmx_mtop_t *mtop, int imt, int ff[], int resid, const char *resname, int verbose) { int i, ag, im, imb, nmb = mtop->nmolblock, nres; gmx_molblock_t *mb; nres = *rescnt; if (verbose) printf("%3d: adding %d, %d, %d\n", nres+1, ff[0], ff[1], ff[2]); /* search molblock for those whose moltype is imt */ for (ag = 0, imb = 0; imb < nmb; imb++) { mb = mtop->molblock + imb; if (mb->type == imt) { for (im = 0; im < mb->nmol; im++) { xrenew(bb, nres+1); for (i = 0; i < 3; i++) { bb[nres].ia[i] = ag + im*mb->natoms_mol + ff[i]; bb[nres].resid = resid; bb[nres].resname = resname; } nres++; } } ag += mb->nmol * mb->natoms_mol; } *rescnt = nres; return bb; }
static void add_change(struct change *change) { struct change *head = buffer->cur_change; change->next = head; xrenew(head->prev, head->nr_prev + 1); head->prev[head->nr_prev++] = change; buffer->cur_change = change; }
void ptr_array_add(struct ptr_array *array, void *ptr) { if (array->count == array->alloc) { // NOTE: if alloc was 1 then new alloc would be 1*3/2 = 1! array->alloc *= 3; array->alloc /= 2; if (array->alloc < 8) { array->alloc = 8; } xrenew(array->ptrs, array->alloc); } array->ptrs[array->count++] = ptr; }
struct state *merge_syntax(struct syntax *syn, struct syntax_merge *m) { // NOTE: string_lists is owned by struct syntax so there's no need to // copy it. Freeing struct condition does not free any string lists. const char *prefix = get_prefix(); struct ptr_array *states = &syn->states; int i, old_count = states->count; states->count += m->subsyn->states.count; if (states->count > states->alloc) { states->alloc = states->count; xrenew(states->ptrs, states->alloc); } memcpy(states->ptrs + old_count, m->subsyn->states.ptrs, sizeof(*states->ptrs) * m->subsyn->states.count); for (i = old_count; i < states->count; i++) { struct state *s = xmemdup(states->ptrs[i], sizeof(struct state)); int j; states->ptrs[i] = s; s->name = xstrdup(fix_name(s->name, prefix)); s->emit_name = xstrdup(s->emit_name); s->conds.ptrs = xmemdup(s->conds.ptrs, sizeof(void *) * s->conds.alloc); for (j = 0; j < s->conds.count; j++) s->conds.ptrs[j] = xmemdup(s->conds.ptrs[j], sizeof(struct condition)); // Mark unvisited so that state that is used only as a return state gets visited. s->visited = false; // Don't complain about unvisited copied states. s->copied = true; } for (i = old_count; i < states->count; i++) { fix_conditions(syn, states->ptrs[i], m, prefix); if (m->delim) update_state_colors(syn, states->ptrs[i]); } m->subsyn->used = true; return states->ptrs[old_count]; }
static int read_blocks(struct buffer *b, int fd) { size_t size = b->st.st_size; unsigned long map_size = 64 * 1024; unsigned char *buf = NULL; bool mapped = false; ssize_t rc; // st_size is zero for some files in /proc. // Can't mmap files in /proc and /sys. if (size >= map_size) { // NOTE: size must be greater than 0 buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); if (buf == MAP_FAILED) { buf = NULL; } else { mapped = true; } } if (!mapped) { ssize_t alloc = map_size; ssize_t pos = 0; buf = xnew(char, alloc); while (1) { rc = xread(fd, buf + pos, alloc - pos); if (rc < 0) { free(buf); return -1; } if (rc == 0) break; pos += rc; if (pos == alloc) { alloc *= 2; xrenew(buf, alloc); } } size = pos; }