Answer the question
In order to leave comments, you need to log in
System allocators in php extension
The question is for those who wrote the php extension, and not just for c++ developers :)
In general, I write an extension for php in c++. I used to use the good old char * for strings, but over time the code became more and errors associated with strings, too, and so I decided to try switching to std::string. Having rewritten everything to std::string php, it began to fall safely with requests, it turned out empirically that it falls on the banal append operation. I scratched my turnips with a surprised face for 2 days, but I switched to plus lines only because they are safe and do not fall, and then 10 minutes ago it dawned on me! Zend uses its own allocators (emalloc, eralloc, ...) and when I habitually wrote system malloc, realloc, etc. then php also fell, and today it dawned on me that std::string allocates memory with standard allocators, and zend does not perceive this, so it crashes. And how to be in this situation? It turns out that all standard c++ classes cannot be used for php extension? And that I have to write my own copy of std::string? Has anyone encountered this?
Answer the question
In order to leave comments, you need to log in
Implement the allocator template class via emalloc , eralloc, and then use it as a template parameter of std::basic_string and other stl templates that accept allocators.
It turns out that sometimes it is useful to read the documentation.
As a white person, I used and read the docks only on std::string, and I couldn’t even think that a class could accept third-party allocators :) jcmvbkbc could you tell me how to correctly describe the allocator class? Now I have done this
class Ealloc : public std::allocator< char >
{
void *allocate(size_t size, void *ptr = nullptr);
void destroy( void * );
};
// ...
class string : public std::basic_string<char, std::char_traits<char>, Ealloc> //...
// ...
void *Data::Ealloc::allocate(size_t size, void *ptr)
{
return ptr == nullptr ? emalloc(size) : erealloc(ptr, size);
}
void Data::Ealloc::destroy( void *ptr )
{
efree(ptr);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question