Skip to content
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
34 changes: 23 additions & 11 deletions php_ketama/ketama.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "pecl-compat/compat.h"
#include "php_ketama.h"
#include "ketama.h"

Expand All @@ -33,10 +34,9 @@ ZEND_DECLARE_MODULE_GLOBALS(ketama)
*/

/* True global resources - no need for thread safety here */
static int le_ketama;
static int le_ketama_continuum;

static void ketama_continuum_dtor( zend_rsrc_list_entry *rsrc TSRMLS_DC )
static void ketama_continuum_dtor( zend_resource *rsrc TSRMLS_DC )
{
ketama_continuum continuum = (ketama_continuum)rsrc->ptr;
ketama_smoke( continuum );
Expand Down Expand Up @@ -164,7 +164,7 @@ PHP_MINFO_FUNCTION(ketama)
PHP_FUNCTION(ketama_roll)
{
char *filename;
long filename_len;
COMPAT_ARG_SIZE_T filename_len;

if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len ) == FAILURE )
{
Expand All @@ -174,7 +174,7 @@ PHP_FUNCTION(ketama_roll)
ketama_continuum c;
if ( ketama_roll( &c, filename ) )
{
ZEND_REGISTER_RESOURCE( return_value, c, le_ketama_continuum );
compat_zend_register_resource(return_value, c, le_ketama_continuum TSRMLS_DC);
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "unable to create Ketama continuum: %s", ketama_error());
}
Expand All @@ -194,8 +194,12 @@ PHP_FUNCTION(ketama_destroy)
return;
}

ZEND_FETCH_RESOURCE( continuum, ketama_continuum, &r, -1, "ketama continuum", le_ketama_continuum );
zend_list_delete( Z_LVAL_P( r ) );
continuum = (ketama_continuum) compat_zend_fetch_resource(r, "ketama continuum", le_ketama_continuum TSRMLS_CC);
if (continuum == NULL) {
return;
}

compat_zend_delete_resource(r TSRMLS_CC);
}
/* }}} */

Expand All @@ -212,7 +216,11 @@ PHP_FUNCTION(ketama_print_continuum)
return;
}

ZEND_FETCH_RESOURCE( continuum, ketama_continuum, &r, -1, "ketama continuum", le_ketama_continuum );
continuum = (ketama_continuum) compat_zend_fetch_resource(r, "ketama continuum", le_ketama_continuum TSRMLS_CC);
if (continuum == NULL) {
return;
}

ketama_print_continuum( continuum );
}
/* }}} */
Expand All @@ -222,7 +230,7 @@ PHP_FUNCTION(ketama_print_continuum)
Prints the latest ketama error */
PHP_FUNCTION(ketama_error)
{
RETURN_STRING( (char *)ketama_error(), 1 );
RETURN_STRING((char *)ketama_error());
}
/* }}} */

Expand All @@ -234,20 +242,24 @@ PHP_FUNCTION(ketama_get_server)
zval *zcontinuum;
ketama_continuum continuum;
char *key;
long key_len;
COMPAT_ARG_SIZE_T key_len;
mcs* server;

if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sr", &key, &key_len, &zcontinuum ) == FAILURE )
{
return;
}

ZEND_FETCH_RESOURCE( continuum, ketama_continuum, &zcontinuum, -1, "ketama continuum", le_ketama_continuum );
continuum = (ketama_continuum) compat_zend_fetch_resource(zcontinuum, "ketama continuum", le_ketama_continuum TSRMLS_CC);
if (continuum == NULL) {
return;
}

server = ketama_get_server( key, continuum );

array_init( return_value );
add_assoc_long( return_value, "point", server->point );
add_assoc_string( return_value, "ip", server->ip, 1 );
add_assoc_string( return_value, "ip", server->ip );
}
/* }}} */

Expand Down
68 changes: 68 additions & 0 deletions php_ketama/pecl-compat/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--------------------------------------------------------------------
The PHP License, version 3.01
Copyright (c) 1999 - 2016 The PHP Group. All rights reserved.
--------------------------------------------------------------------

Redistribution and use in source and binary forms, with or without
modification, is permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.

3. The name "PHP" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact group@php.net.

4. Products derived from this software may not be called "PHP", nor
may "PHP" appear in their name, without prior written permission
from group@php.net. You may indicate that your software works in
conjunction with PHP by saying "Foo for PHP" instead of calling
it "PHP Foo" or "phpfoo"

5. The PHP Group may publish revised and/or new versions of the
license from time to time. Each version will be given a
distinguishing version number.
Once covered code has been published under a particular version
of the license, you may always continue to use it under the terms
of that version. You may also choose to use such covered code
under the terms of any subsequent version of the license
published by the PHP Group. No one other than the PHP Group has
the right to modify the terms applicable to covered code created
under this License.

