/* Create a string capable of holding len bytes plus trailing NUL. */ string str_new(unsigned long len) { string x = new_memory(sizeof(unsigned long) + 1 + len); x->len = len; x->data[len] = '\000'; /* Set trailing NUL byte. */ return x; }
HashItem *construct_label(char const *key, char *rest) { HashItem *item; LabelInfo *li = new_memory(1, sizeof(LabelInfo)); li->d_section = lines_size(&global.d_section) - 1; li->d_filenr = global.d_filecount; if (!*rest) { message_error("Missing label name"); return 0; } item = hashitem_construct(VOIDPTR, rest, li, free); if (hashmap_insert(&symtab, item) == FAILED) { message_show(MSG_ERR); message("%s: %s doubly defined", args_programName(), key); return 0; } return hashitem_construct(VOIDPTR, rest, 0, root_nop); }
Grid_Container::Grid_Container(Spheres &spheres, double search_distance, double xmax, double xmin) : Sphere_Container( spheres ), _array( spheres ), _xmax(xmax), _xmin(xmin), _cells(0), _ci(0) { assert( _xmax > _xmin ); const double axis_length = _xmax - _xmin; assert( axis_length >= 0 ); _axis_divisions = (size_t) ceil( axis_length / search_distance ); // _axis_divisions^num_dim _num_cells = (size_t) ceil( pow( _axis_divisions, num_dim() )); _cell_side = axis_length / _axis_divisions; _cell_diagonal = sqrt(num_dim()) * _cell_side; new_memory(); };
vector<pair<PushDownAutomaton::Transition, PushDownAutomaton::SimulationState> > PushDownAutomaton::SimulationState::next(char head, ofstream& ofs) { vector<pair<PushDownAutomaton::Transition, PushDownAutomaton::SimulationState> > result; for (auto t : automaton.states_[state]) { // if not epsilon transition and head does not match transition go to next iteration if (t.tape_symbol != automaton.epsilon && t.tape_symbol != head) { continue; } // new state stack<char> new_memory(memory); string new_state = t.to; int new_tape_position = t.tape_symbol == automaton.epsilon ? tape_position : tape_position + 1; // check stack top, if matches pop, if epsilon transition keeps going, else next iteration if (!memory.empty() && t.pull_symbol == memory.top()) { new_memory.pop(); } else if (t.pull_symbol != automaton.epsilon) { continue; } // push into the stack if (t.push_symbol != automaton.epsilon) { new_memory.push(t.push_symbol); } // push new simulation state result.push_back(make_pair(t, PushDownAutomaton::SimulationState(new_state, new_memory, new_tape_position, automaton))); } return result; }
void queue_push(register Queue *qp, size_t extra_length, char const *info) { register char *cp; size_t memory_length; size_t available_length; size_t begin_length; size_t n_begin; size_t q_length; if (!extra_length) return; memory_length = qp->d_memory_end - qp->d_memory; q_length = qp->d_read <= qp->d_write ? (size_t)(qp->d_write - qp->d_read) : memory_length - (qp->d_read - qp->d_write); available_length = memory_length - q_length - 1; /* -1, as the Q cannot completely fill up all */ /* available memory in the buffer */ if (message_show(MSG_INFO)) message("push_front %u bytes in `%s'", (unsigned)extra_length, info); if (extra_length > available_length) { /* enlarge the buffer: */ memory_length += extra_length - available_length + BLOCK_QUEUE; cp = new_memory(memory_length, sizeof(char)); if (message_show(MSG_INFO)) message("Reallocating queue at %p to %p", qp->d_memory, cp); if (qp->d_read > qp->d_write) /* q wraps around end */ { size_t tail_len = qp->d_memory_end - qp->d_read; memcpy(cp, qp->d_read, tail_len); /* first part -> begin */ /* 2nd part beyond */ memcpy(cp + tail_len, qp->d_memory, (size_t)(qp->d_write - qp->d_memory)); qp->d_write = cp + q_length; qp->d_read = cp; } else /* q as one block */ { memcpy(cp, qp->d_memory, memory_length);/* cp existing buffer */ qp->d_read = cp + (qp->d_read - qp->d_memory); qp->d_write = cp + (qp->d_write - qp->d_memory); } free(qp->d_memory); /* free old memory */ qp->d_memory_end = cp + memory_length; /* update d_memory_end */ qp->d_memory = cp; /* update d_memory */ } /* Write as much as possible at the begin of the buffer, then write the remaining chars at the end. q_length is increased by the length of the info string The first chars to write are at the end of info, and the 2nd part to write are the initial chars of info, since the initial part of info is then read first. */ /* # chars available at the */ begin_length = qp->d_read - qp->d_memory; /* begin of the buffer */ n_begin = extra_length <= begin_length ? /* determine # to write at */ extra_length /* the begin of the buffer */ : begin_length; memcpy /* write trailing part of */ ( /* info first */ qp->d_read -= n_begin, info + extra_length - n_begin, n_begin ); if (extra_length > begin_length) /* not yet all chars written*/ { /* continue with the remaining number of characters. Insert these at*/ /* the end of the buffer */ extra_length -= begin_length; /* reduce # to write */ memcpy /* d_read wraps to the end */ ( /* write info's rest */ qp->d_read = qp->d_memory_end - extra_length, info, extra_length ); } }
void args_construct(int argc, char **argv, char *options, LongOption const *longOption) { char *cp; size_t nopt; struct option *long_option; size_t nlong = 0; if (!longOption) long_option = new_calloc(1, sizeof(struct option)); else { while (longOption[nlong++].d_name) ; long_option = new_memory(nlong, sizeof(struct option)); for (nopt = 0; nopt < nlong; nopt++) { long_option[nopt].name = longOption[nopt].d_name; long_option[nopt].has_arg = longOption[nopt].d_type; long_option[nopt].flag = 0; long_option[nopt].val = longOption[nopt].d_value; } } memset(&args, 0, sizeof(Args)); string_construct(&args.d_option, 0); args.d_argv = argv; args.d_argc = argc; if ((cp = getenv("home")) || (cp = getenv("HOME"))) /* set home */ args.d_home = new_str(cp); args.d_programName = basename(argv[0]); /* set programname */ args.d_initial_dir = new_getcwd(); /* set initial dir. */ args.d_ok = true; nopt = 0; while (true) { int optchar = getopt_long(args.d_argc, args.d_argv, options, long_option, NULL); switch (optchar) { default: string_addchar(&args.d_option, optchar); new_size(&args.d_optarg, nopt + 1, nopt, sizeof(char *)); args.d_optarg[nopt++] = optarg ? new_str(optarg) : 0; // FALLING THROUGH case 'l': continue; /* at `while(true)' */ case '?': args.d_ok = false; /* stop processing at failure */ break; case EOF: args.d_optind = optind; break; } break; /* leave `while(true)' */ } free(long_option); }