compr *lzo_compress(unsigned char *buf, int buflen) { int r; lzo_bytep in; lzo_bytep out; lzo_bytep wrkmem; lzo_uint out_len; compr *retdata; pthread_mutex_lock(&lzo_mutex); retdata = s_malloc(sizeof(compr)); in = (lzo_bytep) buf; out = (lzo_bytep) lzo_malloc(OUT_LEN); wrkmem = (lzo_bytep) lzo_malloc(LZO1A_MEM_COMPRESS); if (in == NULL || out == NULL || wrkmem == NULL) { LFATAL("out of memory\n"); exit(3); } r = lzo1a_compress(in, buflen, out, &out_len, wrkmem); if (r != LZO_E_OK) { /* this should NEVER happen */ LFATAL("internal error - compression failed: %d\n", r); exit(2); } // LDEBUG("Compressed %i bytes to %lu bytes",buflen,(unsigned long)out_len); if ( out_len > buflen ) { retdata->data = s_malloc(buflen+1); memcpy(&retdata->data[1], buf, buflen); retdata->size = buflen+1; retdata->data[0]=0; } else { retdata->data = s_malloc(out_len+1); retdata->size = out_len+1; memcpy(&retdata->data[1], out, out_len); retdata->data[0]='L'; } lzo_free(wrkmem); lzo_free(out); pthread_mutex_unlock(&lzo_mutex); return retdata; }
void * s_calloc (size_t nmemb, size_t size) { void *ret; ret = s_malloc(nmemb * size); if (ret == NULL) { debugf(DFAT, "Not enough memory!"); } memset(ret, 0, nmemb * size); return ret; }
/* Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. * * The string is always null-termined (all the sds strings are, always) so * even if you create an sds string with: * * mystring = sdsnewlen("abc",3); * * You can print the string with printf() as there is an implicit \0 at the * end of the string. However the string is binary safe and can contain * \0 characters in the middle, as the length is stored in the sds header. */ sds sdsnewlen(const void *init, size_t initlen) { void *sh; sds s; char type = sdsReqType(initlen); /* Empty strings are usually created in order to append. Use type 8 * since type 5 is not good at this. */ if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; int hdrlen = sdsHdrSize(type); unsigned char *fp; /* flags pointer. */ sh = s_malloc(hdrlen+initlen+1); if (!init) memset(sh, 0, hdrlen+initlen+1); if (sh == NULL) return NULL; s = (char*)sh+hdrlen; fp = ((unsigned char*)s)-1; switch(type) { case SDS_TYPE_5: { *fp = type | (initlen << SDS_TYPE_BITS); break; } case SDS_TYPE_8: { SDS_HDR_VAR(8,s); sh->len = initlen; sh->alloc = initlen; *fp = type; break; } case SDS_TYPE_16: { SDS_HDR_VAR(16,s); sh->len = initlen; sh->alloc = initlen; *fp = type; break; } case SDS_TYPE_32: { SDS_HDR_VAR(32,s); sh->len = initlen; sh->alloc = initlen; *fp = type; break; } case SDS_TYPE_64: { SDS_HDR_VAR(64,s); sh->len = initlen; sh->alloc = initlen; *fp = type; break; } } if (initlen && init) memcpy(s, init, initlen); s[initlen] = '\0'; return s; }
s_thread_t * s_thread_create (void * (*f) (void *), void *farg) { s_thread_t *tid; s_thread_arg_t *arg; if ((s_thread_api == NULL) || (s_thread_api->thread_create == NULL)) { return NULL; } tid = (s_thread_t *) s_malloc(sizeof(s_thread_t)); arg = (s_thread_arg_t *) s_malloc(sizeof(s_thread_arg_t)); arg->r = &s_thread_run; arg->f = f; arg->arg = farg; s_thread_cond_init(&arg->cond); s_thread_mutex_init(&arg->mut); s_thread_mutex_lock(arg->mut); arg->flag = 0; s_thread_cond_signal(arg->cond); s_thread_mutex_unlock(arg->mut); s_thread_api->thread_create(tid, arg); s_thread_mutex_lock(arg->mut); while (arg->flag != 1) { if (s_thread_cond_wait(arg->cond, arg->mut)) { debugf(DSYS, "s_thread_cond_wait failed"); return NULL; } } s_thread_mutex_unlock(arg->mut); s_thread_cond_destroy(arg->cond); s_thread_mutex_destroy(arg->mut); s_free(arg); arg = NULL; return tid; }
void s_debug_debugf (unsigned short flags, char *file, int line, char *func, char *fmt, ...) { int n; int s; char *p; va_list args; #if !defined(CONFIG_DEBUG) if ((flags & DFAT) == 0) { return; } #endif fprintf(stderr, "[0x%08X] ", s_thread_self()); if (flags & DFAT) { fprintf(stderr, "FATAL : "); } if (flags & DSYS) { fprintf(stderr, "SYSERR : "); } if (flags & DSER) { fprintf(stderr, "SERVER :: "); } if (flags & DCLI) { fprintf(stderr, "CLIENT :: "); } s = 100; if ((p = s_malloc(sizeof(char) * s)) == NULL) { goto err; } while (1) { va_start(args, fmt); n = vsnprintf(p, s, fmt, args); va_end(args); if (n > -1 && n < s) { break; } if (n > -1) { s = n + 1; } else { s *= 2; } if ((p = s_realloc(p, s)) == NULL) { goto err; } } fprintf(stderr, p); s_free(p); if (flags & DSYS) { fprintf(stderr, " : %s", strerror(errno)); } fprintf(stderr, " [%s (%s:%d)]\n", func, file, line); if (flags & DFAT) { goto err; } return; err: exit(1); }
char *strdup(const char *str) { char *new_str; if (!str) return NULL; new_str = s_malloc(sizeof(char)*(strlen(str)+1)); strcpy(new_str, str); return new_str; }
/* * Add key + data pair to the linked list of nodes */ static KEYNODE * KMapAddKeyNode(KMAP * km,void * key, int n, void * userdata ) { KEYNODE * knode; if( n < 0 ) { return 0; } knode = (KEYNODE*) s_malloc( sizeof(KEYNODE) ); if( !knode ) { return 0; } memset(knode, 0, sizeof(KEYNODE) ); knode->key = (unsigned char*)s_malloc(n); // Alloc the key space if( !knode->key ) { free(knode); return 0; } memcpy(knode->key,key,n); // Copy the key knode->nkey = n; knode->userdata = userdata; if( km->keylist ) // Insert at front of list { knode->next = km->keylist; km->keylist = knode; } else { km->keylist = knode; } return knode; }
int s_config_category_init (s_config_cat_t **cat, char *name) { (*cat) = (s_config_cat_t *) s_malloc(sizeof(s_config_cat_t)); (*cat)->name = strdup(name); if (s_list_init(&((*cat)->variable))) { goto err0; } return 0; err0: s_free((*cat)->name); s_free(*cat); return -1; }
KMAP * KMapNew( void (*userfree)(void*p) ) { KMAP * km = (KMAP*) s_malloc( sizeof(KMAP) ); if( !km ) return 0; memset(km, 0, sizeof(KMAP)); km->userfree = userfree; return km; }
GROUP_ *newgroup () { GROUP_ *tmp; tmp = (GROUP_ *) s_malloc (sizeof (GROUP_)); tmp->members = s_strdup (members); tmp->n = 0; tmp->value = 0.0; tmp->left = tmp->right = NULL; return tmp; }
KMAP * KMapNew( KMapUserFreeFunc userfree ) { KMAP * km = (KMAP*) s_malloc( sizeof(KMAP) ); if( !km ) return 0; memset(km, 0, sizeof(KMAP)); km->userfree = userfree; return km; }
window *window_alloc(sock *Conn) { uint32_t i; window *Window = (window *)s_malloc(sizeof(window)); Window->Frame = (frame **)s_malloc(sizeof(frame **) * Conn->window_size); //&&frame ?? for (i = 0; i < Conn->window_size; i++) { Window->Frame[i] = frame_alloc(Conn); Window->Frame[i]->state = FRAME_EMPTY; } Window->size = Conn->window_size; Window->top = Conn->seq + Window->size; Window->bottom = Conn->seq + 1; Window->rr = Window->bottom; Window->srej = Window->bottom; Window->eof = FALSE; Window->buffsize = Conn->buffsize; return Window; }
int s_image_get_buf (s_surface_t *surface, s_image_t *img) { s_surface_t *s; if (img->buf != NULL) { s_image_free_buf(img); } img->buf = (char *) s_malloc(img->w * img->h * surface->bytesperpixel + 1); s_surface_create_from_data(&s, img->w, img->h, surface->bitsperpixel, img->buf, 0); s_putboxrgb(s, 0, 0, img->w, img->h, img->rgba); s_surface_destroy(s); return 0; }
PQ_STATUS init_pq(priority_queue_t* ptr,priority_queuq_cmp_ptr cmp,uint size) { ptr->priority_queue = (void **)s_malloc(sizeof(void*)*(size+1)); if(NULL == ptr->priority_queue) { /*分配内存失败*/ LOG_ERROR("priority_queue malloc failed!%s",""); return INIT_PQ_FAIL; } ptr->size = 0; ptr->capacity = size + 1; ptr->cmp = cmp; return INIT_PQ_OK; }
void lwqq_async_timer_watch(LwqqAsyncTimerHandle timer,unsigned int timeout_ms,LwqqAsyncTimerCallback fun,void* data) { double second = (timeout_ms) / 1000.0; ev_timer_init(timer,timer_cb_wrap,second,second); LwqqAsyncTimerWrap* wrap = s_malloc(sizeof(*wrap)); wrap->callback = fun; wrap->data = data; timer->data = wrap; ev_timer_start(EV_DEFAULT,timer); if(ev_thread_status!=THREAD_NOW_RUNNING) start_ev_thread(); }
void push_front(deque* d, void* elem) { node* add = (node*) s_malloc(sizeof(node)); add->data = elem; add->next = d->begin->next; add->prev = d->begin; // there's dummy node, so prev must be it! if(d->begin->next) d->begin->next->prev = add; else d->end = add; d->begin->next = add; ++d->size; }
char* r_readdir(r_dir_t *rdir) { size_t len; char *filename; struct dirent *dentry; struct stat fstats; if (rdir == NULL || rdir->dir == NULL || rdir->name == NULL) return NULL; while (true) { if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) { if (STREQ(dentry->d_name, ".") || STREQ(dentry->d_name, "..")) continue; len = strlen(rdir->name) + strlen(dentry->d_name) + 2; filename = (char*) s_malloc(len); snprintf(filename, len, "%s%s%s", rdir->name, rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", dentry->d_name); if (stat(filename, &fstats) < 0) continue; if (S_ISDIR(fstats.st_mode)) { /* put subdirectory on the stack */ if (rdir->stlen == rdir->stcap) { rdir->stcap *= 2; rdir->stack = (char**) s_realloc(rdir->stack, rdir->stcap * sizeof(char*)); } rdir->stack[rdir->stlen++] = filename; continue; } return filename; } if (rdir->stlen > 0) { /* open next subdirectory */ closedir(rdir->dir); if (rdir->d != 0) free(rdir->name); rdir->name = rdir->stack[--rdir->stlen]; rdir->d = 1; if ((rdir->dir = opendir(rdir->name)) == NULL) warn("could not open directory: %s", rdir->name); continue; } /* no more entries */ break; } return NULL; }
GAULFUNC boolean ga_chromosome_char_allocate(population *pop, entity *embryo) { int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!embryo) die("Null pointer to entity structure passed."); if (embryo->chromosome!=NULL) die("This entity already contains chromosomes."); if ( !(embryo->chromosome = s_malloc(pop->num_chromosomes*sizeof(char *))) ) die("Unable to allocate memory"); if ( !(embryo->chromosome[0] = s_malloc(pop->num_chromosomes*pop->len_chromosomes*sizeof(char))) ) die("Unable to allocate memory"); for (i=1; i<pop->num_chromosomes; i++) { embryo->chromosome[i] = &(((char *)embryo->chromosome[i-1])[pop->len_chromosomes]); } return TRUE; }
void code_get_image (s_hashtable_t *htable, s_xml_node_t *node, unsigned int *istyle, unsigned int *irotate, unsigned int *icount, char ***ivar) { int i; int count; char **var; char *cntstr; s_xml_node_t *tmp; FRAME_SHAPE shape; FRAME_SHADOW shadow; FRAME_IMAGE_ROTATION rotate; *istyle = 0; *irotate = 0; *icount = 0; *ivar = NULL; count = atoi(s_xml_node_get_path_value(node, "count")); if (count == 0) { return; } code_get_enum(htable, s_xml_node_get_path_value(node, "rotate"), &rotate); code_get_style(htable, s_xml_node_get_path(node, "style"), &shape, &shadow); cntstr = (char *) s_malloc(sizeof(char *) * 255); var = (char **) s_malloc(sizeof(char **) * count); for (i = 0; i < count; i++) { sprintf(cntstr, "image%d", i); tmp = s_xml_node_get_path(node, cntstr); var[i] = strdup(tmp->value); tmp->dontparse = 1; } if ((tmp = s_xml_node_get_path(node, "style")) != NULL) { tmp->dontparse = 1; } if ((tmp = s_xml_node_get_path(node, "style/shape")) != NULL) { tmp->dontparse = 1; } if ((tmp = s_xml_node_get_path(node, "style/shadow")) != NULL) { tmp->dontparse = 1; } if ((tmp = s_xml_node_get_path(node, "count")) != NULL) { tmp->dontparse = 1; } if ((tmp = s_xml_node_get_path(node, "rotate")) != NULL) { tmp->dontparse = 1; } s_free(cntstr); *istyle = shape | shadow; *irotate = rotate; *icount = count; *ivar = var; }
compr *lzo_decompress(unsigned char *buf, int buflen) { int r; lzo_bytep in; lzo_bytep out; lzo_bytep wrkmem; lzo_uint out_len = 0; lzo_uint in_len = 0; compr *retdata; FUNC; pthread_mutex_lock(&lzo_mutex); retdata = s_malloc(sizeof(compr)); in_len = buflen-1; in = (lzo_bytep) &buf[1]; out = (lzo_bytep) lzo_malloc(BLKSIZE); wrkmem = (lzo_bytep) lzo_malloc(LZO1A_MEM_COMPRESS); //FASTER if (in == NULL || out == NULL || wrkmem == NULL) { LFATAL("out of memory\n"); exit(3); } r = lzo1a_decompress(in, in_len, out, &out_len, NULL); if (r != LZO_E_OK) { /* this should NEVER happen */ LFATAL("internal error - decompression failed: %d\n", r); exit(22); } retdata->data = s_malloc(out_len); retdata->size = out_len; memcpy(retdata->data, out, out_len); lzo_free(wrkmem); lzo_free(out); pthread_mutex_unlock(&lzo_mutex); return retdata; }
int timer_new_slang(void) { chrono_t *t=s_malloc(sizeof(chrono_t)); int t_handle; THREAD_LOCK(chrono_table_lock); if (chrono_table==NULL) chrono_table=table_new(); t_handle = table_add(chrono_table, (vpointer) t); THREAD_UNLOCK(chrono_table_lock); return (int) t_handle; }
static unsigned char * lxynth_init_driver (unsigned char *param, unsigned char *display) { int bpp; int Bpp; DEBUGF ("%s (%s:%d)\n", __FUNCTION__, __FILE__, __LINE__); lxynth_root = (lxynth_root_t *) s_malloc(sizeof(lxynth_root_t)); lxynth_root->eventq = (lxynth_eventq_t *) s_malloc(sizeof(lxynth_eventq_t)); s_list_init(&(lxynth_root->eventq->list)); s_thread_mutex_init(&(lxynth_root->eventq->mut)); lxynth_root->gd = (lxynth_gd_t *) s_malloc(sizeof(lxynth_gd_t)); s_list_init(&(lxynth_root->gd->list)); s_thread_mutex_init(&(lxynth_root->gd->mut)); lxynth_root->gd->active = NULL; s_window_init(&(lxynth_root->window)); s_window_new(lxynth_root->window, WINDOW_MAIN, NULL); s_window_set_coor(lxynth_root->window, WINDOW_NOFORM, 50, 50, (lxynth_root->window->surface->width * 2) / 3, (lxynth_root->window->surface->height * 2) / 3); bpp = lxynth_root->window->surface->bitsperpixel; Bpp = lxynth_root->window->surface->bytesperpixel; if (bpp == 32) { bpp = 24; } xynth_driver.depth = (bpp << 3) | Bpp; s_window_show(lxynth_root->window); lxynth_root->tid = s_thread_create(s_window_main, lxynth_root->window); s_window_atevent(lxynth_root->window, lxynth_atevent); s_window_atexit(lxynth_root->window, lxynth_atexit); lxynth_root->running = 1; install_timer(20, lxynth_timer, NULL); return NULL; }
char *strndup(const char *str, size_t n) { char *new_str=NULL; if (str) { new_str = s_malloc(sizeof(char)*(n+1)); strncpy(new_str, str, n); new_str[n] = '\0'; } return new_str; }
unsigned char *safepassword() { int len; unsigned char *safepasswd; len = strlen((char *) config->passwd); if (len > 16) len = 16; safepasswd = s_malloc(16); memset(safepasswd, 65, 16); memcpy(safepasswd, config->passwd, len); return safepasswd; }
/* * Create a character node */ static KMAPNODE * KMapCreateNode(KMAP * km) { KMAPNODE * mn=(KMAPNODE*)s_malloc( sizeof(KMAPNODE) ); if(!mn) return NULL; memset(mn,0,sizeof(KMAPNODE)); km->nchars++; return mn; }
void taskbar_clock_draw (s_window_t *window, s_timer_t *timer) { int w_; time_t t_; struct tm *t; int _w = 0; char *vbuf; s_surface_t *srf; tbar_data_t *tbar_data; tbar_clock_t *tbar_clock; int c0 = s_rgbcolor(window->surface, 96, 96, 96); int c1 = s_rgbcolor(window->surface, 255, 255, 255); int c2 = s_rgbcolor(window->surface, 220, 220, 220); tbar_data = (tbar_data_t *) window->data; tbar_clock = tbar_data->tbar_clock; t_ = time(NULL); t = localtime(&t_); vbuf = (char *) s_malloc(sizeof(char) * 10); if (t->tm_sec & 1) { sprintf(vbuf, "%02d:%02d ", t->tm_hour, t->tm_min); } else { sprintf(vbuf, "%02d %02d ", t->tm_hour, t->tm_min); } s_font_set_str(tbar_clock->font, vbuf); s_free(vbuf); s_font_get_glyph(tbar_clock->font); if (s_surface_create(&srf, tbar_clock->rect.w, tbar_clock->rect.h, window->surface->bitsperpixel)) { return; } s_fillbox(srf, 0, 0, tbar_clock->rect.w, tbar_clock->rect.h, c0); s_fillbox(srf, 1, 1, tbar_clock->rect.w - 1, tbar_clock->rect.h - 1, c1); s_fillbox(srf, 1, 1, tbar_clock->rect.w - 2, tbar_clock->rect.h - 2, c2); w_ = tbar_clock->font->glyph.img->w; if (tbar_clock->font->glyph.img->w > (tbar_clock->rect.w - 6)) { w_ = tbar_clock->rect.w - 6; _w = tbar_clock->font->glyph.img->w - (tbar_clock->rect.w - 6); } s_putboxpartrgba(srf, 3, 4, w_, tbar_clock->font->glyph.img->h, tbar_clock->font->glyph.img->w, tbar_clock->font->glyph.img->h, tbar_clock->font->glyph.img->rgba, 0, 0); s_putbox(window->surface, tbar_clock->rect.x, tbar_clock->rect.y, tbar_clock->rect.w, tbar_clock->rect.h, srf->vbuf); s_surface_destroy(srf); return; }
/* * Allocat a new hash node, uses Auto Node Recovery if needed and enabled. * * The oldest node is the one with the longest time since it was last touched, * and does not have any direct indication of how long the node has been around. * We don't monitor the actual time since last being touched, instead we use a * splayed global list of node pointers. As nodes are accessed they are splayed * to the front of the list. The oldest node is just the tail node. * */ static SFXHASH_NODE * sfxhash_newnode( SFXHASH * t ) { SFXHASH_NODE * hnode; /* Recycle Old Nodes - if any */ hnode = sfxhash_get_free_node( t ); /* Allocate memory for a node */ if( ! hnode ) { hnode = (SFXHASH_NODE*)s_malloc( t, sizeof(SFXHASH_NODE) + t->keysize + t->datasize ); } /* If we still haven't found hnode, we're at our memory limit. * * Uses Automatic Node Recovery, to recycle the oldest node-based on access * (Unlink and reuse the tail node) */ if( !hnode && t->anr_flag && t->gtail ) { /* Find the oldes node the users willing to let go. */ for(hnode = t->gtail; hnode; hnode = hnode->gprev ) { if( t->anrfree ) /* User has provided a permission+release callback function */ { t->anr_tries++;/* Count # ANR requests */ /* Ask the user for permission to release this node, but let them say no! */ if( t->anrfree( hnode->key, hnode->data ) ) { /* NO, don't recycle this node, user's not ready to let it go. */ continue; } /* YES, user said we can recycle this node */ } sfxhash_gunlink_node( t, hnode ); /* unlink from the global list */ sfxhash_unlink_node( t, hnode ); /* unlink from the row list */ t->count--; t->anr_count++; /* count # of ANR operations */ break; } } /* either we are returning a node or we're all full and the user * won't let us allocate anymore and we return NULL */ return hnode; }
static struct graphics_device * lxynth_init_device (void) { lxynth_device_t *wd; struct graphics_device *gd; DEBUGF ("%s (%s:%d)\n", __FUNCTION__, __FILE__, __LINE__); wd = (lxynth_device_t *) s_malloc(sizeof(lxynth_device_t)); wd->update = (s_rect_t) {-1, -1, -1, -1}; gd = (struct graphics_device *) s_malloc(sizeof(struct graphics_device)); wd->title = NULL; if (s_surface_create(&wd->surface, lxynth_root->window->surface->buf->w , lxynth_root->window->surface->buf->h, lxynth_root->window->surface->bitsperpixel)) { s_free(gd); s_free(wd); return NULL; } gd->size.x1 = 0; gd->size.x2 = wd->surface->width; gd->size.y1 = 0; gd->size.y2 = wd->surface->height; gd->clip.x1 = 0; gd->clip.x2 = gd->size.x2; gd->clip.y1 = 0; gd->clip.y2 = gd->size.y2; gd->drv = &xynth_driver; gd->driver_data = wd; gd->user_data = NULL; s_thread_mutex_lock(lxynth_root->gd->mut); s_list_add(lxynth_root->gd->list, gd, -1); lxynth_root->gd->active = gd; s_thread_mutex_unlock(lxynth_root->gd->mut); return gd; }
int s_handlers_init (s_window_t *window) { window->handlers = (s_handlers_t *) s_malloc(sizeof(s_handlers_t)); if (s_list_init(&(window->handlers->list))) { goto err0; } if (s_thread_mutex_init(&(window->handlers->mut))) { goto err1; } return 0; err1: s_list_uninit(window->handlers->list); err0: s_free(window->handlers); return 1; }
void w_signal_send (w_object_t *from, w_object_t *to, void (*func) (w_signal_t *), void *arg) { s_event_t *event; w_signal_t *signal; s_event_init(&event); event->type = SIGNAL_EVENT; signal = (w_signal_t *) s_malloc(sizeof(w_signal_t)); signal->from = from; signal->to = to; signal->func = func; signal->arg = arg; event->data = (void *) signal; s_eventq_add(to->window->window, event); }