6. Redistributions of any form whatsoever must retain the following
acknowledgment:
"This product includes PHP software, freely available from
<http://www.php.net/software/>".

THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND
ANY EXPRESSED 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 PHP
DEVELOPMENT TEAM OR ITS CONTRIBUTORS 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.

--------------------------------------------------------------------

This software consists of voluntary contributions made by many
individuals on behalf of the PHP Group.

The PHP Group can be contacted via Email at group@php.net.

For more information on the PHP Group and the PHP project,
please see <http://www.php.net>.

PHP includes the Zend Engine, freely available at
<http://www.zend.com>.
33 changes: 33 additions & 0 deletions php_ketama/pecl-compat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
The PECL compatibility library is a set of C include files whose objective is to minimize the pain of making a PHP extension compatible with PHP 5 and 7.

Project home : [https://github.com/flaupretre/pecl-compat](https://github.com/flaupretre/pecl-compat)

# Main features

- A set of miscellaneous compatibility macros,

- A backport of the *zend_string* functions and macros to PHP 5,

- A backport of most PHP 7 *zend_hash* features to PHP 5.

- A compatibility library for simple resource handling

# Examples

For examples of using this library, look at the [PCS extension](https://github.com/flaupretre/pecl-pcs). There, you will see usage examples of most *pecl-compat* features.

# Usage

Using the library is simple. Download the latest release from the [github repository](https://github.com/flaupretre/pecl-compat/releases) and insert it a new subdirectory inside your code tree. Then, include the 'compat.h' file in every '.c' source file.

# History

I first tried to port the [PHK](http://pecl.php.net/package/phk) extension to PHP 7 using raw '#ifdef' directives. Unfortunately, it quickly became clear that it was not possible without making the code totally unreadable. For pure 'bridge' extensions, it may be possible. But, as soon as you make use of hash tables and strings, your code quicly becomes very hard to maintain.

Then, I explored other solutions : separate branches, duplicate code trees... without finding one I would be satisfied with, mostly because all these solutions bring their own set of maintainability issues : separate branches bring a huge versioning problem, and separate code trees are a maintenance headache in the long term. As PHP 5 modules will probably need to be maintained during years, none of these solutions looked convincing to me.

So, I reverted to the solution of keeping a single source tree and move most of my conditional code to a reusable compatibility layer. In order to benefit of the PHP 7 performance increase, most pecl-compat features are PHP 7 features backported to PHP 5. PHP 7 generally calls the underlying PHP functions directly, with the same performance as a pure-PHP7 development. So, when adapting your extension to use pecl-compat, you will, at the same time, change your code to use new PHP 7 features and make it compatible with both PHP 5 & 7. As a side effect, this makes it easier to discard PHP 5 compatibility in the future, when time has come.

# Supported PHP versions

5.3 and above.
109 changes: 109 additions & 0 deletions php_ketama/pecl-compat/compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
+----------------------------------------------------------------------+
| Compatibility macros for different PHP versions |
+----------------------------------------------------------------------+
| Copyright (c) 2015 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Francois Laupretre <francois@tekwire.net> |
+----------------------------------------------------------------------+
*/

#ifndef _COMPAT_H
#define _COMPAT_H

#define PECL_COMPAT_VERSION 1.3

#include <stdio.h>
#include <assert.h>
#include <fcntl.h>

#include "php.h"
#include "zend.h"
#include "zend_extensions.h"
#include "zend_API.h"

#define PHP_5_0_X_API_NO 220040412
#define PHP_5_1_X_API_NO 220051025
#define PHP_5_2_X_API_NO 220060519
#define PHP_5_3_X_API_NO 220090626
#define PHP_5_4_X_API_NO 220100525
#define PHP_5_5_X_API_NO 220121212
#define PHP_5_6_X_API_NO 220131226

#if PHP_MAJOR_VERSION >= 7
# define PHP_7
#endif

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#if HAVE_STRING_H
# include <string.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef PHP_WIN32
# include "win32/time.h"
#elif defined(NETWARE)
# include <sys/timeval.h>
# include <sys/time.h>
#else
# include <sys/time.h>
#endif

#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif

#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif

#ifdef PHP_WIN32
#include <win32/php_stdint.h>
#else
#include <inttypes.h>
#endif

#if ZEND_EXTENSION_API_NO >= PHP_5_6_X_API_NO
#include "zend_virtual_cwd.h"
#else
#include "TSRM/tsrm_virtual_cwd.h"
#endif

#ifdef PHP_7
#include "Zend/zend_portability.h"
#endif

/*-- Include submodules */

#include "src/misc.h"
#include "src/zend_string.h"
#include "src/zend_hash.h"
#include "src/zend_resource.h"

#endif /* _COMPAT_H */
Loading