sdm_t *sdm_dup(const sdm_t *src) { ASSERT_RETURN_IF(!src, NULL); size_t nsubmaps = sdm_size(src); ASSERT_RETURN_IF(!nsubmaps, NULL); const sm_t **maps = sdm_submaps_r(src); ASSERT_RETURN_IF(!maps, NULL); ASSERT_RETURN_IF(!maps[0], NULL); enum eSM_Type t = (enum eSM_Type)maps[0]->f.type; ASSERT_RETURN_IF(t >= SM_TotalTypes, NULL); size_t alloc_size = sizeof(sdm_t) + sizeof(sm_t *) * (nsubmaps - 1); sdm_t *dm = (sdm_t *)__sd_malloc(alloc_size); ASSERT_RETURN_IF(!dm, NULL); size_t i = 0; for (; i < nsubmaps; i++ ) { if (!(dm->maps[i] = sm_dup(maps[i]))) break; /* Allocation error */ } if (i != nsubmaps) { /* Handle allocation error */ for (; i < nsubmaps; i++ ) { if (dm->maps[i]) sm_free(&dm->maps[i]); else break; } free(dm); dm = NULL; } return dm; }
pset do_sm_minimum_cover(pset_family A) { sm_matrix *M; sm_row *sparse_cover; sm_element *pe; pset cover; register int i, base, rownum; register unsigned val; register pset last, p; M = sm_alloc(); rownum = 0; for( p=A->data, last= p+A->count*A->wsize; p< last; p+=A->wsize) { for( i = (p[0] & 0x03ff); i > 0; ) for( val = p[ i], base = -- i << 5; val != 0; base++, val >>= 1) if ( val & 1) { (void) sm_insert(M, rownum, base); } rownum++; } sparse_cover = sm_minimum_cover(M, ((int *) 0), 1, 0); sm_free(M); cover = set_clear(((unsigned int *) malloc(sizeof(unsigned int) * ( ((A->sf_size) <= 32 ? 2 : (((((A->sf_size)-1) >> 5) + 1) + 1))))), A->sf_size); for( pe = sparse_cover->first_col; pe != 0; pe = pe->next_col) { (cover[((( pe->col_num) >> 5) + 1)] |= 1 << (( pe->col_num) & (32-1))); } sm_row_free(sparse_cover); return cover; }
void nvar_free(nvar_t *nvar) { // free individual labels here int i; for (i=0; i<nvar->dim; i++) if (nvar->labels[i]) free(nvar->labels[i]); int nlabels = nvar_count_covariances(nvar); for (i=0; i<nlabels; i++) if (nvar->plabels[i]) free(nvar->plabels[i]); free(nvar->wsums); sm_free(nvar->sum_wprods); // free the array free(nvar->labels); free(nvar->plabels); free(nvar); }
void iwdpm_create_bridge(iwdpm_t self) { sm_t sm = sm_new(4096); iwdp_t iwdp = iwdp_new(self->frontend); if (!sm || !iwdp) { sm_free(sm); return; } self->sm = sm; self->iwdp = iwdp; iwdp->subscribe = iwdpm_subscribe; iwdp->attach = iwdpm_attach; iwdp->select_port = iwdpm_select_port; iwdp->listen = iwdpm_listen; iwdp->connect = iwdpm_connect; iwdp->send = iwdpm_send; iwdp->add_fd = iwdpm_add_fd; iwdp->remove_fd = iwdpm_remove_fd; iwdp->state = self; iwdp->is_debug = &self->is_debug; sm->on_accept = iwdpm_on_accept; sm->on_sent = iwdpm_on_sent; sm->on_recv = iwdpm_on_recv; sm->on_close = iwdpm_on_close; sm->state = self; sm->is_debug = &self->is_debug; }
void bfree(void *buf) { #ifdef SMARTALLOC sm_free(__FILE__, __LINE__, buf); #else free(buf); #endif }
void sm_destroy_thread_samplers() { StackMachine *sm; int i; for (i=1; i<MAXTHREAD; i++) { sm_free(sampler_machines[i]); sampler_machines[i] = NULL; } }
/** * Free the resources used by a character map. */ static void charmap_free(CHARMAP* cm) { int i; for(i = 0; i < MAX_SECTIONS; i++) { sm_free(&cm->sections[i]); } memset(cm, 0, sizeof(CHARMAP)); }
void *sm_unref (void *ptr) { _ptr_to_sm (ptr)->refcount --; if (_ptr_to_sm (ptr)->refcount <= 0) { sm_free (ptr); return NULL; } return ptr; }
void iwdpm_free(iwdpm_t self) { if (self) { pc_free(self->pc); iwdp_free(self->iwdp); sm_free(self->sm); free(self->config); free(self->frontend); memset(self, 0, sizeof(struct iwdpm_struct)); free(self); } }
sm_t *sm_cpy(sm_t **m, const sm_t *src) { RETURN_IF(!m || !src, NULL); /* BEHAVIOR */ size_t i; const enum eSM_Type t = (enum eSM_Type)src->f.type; size_t ss = sm_size(src), src_buf_size = ST_SIZE_TO_ALLOC_SIZE(ss, sm_elem_size(t)); if (*m) { if (sd_is_using_ext_buffer((const sd_t *)src)) { /* If using ext buffer, we'll have grow limits */ sm_reset(*m); size_t oe = st_capacity(*m), os = ST_SIZE_TO_ALLOC_SIZE(oe, sm_elem_size(t)); RETURN_IF(os < src_buf_size, NULL); /* BEHAVIOR */ *m = sm_alloc_raw(t, S_TRUE, *m, os); } else { if ((*m)->f.type == t) { st_reserve(m, ss); } else { sm_free(m); *m = NULL; } } } if (!*m) *m = sm_alloc(t, ss); RETURN_IF(!*m || st_capacity(*m) < ss, NULL); /* BEHAVIOR */ switch (t) { /* * Fast copy: compact structure (without strings) */ case SM_I32I32: case SM_U32U32: case SM_IntInt: case SM_IntPtr: memcpy(*m, src, src_buf_size); break; /* * Slow map copy for types having strings as key or value: */ #define case_SM_CPY_InsertLoop(SM_xy, ST, INSERTF) \ case SM_xy: \ for (i = 0; i < ss; i++) { \ const ST *s = (const ST *)sm_enum_r(*m, i); \ INSERTF(m, s->x.k, s->v); \ } \ break; case_SM_CPY_InsertLoop(SM_IntStr, struct SMapIS, sm_is_insert); case_SM_CPY_InsertLoop(SM_StrInt, struct SMapSI, sm_si_insert); case_SM_CPY_InsertLoop(SM_StrStr, struct SMapSS, sm_ss_insert); case_SM_CPY_InsertLoop(SM_StrPtr, struct SMapSP, sm_sp_insert); #undef case_SM_CPY_InsertLoop default: break; } return *m; }
void *sm_realloc(const char *fname, int lineno, void *ptr, unsigned int size) { unsigned osize; void *buf; char *cp = (char *) ptr; Dmsg4(DT_MEMORY|50, "sm_realloc %s:%d %p %d\n", get_basename(fname), (uint32_t)lineno, ptr, size); if (size <= 0) { e_msg(fname, lineno, M_ABORT, 0, _("sm_realloc size: %d\n"), size); } /* If the old block pointer is NULL, treat realloc() as a malloc(). SVID is silent on this, but many C libraries permit this. */ if (ptr == NULL) { return sm_malloc(fname, lineno, size); } /* If the old and new sizes are the same, be a nice guy and just return the buffer passed in. */ cp -= HEAD_SIZE; struct abufhead *head = (struct abufhead *)cp; osize = head->ablen - (HEAD_SIZE + 1); if (size == osize) { return ptr; } /* Sizes differ. Allocate a new buffer of the requested size. If we can't obtain such a buffer, act as defined in SVID: return NULL from realloc() and leave the buffer in PTR intact. */ // sm_buffers--; // sm_bytes -= head->ablen; if ((buf = smalloc(fname, lineno, size)) != NULL) { memcpy(buf, ptr, (int)sm_min(size, osize)); /* If the new buffer is larger than the old, fill the balance of it with "designer garbage". */ if (size > osize) { memset(((char *) buf) + osize, 0x55, (int) (size - osize)); } /* All done. Free and dechain the original buffer. */ sm_free(fname, lineno, ptr); } Dmsg4(DT_MEMORY|60, _("sm_realloc %d at %p from %s:%d\n"), size, buf, get_basename(fname), (uint32_t)lineno); return buf; }
/** * Free the State Machine resources. */ static void sm_free(STATE_MACHINE* sm) { if(sm->next) { int i = 0; for(i = 0; i < (int)sm->next_maxsize; i++) { STATE_MACHINE* next_state = sm->next[i].state; if(next_state) sm_free(next_state); sm->next[i].state = NULL; } free(sm->next); sm->next = NULL; } memset(sm, 0, sizeof(STATE_MACHINE)); }
int main(int argc, char *argv[]) { struct state_machine *sm; char *filename; int nstates; int i; int use_goto; use_goto = is_goto(argc, argv); sm = sm_make(); sm->tokens = read_tokens(); if (sm->tokens == NULL) panic("Invalid token input"); read_nstates(sm); read_initial_state(sm); read_final_states(sm); setup_transitions(sm); printf("Output filename: "); fflush(stdout); filename = read_line(); if (use_goto) write_output_goto(sm, filename); else write_output_func(sm, filename); sm_free(sm); return 0; }
void SEXP_datatypeTbl_free(SEXP_datatypeTbl_t *t) { rbt_str_free(t->tree); sm_free(t); return; }
static void rbt_i64_node_free(struct rbt_node *n) { if (n != NULL) sm_free(rbt_node_ptr(n)); }
int main(int argc, const char **argv) { if (argc < 3) return syntax_error(argv, 5); int csize = atoi(argv[1]), climit0 = atoi(argv[2]); if (csize < 1 || csize > 4) return syntax_error(argv, 6); if (climit0 < 0) return syntax_error(argv, 7); int exit_code = 0; size_t count = 0; size_t cmax = csize == 4 ? 0xffffffff : 0xffffffff & ((1 << (csize * 8)) - 1); size_t climit = climit0 ? S_MIN((size_t)climit0, cmax) : cmax; #ifdef COUNTER_USE_BITSET #define COUNTER_SET(val) sb_set(&bs, val) #define COUNTER_POPCOUNT sb_popcount(bs) sb_t *bs = sb_alloc(0); sb_eval(&bs, cmax); #else #define COUNTER_SET(val) sm_uu32_insert(&m, val, 1) #define COUNTER_POPCOUNT sm_size(m) sm_t *m = sm_alloc(SM_U32U32, 0); #endif unsigned char buf[3 * 4 * 128]; int i; ssize_t l; for (;;) { l = read(0, buf, sizeof(buf)); l = (l / csize) * csize; if (l <= 0) break; #define CNTLOOP(inc, val) \ for (i = 0; i < l; i += inc) { \ COUNTER_SET(val); \ count++; \ if (COUNTER_POPCOUNT >= climit) \ goto done; \ } switch (csize) { case 1: CNTLOOP(1, buf[i]); break; case 2: CNTLOOP(2, (size_t)(buf[i] << 8 | buf[i + 1])); break; case 3: CNTLOOP(3, (size_t)(buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2])); break; case 4: CNTLOOP(4, (size_t)buf[i] << 24 | (size_t)buf[i + 1] << 16 | (size_t)buf[i + 2] << 8 | (size_t)buf[i + 3]); break; default: goto done; } #undef CNTLOOP } done: printf(FMT_ZU ", " FMT_ZU, count, COUNTER_POPCOUNT); #ifdef COUNTER_USE_BITSET sb_free(&bs); #else sm_free(&m); #endif return exit_code; }
void sm_dec_use_count(StateMachine * sm) { if (!--sm->use_count && sm->is_dead) sm_free(sm); }
void operator delete(void *buf) { sm_free(__FILE__, __LINE__, buf); }
void operator delete(void *buf) { // Dmsg1(000, "free called %p\n", buf); sm_free(__FILE__, __LINE__, buf); }