@@ -22,9 +22,11 @@ namespace phpv8 {
2222 template <class T > class PersistentCollection ;
2323}
2424
25+
2526#include < v8.h>
2627#include < map>
2728#include < string>
29+ #include < utility>
2830
2931extern " C" {
3032#include " php.h"
@@ -57,6 +59,13 @@ extern void php_v8_callback_indexed_property_enumerator(const v8::PropertyCallba
5759
5860extern bool php_v8_callback_access_check (v8::Local<v8::Context> accessing_context, v8::Local<v8::Object> accessed_object, v8::Local<v8::Value> data);
5961
62+ // #define PHP_V8_DEBUG_EXTERNAL_MEM 1
63+
64+ #ifdef PHP_V8_DEBUG_EXTERNAL_MEM
65+ #define php_v8_debug_external_mem (format, ...) fprintf(stderr, (format), ##__VA_ARGS__);
66+ #else
67+ #define php_v8_debug_external_mem (format, ...)
68+ #endif
6069
6170namespace phpv8 {
6271
@@ -95,8 +104,13 @@ namespace phpv8 {
95104 inline bool empty () {
96105 return callbacks.empty ();
97106 }
107+
108+ inline int64_t calculateSize () {
109+ return sizeof (*this ) + (sizeof (std::shared_ptr<Callback>) + sizeof (Callback)) * callbacks.size ();
110+ }
111+
98112 private:
99- std::map<size_t , std::shared_ptr<phpv8:: Callback>> callbacks;
113+ std::map<size_t , std::shared_ptr<Callback>> callbacks;
100114 };
101115
102116
@@ -114,7 +128,20 @@ namespace phpv8 {
114128 return buckets.empty ();
115129 }
116130
131+ int64_t calculateSize ();
132+
133+ inline int64_t getTotalSize () {
134+ if (!size_) {
135+ size_ = calculateSize ();
136+ }
137+
138+ return size_ + adjusted_size_;
139+ }
140+
141+ int64_t adjustSize (int64_t change_in_bytes);
117142 private:
143+ int64_t size_;
144+ int64_t adjusted_size_;
118145 std::map<std::string, std::shared_ptr<CallbacksBucket>> buckets;
119146 };
120147
@@ -149,9 +176,20 @@ namespace phpv8 {
149176 collection[persistent] = std::shared_ptr<phpv8::PersistentData>(data);
150177 }
151178
179+ phpv8::PersistentData *get (v8::Persistent<T> *persistent) {
180+ auto it = collection.find (persistent);
181+
182+ if (it != collection.end ()) {
183+ return it->second .get ();
184+ }
185+
186+ return nullptr ;
187+ }
188+
152189 void remove (v8::Persistent<T, v8::NonCopyablePersistentTraits<T>> *persistent) {
153190 collection.erase (persistent);
154- } private:
191+ }
192+ private:
155193 std::map<v8::Persistent<T> *, std::shared_ptr<phpv8::PersistentData>> collection;
156194 };
157195}
0 commit comments