~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Memory/Manager.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @package    Zend_Memory
 
16
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
17
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
18
 */
 
19
 
 
20
/** Zend_Memory_Container_Movable */
 
21
require_once 'Zend/Memory/Container/Movable.php';
 
22
 
 
23
/** Zend_Memory_Container_Locked */
 
24
require_once 'Zend/Memory/Container/Locked.php';
 
25
 
 
26
/** Zend_Memory_AccessController */
 
27
require_once 'Zend/Memory/AccessController.php';
 
28
 
 
29
 
 
30
/**
 
31
 * Memory manager
 
32
 *
 
33
 * This class encapsulates memory menagement operations, when PHP works
 
34
 * in limited memory mode.
 
35
 *
 
36
 *
 
37
 * @category   Zend
 
38
 * @package    Zend_Memory
 
39
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
40
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
41
 */
 
42
class Zend_Memory_Manager
 
43
{
 
44
    /**
 
45
     * Object storage backend
 
46
     *
 
47
     * @var Zend_Cache_Backend_Interface
 
48
     */
 
49
    private $_backend = null;
 
50
 
 
51
    /**
 
52
     * Memory grow limit.
 
53
     * Default value is 2/3 of memory_limit php.ini variable
 
54
     * Negative value means no limit
 
55
     *
 
56
     * @var integer
 
57
     */
 
58
    private $_memoryLimit = -1;
 
59
 
 
60
    /**
 
61
     * Minimum value size to be swapped.
 
62
     * Default value is 16K
 
63
     * Negative value means that memory objects are never swapped
 
64
     *
 
65
     * @var integer
 
66
     */
 
67
    private $_minSize = 16384;
 
68
 
 
69
    /**
 
70
     * Overall size of memory, used by values
 
71
     *
 
72
     * @var integer
 
73
     */
 
74
    private $_memorySize = 0;
 
75
 
 
76
    /**
 
77
     * Id for next Zend_Memory object
 
78
     *
 
79
     * @var integer
 
80
     */
 
81
    private $_nextId = 0;
 
82
 
 
83
    /**
 
84
     * List of candidates to unload
 
85
     *
 
86
     * It also represents objects access history. Last accessed objects are moved to the end of array
 
87
     *
 
88
     * array(
 
89
     *     <id> => <memory container object>,
 
90
     *     ...
 
91
     *      )
 
92
     *
 
93
     * @var array
 
94
     */
 
95
    private $_unloadCandidates = array();
 
96
 
 
97
    /**
 
98
     * List of object sizes.
 
99
     *
 
100
     * This list is used to calculate modification of object sizes
 
101
     *
 
102
     * array( <id> => <size>, ...)
 
103
     *
 
104
     * @var array
 
105
     */
 
106
    private $_sizes = array();
 
107
 
 
108
    /**
 
109
     * Last modified object
 
110
     *
 
111
     * It's used to reduce number of calls necessary to trace objects' modifications
 
112
     * Modification is not processed by memory manager until we do not switch to another
 
113
     * object.
 
114
     * So we have to trace only _first_ object modification and do nothing for others
 
115
     *
 
116
     * @var Zend_Memory_Container_Movable
 
117
     */
 
118
    private $_lastModified = null;
 
119
 
 
120
    /**
 
121
     * Unique memory manager id
 
122
     *
 
123
     * @var integer
 
124
     */
 
125
    private $_managerId;
 
126
 
 
127
    /**
 
128
     * Tags array, used by backend to categorize stored values
 
129
     *
 
130
     * @var array
 
131
     */
 
132
    private $_tags;
 
133
 
 
134
    /**
 
135
     * This function is intended to generate unique id, used by memory manager
 
136
     */
 
137
    private function _generateMemManagerId()
 
138
    {
 
139
        /**
 
140
         * @todo !!!
 
141
         * uniqid() php function doesn't really garantee the id to be unique
 
142
         * it should be changed by something else
 
143
         * (Ex. backend interface should be extended to provide this functionality)
 
144
         */
 
145
        $this->_managerId = uniqid('ZendMemManager', true);
 
146
        $this->_tags = array($this->_managerId);
 
147
        $this->_managerId .= '_';
 
148
    }
 
149
 
 
150
 
 
151
    /**
 
152
     * Memory manager constructor
 
153
     *
 
154
     * If backend is not specified, then memory objects are never swapped
 
155
     *
 
156
     * @param Zend_Cache_Backend $backend
 
157
     * @param array $backendOptions associative array of options for the corresponding backend constructor
 
158
     */
 
159
    public function __construct($backend = null)
 
160
    {
 
161
        if ($backend === null) {
 
162
            return;
 
163
        }
 
164
 
 
165
        $this->_backend = $backend;
 
166
        $this->_generateMemManagerId();
 
167
 
 
168
        $memoryLimitStr = trim(ini_get('memory_limit'));
 
169
        if ($memoryLimitStr != '') {
 
170
            $this->_memoryLimit = (integer)$memoryLimitStr;
 
171
            switch (strtolower($memoryLimitStr[strlen($memoryLimitStr)-1])) {
 
172
                case 'g':
 
173
                    $this->_memoryLimit *= 1024;
 
174
                    // Break intentionally omitted
 
175
                case 'm':
 
176
                    $this->_memoryLimit *= 1024;
 
177
                    // Break intentionally omitted
 
178
                case 'k':
 
179
                    $this->_memoryLimit *= 1024;
 
180
                    break;
 
181
 
 
182
                default:
 
183
                    break;
 
184
            }
 
185
 
 
186
            $this->_memoryLimit = (int)($this->_memoryLimit*2/3);
 
187
        } // No limit otherwise
 
188
    }
 
189
 
 
190
    /**
 
191
     * Object destructor
 
192
     *
 
193
     * Clean up backend storage
 
194
     */
 
195
    public function __destruct()
 
196
    {
 
197
        if ($this->_backend !== null) {
 
198
            $this->_backend->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $this->_tags);
 
199
        }
 
200
    }
 
