void * /* returns value associated with key */ hashtable_remove(struct hashtable *h, void *k) { /* TODO: consider compacting the table when the load factor drops enough, * or provide a 'compact' method. */ struct entry *e; struct entry **pE; void *v; unsigned int hashvalue, index; hashvalue = hash(h,k); index = indexFor(h->tablelength,hashvalue); pE = &(h->table[index]); e = *pE; while (NULL != e) { /* Check hash value to short circuit heavier comparison */ if ((hashvalue == e->h) && (h->eqfn(k, e->k))) { *pE = e->next; //h->entrycount--; v = e->v; sp_free(e->k); sp_free(e); return v; } pE = &(e->next); e = e->next; } return NULL; }
int hashtable_iterator_remove(struct hashtable_itr *itr) { struct entry *remember_e, *remember_parent; int ret; /* Do the removal */ if (NULL == (itr->parent)) { /* element is head of a chain */ itr->h->table[itr->index] = itr->e->next; } else { /* element is mid-chain */ itr->parent->next = itr->e->next; } /* itr->e is now outside the hashtable */ remember_e = itr->e; itr->h->entrycount--; sp_free(remember_e->k); /* Advance the iterator, correcting the parent */ remember_parent = itr->parent; ret = hashtable_iterator_advance(itr); if (itr->parent == remember_e) { itr->parent = remember_parent; } sp_free(remember_e); return ret; }
void sp_catfree(spcat *c) { uint32_t p = 0; while (p < c->count) { sp_free(c->a, c->i[p]->min); sp_free(c->a, c->i[p]->max); sp_free(c->a, c->i[p]); p++; } sp_free(c->a, c->i); }
int sp_ireset(spi *i) { uint32_t p = 0; while (p < i->icount) { sp_free(i->a, i->i[p]); p++; } sp_free(i->a, i->i); i->i = NULL; return sp_iinit(i, i->a, i->pagesize, i->cmp, i->cmparg); }
void synchronize_image_data(Image ** real_space, Image ** support){ if(global_options.current_real_space_image != real_space){ sp_image_free(*real_space); *real_space = *global_options.current_real_space_image; sp_free(global_options.current_real_space_image); global_options.current_real_space_image = real_space; } if(global_options.current_support != support){ sp_image_free(*support); *support = *global_options.current_support; sp_free(global_options.current_support); global_options.current_support = support; } }
void sp_ifree(spi *i) { uint32_t p = 0; while (p < i->icount) { uint32_t k = 0; while (k < i->i[p]->count) { sp_free(i->a, i->i[p]->i[k]); k++; } sp_free(i->a, i->i[p]); p++; } sp_free(i->a, i->i); i->i = NULL; }
/** * SACT.Delete (1.0~) * スプライトの削除 * @param wNum: 削除するスプライト番号 */ void Delete() { int wNum = getCaliValue(); sp_free(wNum); DEBUG_COMMAND_YET("SACT.Delete %d:\n", wNum); }
struct sp_trie_node *sp_node_compact (struct sp_trie_node *self) { /* Tries to merge the node with the child node. Returns pointer to the compacted node. */ struct sp_trie_node *ch; /* Node that is a subscription cannot be compacted. */ if (sp_node_has_subscribers (self)) return self; /* Only a node with a single child can be compacted. */ if (self->type != 1) return self; /* Check whether combined prefixes would fix into a single node. */ ch = *sp_node_child (self, 0); if (self->prefix_len + ch->prefix_len + 1 > SP_TRIE_PREFIX_MAX) return self; /* Concatenate the prefixes. */ memmove (ch->prefix + self->prefix_len + 1, ch->prefix, ch->prefix_len); memcpy (ch->prefix, self->prefix, self->prefix_len); ch->prefix [self->prefix_len] = self->sparse.children [0]; ch->prefix_len += self->prefix_len + 1; /* Get rid of the obsolete parent node. */ sp_free (self); /* Return the new compacted node. */ return ch; }
struct hashtable * create_hashtable(unsigned int minsize, unsigned int (*hashf) (void*), int (*eqf) (void*,void*)) { struct hashtable *h; unsigned int pindex, size = primes[0]; /* Check requested hashtable isn't too large */ if (minsize > (1u << 30)) return NULL; /* Enforce size as prime */ for (pindex=0; pindex < prime_table_length; pindex++) { if (primes[pindex] > minsize) { size = primes[pindex]; break; } } h = (struct hashtable *)sp_malloc(sizeof(struct hashtable)); if (NULL == h) return NULL; /*oom*/ h->table = (struct entry **)sp_malloc(sizeof(struct entry*) * size); if (NULL == h->table) { sp_free(h); return NULL; } /*oom*/ memset(h->table, 0, size * sizeof(struct entry *)); h->tablelength = size; h->primeindex = pindex; //since we don't expand the hastable, not needed h->entrycount = 0; // TRANSACTIONAL CONFLICTS ON THIS VARIABLE, h->hashfn = hashf; h->eqfn = eqf; #ifdef DEBUG h->loadlimit = (unsigned int) ceil(size * max_load_factor); #else h->loadlimit = (unsigned int)(size * 65/100); #endif return h; }
void nt_gr_set_wallpaper(int no) { sprite_t *sp = night.sp[SPNO_WALL]; if (sp) { sp_remove_updatelist(sp); sp_free(sp); } if (no == 1013 && nt_sco_is_natsu()) { no = 1011; } if (no == 1014 && nt_sco_is_natsu()) { no = 1012; } sp = sp_new(SPNO_WALL, no, 0, 0, SPRITE_WP); sp_add_updatelist(sp); if (no == 0) { sp->cursize.width = sf0->width; sp->cursize.height = sf0->height; sp->update = sp_draw_wall; } night.sp[SPNO_WALL] = sp; }
//-------------------------------------------------------------------------- Hqp_IpRedSpBKP::~Hqp_IpRedSpBKP() { sp_free(_CT); sp_free(_J); sp_free(_J_raw); px_free(_QP2J); px_free(_J2QP); px_free(_pivot); px_free(_blocks); v_free(_zw); v_free(_scale); v_free(_r12); v_free(_xy); iv_free(_CTC_degree); iv_free(_CTC_neigh_start); iv_free(_CTC_neighs); }
/* destroy */ void hashtable_destroy(struct hashtable *h, int free_values) { unsigned int i; struct entry *e, *f; struct entry **table = h->table; if (free_values) { for (i = 0; i < h->tablelength; i++) { e = table[i]; while (NULL != e) { f = e; e = e->next; sp_free(f->k); sp_free(f->v); sp_free(f); } } } else { for (i = 0; i < h->tablelength; i++) { e = table[i]; while (NULL != e) { f = e; e = e->next; sp_free(f->k); sp_free(f); } } } sp_free(h->table); sp_free(h); }
void sp_xsource_destroy (struct sp_sockbase *self) { struct sp_xsource *xsource; xsource = sp_cont (self, struct sp_xsource, sockbase); sp_xsource_term (xsource); sp_free (xsource); }
/** * SACT.SpriteDeleteCount (1.0~) * wNum番からwCount個の範囲のスプライトの削除 * @param wNum: 先頭スプライト番号 * @param wCount: 範囲 */ void SpriteDeleteCount() { int wNum = getCaliValue(); int wCount = getCaliValue(); int i; for (i = wNum; i < (wNum + wCount); i++) { sp_free(i); } DEBUG_COMMAND_YET("SACT.SpriteDeleteCount %d,%d:\n", wNum, wCount); }
int sp_term (void) { #if defined SP_HAVE_WINDOWS int rc; #endif int i; sp_glock_lock (); /* If there are still references to the library, do nothing, just decrement the reference count. */ --sp_ctx_refcount; if (sp_ctx_refcount) { sp_glock_unlock (); return 0; } /* Notify all the open sockets about the process shutdown and wait till all of them are closed. */ sp_mutex_lock (&self.sync); if (self.nsocks) { for (i = 0; i != self.max_socks; ++i) if (self.socks [i]) sp_sock_zombify (self.socks [i]); self.zombie = 1; sp_cond_wait (&self.termcond, &self.sync); } sp_mutex_unlock (&self.sync); /* Final deallocation of the global resources. */ sp_cond_term (&self.termcond); sp_list_term (&self.socktypes); sp_list_term (&self.transports); sp_mutex_term (&self.sync); sp_free (self.socks); self.socks = NULL; /* Shut down the memory allocation subsystem. */ sp_alloc_term (); /* On Windows, uninitialise the socket library. */ #if defined SP_HAVE_WINDOWS rc = WSACleanup (); sp_assert (rc == 0); #endif sp_glock_unlock (); return 0; }
int sp_msgqueue_recv (struct sp_msgqueue *self, void *buf, size_t *len) { int result; struct sp_msgref *msgref; size_t msgsz; size_t to_copy; struct sp_msgqueue_chunk *o; sp_mutex_lock (&self->sync); /* If there is no message in the queue. */ if (sp_slow (!self->count)) { sp_mutex_unlock (&self->sync); return -EAGAIN; } /* Move the message from the pipe to the user. */ msgref = &self->in.chunk->msgs [self->in.pos]; msgsz = sp_msgref_size (msgref); to_copy = msgsz < *len ? msgsz : *len; if (to_copy) memcpy (buf, sp_msgref_data (msgref), to_copy); sp_msgref_term (msgref); /* Move to the next position. */ ++self->in.pos; if (sp_slow (self->in.pos == SP_MSGQUEUE_GRANULARITY)) { o = self->in.chunk; self->in.chunk = self->in.chunk->next; self->in.pos = 0; if (sp_fast (!self->cache)) self->cache = o; else sp_free (o); } /* Adjust the statistics. */ /* TODO: Implement a real queue size limit instead of this fake one. */ result = self->mem >= self->maxmem ? SP_MSGQUEUE_SIGNAL : 0; --self->count; self->mem -= msgsz; if (!self->count) result |= SP_MSGQUEUE_RELEASE; sp_mutex_unlock (&self->sync); *len = msgsz; return result; }
void sp_msgqueue_term (struct sp_msgqueue *self) { int rc; size_t sz; /* Deallocate messages in the pipe. */ while (1) { sz = 0; rc = sp_msgqueue_recv (self, NULL, &sz); if (rc == -EAGAIN) break; } /* There are no more messages in the pipe so there's at most one chunk in the queue. Deallocate it. */ sp_assert (self->in.chunk == self->out.chunk); sp_free (self->in.chunk); /* Deallocate the cached chunk, if any. */ if (self->cache) sp_free (self->cache); sp_mutex_term (&self->sync); }
void nt_gr_set_spR(int no) { sprite_t *sp = night.sp[SPNO_TACHI_R]; if (sp) { sp_remove_updatelist(sp); sp_free(sp); sp = NULL; } if (no) { sp = sp_new(SPNO_TACHI_R, no, 0, 0, SPRITE_NORMAL); sp_add_updatelist(sp); sp_set_loc(sp, TACHI_R_LOC_X, TACHI_R_LOC_Y); } night.sp[SPNO_TACHI_R] = sp; }
static inline int sp_fileclose(spfile *f) { /* leave file incomplete */ if (splikely(f->file)) { sp_free(f->a, f->file); f->file = NULL; } int rc; if (spunlikely(f->fd != -1)) { rc = close(f->fd); if (spunlikely(rc == -1)) return -1; f->fd = -1; } return 0; }
void nt_gr_set_scenery(int no) { sprite_t *sp = night.sp[SPNO_SCENERY]; if (sp) { sp_remove_updatelist(sp); sp_free(sp); sp = NULL; } if (no) { sp = sp_new(SPNO_SCENERY, no, 0, 0, SPRITE_NORMAL); sp_add_updatelist(sp); sp_set_loc(sp, SCENERY_LOC_X, SCENERY_LOC_Y); } night.sp[SPNO_SCENERY] = sp; }
int main() { size_t n = 5; size_t nz = 4; double A[] = {1, 1, 1, 1}; size_t I[] = {0, 1, 2, 3}; size_t J[] = {1, 2, 3, 4}; double AD[] = {33, 2, 2, 2, 2}; size_t *IA, *JA; double *AN; double *b; double x[] = {0, 0, 0, 0, 0}; int it; double xx[] = {1, 1, 1, 1, 1}; double *work; sp_create(n, nz, &IA, &JA, &AN); sp_convert(nz, A, I, J, n, IA, JA, AN); b = nl_dvector_create(n); work = nl_dvector_create(3*n); sp_mult_col_sym(IA, JA, AN, AD, xx, n, b); printf("Метод сопряженных градиентов\n"); printf("\nМатрица A:\n"); sp_print_list_sym(IA, JA, AN, AD, n, NULL, NULL); printf("\nВектор b:\n"); nl_dvector_print(b, n, NULL); it = sp_conj_sym(IA, JA, AN, AD, b, n, 1e-3, 20, x, work); printf("\nРешение системы Ax = b:\n"); nl_dvector_print(x, n, 0); printf("\nЧисло итераций = %i \n", it); sp_free(IA, JA, AN); nl_dvector_free(b); nl_dvector_free(work); return 0; }
void sp_node_term (struct sp_trie_node *self) { int children; int i; /* Trivial case of the recursive algorithm. */ if (!self) return; /* Recursively destroy the child nodes. */ children = self->type <= SP_TRIE_SPARSE_MAX ? self->type : (self->dense.max - self->dense.min + 1); for (i = 0; i != children; ++i) sp_node_term (*sp_node_child (self, i)); /* Deallocate this node. */ sp_free (self); }
void nt_gr_set_face(int no) { sprite_t *sp = night.sp[SPNO_FACE]; if (sp) { sp_remove_updatelist(sp); sp_free(sp); sp = NULL; } if (no) { sp = sp_new(SPNO_FACE, no, 0, 0, SPRITE_NORMAL); sp_add_updatelist(sp); sp_set_loc(sp, FACE_LOC_X, FACE_LOC_Y); night.msgplace = 2; } else { night.msgplace = 0; } night.sp[SPNO_FACE] = sp; }
static inline int sp_mapopenof(spfile *f, char *path, int flags, uint64_t size) { #if _MSC_VER f->fd = sp_win_openfile(path, flags); #else f->fd = open(path, flags, 0600); #endif if (spunlikely(f->fd == -1)) return -1; f->file = sp_strdup(f->a, path); if (spunlikely(f->file == NULL)) { close(f->fd); f->fd = -1; return -1; } f->used = 0; f->creat = (flags & O_CREAT ? 1 : 0); int rc; if (!f->creat) { ssize_t sz = sp_mapsizeof(path); if (spunlikely(sz == -1)) goto err; f->size = sz; rc = sp_map(f, PROT_READ); if (spunlikely(rc == -1)) goto err; return 0; } f->size = 0; rc = sp_mapresize(f, size); if (spunlikely(rc == -1)) goto err; rc = sp_map(f, PROT_READ | PROT_WRITE); if (spunlikely(rc == -1)) goto err; return 0; err: close(f->fd); f->fd = -1; sp_free(f->a, f->file); f->file = NULL; return -1; }
int silly_socket_init() { int err; sp_t spfd = SP_INVALID; int fd[2] = {-1, -1}; struct socket *s = NULL; struct silly_socket *ss = silly_malloc(sizeof(*ss)); memset(ss, 0, sizeof(*ss)); socketpool_init(ss); spfd = sp_create(EVENT_SIZE); if (spfd == SP_INVALID) goto end; s = allocsocket(ss, STYPE_CTRL, PROTOCOL_PIPE); assert(s); err = pipe(fd); //use the pipe and not the socketpair because the pipe will be automatic when the data size small than BUFF_SIZE if (err < 0) goto end; err = sp_add(spfd, fd[0], s); if (err < 0) goto end; ss->spfd = spfd; ss->ctrlsendfd = fd[1]; ss->ctrlrecvfd = fd[0]; ss->eventindex = 0; ss->eventcount = 0; resize_eventbuff(ss, EVENT_SIZE); FD_ZERO(&ss->ctrlfdset); SSOCKET = ss; return 0; end: if (s) freesocket(ss, s); if (spfd != SP_INVALID) sp_free(spfd); if (fd[0] >= 0) close(fd[0]); if (fd[1] >= 0) close(fd[0]); if (ss) silly_free(ss); return -errno; }
static inline int sp_filecomplete(spfile *f) { if (f->creat == 0) return 0; /* remove .incomplete part */ f->creat = 0; char path[1024]; snprintf(path, sizeof(path), "%s", f->file); char *ext = strrchr(path, '.'); assert(ext != NULL); *ext = 0; int rc = rename(f->file, path); if (spunlikely(rc == -1)) return -1; char *p = sp_strdup(f->a, path); if (spunlikely(p == NULL)) return -1; sp_free(f->a, f->file); f->file = p; return 0; }
int main(int argc, char *argv[]) { sp_t sp; int ret; ret = sp_start(&sp, reducefixed, "-REC_SIZE=14 -GROUP_BY -OUT_FILE[0]=rout.txt %s", sp_argv_to_str(argv + 1, argc - 1)); if (ret != SP_OK) { fprintf(stderr, "sp_start: %s\n", sp_get_error_string(sp, ret)); return (1); } if ((ret = sp_wait(sp)) != SP_OK) { fprintf(stderr, "sp_wait: %s\n", sp_get_error_string(sp, ret)); return (1); } /* Free sump pump resources. Not necessary for an exiting program but * called here for testing purposes */ sp_free(&sp); return (0); }
int sp_iinit(spi *i, spa *a, int pagesize, spcmpf cmp, void *cmparg) { i->a = a; i->cmp = cmp; i->cmparg = cmparg; i->count = 0; i->pagesize = pagesize; /* start from 4 pages vector */ i->itop = 2; i->icount = 1; i->i = NULL; int rc = sp_iensure(i); if (spunlikely(rc == -1)) return -1; /* allocate first page */ i->i[0] = sp_ipagealloc(i); if (spunlikely(i->i[0] == NULL)) { sp_free(i->a, i->i); i->i = NULL; return -1; } return 0; }
void silly_socket_exit() { int i; assert(SSOCKET); sp_free(SSOCKET->spfd); close(SSOCKET->ctrlsendfd); close(SSOCKET->ctrlrecvfd); struct socket *s = &SSOCKET->socketpool[0]; for (i = 0; i < MAX_SOCKET_COUNT; i++) { int isnormal = 0; enum stype type = s->type; isnormal += type == STYPE_SOCKET ? 1 : 0; isnormal += type == STYPE_LISTEN ? 1 : 0; isnormal += type == STYPE_HALFCLOSE ? 1 : 0; if (isnormal > 0) close(s->fd); } silly_free(SSOCKET->eventbuff); silly_free(SSOCKET->socketpool); silly_free(SSOCKET); return ; }
void sp_device_data_i_free_state(sp_device_data_i *aThis) { if (aThis && aThis->m_Data.m_State) { sp_free(aThis->m_Data.m_State); } }