forked from gitter-badger/WorldEditArt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderedObjectPool.php
89 lines (81 loc) · 2.13 KB
/
OrderedObjectPool.php
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
<?php
/*
* WorldEditArt
*
* Copyright (C) 2015 PEMapModder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PEMapModder
*/
namespace pemapmodder\worldeditart\utils;
use pemapmodder\worldeditart\WorldEditArt;
use pocketmine\utils\TextFormat;
class OrderedObjectPool{
/** @var WorldEditArt */
private $main;
/** @var object[] */
private $objects = [];
/** @var int */
private $nextObjectId = 0;
public function __construct(WorldEditArt $main){
$this->main = $main;
}
/**
* @param object $object
*
* @return int
*/
public function store($object){
$this->objects[$id = $this->nextId()] = $object;
if(count($this->objects) >= $this->main->getConfig()->getNested("advanced.objectPool.warningSize")){
$this->main->getLogger()->warning("OrderedObjectPool size reached " . count($this->objects) . "! Object summary:");
$summary = [];
foreach($this->objects as $obj){
$class = get_class($obj);
if(isset($summary[$class])){
$summary[$class]++;
}else{
$summary[$class] = 1;
}
}
foreach($summary as $class => $cnt){
$this->main->getLogger()->warning($class . ": $cnt entries");
}
$this->main->getLogger()->warning("The above is most likely caused by a mistake in code that results in a memory leak. Please report the above to the issue tracker on GitHub at " . TextFormat::LIGHT_PURPLE . "http://lgpe.co/weai/");
}
return $id;
}
/**
* @param int $id
*
* @return object|null
*/
public function get($id){
if(isset($this->objects[$id])){
$object = $this->objects[$id];
unset($this->objects[$id]);
return $object;
}
return null;
}
/**
* Warning: avoid using this method to prevent memory leak
*
* @param int $id
*
* @return object|null
*/
public function getWithoutClean($id){
return isset($this->objects[$id]) ? $this->objects[$id] : null;
}
/**
* @return int
*/
private function nextId(){
return $this->nextObjectId++;
}
}