201
 
 
202
    /**
 
203
     * Set memory grow limit
 
204
     *
 
205
     * @param integer $newLimit
 
206
     * @throws Zend_Exception
 
207
     */
 
208
    public function setMemoryLimit($newLimit)
 
209
    {
 
210
        $this->_memoryLimit = $newLimit;
 
211
 
 
212
        $this->_swapCheck();
 
213
    }
 
214
 
 
215
    /**
 
216
     * Get memory grow limit
 
217
     *
 
218
     * @return integer
 
219
     */
 
220
    public function getMemoryLimit()
 
221
    {
 
222
        return $this->_memoryLimit;
 
223
    }
 
224
 
 
225
    /**
 
226
     * Set minimum size of values, which may be swapped
 
227
     *
 
228
     * @param integer $newSize
 
229
     */
 
230
    public function setMinSize($newSize)
 
231
    {
 
232
        $this->_minSize = $newSize;
 
233
    }
 
234
 
 
235
    /**
 
236
     * Get minimum size of values, which may be swapped
 
237
     *
 
238
     * @return integer
 
239
     */
 
240
    public function getMinSize()
 
241
    {
 
242
        return $this->_minSize;
 
243
    }
 
244
 
 
245
    /**
 
246
     * Create new Zend_Memory value container
 
247
     *
 
248
     * @param string $value
 
249
     * @return Zend_Memory_Container_Interface
 
250
     * @throws Zend_Memory_Exception
 
251
     */
 
252
    public function create($value = '')
 
253
    {
 
254
        return $this->_create($value,  false);
 
255
    }
 
256
 
 
257
    /**
 
258
     * Create new Zend_Memory value container, which has value always
 
259
     * locked in memory
 
260
     *
 
261
     * @param string $value
 
262
     * @return Zend_Memory_Container_Interface
 
263
     * @throws Zend_Memory_Exception
 
264
     */
 
265
    public function createLocked($value = '')
 
266
    {
 
267
        return $this->_create($value, true);
 
268
    }
 
269
 
 
270
    /**
 
271
     * Create new Zend_Memory object
 
272
     *
 
273
     * @param string $value
 
274
     * @param boolean $locked
 
275
     * @return Zend_Memory_Container_Interface
 
276
     * @throws Zend_Memory_Exception
 
277
     */
 
278
    private function _create($value, $locked)
 
279
    {
 
280
        $id = $this->_nextId++;
 
281
 
 
282
        if ($locked  ||  ($this->_backend === null) /* Use only memory locked objects if backend is not specified */) {
 
283
            return new Zend_Memory_Container_Locked($value);
 
284
        }
 
285
 
 
286
        // Commit other objects modifications
 
287
        $this->_commit();
 
288
 
 
289
        $valueObject = new Zend_Memory_Container_Movable($this, $id, $value);
 
290
 
 
291
        // Store last object size as 0
 
292
        $this->_sizes[$id] = 0;
 
293
        // prepare object for next modifications
 
294
        $this->_lastModified = $valueObject;
 
295
 
 
296
        return new Zend_Memory_AccessController($valueObject);
 
297
    }
 
298
 
 
299
    /**
 
300
     * Unlink value container from memory manager
 
301
     *
 
302
     * Used by Memory container destroy() method
 
303
     *
 
304
     * @internal
 
305
     * @param integer $id
 
306
     * @return Zend_Memory_Container
 
307
     */
 
308
    public function unlink(Zend_Memory_Container_Movable $container, $id)
 
309
    {
 
310
        if ($this->_lastModified === $container) {
 
311
            // Drop all object modifications
 
312
            $this->_lastModified = null;
 
313
            unset($this->_sizes[$id]);
 
314
            return;
 
315
        }
 
316
 
 
317
        if (isset($this->_unloadCandidates[$id])) {
 
318
            unset($this->_unloadCandidates[$id]);
 
319
        }
 
320
 
 
321
        $this->_memorySize -= $this->_sizes[$id];
 
322
        unset($this->_sizes[$id]);
 
323
    }
 
