Autor Beitrag
tomycat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Sa 24.06.23 19:55 
hallo,
ich habe eine Erweiterung in php abgeschrieben. Leider bekomme ich keine Antwort vom dem Progger.

Ubuntu 22 mit PHP 7

Quellcode per git heruntergeladen...
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
./bulid
./configure --disable-all --with-config-file-path=/etc/php.ini -with-config-file-scan-dir=/etc/php.d
make j4
make install

cd ext
./ext_skel --extname=sum


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum# cat test.php
ausblenden PHP-Quelltext
1:
2:
3:
4:
5:
6:
<?php
$total = sum([1,2,2,3,5,6]);

var_dump($total);

?>


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum# cat sum.c
ausblenden volle Höhe C
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
/*
  +----------------------------------------------------------------------+
  | PHP Version 7                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2017 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:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

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

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_sum.h"

/* proto integer sum(arry $arry) */
PHP_FUNCTION(sum)
{
  zval *input;
        zval *item;
        int32_t total;
        HashPosition position;

  if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &input) == FAILURE) {
    return;
  }

  for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &position);
             (item = zend_hash_get_current_data_ex(Z_ARRVAL_P(input), &position));
            zend_hash_move_forward_ex(Z_ARRVAL_P(input), &position)) {
            if (Z_TYPE_P(item) == IS_LONG) {
               total += Z_LVAL_P(item);
            } else zend_throw_exception(NULL,"oh dear!",0);
        }
     
       RETURN_LOG(total);
}
/* }}} */


