void vpn_user_init (struct config_data_vpn *vpn, char *seed, int len) { int d, d1; struct config_data_vpn *p; char *q; struct msgbuf buf[2]; mp = mempool_new (0, 1, true); d = newprocess ("vpn"); if (d < 0) panic ("newprocess vpn"); d1 = msgopen ("ttyout"); if (d1 < 0) panic ("msgopen ttyout"); msgsenddesc (d, d1); msgsenddesc (d, d1); msgclose (d1); p = mempool_allocmem (mp, sizeof *p); memcpy (p, vpn, sizeof *p); q = mempool_allocmem (mp, len); memcpy (q, seed, len); setmsgbuf (&buf[0], p, sizeof *p, 0); setmsgbuf (&buf[1], q, sizeof *q, 0); if (msgsendbuf (d, 0, buf, 2)) panic ("vpn init failed"); mempool_freemem (mp, q); mempool_freemem (mp, p); msgclose (d); }
RSTokenizer *GetSimpleTokenizer(Stemmer *stemmer, StopWordList *stopwords) { if (!tokpoolLatin_g) { tokpoolLatin_g = mempool_new(16, newLatinTokenizerAlloc, tokenizerFree); } RSTokenizer *t = mempool_get(tokpoolLatin_g); t->Reset(t, stemmer, stopwords, 0); return t; }
struct nmb *nmb_new() { struct nmb *nmb = xmalloc(sizeof(*nmb)); nmb->mpool = mempool_new(); nmb->pma = pma_new(64); nmb->count = 0; nmb->memory_used = 0; return nmb; }
struct nmb *nmb_new(struct env *e) { struct nmb *nmb = xmalloc(sizeof(*nmb)); nmb->mpool = mempool_new(); nmb->pma = pma_new(64); nmb->count = 0; nmb->e = e; return nmb; }
void run_accept(struct server* server) { static int one = 1; struct timeval timeout; timeout.tv_sec = 60; timeout.tv_usec = 0; struct pollfd spfd; spfd.events = POLLIN; spfd.revents = 0; spfd.fd = server->fd; while (1) { struct mempool* pool = mempool_new(); struct connection* conn = pcalloc(pool, sizeof(struct connection)); conn->pool = pool; conn->addrlen = sizeof(struct sockaddr_in6); conn->managed_conn = pcalloc(conn->pool, sizeof(struct netmgr_connection)); conn->managed_conn->pool = conn->pool; conn->managed_conn->extra = conn; conn->compression_state = -1; conn->server = server; buffer_init(&conn->managed_conn->read_buffer, conn->pool); buffer_init(&conn->managed_conn->write_buffer, conn->pool); if (poll(&spfd, 1, -1) < 0) { printf("Error while polling server: %s\n", strerror(errno)); pfree(pool); continue; } if ((spfd.revents ^ POLLIN) != 0) { printf("Error after polling server: %i (poll revents)!\n", spfd.revents); pfree(pool); break; } spfd.revents = 0; int fd = accept(server->fd, (struct sockaddr*) &conn->addr, &conn->addrlen); if (fd < 0) { if (errno == EAGAIN) continue; printf("Error while accepting client: %s\n", strerror(errno)); pfree(pool); continue; } conn->managed_conn->fd = fd; if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, sizeof(timeout))) printf("Setting recv timeout failed! %s\n", strerror(errno)); if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, sizeof(timeout))) printf("Setting send timeout failed! %s\n", strerror(errno)); if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *) &one, sizeof(one))) printf("Setting TCP_NODELAY failed! %s\n", strerror(errno)); if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) { printf("Setting O_NONBLOCK failed! %s, this error cannot be recovered, closing client.\n", strerror(errno)); close(fd); continue; } queue_push(server->prepared_connections, conn); } pthread_cancel (pthread_self()); }
struct nmb *nmb_new(struct tree_options *opts) { struct nmb *nmb = xmalloc(sizeof(*nmb)); nmb->mpool = mempool_new(); nmb->pma = pma_new(64); nmb->opts = opts; nmb->count = 0; ness_mutex_init(&nmb->mtx); ness_rwlock_init(&nmb->rwlock, &nmb->mtx); return nmb; }
int main (int argc, char const *argv[]) { UNUSED_ARG(argc); UNUSED_ARG(argv); unsigned i; memory_pool * mp = mempool_new(POOL_ENTRIES, POOL_ENTRY_SIZE); void * chunks[POOL_ENTRIES]; // Allocate all then free all for(i = 0; i < POOL_ENTRIES; ++i) { //Allocate chunk chunks[i] = mempool_alloc(mp); //Write into it write_chunk(chunks[i], (char)(i % 255)); } for(i = 0; i < POOL_ENTRIES; ++i) { //Verify contents verify_chunk(chunks[i], (char)(i % 255)); //Release mempool_free(mp, chunks[i]); chunks[i] = NULL; } // Allocate all then free by using is_mempool_exhaused for(i = 0; is_mempool_exhaused(mp) == false; ++i) { //Allocate chunk chunks[i] = mempool_alloc(mp); //Write into it write_chunk(chunks[i], (char)(i % 255)); } assert(i == POOL_ENTRIES); for(i = 0; i < POOL_ENTRIES; ++i) { //Verify contents verify_chunk(chunks[i], (char)(i % 255)); //Release mempool_free(mp, chunks[i]); chunks[i] = NULL; } printf("TEST SUCCESSFUL!\n"); return 0; }
int main(void) { mempool_t *pool; void *e[4]; int nb_test = 0; int nb_success = 0; CHECK_NEQ(pool = mempool_new(16, 2), NULL); CHECK_NEQ(e[0] = mempool_alloc(pool), NULL); CHECK_NEQ(e[1] = mempool_alloc(pool), NULL); CHECK_NEQ(e[2] = mempool_alloc(pool), NULL); mempool_free(pool, e[1]); mempool_delete(pool); fprintf(stderr, "Nb test success %i/%i\n", nb_success, nb_test); if(nb_success != nb_test) return 1; return 0; }
void init_plugins() { plugin_pool = mempool_new(); plugins = hashmap_new(16, plugin_pool); DIR* dir = opendir("plugins/"); char lp[PATH_MAX]; uint32_t lua_count = 0; char** lua_paths = NULL; if (dir != NULL) { struct dirent* de = NULL; while ((de = readdir(dir)) != NULL) { snprintf(lp, PATH_MAX, "plugins/%s", de->d_name); if (str_suffixes(de->d_name, ".so")) { // BASIN C struct plugin* pl = xcalloc(sizeof(struct plugin)); pl->hnd = dlopen(lp, RTLD_GLOBAL | RTLD_NOW); if (pl->hnd == NULL) { printf("Error loading plugin! %s\n", dlerror()); free(pl); continue; } pl->filename = xstrdup(de->d_name, 0); void (*init)(struct plugin* plugin) = dlsym(pl->hnd, "init_plugin"); (*init)(pl); pl->type = PLUGIN_BASIN; put_hashmap(plugins, next_plugin_id++, pl); } else if (str_suffixes(de->d_name, ".jar")) { // BUKKIT struct plugin* pl = xcalloc(sizeof(struct plugin)); pl->type = PLUGIN_BUKKIT; char* cp = xmalloc(strlen(lp) + strlen("-Djava.class.path=bukkit.jar:basinBukkit.jar:") + 1); strcpy(cp, "-Djava.class.path=bukkit.jar:basinBukkit.jar:"); // len checks done above strcat(cp, lp); JavaVMOption jvmopt[1]; jvmopt[0].optionString = cp; JavaVMInitArgs vmArgs; vmArgs.version = JNI_VERSION_1_2; vmArgs.nOptions = 1; vmArgs.options = jvmopt; vmArgs.ignoreUnrecognized = JNI_TRUE; long flag = JNI_CreateJavaVM(&pl->javaVM, (void**) &pl->jniEnv, &vmArgs); if (flag == JNI_ERR) { printf("Error creating Java VM, Bukkit plugins will not be loaded.\n"); free(pl); free(cp); continue; } jclass loadHelper = (*pl->jniEnv)->FindClass(pl->jniEnv, "org.basin.bukkit.LoadHelper"); if (loadHelper == NULL) { (*pl->jniEnv)->ExceptionDescribe(pl->jniEnv); (*pl->javaVM)->DestroyJavaVM(pl->javaVM); free(pl); free(cp); continue; } jmethodID loadMainYaml = (*pl->jniEnv)->GetStaticMethodID(pl->jniEnv, loadHelper, "loadMainYaml", "()V"); (*pl->jniEnv)->CallStaticVoidMethod(pl->jniEnv, loadHelper, loadMainYaml); if ((*pl->jniEnv)->ExceptionCheck(pl->jniEnv)) { (*pl->jniEnv)->ExceptionDescribe(pl->jniEnv); (*pl->jniEnv)->ExceptionClear(pl->jniEnv); } pl->filename = xstrdup(de->d_name, 0); put_hashmap(plugins, next_plugin_id++, pl); } else if (endsWith_nocase(de->d_name, ".zip")) { // LUA lua_paths = xrealloc(lua_paths, (lua_count + 1) * sizeof(char*)); lua_paths[lua_count++] = xstrdup(lp, 0); } } closedir(dir); } if (lua_count > 0) { //TODO: create LUA VM } }
struct nearest_map *nearest_init(const colormap *map) { mempool m = NULL; struct nearest_map *centroids = mempool_new(&m, sizeof(*centroids)); centroids->mempool = m; unsigned int skipped=0; unsigned int skip_index[map->colors]; for(unsigned int j=0; j < map->colors; j++) skip_index[j]=0; colormap *subset_palette = get_subset_palette(map); const int selected_heads = subset_palette->colors; centroids->heads = mempool_new(¢roids->mempool, sizeof(centroids->heads[0])*(selected_heads+1)); // +1 is fallback head unsigned int h=0; for(; h < selected_heads; h++) { unsigned int num_candiadtes = 1+(map->colors - skipped)/((1+selected_heads-h)/2); centroids->heads[h] = build_head(subset_palette->palette[h].acolor, map, num_candiadtes, ¢roids->mempool, skip_index, &skipped); if (centroids->heads[h].num_candidates == 0) { break; } } centroids->heads[h].radius = MAX_DIFF; centroids->heads[h].center = (f_pixel){0,0,0,0}; centroids->heads[h].num_candidates = 0; centroids->heads[h].candidates = mempool_new(¢roids->mempool, (map->colors - skipped) * sizeof(centroids->heads[h].candidates[0])); for(unsigned int i=0; i < map->colors; i++) { if (skip_index[i]) continue; centroids->heads[h].candidates[centroids->heads[h].num_candidates++] = (struct color_entry) { .color = map->palette[i].acolor, .index = i, .radius = 999, }; } centroids->num_heads = ++h; // get_subset_palette could have created a copy if (subset_palette != map->subset_palette) { pam_freecolormap(subset_palette); } return centroids; } unsigned int nearest_search(const struct nearest_map *centroids, const f_pixel px, const float min_opaque_val, float *diff) { const int iebug = px.a > min_opaque_val; const struct head *const heads = centroids->heads; for(unsigned int i=0; i < centroids->num_heads; i++) { float headdist = colordifference(px, heads[i].center); if (headdist <= heads[i].radius) { assert(heads[i].num_candidates); unsigned int ind=heads[i].candidates[0].index; float dist = colordifference(px, heads[i].candidates[0].color); /* penalty for making holes in IE */ if (iebug && heads[i].candidates[0].color.a < 1) { dist += 1.f/1024.f; } for(unsigned int j=1; j < heads[i].num_candidates; j++) { float newdist = colordifference(px, heads[i].candidates[j].color); /* penalty for making holes in IE */ if (iebug && heads[i].candidates[j].color.a < 1) { newdist += 1.f/1024.f; } if (newdist < dist) { dist = newdist; ind = heads[i].candidates[j].index; } } if (diff) *diff = dist; return ind; } } assert(0); return 0; } void nearest_free(struct nearest_map *centroids) { mempool_free(centroids->mempool); }
static struct head build_head(f_pixel px, const colormap *map, int num_candidates, mempool *m, unsigned int skip_index[], unsigned int *skipped) { struct sorttmp colors[map->colors]; unsigned int colorsused=0; for(unsigned int i=0; i < map->colors; i++) { if (skip_index[i]) continue; colors[colorsused].index = i; colors[colorsused].radius = colordifference(px, map->palette[i].acolor); colorsused++; } qsort(&colors, colorsused, sizeof(colors[0]), compareradius); assert(colorsused < 2 || colors[0].radius <= colors[1].radius); num_candidates = MIN(colorsused, num_candidates); struct head h; h.candidates = mempool_new(m, num_candidates * sizeof(h.candidates[0])); h.center = px; h.num_candidates = num_candidates; for(unsigned int i=0; i < num_candidates; i++) { h.candidates[i] = (struct color_entry) { .color = map->palette[colors[i].index].acolor, .index = colors[i].index, .radius = colors[i].radius, }; } h.radius = colors[num_candidates-1].radius/4.0f; // /2 squared for(unsigned int i=0; i < num_candidates; i++) { assert(colors[i].radius <= h.radius*4.0f); // divide again as that's matching certain subset within radius-limited subset // - 1/256 is a tolerance for miscalculation (seems like colordifference isn't exact) if (colors[i].radius < h.radius/4.f - 1.f/256.f) { skip_index[colors[i].index]=1; (*skipped)++; } } return h; } static colormap *get_subset_palette(const colormap *map) { // it may happen that it gets palette without subset palette or the subset is too large int subset_size = (map->colors+3)/4; if (map->subset_palette && map->subset_palette->colors <= subset_size) { return map->subset_palette; } const colormap *source = map->subset_palette ? map->subset_palette : map; colormap *subset_palette = pam_colormap(subset_size); for(unsigned int i=0; i < subset_size; i++) { subset_palette->palette[i] = source->palette[i]; } return subset_palette; }