Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: src/pdf/SkPDFTypes.h

Issue 12387086: Unwind PDFObject destructor calls into heap instead of stack. Notice: the order of the destructors … (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/pdf/SkPDFStream.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2010 The Android Open Source Project 3 * Copyright 2010 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkPDFTypes_DEFINED 10 #ifndef SkPDFTypes_DEFINED
(...skipping 11 matching lines...) Expand all
22 /** \class SkPDFObject 22 /** \class SkPDFObject
23 23
24 A PDF Object is the base class for primitive elements in a PDF file. A 24 A PDF Object is the base class for primitive elements in a PDF file. A
25 common subtype is used to ease the use of indirect object references, 25 common subtype is used to ease the use of indirect object references,
26 which are common in the PDF format. 26 which are common in the PDF format.
27 */ 27 */
28 class SkPDFObject : public SkRefCnt { 28 class SkPDFObject : public SkRefCnt {
29 public: 29 public:
30 SK_DECLARE_INST_COUNT(SkPDFObject) 30 SK_DECLARE_INST_COUNT(SkPDFObject)
31 31
32 virtual ~SkPDFObject() { };
33
32 /** Return the size (number of bytes) of this object in the final output 34 /** Return the size (number of bytes) of this object in the final output
33 * file. Compound objects or objects that are computationally intensive 35 * file. Compound objects or objects that are computationally intensive
34 * to output should override this method. 36 * to output should override this method.
35 * @param catalog The object catalog to use. 37 * @param catalog The object catalog to use.
36 * @param indirect If true, output an object identifier with the object. 38 * @param indirect If true, output an object identifier with the object.
37 */ 39 */
38 virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect); 40 virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
39 41
40 /** For non-primitive objects (i.e. objects defined outside this file), 42 /** For non-primitive objects (i.e. objects defined outside this file),
41 * this method will add to resourceList any objects that this method 43 * this method will add to resourceList any objects that this method
(...skipping 30 matching lines...) Expand all
72 SkTDArray<SkPDFObject*>* list); 74 SkTDArray<SkPDFObject*>* list);
73 75
74 /** Static helper function to copy and reference the resources (and all 76 /** Static helper function to copy and reference the resources (and all
75 * their subresources) into a new list. 77 * their subresources) into a new list.
76 * @param resources The resource list. 78 * @param resources The resource list.
77 * @param result The list to add to. 79 * @param result The list to add to.
78 */ 80 */
79 static void GetResourcesHelper(SkTDArray<SkPDFObject*>* resources, 81 static void GetResourcesHelper(SkTDArray<SkPDFObject*>* resources,
80 SkTDArray<SkPDFObject*>* result); 82 SkTDArray<SkPDFObject*>* result);
81 83
84 /** Destroys safely an array of SkPDFObjects.
85 * The stack won't be used much, and stack overflow will be avoided.
86 */
87 static void safeUnref(SkTDArray<SkPDFObject*>* list) {
88 while (list->count() > 0) {
89 SkPDFObject* obj = NULL;
90 list->pop(&obj);
91 if (obj) {
92 // Releases all refs to be owned by list, unref will call destru ctor!
93 if (obj->getRefCnt() == 1) {
94 obj->transferPDFObjectsOwnership(list);
95 }
96 // If unref calls destructor, it won't call recursively
97 // other ~SkPDFObjects().
98 obj->unref();
99 }
100 }
101 }
102
82 protected: 103 protected:
83 /** Subclasses must implement this method to print the object to the 104 /** Subclasses must implement this method to print the object to the
84 * PDF file. 105 * PDF file.
85 * @param catalog The object catalog to use. 106 * @param catalog The object catalog to use.
86 * @param indirect If true, output an object identifier with the object. 107 * @param indirect If true, output an object identifier with the object.
87 * @param stream The writable output stream to send the output to. 108 * @param stream The writable output stream to send the output to.
88 */ 109 */
89 virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog, 110 virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
90 bool indirect) = 0; 111 bool indirect) = 0;
91 112
113 /** Dumps all the SkPDFObject members in the list. When the destructor is
114 * called, the stack won't be used much.
115 * This method will destroy the state of the object. Should be called only
116 * during destructor calls.
117 */
118 virtual void transferPDFObjectsOwnership(SkTDArray<SkPDFObject*>* list) {}
119
92 typedef SkRefCnt INHERITED; 120 typedef SkRefCnt INHERITED;
93 }; 121 };
94 122
95 /** \class SkPDFObjRef 123 /** \class SkPDFObjRef
96 124
97 An indirect reference to a PDF object. 125 An indirect reference to a PDF object.
98 */ 126 */
99 class SkPDFObjRef : public SkPDFObject { 127 class SkPDFObjRef : public SkPDFObject {
100 public: 128 public:
101 SK_DECLARE_INST_COUNT(SkPDFObjRef) 129 SK_DECLARE_INST_COUNT(SkPDFObjRef)
102 130
103 /** Create a reference to an existing SkPDFObject. 131 /** Create a reference to an existing SkPDFObject.
104 * @param obj The object to reference. 132 * @param obj The object to reference.
105 */ 133 */
106 explicit SkPDFObjRef(SkPDFObject* obj); 134 explicit SkPDFObjRef(SkPDFObject* obj);
107 virtual ~SkPDFObjRef(); 135 virtual ~SkPDFObjRef();
108 136
109 // The SkPDFObject interface. 137 // The SkPDFObject interface.
110 virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog, 138 virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
111 bool indirect); 139 bool indirect);
112 virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect); 140 virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
113 141
142 protected:
143 virtual void transferPDFObjectsOwnership(SkTDArray<SkPDFObject*>* list) {
144 list->push(fObj.detach());
145
146 INHERITED::transferPDFObjectsOwnership(list);
147 }
148
114 private: 149 private:
115 SkAutoTUnref<SkPDFObject> fObj; 150 SkAutoTUnref<SkPDFObject> fObj;
116 151
117 typedef SkPDFObject INHERITED; 152 typedef SkPDFObject INHERITED;
118 }; 153 };
119 154
120 /** \class SkPDFInt 155 /** \class SkPDFInt
121 156
122 An integer object in a PDF. 157 An integer object in a PDF.
123 */ 158 */
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 * @param name The name for this dictionary entry. 434 * @param name The name for this dictionary entry.
400 */ 435 */
401 void insertName(const char key[], const SkString& name) { 436 void insertName(const char key[], const SkString& name) {
402 this->insertName(key, name.c_str()); 437 this->insertName(key, name.c_str());
403 } 438 }
404 439
405 /** Remove all entries from the dictionary. 440 /** Remove all entries from the dictionary.
406 */ 441 */
407 void clear(); 442 void clear();
408 443
444 virtual void transferPDFObjectsOwnership(SkTDArray<SkPDFObject*>* list) {
445 for (int i = 0; i < fValue.count(); i++) {
446 list->push(fValue[i].key);
447 fValue[i].key = NULL;
448 list->push(fValue[i].value);
449 fValue[i].value = NULL;
450 }
451 fValue.reset();
452
453 INHERITED::transferPDFObjectsOwnership(list);
454 }
455
409 private: 456 private:
410 struct Rec { 457 struct Rec {
411 SkPDFName* key; 458 SkPDFName* key;
412 SkPDFObject* value; 459 SkPDFObject* value;
413 }; 460 };
414 461
415 public: 462 public:
416 class Iter { 463 class Iter {
417 public: 464 public:
418 explicit Iter(const SkPDFDict& dict); 465 explicit Iter(const SkPDFDict& dict);
419 SkPDFName* next(SkPDFObject** value); 466 SkPDFName* next(SkPDFObject** value);
420 467
421 private: 468 private:
422 const Rec* fIter; 469 const Rec* fIter;
423 const Rec* fStop; 470 const Rec* fStop;
424 }; 471 };
425 472
426 private: 473 private:
427 static const int kMaxLen = 4095; 474 static const int kMaxLen = 4095;
428 475
429 SkTDArray<struct Rec> fValue; 476 SkTDArray<struct Rec> fValue;
430 477
431 typedef SkPDFObject INHERITED; 478 typedef SkPDFObject INHERITED;
432 }; 479 };
433 480
434 #endif 481 #endif
OLDNEW
« no previous file with comments | « src/pdf/SkPDFStream.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698