Skip to content

Commit ed2a237

Browse files
author
Julien Pauli
committed
Fixes, links, rewordings
1 parent f1cbf8b commit ed2a237

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

Book/php7/internal_types/strings/smart_str.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ a request-bound allocation for the underlying ``zend_string``. Example::
7979
smart_str_appends_ex(&my_str, "Hello world", 1); /* 1 means persistent allocation */
8080

8181
Then, depending on what you want to append, you'll use the right API call. If you append a classical C string, you can
82-
use ``smart_str_appends()``. If you make use of a binary string, and thus know its length, then use
83-
``smart_str_appendl()``.
82+
use ``smart_str_appends(smart_str *dst, const char *src)``. If you make use of a binary string, and thus know its
83+
length, then use ``smart_str_appendl(smart_str *dst, const char *src, size_t len)``.
8484

85-
The less specific ``smart_str_append()`` simply appends a ``zend_string`` to your ``smart_str`` string. And if you come
86-
to play with others ``smart_str``, use ``smart_str_append_smart_str()`` to combine them together.
85+
The less specific ``smart_str_append(smart_str *dest, const zend_string *src)`` simply appends a ``zend_string`` to
86+
your ``smart_str`` string. And if you come to play with others ``smart_str``, use
87+
``smart_str_append_smart_str(smart_str *dst, const smart_str *src)`` to combine them together.
8788

8889
smart_str specific tricks
8990
*************************

Book/php7/internal_types/strings/zend_strings.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Strings management: zend_string
22
===============================
33

4-
Any program needs to manage strings. Here, we'll detail the zend_string structure which helps for that.
5-
Every time PHP needs to work with a string, a zend_string structure will be used. This structure is just a simple
4+
Any program needs to manage strings. Here, we'll detail a custom solution that fits PHP needs : ``zend_string``.
5+
Every time PHP needs to work with a string, a ``zend_string`` structure will be used. This structure is just a simple
66
thin wrapper over the ``char *`` string type of the C language.
77

88
It adds memory management facilities, so that a same string can be shared in several places without the need to
99
duplicate it. Also, some strings are "interned", that is they are "persistent" allocated and specialy managed by the
10-
memory manager so that they don't get destroyed across several requests.
10+
memory manager so that they don't get destroyed across several requests. Those later get a permanent allocation from
11+
:doc:`Zend Memory Manager <../../memory_management/zend_memory_manager>`.
1112

1213
Structure and access macros
1314
---------------------------
@@ -73,6 +74,9 @@ shout at you about a memory leak you just created.
7374
If you pass 1, you ask for what we called a "persistent" allocation, that is the engine will use a traditionnal C
7475
``malloc()`` call and will not track the memory allocation in any way.
7576

77+
.. note:: If you need more information about memory management, you may read the :doc:`dedicated chapter
78+
<../../memory_management>`.
79+
7680
Then, we display the string. We access the character array by using the ``ZSTR_VAL()`` macro. ``ZSTR_LEN()`` allows
7781
access to the length information. ``zend_string`` related macros all start with ``ZSTR_**()``, beware that is not the
7882
same as ``Z_STR**()`` macros.

Book/php7/memory_management/zend_memory_manager.rst

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Zend Memory Manager
22
===================
33

4-
Zend Memory Manager, often abbreviated as ZendMM or ZMM, is a C layer that aims to provide abilities to allocate and
5-
release dynamic **request-bound** memory.
4+
Zend Memory Manager, often abbreviated as *ZendMM* or *ZMM*, is a C layer that aims to provide abilities to allocate
5+
and release dynamic **request-bound** memory.
66

77
Note the "request-bound" in the above sentence.
88

@@ -18,7 +18,7 @@ PHP is a share-nothing architecture. Well, not at 100%. Let us explain.
1818
here, you'll get additionnal informations about the different steps and cycles that can be drawn from PHP
1919
lifetime.
2020

21-
PHP can treat several (dozen, thousands?) of requests into the same process. By default, PHP will forget anything it
21+
PHP can treat several hundreds or thousands of requests into the same process. By default, PHP will forget anything it
2222
knows of the current request, when that later finishes.
2323

2424
"Forgetting" things translates to freeing any dynamic buffer that got allocated while treating a request. That means
@@ -35,7 +35,7 @@ default, PHP forgets *a very huge number* of informations from one request to an
3535

3636
There exists however some pretty rare informations you need to persist across several requests. But that's uncommon.
3737

38-
What could be kept unchanged through requests? What we call **persistent** objects. Once more let us insist : those
38+
What could be kept unchanged through requests ? What we call **persistent** objects. Once more let us insist : those
3939
are rare cases. For example, the current PHP executable path won't change from requests to requests. That latter
4040
information is allocated permanently, that means it is allocated with a traditionnal libc's ``malloc()`` call.
4141

@@ -45,19 +45,19 @@ be allocated once.
4545

4646
What you must remember:
4747

48-
* There exists two kinds of dynamic memory allocations while programming PHP (or extensions):
48+
* There exists two kinds of dynamic memory allocations while programming PHP Core or extensions:
4949
* Request-bound dynamic allocations.
5050
* Permanent dynamic allocations.
5151

