示例#1
0
static char* ensure(printbuffer *p, int needed)
{
	char *newbuffer;
	int newsize;
	if(!p || !p->buffer)
		return 0;
	needed += p->offset;
	if(needed <= p->length)
		return p->buffer + p->offset;

	newsize = pow2gt(needed);
	newbuffer = (char*) cJSON_malloc(newsize);
	if(!newbuffer)
	{
		cJSON_free(p->buffer);
		p->length = 0, p->buffer = 0;
		return 0;
	}
	if(newbuffer)
		memcpy(newbuffer, p->buffer, p->length);
	cJSON_free(p->buffer);
	p->length = newsize;
	p->buffer = newbuffer;
	return newbuffer + p->offset;
}
示例#2
0
文件: cJSON.c 项目: FSMaxB/cJSON
/* realloc printbuffer if necessary to have at least "needed" bytes more */
static char* ensure(printbuffer *p, int needed)
{
    char *newbuffer = NULL;
    int newsize = 0;
    if (!p || !p->buffer)
    {
        return NULL;
    }
    needed += p->offset;
    if (needed <= p->length)
    {
        return p->buffer + p->offset;
    }

    if (p->noalloc) {
        return NULL;
    }

    newsize = pow2gt(needed);
    newbuffer = (char*)cJSON_malloc(newsize);
    if (!newbuffer)
    {
        cJSON_free(p->buffer);
        p->length = 0;
        p->buffer = NULL;

        return NULL;
    }
    if (newbuffer)
    {
        memcpy(newbuffer, p->buffer, p->length);
    }
    cJSON_free(p->buffer);
    p->length = newsize;
    p->buffer = newbuffer;

    return newbuffer + p->offset;
}