/** * Load sample with given name. * @param name audio name used inside fokker.dks * @return loaded sample or NULL on error. */ sb_sample *sdl_sample_load(const char *name) { #ifdef HAVE_SDL_MIXER int len, ret; uint8_t *p; sb_sample *sample; ret = dksopen(name); if (ret != 1) { return NULL; } len = dkssize(); p = (uint8_t *) walloc(len); ret = dksread(p, len); assert(ret == 1); dksclose(); sample = (sb_sample *) walloc(sizeof(sb_sample)); sample->chunk = Mix_LoadWAV_RW(SDL_RWFromConstMem(p, len), 1); if (sample->chunk == NULL) { fprintf(stderr, "sdl_sample_load: %s\n", Mix_GetError()); exit(1); } free(p); return sample; #else return NULL; #endif }
/* Convert a table of strings into a single block of memory. The input table consists of an array of null-terminated strings, terminated in a null pointer. Returns the address of a block of memory allocated using the walloc() function. The returned pointer must be deleted using wfree(). Returns NULL on error. */ static uchar *tableToBlock(char **table) { uchar *pBlock; /* Allocated block */ char *pEntry; /* Pointer into block */ size_t sizeBlock; /* Size of table */ int index; /* Index into string table */ assert(table); /* Calculate the size of the data block. Allow for final null byte. */ sizeBlock = 1; for (index = 0; table[index]; index++) { sizeBlock += strlen(table[index]) + 1; } /* Allocate the data block and fill it with the strings */ pBlock = walloc(sizeBlock); if (pBlock != NULL) { pEntry = (char*) pBlock; for (index = 0; table[index]; index++) { strcpy(pEntry, table[index]); pEntry += strlen(pEntry) + 1; } /* Terminate the data block with an extra null string */ *pEntry = '\0'; } return pBlock; }
/* Do output back to the browser in the background. This is a socket write handler. This bypasses the output buffer and writes directly to the socket. */ static void fileWriteEvent(Webs *wp) { char *buf; ssize len, wrote; assert(wp); assert(websValid(wp)); /* Note: websWriteSocket may return less than we wanted. It will return -1 on a socket error. */ if ((buf = walloc(ME_GOAHEAD_LIMIT_BUFFER)) == NULL) { websError(wp, HTTP_CODE_INTERNAL_SERVER_ERROR, "Cannot get memory"); return; } /* OPT - we could potentially save this buffer so that on short-writes, it does not need to be re-read. */ while ((len = websPageReadData(wp, buf, ME_GOAHEAD_LIMIT_BUFFER)) > 0) { if ((wrote = websWriteSocket(wp, buf, len)) < 0) { break; } if (wrote != len) { websPageSeek(wp, - (len - wrote), SEEK_CUR); break; } } wfree(buf); if (len <= 0) { websDone(wp); } }
/** * Sets palette entries firstcolor to firstcolor+n-1 * from pal[0] to pal[n-1]. * @param pal the palette, specify NULL to set all colors to black=(0,0,0) * @param reverse = 1 to read colors in reverse order (pal[n-1] to pal[0]) */ void setpal_range(const char pal[][3], int firstcolor, int n, int reverse) { SDL_Color *cc = (SDL_Color *) walloc(n * sizeof(SDL_Color)); int i, from = (reverse ? n - 1 : 0); for (i = 0; i < n; i++) { if (pal == NULL) { cc[i].r = cc[i].g = cc[i].b = 0; } else { cc[i].r = 4 * pal[from][0]; cc[i].g = 4 * pal[from][1]; cc[i].b = 4 * pal[from][2]; } if (reverse) from--; else from++; } if (draw_with_vircr_mode) { SDL_SetPalette(video_state.surface, video_state.haverealpalette ? SDL_PHYSPAL : SDL_LOGPAL, cc, firstcolor, n); } else { SDL_SetPalette(video_state.surface, SDL_PHYSPAL | SDL_LOGPAL, cc, firstcolor, n); } memcpy(&curpal[firstcolor], cc, n * sizeof(SDL_Color)); wfree(cc); }
bool gl_load_shader(const char* shader_path, GLuint* shader_id, GLenum shader_type) { //Read in shader source FILE* file = fopen(shader_path, "r"); if(file == NULL) { return false; message_log("Error loading file-", shader_path); } fseek(file, 0, SEEK_END); int length = ftell(file); fseek(file, 0, SEEK_SET); GLchar* shader_source = (GLchar*)walloc(sizeof(GLchar)*length+1); fread(shader_source, sizeof(GLchar), length, file); shader_source[length] = (GLchar)0; fclose(file); //Compile shader *shader_id = glCreateShader(shader_type); glShaderSource(*shader_id, 1, (const GLchar**)&shader_source, NULL); glCompileShader(*shader_id); GLint compiled = GL_FALSE; glGetShaderiv(*shader_id, GL_COMPILE_STATUS, &compiled); if(compiled != GL_TRUE ) { message_log("Open GL-", "error compiling shader"); message_log("Source:", shader_source); GLchar buffer[2048]; glGetShaderInfoLog(*shader_id, 2048, NULL, buffer); message_log("Open GL-", buffer); return false; } wfree(shader_source); return true; }
/** * Setup allocated LRU page cache. */ static int setup_cache(struct lru_cache *cache, long pages, gboolean wdelay) { cache->arena = vmm_alloc(pages * DBM_PBLKSIZ); if (NULL == cache->arena) return -1; cache->pagnum = g_hash_table_new(NULL, NULL); cache->used = hash_list_new(NULL, NULL); cache->available = slist_new(); cache->pages = pages; cache->next = 0; cache->write_deferred = wdelay; cache->dirty = walloc(cache->pages); cache->numpag = walloc(cache->pages * sizeof(long)); return 0; }
static void main_server_foreach_callback(client_info *info, void *data) { if (info) { client_info *from = (client_info*)data; char *buffer = (char*) walloc(sizeof(char) * (USERNAME_MAX + strlen(from->client_msg) + 5)); sprintf(buffer, "[%s(%d)] %s", from->client_user->username, from->client_user->uid, from->client_msg); server_sned_to_client(info, buffer); wfree(buffer); } }
/** * Allocate a remote security token. */ sectoken_remote_t * sectoken_remote_alloc(uint8 length) { sectoken_remote_t *token; WALLOC(token); token->length = length; token->v = length ? walloc(length) : NULL; return token; }
void enter_where(struct utmpentry *ep, PERSON *pn) { WHERE *w; w = walloc(pn); w->info = LOGGEDIN; w->tty = ep->line; w->host = ep->host; w->loginat = (time_t)ep->tv.tv_sec; find_idle_and_ttywrite(w); }
void enter_where(struct utmpx *ut, PERSON *pn) { WHERE *w; w = walloc(pn); w->info = LOGGEDIN; strcpy(w->tty, ut->ut_line); strcpy(w->host, ut->ut_host); w->loginat = ut->ut_tv.tv_sec; find_idle_and_ttywrite(w); }
static int init_mode(int new_mode, const char *paletname) { Uint32 mode_flags; const SDL_VideoInfo *vi; int las, las2; int w = (new_mode == SVGA_MODE) ? 800 : 320; int h = (new_mode == SVGA_MODE) ? 600 : 200; init_video(); mode_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWPALETTE; if (!draw_with_vircr_mode) mode_flags |= SDL_ANYFORMAT; //if (wantfullscreen) // mode_flags |= SDL_FULLSCREEN; if (draw_with_vircr_mode && pixel_multiplier > 1) wfree(vircr); pixel_multiplier = (new_mode == SVGA_MODE) ? pixel_multiplier_svga : pixel_multiplier_vga; video_state.surface = SDL_SetVideoMode(w * pixel_multiplier, h * pixel_multiplier, 8, mode_flags); assert(video_state.surface); if (draw_with_vircr_mode) { if (pixel_multiplier > 1) { vircr = (uint8_t *) walloc(w * h); } else { vircr = (uint8_t *) video_state.surface->pixels; } } /* else vircr is preallocated in init_video */ vi = SDL_GetVideoInfo(); video_state.haverealpalette = (vi->vfmt->palette != NULL); dksopen(paletname); dksread(ruutu.normaalipaletti, sizeof(ruutu.normaalipaletti)); for (las = 0; las < 256; las++) for (las2 = 0; las2 < 3; las2++) ruutu.paletti[las][las2] = ruutu.normaalipaletti[las][las2]; dksclose(); setpal_range(ruutu.paletti, 0, 256); all_bitmaps_refresh(); current_mode = new_mode; return 1; }
/** Real declaration of both verbose and verbose_step. */ static void __verbose(verbose_func func, wchar_t* message, ...) { int lenbuf; wchar_t* buf; va_list args; if(!verbose_flag || !message) return; va_start(args, message); lenbuf = _vscwprintf(message, args); buf = walloc(lenbuf); vswprintf(buf, lenbuf+1, message, args); va_end(args); func(buf); xfree(buf); }
/** * Allocate new parsing context for handle and record it. * * @param `handle' the asynchronous HTTP request handle. * @param `maxlines' the max number of lines we want to parse. */ static void gwc_parse_context_set(void *handle, int maxlines) { struct gwc_parse_context *ctx; ctx = walloc(sizeof(*ctx)); ctx->getline = getline_make(MAX_LINE_SIZE); ctx->maxlines = maxlines; ctx->handle = handle; ctx->lines = 0; ctx->processed = 0; http_async_set_opaque(handle, ctx, gwc_parse_context_free); }
sb_mod_file *sdl_load_mod_file(const char *name) { #ifdef HAVE_SDL_MIXER int len, ret; uint8_t *p; sb_mod_file *mod; SDL_RWops *rwops; ret = dksopen(name); if (ret != 1) { return NULL; } len = dkssize(); p = (uint8_t *) walloc(len); ret = dksread(p, len); assert(ret == 1); dksclose(); mod = (sb_mod_file *) walloc(sizeof(sb_mod_file)); rwops = SDL_RWFromConstMem(p, len); mod->music = Mix_LoadMUS_RW(rwops); SDL_FreeRW(rwops); if (mod->music == NULL) { fprintf(stderr, "sdl_load_mod_file: %s\n", Mix_GetError()); exit(1); } free(p); return mod; #else return NULL; #endif }
static WebsUser *createUser(char *username, char *password, char *roles) { WebsUser *user; assert(username); if ((user = walloc(sizeof(WebsUser))) == 0) { return 0; } user->name = sclone(username); user->roles = sclone(roles); user->password = sclone(password); user->abilities = -1; return user; }
/* Upgrade a standard socket to use TLS */ PUBLIC int sslUpgrade(Webs *wp) { Nano *np; assert(wp); if ((np = walloc(sizeof(Nano))) == 0) { return -1; } memset(np, 0, sizeof(Nano)); wp->ssl = np; if ((np->handle = SSL_acceptConnection(socketGetHandle(wp->sid))) < 0) { return -1; } return 0; }
/** * Allocate a new data block of given size. * The block header is at the start of the allocated block. */ pdata_t * pdata_new(int len) { pdata_t *db; char *arena; g_assert(len > 0); arena = walloc(len + EMBEDDED_OFFSET); db = pdata_allocb(arena, len + EMBEDDED_OFFSET, NULL, 0); g_assert((size_t) len == pdata_len(db)); g_assert(db->d_arena == db->d_embedded); return db; }
PUBLIC int sslUpgrade(Webs *wp) { Ms *ms; ssl_t *handle; if (matrixSslNewServerSession(&handle, sslKeys, NULL) < 0) { return -1; } if ((ms = walloc(sizeof(Ms))) == 0) { return -1; } memset(ms, 0x0, sizeof(Ms)); ms->handle = handle; wp->ms = ms; return 0; }
/******************************** Word Counting with Binary Tree * *******************************/ WordNode *addWord(WordNode *p, char *w) { int cond; if (p == NULL) { /* a new word has arrived */ p = walloc(); /* make a new node */ p->word = strdup2(w); p->count = 1; p->left = p->right = NULL; } else if ((cond = strcmp(p->word, w)) == 0) p->count++; /* repeated word */ else if (cond < 0) /* less than into left subtree */ p->left = addWord(p->left, w); else /* greater than into right subtree */ p->right = addWord(p->right, w); return p; }
/** * Setup allocated LRU page cache. */ static int setup_cache(struct lru_cache *cache, long pages, bool wdelay) { cache->arena = vmm_alloc(pages * DBM_PBLKSIZ); if (NULL == cache->arena) return -1; cache->pagnum = htable_create(HASH_KEY_SELF, 0); cache->used = hash_list_new(NULL, NULL); cache->available = slist_new(); cache->pages = pages; cache->next = 0; cache->write_deferred = wdelay; cache->dirty = walloc(cache->pages); WALLOC_ARRAY(cache->numpag, cache->pages); return 0; }
/** * Deserialization routine for tokdata. */ static void deserialize_tokdata(bstr_t *bs, void *valptr, size_t len) { struct tokdata *td = valptr; g_assert(sizeof *td == len); bstr_read_time(bs, &td->last_update); bstr_read_u8(bs, &td->length); if (td->length != 0) { td->token = walloc(td->length); bstr_read(bs, td->token, td->length); } else { td->token = NULL; } }
void enter_lastlog(PERSON *pn) { WHERE *w; static int opened, fd; struct lastlog ll; char doit = 0; /* some systems may not maintain lastlog, don't report errors. */ if (!opened) { fd = open(_PATH_LASTLOG, O_RDONLY, 0); opened = 1; } if (fd == -1 || lseek(fd, (long)pn->uid * sizeof(ll), SEEK_SET) != (off_t)((long)pn->uid * sizeof(ll)) || read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) { /* as if never logged in */ ll.ll_line[0] = ll.ll_host[0] = '\0'; ll.ll_time = 0; } if ((w = pn->whead) == NULL) doit = 1; else if (ll.ll_time != 0) { /* if last login is earlier than some current login */ for (; !doit && w != NULL; w = w->next) if (w->info == LOGGEDIN && w->loginat < ll.ll_time) doit = 1; /* * and if it's not any of the current logins * can't use time comparison because there may be a small * discrepancy since login calls time() twice */ for (w = pn->whead; doit && w != NULL; w = w->next) if (w->info == LOGGEDIN && strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0) doit = 0; } if (doit) { w = walloc(pn); w->info = LASTLOG; asprintf(&w->tty, "%s", ll.ll_line); asprintf(&w->host, "%s", ll.ll_host); w->loginat = ll.ll_time; } }
static int load_diphs(CONFIG *config) { int i,j; if (dico == 0) dico = walloc(FRAME,NFRAMES); if((config->dfd=fopen(config->diphone_file,"rb")) == NULL) { fprintf(stderr,"Can't open file %s\n",config->diphone_file); return -1; } /* zero the first one... */ for(i=0;i<FR_DATA;i++) dico[0].frame[i] = 0; dico[0].frame[2] = FR_SZ; /* note the use of 1 to tie in with indexing */ for(i=1;(fread((char *)&dico[i],sizeof(FRAME),1,config->dfd) != 0) && (i < NFRAMES);i++) { ; } /* check the first little bit is as we expect... */ if ((dico[1].frame[0] != 181) || (dico[1].frame[1] != 176)) { if ((SWAPSHORT(dico[1].frame[0]) == 181) && (SWAPSHORT(dico[1].frame[1]) == 176)) { /* Its bytes swapped */ for (j=1;j<i;j++) swap_bytes_short(dico[j].frame,FR_DATA); } else { fprintf(stderr,"File %s apparently corrupted\n", config->diphone_file); fclose(config->dfd); return -1; } } fclose(config->dfd); return 0; }
PUBLIC char *websReadWholeFile(char *path) { WebsFileInfo sbuf; char *buf; int fd; if (websStatFile(path, &sbuf) < 0) { return 0; } buf = walloc(sbuf.size + 1); if ((fd = websOpenFile(path, O_RDONLY, 0)) < 0) { return 0; } websReadFile(fd, buf, sbuf.size); buf[sbuf.size] = '\0'; websCloseFile(fd); return buf; }
/** * Open the .dat file, creating it if missing. * * @return -1 on error with errno set, 0 if OK with cleared errno. */ static int big_open(DBMBIG *dbg) { struct datfile *file; filestat_t buf; g_assert(-1 == dbg->fd); g_assert(dbg->file != NULL); file = dbg->file; dbg->fd = file_open(file->datname, file->flags | O_CREAT, file->mode); if (-1 == dbg->fd) return -1; big_datfile_free_null(&dbg->file); dbg->bitbuf = walloc(BIG_BLKSIZE); if (-1 == fstat(dbg->fd, &buf)) { buf.st_size = 0; } else { if (buf.st_size < BIG_BLKSIZE) { buf.st_size = 0; } else { dbg->bitmaps = 1 + (buf.st_size - BIG_BLKSIZE) / (BIG_BITCOUNT * BIG_BLKSIZE); } } /* * Create a first bitmap if the file is empty. * No need to flush it to disk, this will happen at the first allocation. */ if (0 == buf.st_size) { memset(dbg->bitbuf, 0, BIG_BLKSIZE); bit_field_set(dbg->bitbuf, 0); /* First page is the bitmap itself */ dbg->bitbno = 0; dbg->bitmaps = 1; } errno = 0; return 0; }
struct word *addtree(struct word *root, char *word, int n){ int cmpresult = 0; if(root == NULL){ root = walloc(); root->name = strcopy(word); root->next = strcopy(""); root->leftchild = NULL; root->rightchild = NULL; }else if((cmpresult = cmpfirstcharacters(root->name, word, n)) == 0){ if(strlen(root->next) != 0) strcat(root->next, ","); strcat(root->next, word); }else if(cmpresult < 0){ root->rightchild = addtree(root->rightchild, word, n); }else{ root->leftchild = addtree(root->leftchild, word, n); } return root; }
PUBLIC int sslUpgrade(Webs *wp) { Ms *ms; ssl_t *handle; int flags; flags = ME_GOAHEAD_VERIFY_PEER ? SSL_FLAGS_CLIENT_AUTH : 0; if (matrixSslNewServerSession(&handle, sslKeys, NULL, flags) < 0) { error("Cannot create new matrixssl session"); return -1; } if ((ms = walloc(sizeof(Ms))) == 0) { return -1; } memset(ms, 0x0, sizeof(Ms)); ms->handle = handle; wp->ssl = ms; return 0; }
/** * A "daemon" is a task equipped with a work queue. * * When the daemon is initially created, it has an empty work queue and it is * put in the "sleeping" state where it is not scheduled. * * As long as there is work in the work queue, the task is scheduled. * It goes back to sleep when the work queue becomes empty. * * The `steps' given represent the processing to be done on each item of * the work queue. The `start_cb' callback is invoked before working on a * new item, so that the context can be initialized. The `end_cb' callback * is invoked when the item has been processed (successfully or not). * * Since a daemon is not supposed to exit (although it can), there is no * `done' callback. * * Use bg_daemon_enqueue() to enqueue more work to the daemon. */ struct bgtask * bg_daemon_create( const char *name, /**< Task name (for tracing) */ const bgstep_cb_t *steps, /**< Work to perform (copied) */ int stepcnt, /**< Number of steps */ gpointer ucontext, /**< User context */ bgclean_cb_t ucontext_free, /**< Free routine for context */ bgstart_cb_t start_cb, /**< Starting working on an item */ bgend_cb_t end_cb, /**< Done working on an item */ bgclean_cb_t item_free, /**< Free routine for work queue items */ bgnotify_cb_t notify) /**< Start/Stop notify (optional) */ { struct bgtask *bt; int stepsize; g_assert(stepcnt > 0); g_assert(steps); bt = bg_task_alloc(); bt->flags |= TASK_F_DAEMON; bt->name = name; bt->ucontext = ucontext; bt->uctx_free = ucontext_free; bt->start_cb = start_cb; bt->end_cb = end_cb; bt->item_free = item_free; bt->notify = notify; stepsize = stepcnt * sizeof(bgstep_cb_t *); bt->stepcnt = stepcnt; bt->stepvec = walloc(stepsize); memcpy(bt->stepvec, steps, stepsize); bg_runcount++; /* One more task to schedule */ bg_sched_sleep(bt); /* Record sleeping task */ if (bg_debug > 1) { g_debug("BGTASK created daemon task \"%s\" (%d step%s)", name, stepcnt, 1 == stepcnt ? "" : "s"); } return bt; }
void init_video(void) { int ret; if (!video_state.init_done) { ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_NOPARACHUTE); if (ret) { fprintf(stderr, "SDL_Init failed with %d. Is your DISPLAY environment variable set?\n", ret); exit(1); } signal(SIGINT, sigint_handler); atexit(SDL_Quit); video_state.init_done = 1; SDL_WM_SetCaption("Triplane Classic", "Triplane Classic"); SDL_ShowCursor(SDL_DISABLE); if (!draw_with_vircr_mode) { vircr = (unsigned char *) walloc(800 * 600); } } }
static int load_index(CONFIG *config) { char s[100]; int i; if (indx == 0) indx = walloc(ENTRY,NDIPHS); if((config->xfd=fopen(config->index_file,"rb")) == NULL) { (void)fprintf(stderr,"Can't open file %s\n",config->index_file); return -1; } for(i=0;(fgets(s,100,config->xfd) != NULL) && (i < NDIPHS);i++) { sscanf(s,"%s %d %d %d",indx[i].diph,&indx[i].beg,&indx[i].mid,&indx[i].end); } nindex = i; fclose(config->xfd); return 0; }