Skip to content

Hints for developers

Kedarius edited this page Sep 14, 2010 · 4 revisions

This page should contain some ideas about how to solve some common troubles that you may encounter when you add functions to php-libvirt.

Long long problem
A lot of libvirt functions do return unsigned long long values. On the other hand, PHP does use long for all integers. This means that the largest number on 32bit systems is 2147483647. In case of bytes it means only 2GB. What happen when you try to return larger number may vary a lot.
As solution to this seems to be feasible to return the long long numbers as strings. If the user (PHP developer) is sure that the number is within the 32bit range he can directly use the string as number and she will not find a difference. If the user is unsure, she need to use bignum extension ( http://www.php.net/manual/en/ref.gmp.php ) to work with the number. The library has many ways how to deal with big integers. One of the most common use will be dividing and moduling the number with some exponent, i.e. to convert bytes to gigabytes+bytes. This can be done easily with code like this:

$mb=gmp_intval(gmp_div_q ( $number , 1024*1024)); $b=gmp_intval(gmp_mod ( $number , 1024*1024));

This approach seems to be working. I have added macros for ease of use and php.ini option to disable this behaviour.

LONGLONG_INIT

LONGLONG_ASSOC(return_value, “data_total”, jobinfo.dataTotal);

LONGLONG_INDEX(return_value, stats[i].tag,stats[i].val)

Behaviour is same as ZEND’s respective macros…

Clone this wiki locally