str_ptr, str_rc, str_intern
Zpp has zend_string* management is distributed over a small number C++ wrapper classes.
zpp::str_ptr is the base class, but does no manipulation of zend_string* reference counts. str_rc adds the mechanism of zend_string* reference counting. Its a wrapper that adds zend_string* using methods that do not need reference count changes.
zpp::str_rc inherits from zpp::str_ptr and adds methods which require reference counting.
In the PHP architecture, strings are special. "interned", means it is undeletable during a request, and has a unique value in PHP internal interned string table. When a .php file is loaded all string literals become interned. The str_intern class gets this pointer when assigned.
String classes
| class | parent | has |
|---|---|---|
| str_ptr | Methods for using zend_string* | |
| str_rc | str_ptr | reference counts contained string, from PHP request memory |
| str_perm | str_rc | Allocated in permanent - ie non-request memory. |
| str_intern | str_rc | Permanent allocation which can becomes new or existing "interned" string. |
| strm_out | A stream-like class for some PHP types using << operator to write to std::output | |
| str_buf | strm_out | Stream buffer outputs to PHP smart_str, using << operator |
| rqstring | C++ string with PHP request memory allocator | std::basic_string<char, char_Traits<char>, alloc_phpreq<char>> |
zpp::state_init and str_intern
Strings are interned because they will be reused in multiple requests. This means their storage is persistent, created at module initialization, and deallocated at module shutdown.
As a result, most string literals in a PHP script, are "interned", as part of PHP compilation and caching. The zend_string hash and a big interned string hash table makes for the singleinstance of each interned string value.
Example from wcc/finder.*
class Finder_init : public state_init
{
public:
Finder_init() : state_init() {}
str_intern folders_key;
str_intern php_ext;
str_intern dir_sep;
void init() override
{
folders_key = "folders";
dir_sep = "/";
php_ext = ".php";
}
};
// Interned strings stored in static memory
Finder_init FDit;
This also suggests that it is good idea for Wcc classes to setup module initialised instances of str_intern. And creating as many reused values as possible is a good idea. It is okay to have a few duplicate string values in multiple compilation units, or extensions, which will end up referencing the same interned zend_string.
With more experience from creating multiple classes, its seems that str_perm is hardly needed at all. str_intern also uses "permanent memory". They were created while trying out different kinds of string constructors and learning what PHP can do with its zend_string functions. For temporary string creation, str_rc can be initialized from a C string literal embedded in the code.
state_init
static members of this class need to be linked to a linked list, in order to get PHP event invocations. The name is derived from the extension name, so one such list for each extension.
str_intern for function names.
To use zpp classes call any function in PHP, its name needs to be in zend_string. For up to 4 arguments, the call can be made through a obj_ptr class, using one of its matching "call" methods
/* Methods of zpp::obj_ptr.
If obj_ptr contains a nullptr, its a global function call.
else its a call to a method of its zend_object
*/
val_rc call(str_ptr method);
val_rc call(str_ptr method, HashTable* args);
val_rc call(str_ptr method, zval* arg1);
//...
val_rc call(str_ptr method,
zval* arg1, zval* arg2, zval* arg3, zval* arg4);
str_ptr
This is a basic wrapper aroung the zend_string*, but does not do reference counting. It has a number of methods that return a str_rc, with a a new zend_string* that is created. Methods that are likely to change the reference count of the contained zend_string* are handled by str_rc, such as lowercase, uppercase and trim functions.
str_intern
Does "internment". Allocated as persistant memory, and then posted to the interned string array. If such a string is already allocated, in the global interned string table, the previously allocated version replaces the newly allocated one, which is deallocated, so only one copy remains. Lots of str_intern members are assigned to state_init instances, called to inititialised MODULE_INIT time, from C strings in the source code, by assignment operator=. Such strings cannot be reference counted, and have their IMMUTABLE flag set. str_rc recognizes this and will not modify their reference count.
str_empty
PHP has a singular empty immutable zend_string*, a valid pointer to a terminating null as the empty string. To get a str_ptr value that is the zend_string canonical "empty string", call str_ptr::empty_string(). This never gets reference counted.
str_buf
This is a wrapper around the zend smart_str C API. It inherits from strm_out, with provides virtual append functions, and the stream operator "<<" for convenience. It has less sophisticated features and class foundation than may be in C++ std::stream, only what was found useful to work for zpp classes.
The strm_buf virtual append functions use the existing PHP smart_str functions as a growing buffer that behaves like, and converts to zend_string structure. Final conversion for export is accessed by the zstr() method. Its zstr() method finalizes the smart_str and stores its null-terminated zend_string* in final_ before returning it. The stored str_rc will be wiped if the str_buf is appended to again. It should be assigned to another str_rc if str_buffer reuse is required. Eventually it also stored a str_rc copy of its final state when the zstr() method is called. Finalization means a call to zstr(). Note that a trick is that zstr() returns a str_ptr of the internal final str_rc, which could be assigned to an external str_rc, but also could be invalidated by another change to the str_buf.
Users of the data() and size() methods , and retrieving a std::string_view using vstr(), must be aware that these do not finalize the str_buf, and may become invalid on subsequent finalisation or append calls. Another perculiarity is that the reset() method calls the internal lose() method, which nulls the smart_str buffer, and decrements and zeros the reference to the previous final string.
rqstring
C++ string types, versitile and generic though they are, are not buffer compatible with reference counted PHP zend_string. A few methods will create an rqstring.
Mostly std::string_view is used to access some of the C++ generic string functions, has specific uses internal in str_ptr / str_rc methods, and this can be returned from a str_ptr class. C++11 does not provide std::string_view last time I checked, so this was a good motivation to to make PHP extension framework for updated C++.
The following definition for rqstring and its allocator code compiled, by taking a published online recipe, and zpp::rqstring should be a reasonable substitute for places if someone would like to use code something like std::string. There are currently here no usage examples compiled, linked and tested using zpp::rqstring, only because its only just been substituted for std::string, which also has had little usage.
typedef std::basic_string<char,std::char_traits<char>, alloc_phpreq<char> > rqstring;
class str_buf : public str_out {
protected:
// temporarystore final, for suspected reference drops
str_rc final_;
smart_str buf;
public:
// .... some interesting functions
// Uses php_printf_to_smart_str .
void printf(const char *format, ...);
// Finalize, 0-terminate, return as std::rqstring,
rqstring str();
// If smart_str contains something, finalize, 0-terminate and store in final_. If already finalized, return
// most recent set final_, which is initially empty zend_string.
str_ptr zstr();
// view of content so far, does not finalize
std::string_view vstr() const;
void reset(); // release the final_ string, and start again
const char* data() const;
size_t size() const;
// ....
zpp::strm_buf encloses a zend_smart_str structure, and uses its C functions to append.
using namespace zpp;
strm_buf buf;
buf << "Start appending " << 101 << " dalmations" << endl;
str_ptr a101 = buf.zstr(); // value exists until buf is changed or destructed