static void * evthread_posix_cond_alloc(unsigned condflags) { pthread_cond_t *cond = mm_malloc(sizeof(pthread_cond_t)); if (!cond) return NULL; if (pthread_cond_init(cond, NULL)) { mm_free(cond); return NULL; } return cond; }
/* * Callbacks */ void dxml_error(void *ctx, const char *fmt, va_list ap) { CTX_T *data = (CTX_T*)ctx; int len; char dummy[1], *msg; va_list ap_copy; va_copy(ap_copy, ap); len = vsnprintf(dummy, 1, fmt, ap_copy); va_end(ap_copy); msg = mm_malloc(sizeof(char) * (len + 1)); vsnprintf(msg, len + 1, fmt, ap); linklst_add(msg, data->errors); }
/***************************************************************************** * Creates the data to be passed to the SAX parser ****************************************************************************/ void* create_dreme_io_xml_sax_context(void *user_data, DREME_IO_XML_CALLBACKS_T *callbacks) { PS_T *ps; CHARBUF_T *buf; ps = (PS_T*)mm_malloc(sizeof(PS_T)); memset(ps, 0, sizeof(PS_T)); ps->state = PS_START; ps->udepth = 0; ps->callbacks = callbacks; ps->user_data = user_data; ps->motif_id = NULL; ps->last_pos = 0; //set up character buffer buf = &(ps->characters); buf->buffer = mm_malloc(sizeof(char)*10); buf->buffer[0] = '\0'; buf->size = 10; buf->pos = 0; //set up expected queue ps->expected_stack = linklst_create(); return ps; }
void *mm_realloc(void *ptr, size_t size) { if (!ptr) { return mm_malloc(size); } else { if (size == 0) { return NULL; } struct metadata *block = get_block_ptr(ptr); mm_free(ptr); void *valid = mm_malloc(size); if (!valid) { return NULL; } if (size < block->size) { memcpy(valid, ptr, size); } else { memcpy(valid, ptr, block->size); } return valid; } }
int main(int argc, char **argv) { int *data; data = (int*) mm_malloc(100); data[0] = 1; mm_free(data); printf("malloc sanity test successful!\n"); return 0; }
/* * mm_realloc - Implemented simply in terms of mm_malloc and mm_free */ void *mm_realloc(void *ptr, size_t size) { if(ptr == NULL) return mm_malloc(size); if(size == 0){ mm_free(ptr); return NULL; } return NULL; }
KARLIN_INPUT_T *make_karlin_input( MATRIX_T *matrix, /* scoring matrix */ ARRAY_T *probs /* letter freq distribution */ ) { int i, j; double escore; long lowest, highest; ARRAY_T *score_probs; int nscores; int alen = get_num_rows(matrix); /* size of alphabet */ KARLIN_INPUT_T *karlin_input; /* data to return */ /* find the highest and lowest scores in the scoring matrix */ lowest = 1; highest = -1; for (i=0; i<alen; i++) { for (j=0; j<alen; j++) { double s = get_matrix_cell(i, j, matrix); if (s < lowest) lowest = s; if (s > highest) highest = s; } } if (lowest >= 0) die("Lowest score in scoring matrix must be negative, is %f.", (double)lowest); if (highest<= 0) die("Highest score in scoring matrix must be positve, is %f.", (double)highest); /* allocate the array of score probabilities and set to 0 */ nscores = highest - lowest + 1; score_probs = allocate_array(nscores); init_array(0, score_probs); /* compute the probabilities of different scores */ escore = 0; for (i=0; i<alen; i++) { for (j=0; j<alen; j++) { int s = get_matrix_cell(i, j, matrix); double pi = get_array_item(i, probs); double pj = get_array_item(j, probs); double sp = get_array_item(s-lowest, score_probs); set_array_item(s-lowest, sp + pi*pj, score_probs); /* cumulative prob. of score */ escore += pi*pj*s; /*printf("i %d j %d s %d pi %f pj %f sp %f escore %f\n",i,j,s, pi, pj, sp, escore);*/ } } karlin_input = (KARLIN_INPUT_T *)mm_malloc(sizeof(KARLIN_INPUT_T)); karlin_input->low = lowest; karlin_input->high = highest; karlin_input->escore = escore; karlin_input->prob = score_probs; return(karlin_input); } /* make_karlin_input */
static void * evthread_win32_lock_create(unsigned locktype) { CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION)); if (!lock) return NULL; if (InitializeCriticalSectionAndSpinCount(lock, SPIN_COUNT) == 0) { mm_free(lock); return NULL; } return lock; }
/************************************************************************ * Copy one state of an MHMM. ************************************************************************/ static void copy_state (MHMM_STATE_T * a_state, MHMM_STATE_T * new_state) { new_state->type = a_state->type; /* Allocate memory. */ new_state->itrans_out = (int *)mm_malloc(a_state->ntrans_out * sizeof(int)); new_state->trans_out = allocate_array(a_state->ntrans_out); new_state->itrans_in = (int *)mm_malloc(a_state->ntrans_in * sizeof(int)); new_state->trans_in = allocate_array(a_state->ntrans_in); new_state->emit = allocate_array(get_array_length(a_state->emit)); new_state->emit_odds = allocate_array(get_array_length(a_state->emit_odds)); /* Outgoing transitions. */ new_state->ntrans_out = a_state->ntrans_out; copy_int_array(a_state->ntrans_out, a_state->itrans_out, new_state->itrans_out); copy_array(a_state->trans_out, new_state->trans_out); /* Incoming transitions. */ new_state->ntrans_in = a_state->ntrans_in; copy_int_array(a_state->ntrans_in, a_state->itrans_in, new_state->itrans_in); copy_array(a_state->trans_in, new_state->trans_in); /* Emissions. */ copy_array(a_state->emit, new_state->emit); copy_array(a_state->emit_odds, new_state->emit_odds); new_state->num_sites = a_state->num_sites; // Descriptive information. new_state->i_motif = a_state->i_motif; new_state->w_motif = a_state->w_motif; strcpy(new_state->motif_id, a_state->motif_id); new_state->i_position = a_state->i_position; new_state->id_char = a_state->id_char; }
static void test_realloc_5(void) { ptr = mm_malloc(5); *ptr = 2; ptr2 = mm_realloc(ptr, 16); *(ptr2 + 1) = 4; *(ptr2 + 2) = 6; *(ptr2 + 3) = 8; CU_ASSERT(ptr2[0] == 2); CU_ASSERT(ptr2[1] == 4); CU_ASSERT(ptr2[2] == 6); CU_ASSERT(ptr2[3] == 8); }
void *mm_realloc(void *ptr, size_t size) { blockHdr *bp = ptr-BLK_HDR_SIZE; void *newptr = mm_malloc(size); if (newptr == NULL) return NULL; int copySize = bp->size-BLK_HDR_SIZE; if (size < copySize) copySize = size; memcpy(newptr, ptr, copySize); mm_free(ptr); return newptr; }
/***************************************************************************** * Stores a parser warning message for return ****************************************************************************/ static void local_warning(CTX_T *data, char *format, ...) { va_list argp; int len; char dummy[1], *msg; va_start(argp, format); len = vsnprintf(dummy, 1, format, argp); va_end(argp); msg = mm_malloc(sizeof(char) * (len + 1)); va_start(argp, format); vsnprintf(msg, len + 1, format, argp); va_end(argp); linklst_add(msg, data->warnings); }
//changes the size allocated to a block, if possible void* mm_realloc(void* ptr, size_t size) { size_t oldsize; BlockInfo * blockInfo,*block; void * copyptr; if(ptr == NULL && (size > 0)){ copyptr = mm_malloc(size); mm_free(ptr); } if(size == 0){ mm_free(ptr); copyptr = NULL; } //code for iterating through the freelist. upon each call of realloc, search the freelist, and coalesce all unused blocks //likely inefficent, but sufficient for the project. prevents us from getting a multitude of free blocks that are too big //to be used, which causes mem_sbrk to fail and the program to quit. for(block = (BlockInfo *)UNSCALED_POINTER_ADD(mem_heap_lo(), WORD_SIZE); /* first block on heap */ SIZE(block->sizeAndTags) != 0 && block < mem_heap_hi(); block = (BlockInfo *)UNSCALED_POINTER_ADD(block, SIZE(block->sizeAndTags))) { /* and allocated/free specific data */ if ((block->sizeAndTags & TAG_USED) == 0) { coalesceFreeBlock(block); } } copyptr = mm_malloc(size);//call to mm_malloc for the requested size blockInfo = (BlockInfo *)UNSCALED_POINTER_SUB(ptr,WORD_SIZE); //pointer to the header of ptr oldsize = SIZE(blockInfo->sizeAndTags);//get the size of ptr if(size < oldsize){//if the size of ptr is greater than requested oldsize = size;// change the value of bytes to be copied to the requested size } memcpy(copyptr, ptr, oldsize); //copy "n = oldsize" bytes from ptr to copyptr mm_free(ptr); //return ptr to the freelist return copyptr; // return copyptr }
/* Allocation functions for zlib */ void * mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) { int len = size * ncount; void *address; if (len <= 0) fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); address = mm_malloc(mm, len); return (address); }
/*********************************************************************** * Convert a tree of motifs into an array of motifs with a count. * This is intended to allow backwards compatibility with the older * version. ***********************************************************************/ void motif_tree_to_array(RBTREE_T *motif_tree, MOTIF_T **motif_array, int *num) { int count, i; MOTIF_T *motifs; RBNODE_T *node; count = rbtree_size(motif_tree); motifs = mm_malloc(sizeof(MOTIF_T) * count); for (i = 0, node = rbtree_first(motif_tree); node != NULL; i++, node = rbtree_next(node)) { copy_motif((MOTIF_T*)rbtree_value(node), motifs+i); } *motif_array = motifs; *num = count; }
/* * mm_realloc - Implemented simply in terms of mm_malloc and mm_free */ void *mm_realloc(void *ptr, size_t size) { #ifdef __DEBUG__ printf("Trying to realloc...\n"); #endif void *oldptr = ptr; void *newptr; size_t copySize; newptr = mm_malloc(size); //alloc new mem if(size==0) { mm_free(oldptr); return 0; } if(ptr == NULL) { return mm_malloc(size); } copySize = getSize(getBlockHeader(ptr)); if(size<copySize) copySize = size; memcpy(newptr, oldptr, copySize); //copy mm_free(oldptr); //free old mem return newptr; }
/* Allocation functions for zlib */ void * mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) { size_t len = (size_t) size * ncount; void *address; if (len == 0 || ncount > SIZE_T_MAX / size) fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); address = mm_malloc(mm, len); return (address); }
/*parse_cisml {{{*/ int parse_cisml(CISML_CALLBACKS_T *callbacks, void *state, const char *file_name) { CISML_PARSER_T cisml_parser; CHARBUF_T *buf; xmlSAXHandler handler; int result; DEBUG_FMT(HIGHER_VERBOSE, "CISML parser processing \"%s\"\n", file_name); //initilise parser state cisml_parser.callbacks = callbacks; cisml_parser.invoker_state = state; cisml_parser.state = PS_START; cisml_parser.multi = MP_UNDECIDED; cisml_parser.udepth = 0; //set up character buffer buf = &(cisml_parser.characters); buf->buffer = mm_malloc(sizeof(char)*10); buf->buffer[0] = '\0'; buf->size = 10; buf->pos = 0; //set up handler memset(&handler, 0, sizeof(xmlSAXHandler)); handler.startDocument = handle_cisml_start_doc; handler.endDocument = handle_cisml_end_doc; handler.characters = handle_cisml_characters; handler.startElement = handle_cisml_start_ele; handler.endElement = handle_cisml_end_ele; //parse result = xmlSAXUserParseFile(&handler, &cisml_parser, file_name); //clean up memory free(buf->buffer); buf->buffer = NULL; //check result if (result != 0) { DEBUG_FMT(HIGH_VERBOSE, "CISML parser halted due to SAX error; error code given: %d\n", result); } else { if (cisml_parser.state == PS_END) { DEBUG_MSG(HIGHER_VERBOSE, "CISML parser completed\n"); } else { DEBUG_FMT(HIGH_VERBOSE, "CISML parser did not reach end state; final state was %s\n", state_names[cisml_parser.state]); } } // return true on success return (result == 0 && cisml_parser.state == PS_END); }
int main (int argc, char * argv[]) { int nthreads; int iterations; int objSize; int repetitions; if (argc > 4) { nthreads = atoi(argv[1]); iterations = atoi(argv[2]); objSize = atoi(argv[3]); repetitions = atoi(argv[4]); } else { fprintf (stderr, "Usage: %s nthreads iterations objSize repetitions\n", argv[0]); exit(1); } pthread_t threads[nthreads]; int numCPU = getNumProcessors(); pthread_setconcurrency(numCPU); int i; /* Call allocator-specific initialization function */ mm_init(); pthread_attr_t attr; initialize_pthread_attr(PTHREAD_CREATE_JOINABLE, SCHED_RR, -10, PTHREAD_EXPLICIT_SCHED, PTHREAD_SCOPE_SYSTEM, &attr); timer_start(); for (i = 0; i < nthreads; i++) { struct workerArg * w = (struct workerArg *)mm_malloc(sizeof(struct workerArg)); w->_objSize = objSize; w->_repetitions = repetitions / nthreads; w->_iterations = iterations; w->_cpu = (i+1)%numCPU; pthread_create(&threads[i], &attr, &worker, (void *)w); } for (i = 0; i < nthreads; i++) { pthread_join(threads[i], NULL); } double t = timer_stop(); printf ("Time elapsed = %f seconds\n", t); printf ("Memory used = %d bytes\n",mem_usage()); return 0; }
void * win32_init(struct event_base *base) { struct win32op *winop; size_t size; if (!(winop = mm_calloc(1, sizeof(struct win32op)))) return NULL; winop->num_fds_in_fd_sets = NEVENT; size = FD_SET_ALLOC_SIZE(NEVENT); if (!(winop->readset_in = mm_malloc(size))) goto err; if (!(winop->writeset_in = mm_malloc(size))) goto err; if (!(winop->readset_out = mm_malloc(size))) goto err; if (!(winop->writeset_out = mm_malloc(size))) goto err; if (!(winop->exset_out = mm_malloc(size))) goto err; winop->readset_in->fd_count = winop->writeset_in->fd_count = 0; winop->readset_out->fd_count = winop->writeset_out->fd_count = winop->exset_out->fd_count = 0; if (evsig_init_(base) < 0) winop->signals_are_broken = 1; evutil_weakrand_seed_(&base->weakrand_seed, 0); return (winop); err: XFREE(winop->readset_in); XFREE(winop->writeset_in); XFREE(winop->readset_out); XFREE(winop->writeset_out); XFREE(winop->exset_out); XFREE(winop); return (NULL); }
void *realloc(void *oldptr, size_t size) { //REQUIRES (oldptr!=NULL); REQUIRES ((size_t)(oldptr)%8 == 0); //printf ("realloc %d\n",(int)size); size_t oldsize; void *newptr; /* If size == 0 then this is just free, and we return NULL. */ if(size == 0) { mm_free(oldptr); return 0; } /* If oldptr is NULL, then this is just malloc. */ if(oldptr == NULL) { return mm_malloc(size); } newptr = mm_malloc(size); /* If realloc() fails the original block is left untouched */ if(!newptr) { return 0; } /* Copy the old data. */ oldsize = GET_SIZE(HDRP(oldptr)); if(size < oldsize) oldsize = size; memcpy(newptr, oldptr, oldsize); /* Free the old block. */ mm_free(oldptr); ENSURES ( (size_t)(newptr)%8 == 0); return newptr; }
static void push_state(JSONRD_T *jsonrd, PS_EN state) { HISTORY_T *stack; stack = &(jsonrd->stack); if (stack->used >= stack->length) { if (stack->length == 0) { stack->length = 4; stack->states = mm_malloc(sizeof(PS_EN) * stack->length); } else { stack->length *= 2; stack->states = mm_realloc(stack->states, sizeof(PS_EN) * stack->length); } } stack->states[stack->used++] = state; }
void *epoll_init(EL_P loop) { EEPOLL_P ept = (EEPOLL_P)mm_malloc(sizeof(struct evt_epoll)); memset(ept, 0, sizeof(struct evt_epoll)); /* use epoll_create1 first */ #ifdef EPOLL_CLOEXEC ept->fd = epoll_create1(EPOLL_CLOEXEC); #else ept->fd = epoll_create(1); if (ept->fd >= 0) fd_cloexec(ept->fd); #endif if (ept->fd < 0) { goto epoll_init_failed; } ept->feature = loop->poll_feature; ept->nevent = EPOLL_INIT_NEVENT; ept->event = (EP_EVT*)mm_malloc(sizeof(EP_EVT) * EPOLL_INIT_NEVENT); /* init fucntion pointer with loop*/ loop->poll_destroy = epoll_destroy; loop->poll_dispatch = epoll_dispatch; loop->poll_update = epoll_update; log_inner("epoll(%d) init complete!", ept->fd); return ept; epoll_init_failed: log_error("epoll init failed!"); if (ept->fd > 0) close (ept->fd); if (ept->event) mm_free(ept->event); mm_free(ept); return NULL; }
/* * realloc - Naive implementation of realloc */ void *realloc(void *ptr, size_t size) { size_t oldsize; void *newptr; /* If size == 0 then this is just free, and we return NULL. */ if(size == 0) { mm_free(ptr); return 0; } /* If oldptr is NULL, then this is just malloc. */ if(ptr == NULL) { return mm_malloc(size); } newptr = mm_malloc(size); /* If realloc() fails the original block is left untouched */ if(!newptr) { return 0; } /* Copy the old data. */ oldsize = GET_SIZE(HDRP(ptr)); if(size < oldsize) oldsize = size; memcpy(newptr, ptr, oldsize); /* Free the old block. */ mm_free(ptr); line_count++; if (CHECK && CHECK_REALLOC) { mm_check('r', ptr, size); } return newptr; }
/***************************************************************************** * Creates a JSON reader that can extract data sections marked with the * landmark @JSON_VAR ****************************************************************************/ JSONRD_T* jsonrd_create(JSONRD_CALLBACKS_T *callbacks, void *user_data) { JSONRD_T *jsonrd; jsonrd = mm_malloc(sizeof(JSONRD_T)); memset(jsonrd, 0, sizeof(JSONRD_T)); jsonrd->state = PS_FIND_LANDMARK; jsonrd->prior_states = rbtree_create(rbtree_intcmp, rbtree_intcpy, free, NULL, NULL); jsonrd->landmark = bmstr_create("@JSON_VAR "); jsonrd->buf = str_create(0); jsonrd->user_data = user_data; // setup callbacks jsonrd->callbacks.error = callbacks->error; jsonrd->callbacks.start_data = callbacks->start_data; jsonrd->callbacks.end_data = callbacks->end_data; jsonrd->callbacks.start_property = callbacks->start_property; jsonrd->callbacks.end_property = callbacks->end_property; jsonrd->callbacks.start_object = callbacks->start_object; jsonrd->callbacks.end_object = callbacks->end_object; jsonrd->callbacks.start_list = callbacks->start_list; jsonrd->callbacks.end_list = callbacks->end_list; jsonrd->callbacks.atom_null = callbacks->atom_null; jsonrd->callbacks.atom_bool = callbacks->atom_bool; jsonrd->callbacks.atom_number = callbacks->atom_number; jsonrd->callbacks.atom_string = callbacks->atom_string; // setup state stack jsonrd->stack.used = 0; jsonrd->stack.length = 4; jsonrd->stack.states = mm_malloc(sizeof(PS_EN) * jsonrd->stack.length); // setup token jsonrd->token.state = TS_FIND_TOKEN; jsonrd->token.type = TOK_STARTOBJ; jsonrd->token.value_bool = false; jsonrd->token.num_state = NS_BEGIN; jsonrd->token.value_number = 0; jsonrd->token.str_state = SS_NORMAL; jsonrd->token.value_string = str_create(0); return jsonrd; }
/***************************************************************************** * MEME > training_set > /alphabet * Read in the number of symbols in the alphabet and if it is nucleotide or * amino-acid (RNA is apparently classed as nucleotide). ****************************************************************************/ void mxml_end_alphabet(void *ctx) { PARMSG_T *message; CTX_T *data; RBNODE_T *node; char *id, symbol; bool *exists; int i; data = (CTX_T*)ctx; if (data->alph == NULL) { // Custom alphabet alph_reader_done(data->alph_rdr); // report any errors that the alphabet reader found while (alph_reader_has_message(data->alph_rdr)) { message = alph_reader_next_message(data->alph_rdr); if (message->severity == SEVERITY_ERROR) { local_error(data, "Alphabet error: %s.\n", message->message); } else { local_warning(data, "Alphabet warning: %s.\n", message->message); } parmsg_destroy(message); } // try to get an alphabet data->alph = alph_reader_alphabet(data->alph_rdr); alph_reader_destroy(data->alph_rdr); data->alph_rdr = NULL; } else { // legacy alphabet exists = mm_malloc(sizeof(bool) * alph_size_core(data->alph)); // set list to false for (i = 0; i < alph_size_core(data->alph); i++) exists[i] = false; // check that id's were defined for all the core alphabet symbols for (node = rbtree_first(data->letter_lookup); node != NULL; node = rbtree_next(node)) { id = (char*)rbtree_key(node); symbol = ((char*)rbtree_value(node))[0]; if (exists[alph_indexc(data->alph, symbol)]) { // duplicate! local_error(data, "The letter identifier %s is not the first to refer to symbol %c.\n", id, symbol); } exists[alph_indexc(data->alph, symbol)] = true; } // now check for missing identifiers for (i = 0; i < alph_size_core(data->alph); i++) { if (!exists[i]) { // missing id for symbol local_error(data, "The symbol %c does not have an assigned identifier.\n", alph_char(data->alph, i)); } } free(exists); } }
// Unchecked void* mm_realloc(void* ptr, size_t size) { if (ptr == NULL) { return mm_malloc(size); } else if (ptr != NULL && size == 0) { free(ptr); return NULL; } else { void *newPtr = mm_malloc(size); BlockInfo *oldBlock = (BlockInfo*)POINTER_SUB(ptr, WORD_SIZE); size_t oldBlockSize = oldBlock->sizeAndTags; size_t oldDataSize = oldBlockSize - WORD_SIZE; size_t sizeToCopy = size < oldDataSize ? size : oldDataSize; size_t i; // assume size to be copies is the multiple of WORD_SIZE for (i = 0; i < sizeToCopy; ++i) { *(char*)newPtr = *(char*)ptr; } free(ptr); return newPtr; } }
static void * evthread_posix_lock_alloc(unsigned locktype) { pthread_mutexattr_t *attr = NULL; pthread_mutex_t *lock = mm_malloc(sizeof(pthread_mutex_t)); if (!lock) return NULL; if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE) attr = &attr_recursive; if (pthread_mutex_init(lock, attr)) { mm_free(lock); return NULL; } return lock; }
/* * mm_realloc - Implemented simply in terms of mm_malloc and mm_free */ void *mm_realloc(void *ptr, size_t size) { void *oldptr = ptr; void *newptr; size_t oldsize = GET_SIZE(HDRP(ptr)); // size_t copySize; if (oldptr==NULL){ return mm_malloc(size); } if (size==0){ mm_free(ptr); // return 0 } if (oldsize==size){ return oldptr; } if (size < oldsize){ newptr = mm_malloc(size); memcpy(newptr, oldptr, size); mm_free(oldptr); } if (size > oldsize){ newptr = mm_malloc(size); memcpy(newptr, oldptr, oldsize); mm_free(oldptr); } return newptr; }
FAR void *malloc(size_t size) { #ifdef CONFIG_BUILD_KERNEL FAR void *brkaddr; FAR void *mem; /* Loop until we successfully allocate the memory or until an error * occurs. If we fail to allocate memory on the first pass, then call * sbrk to extend the heap by one page. This may require several * passes if more the size of the allocation is more than one page. * * An alternative would be to increase the size of the heap by the * full requested allocation in sbrk(). Then the loop should never * execute more than twice (but more memory than we need may be * allocated). */ do { mem = mm_malloc(USR_HEAP, size); if (!mem) { brkaddr = sbrk(size); if (brkaddr == (FAR void *)-1) { return NULL; } } } while (mem == NULL); return mem; #else return mm_malloc(USR_HEAP, size); #endif }