<commit_before><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Memory
 */

namespace Zend\Memory;

use Zend\Cache\Storage\ClearByNamespaceInterface as ClearByNamespaceCacheStorage;
use Zend\Cache\Storage\FlushableInterface as FlushableCacheStorage;
use Zend\Cache\Storage\StorageInterface as CacheStorage;

/**
 * Memory manager
 *
 * This class encapsulates memory menagement operations, when PHP works
 * in limited memory mode.
 *
 * @category   Zend
 * @package    Zend_Memory
 */
class MemoryManager
{
    /**
     * Storage cache object
     *
     * @var CacheStorage
     */
    private $cache = null;

    /**
     * Memory grow limit.
     * Default value is 2/3 of memory_limit php.ini variable
     * Negative value means no limit
     *
     * @var integer
     */
    private $memoryLimit = -1;

    /**
     * Minimum value size to be swapped.
     * Default value is 16K
     * Negative value means that memory objects are never swapped
     *
     * @var integer
     */
    private $minSize = 16384;

    /**
     * Overall size of memory, used by values
     *
     * @var integer
     */
    private $memorySize = 0;

    /**
     * Id for next Zend_Memory object
     *
     * @var integer
     */
    private $nextId = 0;

    /**
     * List of candidates to unload
     *
     * It also represents objects access history. Last accessed objects are moved to the end of array
     *
     * array(
     *     <id> => <memory container object>,
     *     ...
     *      )
     *
     * @var array
     */
    private $unloadCandidates = array();

    /**
     * List of object sizes.
     *
     * This list is used to calculate modification of object sizes
     *
     * array( <id> => <size>, ...)
     *
     * @var array
     */
    private $sizes = array();

    /**
     * Last modified object
     *
     * It's used to reduce number of calls necessary to trace objects' modifications
     * Modification is not processed by memory manager until we do not switch to another
     * object.
     * So we have to trace only _first_ object modification and do nothing for others
     *
     * @var \Zend\Memory\Container\Movable
     */
    private $lastModified = null;

    /**
     * Unique memory manager id
     *
     * @var integer
     */
    private $managerId;

    /**
     * This function is intended to generate unique id, used by memory manager
     */
    private function _generateMemManagerId()
    {
        /**
         * @todo !!!
         * uniqid() php function doesn't really guarantee the id to be unique
         * it should be changed by something else
         * (Ex. backend interface should be extended to provide this functionality)
         */
        $this->managerId = str_replace('.', '_', uniqid('ZendMemManager', true)) . '_';
    }

    /**
     * Memory manager constructor
     *
     * If cache is not specified, then memory objects are never swapped
     *
     * @param  CacheStorage $cache
     */
    public function __construct(CacheStorage $cache = null)
    {
        if ($cache === null) {
            return;
        }

        $this->cache = $cache;
        $this->_generateMemManagerId();

        $memoryLimitStr = trim(ini_get('memory_limit'));
        if ($memoryLimitStr != ''  &&  $memoryLimitStr != -1) {
            $this->memoryLimit = (integer)$memoryLimitStr;
            switch (strtolower($memoryLimitStr[strlen($memoryLimitStr)-1])) {
                case 'g':
                    $this->memoryLimit *= 1024;
                    // Break intentionally omitted
                case 'm':
                    $this->memoryLimit *= 1024;
                    // Break intentionally omitted
                case 'k':
                    $this->memoryLimit *= 1024;
                    break;

                default:
                    break;
            }

            $this->memoryLimit = (int) ($this->memoryLimit*2/3);
        } // No limit otherwise
    }

