Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8050299f authored by Mathias Agopian's avatar Mathias Agopian
Browse files

new String8, String16 ctors to initialize empty static strings with static linkage

when libutils is statically linked, the ordering of the static
initializer is not guaranteed and therefore it's unsafe to use
empty static strings: e.g.:

static String8 sThisStaticStringIsNotSafe;

instead, this new constructor can be used:

static String8 sThisStaticStringIsSafe(kEmptyString);

Change-Id: Ia3daf1cab1c97d021c0ee9c2b394b5e27e8d6c0d
parent 002e1e58
Loading
Loading
Loading
Loading
+9 −0
Original line number Original line Diff line number Diff line
@@ -41,7 +41,16 @@ class TextOutput;
class String16
class String16
{
{
public:
public:
    /* use String16(StaticLinkage) if you're statically linking against
     * libutils and declaring an empty static String16, e.g.:
     *
     *   static String16 sAStaticEmptyString(String16::kEmptyString);
     *   static String16 sAnotherStaticEmptyString(sAStaticEmptyString);
     */
    enum StaticLinkage { kEmptyString };

                                String16();
                                String16();
    explicit                    String16(StaticLinkage);
                                String16(const String16& o);
                                String16(const String16& o);
                                String16(const String16& o,
                                String16(const String16& o,
                                         size_t len,
                                         size_t len,
+9 −0
Original line number Original line Diff line number Diff line
@@ -37,7 +37,16 @@ class TextOutput;
class String8
class String8
{
{
public:
public:
    /* use String8(StaticLinkage) if you're statically linking against
     * libutils and declaring an empty static String8, e.g.:
     *
     *   static String8 sAStaticEmptyString(String8::kEmptyString);
     *   static String8 sAnotherStaticEmptyString(sAStaticEmptyString);
     */
    enum StaticLinkage { kEmptyString };

                                String8();
                                String8();
    explicit                    String8(StaticLinkage);
                                String8(const String8& o);
                                String8(const String8& o);
    explicit                    String8(const char* o);
    explicit                    String8(const char* o);
    explicit                    String8(const char* o, size_t numChars);
    explicit                    String8(const char* o, size_t numChars);
+13 −0
Original line number Original line Diff line number Diff line
@@ -93,6 +93,19 @@ String16::String16()
{
{
}
}


String16::String16(StaticLinkage)
    : mString(0)
{
    // this constructor is used when we can't rely on the static-initializers
    // having run. In this case we always allocate an empty string. It's less
    // efficient than using getEmptyString(), but we assume it's uncommon.

    char16_t* data = static_cast<char16_t*>(
            SharedBuffer::alloc(sizeof(char16_t))->data());
    data[0] = 0;
    mString = data;
}

String16::String16(const String16& o)
String16::String16(const String16& o)
    : mString(o.mString)
    : mString(o.mString)
{
{
+13 −4
Original line number Original line Diff line number Diff line
@@ -47,16 +47,12 @@ void initialize_string8();


static inline char* getEmptyString()
static inline char* getEmptyString()
{
{
    if (!gEmptyStringBuf) initialize_string8();

    gEmptyStringBuf->acquire();
    gEmptyStringBuf->acquire();
    return gEmptyString;
    return gEmptyString;
}
}


void initialize_string8()
void initialize_string8()
{
{
    if (gEmptyStringBuf) return;

    // HACK: This dummy dependency forces linking libutils Static.cpp,
    // HACK: This dummy dependency forces linking libutils Static.cpp,
    // which is needed to initialize String8/String16 classes.
    // which is needed to initialize String8/String16 classes.
    // These variables are named for Darwin, but are needed elsewhere too,
    // These variables are named for Darwin, but are needed elsewhere too,
@@ -146,6 +142,19 @@ String8::String8()
{
{
}
}


String8::String8(StaticLinkage)
    : mString(0)
{
    // this constructor is used when we can't rely on the static-initializers
    // having run. In this case we always allocate an empty string. It's less
    // efficient than using getEmptyString(), but we assume it's uncommon.

    char* data = static_cast<char*>(
            SharedBuffer::alloc(sizeof(char))->data());
    data[0] = 0;
    mString = data;
}

String8::String8(const String8& o)
String8::String8(const String8& o)
    : mString(o.mString)
    : mString(o.mString)
{
{