fast_task_queue.c 17.0 KB
Newer Older
Y
yuqing 已提交
1 2 3 4 5
//fast_task_queue.c

#include <errno.h>
#include <sys/resource.h>
#include <pthread.h>
Y
yuqing 已提交
6
#include <inttypes.h>
Y
yuqing 已提交
7 8 9 10 11 12 13
#include "fast_task_queue.h"
#include "logger.h"
#include "shared_func.h"
#include "pthread_func.h"

static struct fast_task_queue g_free_queue;

Y
yuqing 已提交
14
struct mpool_node {
Y
yuqing 已提交
15 16
	struct fast_task_info *blocks;
	struct fast_task_info *last_block;   //last block
Y
yuqing 已提交
17 18 19 20 21 22
	struct mpool_node *next;
};

struct mpool_chain {
    struct mpool_node *head;
    struct mpool_node *tail;
Y
yuqing 已提交
23 24 25
};

static struct mpool_chain g_mpool = {NULL, NULL};
Y
yuqing 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

int task_queue_init(struct fast_task_queue *pQueue)
{
	int result;

	if ((result=init_pthread_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"init_pthread_lock fail, errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return result;
	}

	pQueue->head = NULL;
	pQueue->tail = NULL;

	return 0;
}

Y
yuqing 已提交
45
static struct mpool_node *malloc_mpool(const int total_alloc_size)
Y
yuqing 已提交
46 47 48 49
{
	struct fast_task_info *pTask;
	char *p;
	char *pCharEnd;
Y
yuqing 已提交
50
	struct mpool_node *mpool;
Y
yuqing 已提交
51

Y
yuqing 已提交
52
	mpool = (struct mpool_node *)malloc(sizeof(struct mpool_node));
Y
yuqing 已提交
53 54 55 56 57
	if (mpool == NULL)
	{
		logError("file: "__FILE__", line: %d, " \
			"malloc %d bytes fail, " \
			"errno: %d, error info: %s", \
Y
yuqing 已提交
58
			__LINE__, (int)sizeof(struct mpool_node), \
Y
yuqing 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			errno, STRERROR(errno));
		return NULL;
	}

	mpool->next = NULL;
	mpool->blocks = (struct fast_task_info *)malloc(total_alloc_size);
	if (mpool->blocks == NULL)
	{
		logError("file: "__FILE__", line: %d, " \
			"malloc %d bytes fail, " \
			"errno: %d, error info: %s", \
			__LINE__, total_alloc_size, \
			errno, STRERROR(errno));
		free(mpool);
		return NULL;
	}
	memset(mpool->blocks, 0, total_alloc_size);

	pCharEnd = ((char *)mpool->blocks) + total_alloc_size;
Y
yuqing 已提交
78
	for (p=(char *)mpool->blocks; p<pCharEnd; p += g_free_queue.block_size)
Y
yuqing 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	{
		pTask = (struct fast_task_info *)p;
		pTask->size = g_free_queue.min_buff_size;

		pTask->arg = p + ALIGNED_TASK_INFO_SIZE;
		if (g_free_queue.malloc_whole_block)
		{
			pTask->data = (char *)pTask->arg + \
					g_free_queue.arg_size;
		}
		else
		{
			pTask->data = (char *)malloc(pTask->size);
			if (pTask->data == NULL)
			{
				char *pt;

				logError("file: "__FILE__", line: %d, " \
					"malloc %d bytes fail, " \
					"errno: %d, error info: %s", \
					__LINE__, pTask->size, \
					errno, STRERROR(errno));

				for (pt=(char *)mpool->blocks; pt < p; \
Y
yuqing 已提交
103
					pt += g_free_queue.block_size)
Y
yuqing 已提交
104 105 106 107 108 109 110 111 112 113 114
				{
					free(((struct fast_task_info *)pt)->data);
				}

				free(mpool->blocks);
				free(mpool);
				return NULL;
			}
		}
	}

Y
yuqing 已提交
115 116
	mpool->last_block = (struct fast_task_info *)(pCharEnd - g_free_queue.block_size);
	for (p=(char *)mpool->blocks; p<(char *)mpool->last_block; p += g_free_queue.block_size)
Y
yuqing 已提交
117 118
	{
		pTask = (struct fast_task_info *)p;
Y
yuqing 已提交
119
		pTask->next = (struct fast_task_info *)(p + g_free_queue.block_size);
Y
yuqing 已提交
120 121 122 123 124 125
	}
	mpool->last_block->next = NULL;

	return mpool;
}

Y
yuqing 已提交
126 127 128
int free_queue_init_ex(const int max_connections, const int init_connections,
        const int alloc_task_once, const int min_buff_size,
        const int max_buff_size, const int arg_size)
Y
yuqing 已提交
129
{
Y
yuqing 已提交
130
#define MAX_DATA_SIZE  (256 * 1024 * 1024)
Y
yuqing 已提交
131
	int64_t total_size;
Y
yuqing 已提交
132
	struct mpool_node *mpool;
Y
yuqing 已提交
133
	int alloc_size;
Y
yuqing 已提交
134
    int alloc_once;
Y
yuqing 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	int result;
	int loop_count;
	int aligned_min_size;
	int aligned_max_size;
	int aligned_arg_size;
	rlim_t max_data_size;

	if ((result=init_pthread_lock(&(g_free_queue.lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"init_pthread_lock fail, errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return result;
	}

	aligned_min_size = MEM_ALIGN(min_buff_size);
	aligned_max_size = MEM_ALIGN(max_buff_size);
	aligned_arg_size = MEM_ALIGN(arg_size);
Y
yuqing 已提交
153 154
	g_free_queue.block_size = ALIGNED_TASK_INFO_SIZE + aligned_arg_size;
	alloc_size = g_free_queue.block_size * init_connections;
Y
yuqing 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if (aligned_max_size > aligned_min_size)
	{
		total_size = alloc_size;
		g_free_queue.malloc_whole_block = false;
		max_data_size = 0;
	}
	else
	{
		struct rlimit rlimit_data;

		if (getrlimit(RLIMIT_DATA, &rlimit_data) < 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"call getrlimit fail, " \
				"errno: %d, error info: %s", \
				__LINE__, errno, STRERROR(errno));
			return errno != 0 ? errno : EPERM;
		}
		if (rlimit_data.rlim_cur == RLIM_INFINITY)
		{
Y
yuqing 已提交
175
			max_data_size = MAX_DATA_SIZE;
Y
yuqing 已提交
176 177 178 179
		}
		else
		{
			max_data_size = rlimit_data.rlim_cur;
Y
yuqing 已提交
180
			if (max_data_size > MAX_DATA_SIZE)
Y
yuqing 已提交
181
			{
Y
yuqing 已提交
182
				max_data_size = MAX_DATA_SIZE;
Y
yuqing 已提交
183 184 185
			}
		}

Y
yuqing 已提交
186 187
		if (max_data_size >= (int64_t)(g_free_queue.block_size + aligned_min_size) *
			(int64_t)init_connections)
Y
yuqing 已提交
188 189
		{
			total_size = alloc_size + (int64_t)aligned_min_size *
Y
yuqing 已提交
190
					init_connections;
Y
yuqing 已提交
191
			g_free_queue.malloc_whole_block = true;
Y
yuqing 已提交
192
			g_free_queue.block_size += aligned_min_size;
Y
yuqing 已提交
193 194 195 196 197 198 199 200 201 202
		}
		else
		{
			total_size = alloc_size;
			g_free_queue.malloc_whole_block = false;
			max_data_size = 0;
		}
	}

	g_free_queue.max_connections = max_connections;
203
	g_free_queue.alloc_connections = init_connections;
Y
yuqing 已提交
204 205 206
    if (alloc_task_once <= 0)
    {
        g_free_queue.alloc_task_once = 256;
Y
yuqing 已提交
207 208 209
		alloc_once = MAX_DATA_SIZE / g_free_queue.block_size;
        if (g_free_queue.alloc_task_once > alloc_once)
        {
210
            g_free_queue.alloc_task_once = alloc_once > 0 ? alloc_once : 1;
Y
yuqing 已提交
211
        }
Y
yuqing 已提交
212 213 214 215 216
    }
    else
    {
        g_free_queue.alloc_task_once = alloc_task_once;
    }
Y
yuqing 已提交
217 218 219 220
	g_free_queue.min_buff_size = aligned_min_size;
	g_free_queue.max_buff_size = aligned_max_size;
	g_free_queue.arg_size = aligned_arg_size;

Y
yuqing 已提交
221 222 223 224 225 226 227 228
	logDebug("file: "__FILE__", line: %d, "
		"max_connections: %d, init_connections: %d, alloc_task_once: %d, "
        "min_buff_size: %d, max_buff_size: %d, block_size: %d, "
        "arg_size: %d, max_data_size: %d, total_size: %"PRId64,
        __LINE__, max_connections, init_connections,
        g_free_queue.alloc_task_once, aligned_min_size, aligned_max_size,
        g_free_queue.block_size, aligned_arg_size, (int)max_data_size, total_size);

Y
yuqing 已提交
229
	if ((!g_free_queue.malloc_whole_block) || (total_size <= max_data_size))
Y
yuqing 已提交
230 231
	{
		loop_count = 1;
Y
yuqing 已提交
232
		mpool = malloc_mpool(total_size);
Y
yuqing 已提交
233 234 235 236
		if (mpool == NULL)
		{
			return errno != 0 ? errno : ENOMEM;
		}
Y
yuqing 已提交
237 238
		g_mpool.head = mpool;
		g_mpool.tail = mpool;
Y
yuqing 已提交
239 240 241 242
	}
	else
	{
		int remain_count;
243
		int alloc_count;
Y
yuqing 已提交
244 245 246
		int current_alloc_size;

		loop_count = 0;
Y
yuqing 已提交
247 248
		remain_count = init_connections;
		alloc_once = max_data_size / g_free_queue.block_size;
Y
yuqing 已提交
249 250
		while (remain_count > 0)
		{
251
			alloc_count = (remain_count > alloc_once) ?
Y
yuqing 已提交
252
					alloc_once : remain_count;
253
			current_alloc_size = g_free_queue.block_size * alloc_count;
Y
yuqing 已提交
254
			mpool = malloc_mpool(current_alloc_size);
Y
yuqing 已提交
255 256 257 258 259 260
			if (mpool == NULL)
			{
				free_queue_destroy();
				return errno != 0 ? errno : ENOMEM;
			}

Y
yuqing 已提交
261
			if (g_mpool.tail == NULL)
Y
yuqing 已提交
262
			{
Y
yuqing 已提交
263
				g_mpool.head = mpool;
Y
yuqing 已提交
264 265 266
			}
			else
			{
Y
yuqing 已提交
267
				g_mpool.tail->next = mpool;
268
				g_mpool.tail->last_block->next = mpool->blocks;  //link previous mpool to current
Y
yuqing 已提交
269
			}
Y
yuqing 已提交
270
            g_mpool.tail = mpool;
Y
yuqing 已提交
271

272
			remain_count -= alloc_count;
Y
yuqing 已提交
273 274 275 276 277 278 279 280 281 282 283
			loop_count++;
		}

		logDebug("file: "__FILE__", line: %d, " \
			"alloc_once: %d", __LINE__, alloc_once);
	}

	logDebug("file: "__FILE__", line: %d, " \
		"malloc task info as whole: %d, malloc loop count: %d", \
		__LINE__, g_free_queue.malloc_whole_block, loop_count);

Y
yuqing 已提交
284
	if (g_mpool.head != NULL)
Y
yuqing 已提交
285
	{
Y
yuqing 已提交
286 287
		g_free_queue.head = g_mpool.head->blocks;
		g_free_queue.tail = g_mpool.tail->last_block;
Y
yuqing 已提交
288 289 290 291 292
	}

	return 0;
}

Y
yuqing 已提交
293 294 295 296 297 298 299
int free_queue_init(const int max_connections, const int min_buff_size,
		const int max_buff_size, const int arg_size)
{
    return free_queue_init_ex(max_connections, max_connections,
        0, min_buff_size, max_buff_size, arg_size);
}

Y
yuqing 已提交
300 301
void free_queue_destroy()
{
Y
yuqing 已提交
302 303
	struct mpool_node *mpool;
	struct mpool_node *mp;
Y
yuqing 已提交
304

Y
yuqing 已提交
305
	if (g_mpool.head == NULL)
Y
yuqing 已提交
306 307 308 309 310 311 312 313 314 315
	{
		return;
	}

	if (!g_free_queue.malloc_whole_block)
	{
		char *p;
		char *pCharEnd;
		struct fast_task_info *pTask;

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        mpool = g_mpool.head;
        while (mpool != NULL)
        {
            pCharEnd = (char *)mpool->last_block + g_free_queue.block_size;
            for (p=(char *)mpool->blocks; p<pCharEnd; p += g_free_queue.block_size)
            {
                pTask = (struct fast_task_info *)p;
                if (pTask->data != NULL)
                {
                    free(pTask->data);
                    pTask->data = NULL;
                }
            }
            mpool = mpool->next;
        }
Y
yuqing 已提交
331 332
	}

Y
yuqing 已提交
333
	mpool = g_mpool.head;
Y
yuqing 已提交
334 335 336 337 338 339 340 341
	while (mpool != NULL)
	{
		mp = mpool;
		mpool = mpool->next;

		free(mp->blocks);
		free(mp);
	}
Y
yuqing 已提交
342
	g_mpool.head = g_mpool.tail = NULL;
Y
yuqing 已提交
343 344 345 346

	pthread_mutex_destroy(&(g_free_queue.lock));
}

Y
yuqing 已提交
347 348 349 350 351 352
static int free_queue_realloc()
{
	struct mpool_node *mpool;
	struct fast_task_info *head;
	struct fast_task_info *tail;
	int remain_count;
353
	int alloc_count;
Y
yuqing 已提交
354 355 356 357
	int current_alloc_size;

    head = tail = NULL;
	remain_count = g_free_queue.max_connections -
358 359
        g_free_queue.alloc_connections;
    alloc_count = (remain_count > g_free_queue.alloc_task_once) ?
Y
yuqing 已提交
360
        g_free_queue.alloc_task_once : remain_count;
361
    if (alloc_count > 0)
Y
yuqing 已提交
362
	{
363
		current_alloc_size = g_free_queue.block_size * alloc_count;
Y
yuqing 已提交
364 365 366
		mpool = malloc_mpool(current_alloc_size);
		if (mpool == NULL)
		{
Y
yuqing 已提交
367
            return ENOMEM;
Y
yuqing 已提交
368 369 370 371 372 373 374 375 376 377 378 379
		}

		if (g_mpool.tail == NULL)
		{
			g_mpool.head = mpool;
		}
		else
		{
			g_mpool.tail->next = mpool;
		}
        g_mpool.tail = mpool;

380
        head = mpool->blocks;
Y
yuqing 已提交
381 382
        tail = mpool->last_block;

383
		remain_count -= alloc_count;
Y
yuqing 已提交
384
	}
Y
yuqing 已提交
385 386 387
    else {
        return ENOSPC;
    }
Y
yuqing 已提交
388 389 390 391 392 393 394 395 396 397 398

    if (g_free_queue.head == NULL)
    {
        g_free_queue.head = head;
    }
    if (g_free_queue.tail != NULL)
    {
        g_free_queue.tail->next = head;
    }
    g_free_queue.tail = tail;

399
    g_free_queue.alloc_connections += alloc_count;
Y
yuqing 已提交
400

Y
yuqing 已提交
401
	logDebug("file: "__FILE__", line: %d, "
402 403
		"alloc_connections: %d, realloc %d elements", __LINE__,
        g_free_queue.alloc_connections, alloc_count);
Y
yuqing 已提交
404

Y
yuqing 已提交
405
    return 0;
Y
yuqing 已提交
406 407
}

Y
yuqing 已提交
408 409
struct fast_task_info *free_queue_pop()
{
Y
yuqing 已提交
410
    struct fast_task_info *pTask;
411 412
    int i;

Y
yuqing 已提交
413 414 415 416 417
	if ((pTask=task_queue_pop(&g_free_queue)) != NULL)
    {
        return pTask;
    }

418
    if (g_free_queue.alloc_connections >= g_free_queue.max_connections)
Y
yuqing 已提交
419 420 421 422
    {
        return NULL;
    }

423
    for (i=0; i<10; i++)
Y
yuqing 已提交
424
    {
425 426
        pthread_mutex_lock(&g_free_queue.lock);
        if (g_free_queue.alloc_connections >= g_free_queue.max_connections)
Y
yuqing 已提交
427
        {
428 429 430 431 432
            if (g_free_queue.head == NULL)
            {
                pthread_mutex_unlock(&g_free_queue.lock);
                return NULL;
            }
Y
yuqing 已提交
433
        }
434 435 436 437 438 439 440 441 442 443 444
        else
        {
            if (g_free_queue.head == NULL && free_queue_realloc() != 0)
            {
                pthread_mutex_unlock(&g_free_queue.lock);
                return NULL;
            }
        }
        pthread_mutex_unlock(&g_free_queue.lock);

        if ((pTask=task_queue_pop(&g_free_queue)) != NULL)
Y
yuqing 已提交
445
        {
446
            return pTask;
Y
yuqing 已提交
447 448 449
        }
    }

450
    return NULL;
Y
yuqing 已提交
451 452
}

453 454
static int _realloc_buffer(struct fast_task_info *pTask, const int new_size,
        const bool copy_data)
Y
yuqing 已提交
455 456
{
	char *new_buff;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    new_buff = (char *)malloc(new_size);
    if (new_buff == NULL)
    {
        logError("file: "__FILE__", line: %d, "
                "malloc %d bytes fail, "
                "errno: %d, error info: %s",
                __LINE__, new_size,
                errno, STRERROR(errno));
        return errno != 0 ? errno : ENOMEM;
    }
    else
    {
        if (copy_data && pTask->offset > 0) {
            memcpy(new_buff, pTask->data, pTask->offset);
        }
        free(pTask->data);
        pTask->size = new_size;
        pTask->data = new_buff;
        return 0;
    }
}

int free_queue_push(struct fast_task_info *pTask)
{
Y
yuqing 已提交
481 482 483 484 485 486 487 488 489
	int result;

	*(pTask->client_ip) = '\0';
	pTask->length = 0;
	pTask->offset = 0;
	pTask->req_count = 0;

	if (pTask->size > g_free_queue.min_buff_size) //need thrink
	{
490
        _realloc_buffer(pTask, g_free_queue.min_buff_size, false);
Y
yuqing 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
	}

	if ((result=pthread_mutex_lock(&g_free_queue.lock)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	pTask->next = g_free_queue.head;
	g_free_queue.head = pTask;
	if (g_free_queue.tail == NULL)
	{
		g_free_queue.tail = pTask;
	}

	if ((result=pthread_mutex_unlock(&g_free_queue.lock)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return result;
}

int free_queue_count()
{
	return task_queue_count(&g_free_queue);
}

524
int free_queue_alloc_connections()
525
{
526
    return g_free_queue.alloc_connections;
527 528
}

529 530 531 532 533 534 535 536 537 538 539 540
int free_queue_set_buffer_size(struct fast_task_info *pTask,
        const int expect_size)
{
    return task_queue_set_buffer_size(&g_free_queue, pTask, expect_size);
}

int free_queue_realloc_buffer(struct fast_task_info *pTask,
        const int expect_size)
{
    return task_queue_realloc_buffer(&g_free_queue, pTask, expect_size);
}

541 542 543 544 545 546 547 548 549 550 551
int free_queue_set_max_buffer_size(struct fast_task_info *pTask)
{
    return task_queue_set_buffer_size(&g_free_queue, pTask,
            g_free_queue.max_buff_size);
}

int free_queue_realloc_max_buffer(struct fast_task_info *pTask)
{
    return task_queue_realloc_buffer(&g_free_queue, pTask,
            g_free_queue.max_buff_size);
}
Y
yuqing 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
int task_queue_push(struct fast_task_queue *pQueue, \
		struct fast_task_info *pTask)
{
	int result;

	if ((result=pthread_mutex_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return result;
	}

	pTask->next = NULL;
	if (pQueue->tail == NULL)
	{
		pQueue->head = pTask;
	}
	else
	{
		pQueue->tail->next = pTask;
	}
	pQueue->tail = pTask;

	if ((result=pthread_mutex_unlock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return 0;
}

struct fast_task_info *task_queue_pop(struct fast_task_queue *pQueue)
{
	struct fast_task_info *pTask;
	int result;

	if ((result=pthread_mutex_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return NULL;
	}

	pTask = pQueue->head;
	if (pTask != NULL)
	{
		pQueue->head = pTask->next;
		if (pQueue->head == NULL)
		{
			pQueue->tail = NULL;
		}
	}

	if ((result=pthread_mutex_unlock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return pTask;
}

int task_queue_count(struct fast_task_queue *pQueue)
{
	struct fast_task_info *pTask;
	int count;
	int result;

	if ((result=pthread_mutex_lock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_lock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
		return 0;
	}

	count = 0;
	pTask = pQueue->head;
	while (pTask != NULL)
	{
		pTask = pTask->next;
		count++;
	}

	if ((result=pthread_mutex_unlock(&(pQueue->lock))) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"call pthread_mutex_unlock fail, " \
			"errno: %d, error info: %s", \
			__LINE__, result, STRERROR(result));
	}

	return count;
}

657 658
int task_queue_get_new_buffer_size(const int min_buff_size,
        const int max_buff_size, const int expect_size, int *new_size)
659
{
660
    if (min_buff_size == max_buff_size)
661 662 663 664 665 666
    {
        logError("file: "__FILE__", line: %d, "
                "can't change buffer size because NOT supported", __LINE__);
        return EOPNOTSUPP;
    }

667
    if (expect_size > max_buff_size)
668 669 670 671
    {
        logError("file: "__FILE__", line: %d, "
                "can't change buffer size because expect buffer size: %d "
                "exceeds max buffer size: %d", __LINE__, expect_size,
672
                max_buff_size);
673 674 675
        return EOVERFLOW;
    }

676 677
    *new_size = min_buff_size;
    if (expect_size > min_buff_size)
678 679 680 681 682
    {
        while (*new_size < expect_size)
        {
            *new_size *= 2;
        }
683
        if (*new_size > max_buff_size)
684
        {
685
            *new_size = max_buff_size;
686 687 688 689 690 691
        }
    }

    return 0;
}

692 693 694 695
#define  _get_new_buffer_size(pQueue, expect_size, new_size) \
    task_queue_get_new_buffer_size(pQueue->min_buff_size, \
            pQueue->max_buff_size, expect_size, new_size)

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
int task_queue_set_buffer_size(struct fast_task_queue *pQueue,
        struct fast_task_info *pTask, const int expect_size)
{
    int result;
    int new_size;

    if ((result=_get_new_buffer_size(pQueue, expect_size, &new_size)) != 0) {
        return result;
    }
    if (pTask->size == new_size)  //do NOT need change buffer size
    {
        return 0;
    }

    return _realloc_buffer(pTask, new_size, false);
}

int task_queue_realloc_buffer(struct fast_task_queue *pQueue,
        struct fast_task_info *pTask, const int expect_size)
{
    int result;
    int new_size;

    if (pTask->size >= expect_size)  //do NOT need change buffer size
    {
        return 0;
    }

    if ((result=_get_new_buffer_size(pQueue, expect_size, &new_size)) != 0) {
        return result;
    }

    return _realloc_buffer(pTask, new_size, true);
}