本文整理汇总了C++中s_malloc函数的典型用法代码示例。如果您正苦于以下问题:C++ s_malloc函数的具体用法?C++ s_malloc怎么用?C++ s_malloc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了s_malloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pthread_mutex_lock
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;
}
示例2: s_calloc
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;
}
示例3: sdsnewlen
/* 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;
}
示例4: s_thread_create
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;
}
示例5: s_debug_debugf
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);
}
示例6: s_malloc
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;
}
示例7: KMapAddKeyNode
/*
* 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;
}
示例8: s_config_category_init
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;
}
示例9: KMapNew
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;
}
示例10: s_malloc
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;
}
示例11: KMapNew
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;
}
示例12:
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;
}
示例13: s_image_get_buf
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;
}
示例14: init_pq
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;
}
示例15: lwqq_async_timer_watch
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();
}