int add_defined (struct fsm *net, char *string) { struct defined *defined, *defined_prev; int redefine; redefine = 0; if (net == NULL) { return 0; } fsm_count(net); if (defines == NULL) { defined = xxmalloc(sizeof(struct defined)); defines = defined; defined->next = NULL; } else { for (defined = defines; defined != NULL; defined = defined->next) { defined_prev = defined; if (strcmp(defined->name, string) == 0) { redefine = 1; break; } } if (redefine == 0) { defined_prev->next = xxmalloc(sizeof(struct defined)); defined = defined_prev->next; defined->next = NULL; } } if (redefine) { fsm_destroy(defined->net); xxfree(defined->name); } defined->name = xxstrdup(string); defined->net = net; return(redefine); }
struct apply_handle *apply_init(struct fsm *net) { struct apply_handle *h; srand((unsigned int) time(NULL)); h = malloc(sizeof(struct apply_handle)); /* Init */ h->iterate_old = 0; h->iterator = 0; h->instring = NULL; h->flag_list = NULL; h->flag_lookup = NULL; h->last_net = net; h->outstring = xxmalloc(sizeof(char)*DEFAULT_OUTSTRING_SIZE); h->outstringtop = DEFAULT_OUTSTRING_SIZE; *(h->outstring) = '\0'; h->gstates = net->states; h->gsigma = net->sigma; h->printcount = 1; apply_create_statemap(h, net); h->searchstack = xxmalloc(sizeof(struct searchstack) * DEFAULT_STACK_SIZE); h->apply_stack_top = DEFAULT_STACK_SIZE; apply_stack_clear(h); apply_create_sigarray(h, net); return(h); }
static void * my_memalign_hook (size_t size, size_t alignment, const void *) { // Check for non power-of-two alignment, or mistake in size. if ((alignment == 0) || (alignment & (alignment - 1))) { return NULL; } // Try to just allocate an object of the requested size. // If it happens to be aligned properly, just return it. void * ptr = xxmalloc (size); if (((size_t) ptr & (alignment - 1)) == (size_t) ptr) { // It is already aligned just fine; return it. return ptr; } // It was not aligned as requested: free the object. xxfree (ptr); // Now get a big chunk of memory and align the object within it. // NOTE: this REQUIRES that the underlying allocator be able // to free the aligned object, or ignore the free request. void * buf = xxmalloc (2 * alignment + size); void * alignedPtr = (void *) (((size_t) buf + alignment - 1) & ~(alignment - 1)); return alignedPtr; }
static void sigma_to_pairs(struct fsm *net) { int i, j, x, y, z, next_x = 0; struct fsm_state *fsm; fsm = net->states; epsilon_symbol = -1; maxsigma = sigma_max(net->sigma); maxsigma++; single_sigma_array = xxmalloc(2*maxsigma*maxsigma*sizeof(int)); double_sigma_array = xxmalloc(maxsigma*maxsigma*sizeof(int)); for (i=0; i < maxsigma; i++) { for (j=0; j< maxsigma; j++) { *(double_sigma_array+maxsigma*i+j) = -1; } } /* f(x) -> y,z sigma pair */ /* f(y,z) -> x simple entry */ /* if exists f(n) <-> EPSILON, EPSILON, save n */ /* symbol(x) x>=1 */ /* Forward mapping: */ /* *(double_sigma_array+maxsigma*in+out) */ /* Backmapping: */ /* *(single_sigma_array+(symbol*2) = in(symbol) */ /* *(single_sigma_array+(symbol*2+1) = out(symbol) */ /* Table for checking whether a state is final */ x = 0; net->arity = 1; for (i=0; (fsm+i)->state_no != -1; i++) { y = (fsm+i)->in; z = (fsm+i)->out; if ((y == -1) || (z == -1)) continue; if (y != z || y == UNKNOWN || z == UNKNOWN) net->arity = 2; if (*(double_sigma_array+maxsigma*y+z) == -1) { *(double_sigma_array+maxsigma*y+z) = x; *(single_sigma_array+next_x) = y; next_x++; *(single_sigma_array+next_x) = z; next_x++; if (y == EPSILON && z == EPSILON) { epsilon_symbol = x; } x++; } } num_symbols = x; }
static void memoize_e_closure(struct fsm_state *fsm) { int i, state, laststate, *redcheck; struct e_closure_memo *ptr; e_closure_memo = xxcalloc(num_states,sizeof(struct e_closure_memo)); marktable = xxcalloc(num_states,sizeof(int)); /* Table for avoiding redundant epsilon arcs in closure */ redcheck = xxmalloc(num_states*sizeof(int)); for (i=0; i < num_states; i++) { ptr = e_closure_memo+i; ptr->state = i; ptr->target = NULL; *(redcheck+i) = -1; } laststate = -1; for (i=0; ;i++) { state = (fsm+i)->state_no; if (state != laststate) { if (!int_stack_isempty()) { deterministic = 0; ptr = e_closure_memo+laststate; ptr->target = e_closure_memo+int_stack_pop(); while (!int_stack_isempty()) { ptr->next = xxmalloc(sizeof(struct e_closure_memo)); ptr->next->state = laststate; ptr->next->target = e_closure_memo+int_stack_pop(); ptr->next->next = NULL; ptr = ptr->next; } } } if (state == -1) { break; } if ((fsm+i)->target == -1) { continue; } /* Check if we have a redundant epsilon arc */ if ((fsm+i)->in == EPSILON && (fsm+i)->out == EPSILON) { if (*(redcheck+((fsm+i)->target)) != (fsm+i)->state_no) { if ((fsm+i)->target != (fsm+i)->state_no) { int_stack_push((fsm+i)->target); *(redcheck+((fsm+i)->target)) = (fsm+i)->state_no; } } laststate = state; } } xxfree(redcheck); }
void apply_create_sigarray(struct apply_handle *h, struct fsm *net) { struct sigma *sig; struct fsm_state *fsm; int i, maxsigma; fsm = net->states; maxsigma = sigma_max(net->sigma); // Default size created at init, resized later if necessary h->sigmatch_array = xxcalloc(1024,sizeof(struct sigmatch_array)); h->sigmatch_array_size = 1024; h->sigs = xxmalloc(sizeof(char **)*(maxsigma+1)); h->has_flags = 0; h->flag_list = NULL; /* Malloc first array of trie and store trie ptrs to free later */ h->sigma_trie = xxcalloc(256,sizeof(struct sigma_trie)); h->sigma_trie_arrays = xxmalloc(sizeof(struct sigma_trie_arrays)); h->sigma_trie_arrays->arr = h->sigma_trie; h->sigma_trie_arrays->next = NULL; for (i=0;i<256;i++) (h->sigma_trie+i)->next = NULL; for (sig = h->gsigma; sig != NULL && sig->number != -1; sig = sig->next) { if (flag_check(sig->symbol)) { h->has_flags = 1; apply_add_flag(h, flag_get_name(sig->symbol)); } *(h->sigs+(sig->number)) = sig->symbol; /* Add sigma entry to trie */ if (sig->number > IDENTITY) { apply_add_sigma_trie(h, sig->number, sig->symbol); } } if (h->has_flags) { h->flag_lookup = xxmalloc(sizeof(struct flag_lookup)*(maxsigma+1)); for (i=0; i <= maxsigma; i++) { (h->flag_lookup+i)->type = 0; (h->flag_lookup+i)->name = NULL; (h->flag_lookup+i)->value = NULL; } for (sig = h->gsigma; sig != NULL ; sig = sig->next) { if (flag_check(sig->symbol)) { (h->flag_lookup+sig->number)->type = flag_get_type(sig->symbol); (h->flag_lookup+sig->number)->name = flag_get_name(sig->symbol); (h->flag_lookup+sig->number)->value = flag_get_value(sig->symbol); } } } }
static void * my_realloc_hook (void * ptr, size_t sz, const void *) { // NULL ptr = malloc. if (ptr == NULL) { return xxmalloc(sz); } if (sz == 0) { xxfree (ptr); #if defined(__APPLE__) // 0 size = free. We return a small object. This behavior is // apparently required under Mac OS X and optional under POSIX. return xxmalloc(1); #else // For POSIX, don't return anything. return NULL; #endif } size_t objSize = xxmalloc_usable_size(ptr); #if 0 // Custom logic here to ensure we only do a logarithmic number of // reallocations (with a constant space overhead). // Don't change size if the object is shrinking by less than half. if ((objSize / 2 < sz) && (sz <= objSize)) { // Do nothing. return ptr; } // If the object is growing by less than 2X, double it. if ((objSize < sz) && (sz < objSize * 2)) { sz = objSize * 2; } #endif void * buf = xxmalloc(sz); if (buf != NULL) { // Successful malloc. // Copy the contents of the original object // up to the size of the new block. size_t minSize = (objSize < sz) ? objSize : sz; memcpy (buf, ptr, minSize); xxfree (ptr); } // Return a pointer to the new one. return buf; }
struct nvpair *nvpair_create() { struct nvpair *n; n = xxmalloc(sizeof(*n)); n->table = hash_table_create(7, hash_string); return n; }
void pfs_resolve_add_entry( const char *prefix, const char *redirect, mode_t mode ) { assert(prefix); assert(redirect); char real_redirect[PFS_PATH_MAX]; struct pfs_mount_entry *ns = pfs_process_current_ns(); if (!ns) ns = mount_list; assert(ns); debug(D_RESOLVE,"resolving %s in parent ns",redirect); switch (pfs_resolve_ns(find_parent_ns(ns), redirect, real_redirect, mode, 0)) { case PFS_RESOLVE_CHANGED: case PFS_RESOLVE_UNCHANGED: break; default: debug(D_NOTICE,"couldn't resolve redirect %s",prefix); return; } struct pfs_mount_entry *m = (struct pfs_mount_entry *) xxmalloc(sizeof(*m)); memcpy(m, ns, sizeof(*m)); memset(ns, 0, sizeof(*ns)); strcpy(ns->prefix, prefix); strcpy(ns->redirect, real_redirect); ns->mode = mode; ns->next = m; ns->refcount = m->refcount; m->refcount = 1; pfs_resolve_cache_flush(); }
struct work_queue_master *duplicate_work_queue_master(struct work_queue_master *master) { struct work_queue_master *m; m = xxmalloc(sizeof(struct work_queue_master)); if(!m) return NULL; strncpy(m->addr, master->addr, LINK_ADDRESS_MAX); strncpy(m->proj, master->proj, WORK_QUEUE_NAME_MAX); m->port = master->port; m->priority = master->priority; m->capacity = master->capacity; m->tasks_waiting = master->tasks_waiting; m->tasks_running = master->tasks_running; m->tasks_complete = master-> tasks_complete; m->total_tasks_dispatched = master-> total_tasks_dispatched; m->workers_init = master->workers_init; m->workers_ready = master->workers_ready; m->workers_busy = master->workers_busy; m->workers_full = master->workers_full; m->workers = master->workers; if(master->workers_by_pool) { m->workers_by_pool = xxstrdup(master->workers_by_pool); } else { m->workers_by_pool = NULL; } strncpy(m->owner, master->owner, USERNAME_MAX); return m; }
int add_defined(struct defined_networks *def, struct fsm *net, char *string) { struct defined_networks *d; if (net == NULL) return 0; fsm_count(net); for (d = def; d != NULL; d = d->next) { if (d->name != NULL && strcmp(d->name, string) == 0) { xxfree(d->net); xxfree(d->name); d->net = net; d->name = xxstrdup(string); return 1; } } if (def->name == NULL) { d = def; } else { d = xxmalloc(sizeof(struct defined_networks)); d->next = def->next; def->next = d; } d->name = xxstrdup(string); d->net = net; return 0; }
char *chirp_ticket_tostring(struct chirp_ticket *ct) { size_t n; const char *s; char *result; buffer_t *B; B = buffer_create(); buffer_printf(B, "subject \"%s\"\n", ct->subject); buffer_printf(B, "ticket \"%s\"\n", ct->ticket); buffer_printf(B, "expiration \"%lu\"\n", (unsigned long) ct->expiration); for(n = 0; n < ct->nrights; n++) { buffer_printf(B, "rights \"%s\" \"%s\"\n", ct->rights[n].directory, ct->rights[n].acl); } s = buffer_tostring(B, &n); result = xxmalloc(n + 1); memset(result, 0, n + 1); memcpy(result, s, n); buffer_delete(B); return result; }
void fsm_construct_add_arc(struct fsm_construct_handle *handle, int source, int target, char *in, char *out) { struct fsm_state_list *sl; struct fsm_trans_list *tl; int symin, symout; fsm_construct_check_size(handle, source); fsm_construct_check_size(handle, target); if (source > handle->maxstate) handle->maxstate = source; if (target > handle->maxstate) handle->maxstate = target; sl = (handle->fsm_state_list)+target; sl->used = 1; sl = (handle->fsm_state_list)+source; sl->used = 1; tl = xxmalloc(sizeof(struct fsm_trans_list)); tl->next = sl->fsm_trans_list; sl->fsm_trans_list = tl; if ((symin = fsm_construct_check_symbol(handle,in)) == -1) symin = fsm_construct_add_symbol(handle,in); if ((symout = fsm_construct_check_symbol(handle,out)) == -1) symout = fsm_construct_add_symbol(handle,out); tl->in = symin; tl->out = symout; tl->target = target; }
static int ticket_translate(const char *name, char *ticket_subject) { char command[PATH_MAX * 2 + 4096]; const char *dummy; if(chirp_ticket_isticketsubject(name, &dummy)) { strcpy(ticket_subject, name); return 1; } char *pk = xxmalloc(65536); /* max size of public key */ sprintf(command, "sed '/^\\s*#/d' < '%s' | openssl rsa -pubout 2> /dev/null", name); FILE *pkf = popen(command, "r"); int length = fread(pk, sizeof(char), 65536, pkf); if(length <= 0) { pclose(pkf); return 0; } pk[length] = 0; pclose(pkf); /* load the digest */ const char *digest = chirp_ticket_digest(pk); sprintf(ticket_subject, "ticket:%s", digest); return 1; }
static INT64_T do_ticket_get(int argc, char **argv) { char *subject; char *ticket; time_t duration; char **rights; INT64_T result = chirp_reli_ticket_get(current_host, argv[1], &subject, &ticket, &duration, &rights, stoptime); if(result == 0) { printf("%s\n", subject); free(subject); /* base64 encode the ticket so it fits on one line */ char *bticket = xxmalloc(sizeof(char) * strlen(ticket) * 2 + 10); /* double is more than 4/3 needed */ b64_encode(ticket, strlen(ticket), bticket, strlen(ticket) * 2 + 10); printf("%s\n", bticket); free(bticket); free(ticket); printf("%llu\n", (unsigned long long) duration); char **tmp = rights; for(; tmp && tmp[0] && tmp[1]; tmp += 2) { printf("%s %s\n", tmp[0], tmp[1]); free(tmp[0]); free(tmp[1]); } free(rights); } return result; }
buffer_t *buffer_create(void) { buffer_t *b = xxmalloc(sizeof(buffer_t)); b->buf = NULL; b->size = 0; return b; }
char *file_to_mem(char *name) { FILE *infile; size_t numbytes; char *buffer; infile = fopen(name, "r"); if(infile == NULL) { printf("Error opening file '%s'\n",name); return NULL; } fseek(infile, 0L, SEEK_END); numbytes = ftell(infile); fseek(infile, 0L, SEEK_SET); buffer = (char*)xxmalloc((numbytes+1) * sizeof(char)); if(buffer == NULL) { printf("Error reading file '%s'\n",name); return NULL; } if (fread(buffer, sizeof(char), numbytes, infile) != numbytes) { printf("Error reading file '%s'\n",name); return NULL; } fclose(infile); *(buffer+numbytes)='\0'; return(buffer); }
struct io_buf_handle *io_init() { struct io_buf_handle *iobh; iobh = xxmalloc(sizeof(struct io_buf_handle)); (iobh->io_buf) = NULL; (iobh->io_buf_ptr) = NULL; return(iobh); }
static int ast_do_simple( int line, int argc, char **argv, int fds[3], time_t stoptime ) { char *cmd; int length=0; int i; for( i=0; i<argc; i++ ) { length+=strlen(argv[i])+1; } cmd = xxmalloc(length+1); cmd[0] = 0; for( i=0; i<argc; i++ ) { strcat(cmd,argv[i]); strcat(cmd," "); } ftsh_error(FTSH_ERROR_COMMAND,line,"%s",cmd); free(cmd); if(hash_table_lookup(ftable,argv[0]) || builtin_lookup(argv[0]) ) { return ast_do_internal(line,argc,argv,fds,stoptime); } else { return ast_do_external(line,argc,argv,fds,stoptime); } }
struct list *get_workload_specs(const char *path) { struct list *specs = list_create(); if(!specs) { fprintf(stderr, "Cannot allocate memory for creating list!\n"); return NULL; } // Read in new configuration from file FILE *fp; fp = fopen(path, "r"); if(!fp) { fprintf(stderr, "Failed to open workload specification file at %s.\n", path); return NULL; } char *line; int line_count = 0; while((line = get_line(fp))) { line_count++; string_chomp(line); if(string_isspace(line)) { // skip empty lines continue; } if(line[0] == '#') { // skip comment lines continue; } int submit, input, exe, output, num; if(sscanf(line, "%d %d %d %d %d", &submit, &input, &exe, &output, &num) == 5) { if(submit < 0 || input <=0 || exe <=0 || output <=0 || num <=0) { fprintf(stderr, "Other than the submit_time field, every other field should be greater than 0.\n"); goto fail; } struct task_series *ts = (struct task_series *)xxmalloc(sizeof(struct task_series)); ts->submit_time = submit; ts->input_size = input; ts->execution_time = exe; ts->output_size = output; ts->num_of_tasks = num; list_push_priority(specs, ts, ts->submit_time); } else { fprintf(stderr, "Line %d is invalid: %s\n", line_count, line); goto fail; } } fclose(fp); return specs; fail: // delete list list_free(specs); list_delete(specs); fclose(fp); return NULL; }
void apply_add_sigma_trie(struct apply_handle *h, int number, char *symbol, int len) { /* Create a trie of sigma symbols (prefixes) so we can */ /* quickly (in O(n)) tokenize an arbitrary string into */ /* integer sequences representing symbols, using longest- */ /* leftmost factorization. */ int i; struct sigma_trie *st; struct sigma_trie_arrays *sta; st = h->sigma_trie; for (i = 0; i < len; i++) { st = st+(unsigned char)*(symbol+i); if (i == (len-1)) { st->signum = number; } else { if (st->next == NULL) { st->next = xxcalloc(256,sizeof(struct sigma_trie)); st = st->next; /* store these arrays to free them later */ sta = xxmalloc(sizeof(struct sigma_trie_arrays)); sta->arr = st; sta->next = h->sigma_trie_arrays; h->sigma_trie_arrays = sta; } else { st = st->next; } } } }
void apply_add_sigma_trie(struct apply_handle *h, int number, char *symbol) { int i, len; struct sigma_trie *st; struct sigma_trie_arrays *sta; len = strlen(symbol); st = h->sigma_trie; for (i = 0; i < len; i++) { st = st+(unsigned char)*(symbol+i); if (i == (len-1)) { st->signum = number; } else { if (st->next == NULL) { st->next = xxcalloc(256,sizeof(struct sigma_trie)); st = st->next; /* store these arrays to free them later */ sta = xxmalloc(sizeof(struct sigma_trie_arrays)); sta->arr = st; sta->next = h->sigma_trie_arrays; h->sigma_trie_arrays = sta; } else { st = st->next; } } } }
static int nhash_insert(int hashval, int *set, int setsize) { struct nhash_list *tableptr; int i, fs = 0; current_setnum++; tableptr = table+hashval; nhash_load++; for (i = 0; i < setsize; i++) { if (finals[*(set+i)]) fs = 1; } if (tableptr->size == 0) { tableptr->set_offset = move_set(set, setsize); tableptr->size = setsize; tableptr->setnum = current_setnum; add_T_ptr(current_setnum, setsize, tableptr->set_offset, fs); return(current_setnum); } tableptr = xxmalloc(sizeof(struct nhash_list)); tableptr->next = (table+hashval)->next; (table+hashval)->next = tableptr; tableptr->setnum = current_setnum; tableptr->size = setsize; tableptr->set_offset = move_set(set, setsize); add_T_ptr(current_setnum, setsize, tableptr->set_offset, fs); return(current_setnum); }
int sigma_sort(struct fsm *net) { #ifdef ORIGINAL int(*comp)() = ssortcmp; #else int(*comp)(const void*, const void*) = ssortcmp; #endif int size, i, max, *replacearray; struct ssort *ssort; struct sigma *sigma; struct fsm_state *fsm_state; size = sigma_max(net->sigma); if (size < 0) { return 1; } ssort = xxmalloc(sizeof(struct ssort)*size); for (i=0, sigma=net->sigma; sigma != NULL; sigma=sigma->next) { if (sigma->number > IDENTITY) { ssort[i].symbol = (char *)sigma->symbol; ssort[i].number = sigma->number; i++; } } max = i; qsort(ssort, max, sizeof(struct ssort), comp); replacearray = xxmalloc(sizeof(int)*(size+3)); for (i=0; i<max; i++) replacearray[(ssort+i)->number] = i+3; /* Replace arcs */ for(i=0, fsm_state = net->states; (fsm_state+i)->state_no != -1; i++) { if ((fsm_state+i)->in > IDENTITY) (fsm_state+i)->in = replacearray[(fsm_state+i)->in]; if ((fsm_state+i)->out > IDENTITY) (fsm_state+i)->out = replacearray[(fsm_state+i)->out]; } /* Replace sigma */ for (i=0, sigma=net->sigma; sigma != NULL; sigma=sigma->next) { if (sigma->number > IDENTITY) { sigma->number = i+3; sigma->symbol = (ssort+i)->symbol; i++; } } xxfree(replacearray); xxfree(ssort); return(1); }
void sigma_cleanup (struct fsm *net, int force) { int i,j,first,maxsigma,*attested; struct fsm_state *fsm; struct sigma *sig, *sig_prev, *sign; if (force == 0) { if (sigma_find_number(IDENTITY, net->sigma) != -1) return; if (sigma_find_number(UNKNOWN, net->sigma) != -1) return; } maxsigma = sigma_max(net->sigma); if (maxsigma < 0) { return; } attested = xxmalloc(sizeof(int)*(maxsigma+1)); for (i=0; i<=maxsigma; i++) *(attested+i) = 0; fsm = net->states; for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->in >=0) *(attested+(fsm+i)->in) = 1; if ((fsm+i)->out >=0) *(attested+(fsm+i)->out) = 1; } for (i=3,j=3; i<=maxsigma;i++ ) { if (*(attested+i)) { *(attested+i) = j; j++; } } for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->in > 2) (fsm+i)->in = *(attested+(fsm+i)->in); if ((fsm+i)->out > 2) (fsm+i)->out = *(attested+(fsm+i)->out); } sig_prev = NULL; for (sig = net->sigma; sig != NULL && sig->number != -1; sig = sign) { first = 1; sign = sig->next; if (!*(attested+(sig->number))) { xxfree(sig->symbol); xxfree(sig); if (sig_prev != NULL) { sig_prev->next = sign; first = 0; } else { first = 0; net->sigma = sign; } } else { sig->number = sig->number >= 3 ? *(attested+(sig->number)) : sig->number; } if (first) sig_prev = sig; } xxfree(attested); return; }
void apply_create_statemap(struct apply_handle *h, struct fsm *net) { int i; struct fsm_state *fsm; fsm = net->states; h->statemap = xxmalloc(sizeof(int)*net->statecount); h->marks = xxmalloc(sizeof(int)*net->statecount); for (i=0; i < net->statecount; i++) { *(h->statemap+i) = -1; *(h->marks+i) = 0; } for (i=0; (fsm+i)->state_no != -1; i++) { if (*(h->statemap+(fsm+i)->state_no) == -1) { *(h->statemap+(fsm+i)->state_no) = i; } } }
static void add_mount_entry( const char *prefix, const char *redirect ) { struct mount_entry * m = xxmalloc(sizeof(*m)); strcpy(m->prefix,prefix); strcpy(m->redirect,redirect); m->next = mount_list; mount_list = m; }
void debug_config_fatal(void (*callback) ()) { struct fatal_callback *f; f = xxmalloc(sizeof(*f)); f->callback = callback; f->next = fatal_callback_list; fatal_callback_list = f; }
struct sigma *sigma_create() { struct sigma *sigma; sigma = xxmalloc(sizeof(struct sigma)); sigma->number = -1; /*Empty sigma*/ sigma->next = NULL; sigma->symbol = NULL; return(sigma); }
void * operator new (size_t sz) throw (std::bad_alloc) { void * ptr = xxmalloc (sz); if (ptr == NULL) { throw std::bad_alloc(); } else { return ptr; } }