324
 
 
325
    /**
 
326
     * Process value update
 
327
     *
 
328
     * @internal
 
329
     * @param Zend_Memory_Container_Movable $container
 
330
     * @param integer $id
 
331
     */
 
332
    public function processUpdate(Zend_Memory_Container_Movable $container, $id)
 
333
    {
 
334
        /**
 
335
         * This method is automatically invoked by memory container only once per
 
336
         * "modification session", but user may call memory container touch() method
 
337
         * several times depending on used algorithm. So we have to use this check
 
338
         * to optimize this case.
 
339
         */
 
340
        if ($container === $this->_lastModified) {
 
341
            return;
 
342
        }
 
343
 
 
344
        // Remove just updated object from list of candidates to unload
 
345
        if( isset($this->_unloadCandidates[$id])) {
 
346
            unset($this->_unloadCandidates[$id]);
 
347
        }
 
348
 
 
349
        // Reduce used memory mark
 
350
        $this->_memorySize -= $this->_sizes[$id];
 
351
 
 
352
        // Commit changes of previously modified object if necessary
 
353
        $this->_commit();
 
354
 
 
355
        $this->_lastModified = $container;
 
356
    }
 
357
 
 
358
    /**
 
359
     * Commit modified object and put it back to the loaded objects list
 
360
     */
 
361
    private function _commit()
 
362
    {
 
363
        if (($container = $this->_lastModified) === null) {
 
364
            return;
 
365
        }
 
366
 
 
367
        $this->_lastModified = null;
 
368
 
 
369
        $id = $container->getId();
 
370
 
 
371
        // Calculate new object size and increase used memory size by this value
 
372
        $this->_memorySize += ($this->_sizes[$id] = strlen($container->getRef()));
 
373
 
 
374
        if ($this->_sizes[$id] > $this->_minSize) {
 
375
            // Move object to "unload candidates list"
 
376
            $this->_unloadCandidates[$id] = $container;
 
377
        }
 
378
 
 
379
        $container->startTrace();
 
380
 
 
381
        $this->_swapCheck();
 
382
    }
 
383
 
 
384
    /**
 
385
     * Check and swap objects if necessary
 
386
     *
 
387
     * @throws Zend_MemoryException
 
388
     */
 
389
    private function _swapCheck()
 
390
    {
 
391
        if ($this->_memoryLimit < 0  ||  $this->_memorySize < $this->_memoryLimit) {
 
392
            // Memory limit is not reached
 
393
            // Do nothing
 
394
            return;
 
395
        }
 
396
 
 
397
        // walk through loaded objects in access history order
 
398
        foreach ($this->_unloadCandidates as $id => $container) {
 
399
            $this->_swap($container, $id);
 
400
            unset($this->_unloadCandidates[$id]);
 
401
 
 
402
            if ($this->_memorySize < $this->_memoryLimit) {
 
403
                // We've swapped enough objects
 
404
                return;
 
405
            }
 
406
        }
 
407
 
 
408
        throw new Zend_Memory_Exception('Memory manager can\'t get enough space.');
 
409
    }
 
410
 
 
411
 
 
412
    /**
 
413
     * Swap object data to disk
 
414
     * Actualy swaps data or only unloads it from memory,
 
415
     * if object is not changed since last swap
 
416
     *
 
417
     * @param Zend_Memory_Container_Movable $container
 
418
     * @param integer $id
 
419
     */
 
420
    private function _swap(Zend_Memory_Container_Movable $container, $id)
 
421
    {
 
422
        if ($container->isLocked()) {
 
423
            return;
 
424
        }
 
425
 
 
426
        if (!$container->isSwapped()) {
 
427
            $this->_backend->save($container->getRef(), $this->_managerId . $id, $this->_tags);
 
428
        }
 
429
 
 
430
        $this->_memorySize -= $this->_sizes[$id];
 
431
 
 
432
        $container->markAsSwapped();
 
433
        $container->unloadValue();
 
434
    }
 
435
 
 
436
    /**
 
437
     * Load value from swap file.
 
438
     *
 
439
     * @internal
 
440
     * @param Zend_Memory_Container_Movable $container
 
441
     * @param integer $id
 
442
     */
 
443
    public function load(Zend_Memory_Container_Movable $container, $id)
 
444
    {
 
445
        $value = $this->_backend->load($this->_managerId . $id, true);
 
446
 
 
447
        // Try to swap other objects if necessary
 
448
        // (do not include specified object into check)
 
449
        $this->_memorySize += strlen($value);
 
450
        $this->_swapCheck();
 
451
 
 
452
        // Add loaded obect to the end of loaded objects list
 
453
        $container->setValue($value);
 
454
 
 
455
        if ($this->_sizes[$id] > $this->_minSize) {
 
456
            // Add object to the end of "unload candidates list"
 
457
            $this->_unloadCandidates[$id] = $container;
 
458
        }
 
459
    }
 
460
}