1/*
2 * Copyright (C) 2010 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#ifdef SKIP_STATIC_CONSTRUCTORS_ON_GCC
29#define ATOMICSTRING_HIDE_GLOBALS 1
30#endif
31
32#include "AtomicString.h"
33#include "DynamicAnnotations.h"
34#include "StaticConstructors.h"
35#include "StringImpl.h"
36
37namespace WTF {
38
39StringImpl* StringImpl::empty()
40{
41    // FIXME: This works around a bug in our port of PCRE, that a regular expression
42    // run on the empty string may still perform a read from the first element, and
43    // as such we need this to be a valid pointer. No code should ever be reading
44    // from a zero length string, so this should be able to be a non-null pointer
45    // into the zero-page.
46    // Replace this with 'reinterpret_cast<UChar*>(static_cast<intptr_t>(1))' once
47    // PCRE goes away.
48    static UChar emptyUCharData = 0;
49    DEFINE_STATIC_LOCAL(StringImpl, emptyString, (&emptyUCharData, 0, ConstructStaticString));
50    WTF_ANNOTATE_BENIGN_RACE(&emptyString, "Benign race on StringImpl::emptyString reference counter");
51    return &emptyString;
52}
53
54JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, nullAtom)
55JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, emptyAtom, "")
56JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, textAtom, "#text")
57JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, commentAtom, "#comment")
58JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, starAtom, "*")
59JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, xmlAtom, "xml")
60JS_EXPORTDATA DEFINE_GLOBAL(AtomicString, xmlnsAtom, "xmlns")
61
62void AtomicString::init()
63{
64    static bool initialized;
65    if (!initialized) {
66        // Initialization is not thread safe, so this function must be called from the main thread first.
67        ASSERT(isMainThread());
68
69        // Use placement new to initialize the globals.
70        new ((void*)&nullAtom) AtomicString;
71        new ((void*)&emptyAtom) AtomicString("");
72        new ((void*)&textAtom) AtomicString("#text");
73        new ((void*)&commentAtom) AtomicString("#comment");
74        new ((void*)&starAtom) AtomicString("*");
75        new ((void*)&xmlAtom) AtomicString("xml");
76        new ((void*)&xmlnsAtom) AtomicString("xmlns");
77
78        initialized = true;
79    }
80}
81
82}
83