xref: /PHP-7.0/Zend/zend_smart_str.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Dmitry Stogov <dmitry@zend.com>                              |
16    +----------------------------------------------------------------------+
17  */
18 
19 #include <zend.h>
20 #include "zend_smart_str_public.h"
21 
22 #define SMART_STR_OVERHEAD (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE)
23 
24 #ifndef SMART_STR_PAGE
25 # define SMART_STR_PAGE 4096
26 #endif
27 
28 #ifndef SMART_STR_START_SIZE
29 # define SMART_STR_START_SIZE (256 - SMART_STR_OVERHEAD - 1)
30 #endif
31 
32 #define SMART_STR_NEW_SIZE(len) \
33 	(((len + SMART_STR_OVERHEAD + SMART_STR_PAGE) & ~(SMART_STR_PAGE - 1)) - SMART_STR_OVERHEAD - 1)
34 
smart_str_erealloc(smart_str * str,size_t len)35 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len)
36 {
37 	if (UNEXPECTED(!str->s)) {
38 		str->a = len < SMART_STR_START_SIZE
39 				? SMART_STR_START_SIZE
40 				: SMART_STR_NEW_SIZE(len);
41 		str->s = zend_string_alloc(str->a, 0);
42 		ZSTR_LEN(str->s) = 0;
43 	} else {
44 		str->a = SMART_STR_NEW_SIZE(len);
45 		str->s = (zend_string *) erealloc2(str->s, _ZSTR_HEADER_SIZE + str->a + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s) + 1);
46 	}
47 }
48 
smart_str_realloc(smart_str * str,size_t len)49 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len)
50 {
51 	if (UNEXPECTED(!str->s)) {
52 		str->a = len < SMART_STR_START_SIZE
53 				? SMART_STR_START_SIZE
54 				: SMART_STR_NEW_SIZE(len);
55 		str->s = zend_string_alloc(str->a, 1);
56 		ZSTR_LEN(str->s) = 0;
57 	} else {
58 		str->a = SMART_STR_NEW_SIZE(len);
59 		str->s = (zend_string *) realloc(str->s, _ZSTR_HEADER_SIZE + str->a + 1);
60 	}
61 }
62