1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is HTML Parser C++ Translator code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2008
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Henri Sivonen <hsivonen@iki.fi>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsHtml5OwningUTF16Buffer.h"
39 :
40 0 : nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(PRUnichar* aBuffer)
41 : : nsHtml5UTF16Buffer(aBuffer, 0),
42 : next(nsnull),
43 0 : key(nsnull)
44 : {
45 0 : MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer);
46 0 : }
47 :
48 0 : nsHtml5OwningUTF16Buffer::nsHtml5OwningUTF16Buffer(void* aKey)
49 : : nsHtml5UTF16Buffer(nsnull, 0),
50 : next(nsnull),
51 0 : key(aKey)
52 : {
53 0 : MOZ_COUNT_CTOR(nsHtml5OwningUTF16Buffer);
54 0 : }
55 :
56 0 : nsHtml5OwningUTF16Buffer::~nsHtml5OwningUTF16Buffer()
57 : {
58 0 : MOZ_COUNT_DTOR(nsHtml5OwningUTF16Buffer);
59 0 : DeleteBuffer();
60 :
61 : // This is to avoid dtor recursion on 'next', bug 706932.
62 0 : nsRefPtr<nsHtml5OwningUTF16Buffer> tail;
63 0 : tail.swap(next);
64 0 : while (tail && tail->mRefCnt == 1) {
65 0 : nsRefPtr<nsHtml5OwningUTF16Buffer> tmp;
66 0 : tmp.swap(tail->next);
67 0 : tail.swap(tmp);
68 : }
69 0 : }
70 :
71 : // static
72 : already_AddRefed<nsHtml5OwningUTF16Buffer>
73 0 : nsHtml5OwningUTF16Buffer::FalliblyCreate(PRInt32 aLength)
74 : {
75 0 : const mozilla::fallible_t fallible = mozilla::fallible_t();
76 0 : PRUnichar* newBuf = new (fallible) PRUnichar[aLength];
77 0 : if (!newBuf) {
78 0 : return nsnull;
79 : }
80 : nsRefPtr<nsHtml5OwningUTF16Buffer> newObj =
81 0 : new (fallible) nsHtml5OwningUTF16Buffer(newBuf);
82 0 : if (!newObj) {
83 0 : delete[] newBuf;
84 0 : return nsnull;
85 : }
86 0 : return newObj.forget();
87 : }
88 :
89 : void
90 0 : nsHtml5OwningUTF16Buffer::Swap(nsHtml5OwningUTF16Buffer* aOther)
91 : {
92 0 : nsHtml5UTF16Buffer::Swap(aOther);
93 0 : }
94 :
95 :
96 : // Not using macros for AddRef and Release in order to be able to refcount on
97 : // and create on different threads.
98 :
99 : nsrefcnt
100 0 : nsHtml5OwningUTF16Buffer::AddRef()
101 : {
102 0 : NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "Illegal refcount.");
103 0 : ++mRefCnt;
104 0 : NS_LOG_ADDREF(this, mRefCnt, "nsHtml5OwningUTF16Buffer", sizeof(*this));
105 0 : return mRefCnt;
106 : }
107 :
108 : nsrefcnt
109 0 : nsHtml5OwningUTF16Buffer::Release()
110 : {
111 0 : NS_PRECONDITION(0 != mRefCnt, "Release without AddRef.");
112 0 : --mRefCnt;
113 0 : NS_LOG_RELEASE(this, mRefCnt, "nsHtml5OwningUTF16Buffer");
114 0 : if (mRefCnt == 0) {
115 0 : mRefCnt = 1; /* stabilize */
116 0 : delete this;
117 0 : return 0;
118 : }
119 0 : return mRefCnt;
120 : }
|