    /**
     * Object destructor
     *
     * Clean up cache storage
     */
    public function __destruct()
    {
        if ($this->cache !== null) {
            if ($this->cache instanceof ClearByNamespaceCacheStorage) {<commit_msg>Add/update comments where a break seems to be intentionally omitted<commit_after><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Memory
 */

namespace Zend\Memory;

use Zend\Cache\Storage\ClearByNamespaceInterface as ClearByNamespaceCacheStorage;
use Zend\Cache\Storage\FlushableInterface as FlushableCacheStorage;
use Zend\Cache\Storage\StorageInterface as CacheStorage;

/**
 * Memory manager
 *
 * This class encapsulates memory menagement operations, when PHP works
 * in limited memory mode.
 *
 * @category   Zend
 * @package    Zend_Memory
 */
class MemoryManager
{
    /**
     * Storage cache object
     *
     * @var CacheStorage
     */
    private $cache = null;

    /**
     * Memory grow limit.
     * Default value is 2/3 of memory_limit php.ini variable
     * Negative value means no limit
     *
     * @var integer
     */
    private $memoryLimit = -1;

    /**
     * Minimum value size to be swapped.
     * Default value is 16K
     * Negative value means that memory objects are never swapped
     *
     * @var integer
     */
    private $minSize = 16384;

    /**
     * Overall size of memory, used by values
     *
     * @var integer
     */
    private $memorySize = 0;

    /**
     * Id for next Zend_Memory object
     *
     * @var integer
     */
    private $nextId = 0;

    /**
     * List of candidates to unload
     *
     * It also represents objects access history. Last accessed objects are moved to the end of array
     *
     * array(
     *     <id> => <memory container object>,
     *     ...
     *      )
     *
     * @var array
     */
    private $unloadCandidates = array();

    /**
     * List of object sizes.
     *
     * This list is used to calculate modification of object sizes
     *
     * array( <id> => <size>, ...)
     *
     * @var array
     */
    private $sizes = array();

    /**
     * Last modified object
     *
     * It's used to reduce number of calls necessary to trace objects' modifications
     * Modification is not processed by memory manager until we do not switch to another
     * object.
     * So we have to trace only _first_ object modification and do nothing for others
     *
     * @var \Zend\Memory\Container\Movable
     */
    private $lastModified = null;

    /**
     * Unique memory manager id
     *
     * @var integer
     */
    private $managerId;

    /**
     * This function is intended to generate unique id, used by memory manager
     */
    private function _generateMemManagerId()
    {
        /**
         * @todo !!!
         * uniqid() php function doesn't really guarantee the id to be unique
         * it should be changed by something else
         * (Ex. backend interface should be extended to provide this functionality)
         */
        $this->managerId = str_replace('.', '_', uniqid('ZendMemManager', true)) . '_';
    }

    /**
     * Memory manager constructor
     *
     * If cache is not specified, then memory objects are never swapped
     *
     * @param  CacheStorage $cache
     */
    public function __construct(CacheStorage $cache = null)
    {
        if ($cache === null) {
            return;
        }

        $this->cache = $cache;
        $this->_generateMemManagerId();

        $memoryLimitStr = trim(ini_get('memory_limit'));
        if ($memoryLimitStr != ''  &&  $memoryLimitStr != -1) {
            $this->memoryLimit = (integer)$memoryLimitStr;
            switch (strtolower($memoryLimitStr[strlen($memoryLimitStr)-1])) {
                case 'g':
                    $this->memoryLimit *= 1024;
                    // no break
                case 'm':
                    $this->memoryLimit *= 1024;
                    // no break
                case 'k':
                    $this->memoryLimit *= 1024;
                    break;
                default:
                    break;
            }

            $this->memoryLimit = (int) ($this->memoryLimit*2/3);
        } // No limit otherwise
    }

    /**
     * Object destructor
     *
     * Clean up cache storage
     */
    public function __destruct()
    {
        if ($this->cache !== null) {
            if ($this->cache instanceof ClearByNamespaceCacheStorage) {
{'fraction_non_alphanumeric': 0.10464465584778959, 'fraction_numerical': 0.008282036933407946, 'mean_word_length': 1.869621066152858, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5357103', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2375, 'n_words': 761}