Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced VLA with SmallVector template, added -pedantic compiler swit… #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ endif
# you want to leave that flag out on production servers).
#

COMPILER_FLAGS = -Wall -c -std=c++11 -fvisibility=hidden -DBUILDING_PHPCPP -Wno-write-strings -MD
COMPILER_FLAGS = -Wall -c -std=c++11 -fvisibility=hidden -DBUILDING_PHPCPP -Wno-write-strings -MD -pedantic
SHARED_COMPILER_FLAGS = -fpic
STATIC_COMPILER_FLAGS =
PHP_COMPILER_FLAGS = ${COMPILER_FLAGS} `${PHP_CONFIG} --includes`
Expand Down
16 changes: 8 additions & 8 deletions zend/classimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ int ClassImpl::getClosure(zval *object, zend_class_entry **entry_ptr, zend_funct

// done
return SUCCESS;
};
}

/**
* Retrieve pointer to our own object handlers
Expand Down Expand Up @@ -1175,7 +1175,7 @@ zend_object_iterator *ClassImpl::getIterator(zend_class_entry *entry, zval *obje

// retrieve the traversable object
Traversable *traversable = dynamic_cast<Traversable*>(ObjectImpl::find(object)->object());

// use might throw an exception in the getIterator() function
try
{
Expand All @@ -1186,10 +1186,10 @@ zend_object_iterator *ClassImpl::getIterator(zend_class_entry *entry, zval *obje
// the iteraters itself, we can no longer let c++ allocate the buffer + object
// directly, so we first allocate the buffer, which is going to be cleaned up by php)
auto *buffer = emalloc(sizeof(IteratorImpl));

// and then we use placement-new to allocate the implementation
auto *wrapper = new(buffer)IteratorImpl(object, userspace);

// done
return wrapper->implementation();
}
Expand Down Expand Up @@ -1351,7 +1351,7 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
entry.get_static_method = &ClassImpl::getStaticMethod;

// for traversable classes we install a special method to get the iterator
if (_base->traversable())
if (_base->traversable())
{
// install iterator functions
entry.get_iterator = &ClassImpl::getIterator;
Expand Down Expand Up @@ -1406,10 +1406,10 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
// otherwise report an error
else std::cerr << "Derived class " << name() << " is initialized before base class " << interface->name() << ": interface is ignored" << std::endl;
}

// we may have to expose the Traversable or Serializable interfaces
if (_base->traversable()) zend_class_implements(_entry, 1, zend_ce_traversable);
if (_base->serializable()) zend_class_implements(_entry, 1, zend_ce_serializable);
if (_base->traversable()) zend_class_implements(_entry, 1, zend_ce_traversable);
if (_base->serializable()) zend_class_implements(_entry, 1, zend_ce_serializable);

// this pointer has to be copied to temporary pointer, as &this causes compiler error
ClassImpl *impl = this;
Expand Down
1 change: 1 addition & 0 deletions zend/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
/**
* Specific zend implementation files for internal use only
*/
#include "smallvector.h"
#include "init.h"
#include "callable.h"
#include "nativefunction.h"
Expand Down
4 changes: 2 additions & 2 deletions zend/parametersimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class ParametersImpl : public Parameters
reserve(argc);

// array to store all the arguments in
zval arguments[argc];
SmallVector<zval> arguments{argc};

// retrieve the arguments
zend_get_parameters_array_ex(argc, arguments);
zend_get_parameters_array_ex(argc, &arguments[0]);

// loop through the arguments
for (uint32_t i=0; i<argc; i++)
Expand Down
33 changes: 33 additions & 0 deletions zend/smallvector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Php
{

template <typename TElement, int InlineCapacity = 10>
class SmallVector
{
private:
std::vector<TElement> _vector;
TElement *_data;
TElement _buffer[InlineCapacity];

public:
SmallVector(uint32_t size) : SmallVector((int) size) {}
SmallVector(int size)
{
if (size > InlineCapacity)
{
_vector.resize(size);
_data = &_vector[0];
}
else
{
_data = &_buffer[0];
}
}

TElement &operator[](int i)
{
return _data[i];
};
};

}
14 changes: 7 additions & 7 deletions zend/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ static Value do_exec(const zval *object, zval *method, int argc, zval *argv)

// remember current state of the PHP engine
State state;

// call the function
// we're casting the const away here, object is only const so we can call this method
// from const methods after all..
Expand Down Expand Up @@ -892,13 +892,13 @@ Value Value::call(const char *name)
Value Value::exec(int argc, Value *argv) const
{
// array of zvals to execute
zval params[argc];
SmallVector<zval> params{argc};

// convert all the values
for(int i = 0; i < argc; i++) { params[i] = *argv[i]._val; }

// call helper function
return do_exec(nullptr, _val, argc, params);
return do_exec(nullptr, _val, argc, &params[0]);
}

/**
Expand All @@ -914,13 +914,13 @@ Value Value::exec(const char *name, int argc, Value *argv) const
Value method(name);

// array of zvals to execute
zval params[argc];
SmallVector<zval> params{argc};

// convert all the values
for(int i = 0; i < argc; i++) { params[i] = *argv[i]._val; }

// call helper function
return do_exec(_val, method._val, argc, params);
return do_exec(_val, method._val, argc, &params[0]);
}

/**
Expand All @@ -936,13 +936,13 @@ Value Value::exec(const char *name, int argc, Value *argv)
Value method(name);

// array of zvals to execute
zval params[argc];
SmallVector<zval> params{argc};

// convert all the values
for(int i = 0; i < argc; i++) { params[i] = *argv[i]._val; }

// call helper function
return do_exec(_val, method._val, argc, params);
return do_exec(_val, method._val, argc, &params[0]);
}

/**
Expand Down