Skip to content

Commit

Permalink
add ZAPI_GV macro
Browse files Browse the repository at this point in the history
  • Loading branch information
zzusoftboy committed Jun 6, 2017
1 parent ca0a56e commit 4e64855
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
31 changes: 31 additions & 0 deletions include/zapi/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define ZAPI_GLOBAL_H

#include "php/main/php.h"
#include "php/Zend/zend_API.h"

#include "zapi/Version.h"
#include "zapi/Config.h"
Expand Down Expand Up @@ -198,4 +199,34 @@ using Callback = std::function<void()>();

#define ZAPI_ASSERT(x) (x);


// The way how PHP C API deals with "global" variables is peculiar.
//
// The following macros are supposed to turn into a structure that is going
// to be instantiated for each parallel running request, and for which the
// PHP engine allocates a certain amount of memory, and a magic pointer that
// is passed and should be forwarded to every thinkable PHP function.
//
// We don't use this architecture. We have our own environment object
// that makes much more sense, and that we use. However, the Zend engine
// expects this structure and this structure to exist.
ZEND_BEGIN_MODULE_GLOBALS(zapi)
ZEND_END_MODULE_GLOBALS(zapi)

// And now we're going to define a macro. This also is a uncommon architecture
// from PHP to get access to a variable from the structure above.
#ifdef ZTS
# define ZAPI_G(v) TSRM(zapi_globals_id, zapi_globals *, v)
#else
# define ZAPI_G(v) (zapi_globals.v)
#endif


// We're almost there, we now need to declare an instance of the
// structure defined above (if building for a single thread) or some
// sort of impossible to understand magic pointer-to-a-pointer (for
// multi-threading builds). We make this a static variable because
// this already is bad enough.
ZEND_EXTERN_MODULE_GLOBALS(zapi)

#endif //ZAPI_GLOBAL_H
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(ZAPI_SRC
kernel/Global.cpp)
kernel/Global.cpp
vm/Extension.cpp)

zapi_add_library(${ZAPI_PROJECT_NAME} SHARED ${ZAPI_SRC}
${ZAPI_PUBLIC_HEADERS}
Expand Down
40 changes: 40 additions & 0 deletions src/vm/Extension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017-2018 zzu_softboy <zzu_softboy@163.com>
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Created by zzu_softboy on 06/06/2017.

#include "zapi/vm/Extension.h"
#include "php/Zend/zend_API.h"

#ifdef ZTS
# include "TSRM.h"
#endif

namespace zapi
{
namespace vm
{

/**
* We're almost there, we now need to declare an instance of the
* structure defined above (if building for a single thread) or some
* sort of impossible to understand magic pointer-to-a-pointer (for
* multi-threading builds). We make this a static variable because
* this already is bad enough.
*/
ZEND_DECLARE_MODULE_GLOBALS(zapi)

static void init_globals(zend_zapi_globals *globals);

} // vm
} // zapi

0 comments on commit 4e64855

Please sign in to comment.