/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(sum)
{
  /* If you have INI entries, uncomment these lines
  REGISTER_INI_ENTRIES();
  */
  return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(sum)
{
  /* uncomment this line if you have INI entries
  UNREGISTER_INI_ENTRIES();
  */
  return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(sum)
{
#if defined(COMPILE_DL_SUM) && defined(ZTS)
  ZEND_TSRMLS_CACHE_UPDATE();
#endif
  return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(sum)
{
  return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(sum)
{
  php_info_print_table_start();
  php_info_print_table_header(2, "sum support", "enabled");
  php_info_print_table_end();

  /* Remove comments if you have entries in php.ini
  DISPLAY_INI_ENTRIES();
  */
}
/* }}} */

/* {{{ sum_functions[]
 *
 * Every user visible function must have an entry in sum_functions[].
 */
const zend_function_entry sum_functions[] = {
  PHP_FE(sum,  NULL)    /* For testing, remove later. */
  PHP_FE_END  /* Must be the last line in sum_functions[] */
};
/* }}} */

/* {{{ sum_module_entry
 */
zend_module_entry sum_module_entry = {
  STANDARD_MODULE_HEADER,
  "sum",
  sum_functions,
  PHP_MINIT(sum),
  PHP_MSHUTDOWN(sum),
  PHP_RINIT(sum),    /* Replace with NULL if there's nothing to do at request start */
  PHP_RSHUTDOWN(sum),  /* Replace with NULL if there's nothing to do at request end */
  PHP_MINFO(sum),
  PHP_SUM_VERSION,
  STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_SUM
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(sum)
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum# cat config.m4
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
dnl $Id$
dnl config.m4 for extension sum

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

PHP_ARG_ENABLE(sum, whether to enable sum support,
[  --enable-sum           Enable sum support])

if test "$PHP_SUM" != "no"; then
  PHP_NEW_EXTENSION(sum, sum.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
fi


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum#
ausblenden Quelltext
1:
2:
3:
4:
phpize
./configure
make
make install


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum# php -dextension=sum.so -m
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
[PHP Modules]
Core
date
pcre
Reflection
SPL
standard
sum        <---- ist vorhanden

[Zend Modules]


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum# php -m
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
[PHP Modules]
Core
date
pcre
Reflection
SPL
standard
                         <------ ist NICHT vorhanden
[Zend Modules]


root@tomycat-SATELLITE-L70-C-12T:/home/tomycat/php-src/ext/sum# php test.php
Zitat:
Fatal error: Uncaught Error: Call to undefined function sum() in /home/tomycat/php-src/ext/sum/test.php:2
Stack trace:
#0 {main}
thrown in /home/tomycat/php-src/ext/sum/test.php on line 2


Ich bin zur Zeit für Jeden Tip sehr dankbar.
Wenn ich ... Klammer auf... Code... Klammer zu nehme..., dann ist Meine Shell abgeschnitten ?!

Moderiert von user profile iconTh69: Code-Tags hinzugefügt
Moderiert von user profile iconTh69: PHP-Tags hinzugefügt
Moderiert von user profile iconTh69: Quote-Tags hinzugefügt
Moderiert von user profile iconTh69: Topic aus Algorithmen, Optimierung und Assembler verschoben am So 25.06.2023 um 09:14
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Mo 26.06.23 23:18 
ich bin ein Stück weitergekommen.
make clean war die Lösung.
habs nochmals kompiliert.

Wenn ich jetzt php test.php ausführe kommt:
int(-1453636576) ... Eine lange wirre negative Zahl. Richtig wäre int(21)

Eine Idee wie ich den Fehler eingrenzen kann?

Moderiert von user profile iconTh69: Code-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 27.06.23 09:31 
Hast du die Funktion sum in sum.c selbst geschrieben?
Dort ist int32_t total uninitialisiert!

PS: Und demnächst bei einem Beitrag selber die Code-Tags verwenden!

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Di 27.06.23 18:05 
thx th69,
ich verstehe nicht ?!

-----------
Ich habe jetzt folgendes probiert

ausblenden Quelltext
1:
2:
3:
4:
<?php
$total = sum(1234);
var_dump($total);
?>

die sum.c

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
PHP_FUNCTION(sum)
{
        zval *input;
        zval *item;
        int32_t total;
        HashPosition position;

        if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &input) == FAILURE) {
                return;
        }

//      for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &position);
  //           (item = zend_hash_get_current_data_ex(Z_ARRVAL_P(input), &position));
    //        zend_hash_move_forward_ex(Z_ARRVAL_P(input), &position)) {
      //      if (Z_TYPE_P(item) == IS_LONG) {
        //       total += Z_LVAL_P(item);
          //  } else zend_throw_exception(NULL,"oh dear!",0);
      //  }
       total = input + 2;
       RETURN_LONG(total);
}

php test.php
int(1266) // Es sollte eigentlich 1236 herauskommen, aber egal :-)

----------------- 2.Test----------
ausblenden Quelltext
1:
2:
3:
4:
<?php
$total = sum([1,2,3,4]);
var_dump($total);
?>

meine sum.c
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
PHP_FUNCTION(sum)
{
        zval *input;
        zval *item;
        int32_t total;
        HashPosition position;

        if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &input) == FAILURE) {
                return;
        }

//      for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &position);
  //           (item = zend_hash_get_current_data_ex(Z_ARRVAL_P(input), &position));
    //        zend_hash_move_forward_ex(Z_ARRVAL_P(input), &position)) {
      //      if (Z_TYPE_P(item) == IS_LONG) {
        //       total += Z_LVAL_P(item);
          //  } else zend_throw_exception(NULL,"oh dear!",0);
      //  }
       total = input;
       RETURN_LONG(total);
}


php test.php
int(-1973341904) // wie kommt die Zahl zu stande :-)

------------------ Das habe ich herausgefunden...
zend_parse_parameters <--- Parameter 2 steht für den Type, a ist Array und l steht für Long.
wie packe ich den Array in den Ausgang bzw RETURN_LONG rein. So könnte ich den Fehler eingrenzen.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 27.06.23 19:21 
Wenn du die Summe mittels += berechnen willst, dann mußt du selbstverständlich den Ausgangswert auf das 'neutrale Element der Addition' setzen (ansonsten steht in der lokalen Variablen ein zufälliger Wert drin)...

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Mi 28.06.23 09:57 
@TH69

Danke, dein Tipp war Goldwert !!!

Das habe ich hinzugefügt, jetzt geht's.
ausblenden Quelltext
1:
2:
int32_t total = 0;
HashPosition position = 0;


Jetzt kommt das grosse ABER :-)

Wieso funkt das Code bei dem Video? Ich habe alles 1 zu 1 abgetippt ?

PHP Extension Development for Beginners with Joe Watkins (Stelle 1:16:51)

Noch eine Kleine Frage, wie bekomme ich das erste Array vom Input in RETURN_LONG(hier_rein); rein?

Moderiert von user profile iconTh69: URL-Titel hinzugefügt
Moderiert von user profile iconTh69: Code-Tags hinzugefügt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 28.06.23 12:12 
Wie schon geschrieben: Zufall, daß dann gerade 0 an der Speicherstelle steht.

Und deine letzte Frage ergibt keinen Sinn: wie willst du ein Array in einen LONG packen?

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Mi 28.06.23 12:30 
user profile iconTh69 hat folgendes geschrieben Zum zitierten Posting springen:
Wie schon geschrieben: Zufall, daß dann gerade 0 an der Speicherstelle steht.


Sorry, das wäre ein Zufall, wie ein 6er im Lotto. Er hat die Php Datei öfters gestartet?!

user profile iconTh69 hat folgendes geschrieben Zum zitierten Posting springen:
Und deine letzte Frage ergibt keinen Sinn: wie willst du ein Array in einen LONG packen?


In C# wäre das...
ausblenden Quelltext
1:
2:
3:
4:
5:
schnippp
// Input_array wird in Zend entgegengenommen.
int Nur_eine_Zahl = Input_array[0];
return Nur_eine_Zahl;
schnippp
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 28.06.23 13:30 
Achso, du meinst den ersten Wert des Arrays.
Das geht in C genauso mittels des Index-Operators.

Für diesen Beitrag haben gedankt: tomycat