5252
* Request-bound dynamic memory allocations
5353
* Must only be performed when PHP is treating a request (not before, nor after).
54-
* Can only be performed using the ZendMM dynamic memory allocation API.
55-
* Are very common, basically 95% of your dynamic allocations will be request-bound.
56-
* Are tracked by ZendMM, and you'll be informed about bad usage of the memory area, or if you leak.
54+
* Should only be performed using the ZendMM dynamic memory allocation API.
55+
* Are very common in extensions design, basically 95% of your dynamic allocations will be request-bound.
56+
* Are tracked by ZendMM, and you'll be informed about leaking.
5757

5858
* Permanent dynamic memory allocations
5959
* Should not be performed while PHP is treating a request (not forbidden, but a bad idea).
60-
* Are not tracked by ZendMM, and you won't be informed about bad usage of the memory area, or if you leak.
60+
* Are not tracked by ZendMM, and you won't be informed about leaking.
6161
* Should be pretty rare in an extension.
6262

6363
Also, keep in mind that all PHP source code has been based on such a memory level. Thus, many internal structures get
@@ -82,8 +82,8 @@ Persistent allocated one::
8282
zend_array ar;
8383
zend_hash_init(&ar, 8, NULL, NULL, 1);
8484

85-
It is always the same in all the different Zend APIs. Usually, it is weither a "0" to pass as last parameter to mean
86-
"I want this structure to be allocated using ZendMM, so request-bound", or "1" meaning "I want this structure to get
85+
It is always the same in all the different Zend APIs. Usually, it is whether a *"0"* to pass as last parameter to mean
86+
"I want this structure to be allocated using ZendMM, so request-bound", or *"1"* meaning "I want this structure to get
8787
allocated bypassing ZendMM and using a traditionnal libc's ``malloc()`` call".
8888

8989
Obviously, those structures provide an API that remembers how it did allocate the structure, to use the right
@@ -127,17 +127,17 @@ Whatever happens, pointers returned by ZendMM must be freed using ZendMM, aka ``
127127
.. note:: A note on persistent allocations. Persistent allocations stay alive between requests. You traditionnaly use
128128
the common libc ``malloc/free`` to perform that, but ZendMM has got some shortcuts to libc allocator : the
129129
"persistent" API. This API starts by the *"p"* letter and let you choose between ZendMM alloc, or persistent
130-
alloc. Hence a ``pemalloc(<size>, 1)`` is nothing more than a ``malloc()``, a ``pefree(<ptr>, 1)`` is a
131-
``free()`` and a ``pestrdup(<ptr>, 1)`` is a ``strdup()``. Just to say.
130+
alloc. Hence a ``pemalloc(size_t, 1)`` is nothing more than a ``malloc()``, a ``pefree(void *, 1)`` is a
131+
``free()`` and a ``pestrdup(void *, 1)`` is a ``strdup()``. Just to say.
132132

133133
Zend Memory Manager debugging shields
134134
*************************************
135135

136136
ZendMM provides the following abilities:
137137

138138
* Memory consumption management.
139-
* Memory leak tracking.
140-
* Buffer overflows or underflows.
139+
* Memory leak tracking and automatic-free.
140+
* Speed up in allocations by pre-allocating well-known-sized buffers and keeping a warm cache on free
141141

142142
Memory consumption management
143143
-----------------------------
@@ -183,7 +183,7 @@ If you forget to free blocks, they will all get displayed on *stderr*. This proc
183183
in the following conditions:
184184

185185
* You are using :doc:`a debug build<../build_system/building_php>` of PHP
186-
* You have report_memleaks=On in php.ini (default)
186+
* You have *report_memleaks=On* in php.ini (default)
187187

188188
Here is an example of a simple leak into an extension::
189189

@@ -206,14 +206,21 @@ Beware however:
206206
than using it. Hence, ZendMM can only warn you about allocations it is aware of, every traditionnal libc allocation
207207
won't be reported in here, f.e.
208208
* If PHP shuts down in an incorrect maner (what we call an unclean shutdown), ZendMM will report tons of leaks. This is
209-
because when incorrectly shutdown, the engine uses a longjmp() call to a catch block, preventing every code that cleans
210-
memory to fire-in. Thus, many leaks get reported. This happens especially after a call to PHP's exit()/die(), or if a
211-
fatal error gets triggered in some critical parts of PHP.
212-
* If you use a non-debug build of PHP, nothing shows on stderr, ZendMM is dumb.
209+
because when incorrectly shutdown, the engine uses a
210+
`longjmp() <http://man7.org/linux/man-pages/man3/longjmp.3.html>`_ call to a catch block, preventing every code that
211+
cleans memory to fire-in. Thus, many leaks get reported. This happens especially after a call to PHP's exit()/die(),
212+
or if a fatal error gets triggered in some critical parts of PHP.
213+
* If you use a non-debug build of PHP, nothing shows on *stderr*, ZendMM is dumb but will still clean any allocated
214+
request-bound buffer that's not been explicitely freed by the programmer
213215

214216
What you must remember is that ZendMM leak tracking is a nice bonus tool to have, but it does not replace a
215217
:doc:`true C memory debugger <./memory_debugging>`.
216218

219+
ZendMM internal design
220+
**********************
221+
222+
.. todo:: todo
223+
217224
Common errors and mistakes
218225
**************************
219226

0 commit comments

Comments
 (0)