Commit 17adfc84 authored by Adam Reichold's avatar Adam Reichold Committed by Albert Astals Cid

Adjust memory layout computation of GooString

GooString uses the small string optimization but the static buffer size is
hard-coded and hence the final object size becomes dependent on architecture.
This adds a helper class to compute the memory layout at compile time so that
the target object size of e.g. 32 or another multiple of 16 is achieved.

This also adds an overload so that the C string returned by GooString's
getCString method respect the constness of this and fixes a constness issue
in the lexer tests.
parent d1720740
......@@ -20,6 +20,7 @@
// Copyright (C) 2008-2010, 2012, 2014 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2012-2014 Fabio D'Urso <fabiodurso@hotmail.it>
// Copyright (C) 2013 Jason Crain <jason@aquaticape.us>
// Copyright (C) 2015 Adam Reichold <adam.reichold@t-online.de>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
......@@ -114,13 +115,14 @@ public:
~GooString();
// Get length.
int getLength() { return length; }
int getLength() const { return length; }
// Get C string.
char *getCString() const { return s; }
char *getCString() { return s; }
const char *getCString() const { return s; }
// Get <i>th character.
char getChar(int i) { return s[i]; }
char getChar(int i) const { return s[i]; }
// Change <i>th character.
void setChar(int i, char c) { s[i] = c; }
......@@ -170,11 +172,17 @@ private:
GooString(const GooString &other);
GooString& operator=(const GooString &other);
// you can tweak this number for a different speed/memory usage tradeoffs.
// In libc malloc() rounding is 16 so it's best to choose a value that
// results in sizeof(GooString) be a multiple of 16.
// 24 makes sizeof(GooString) to be 32.
static const int STR_STATIC_SIZE = 24;
// You can tweak the final object size for different time/space tradeoffs.
// In libc malloc(), rounding is 16 so it's best to choose a value that
// is a multiple of 16.
class MemoryLayout {
char c[sizeof(char*)];
int i;
char* s;
};
static const int STR_FINAL_SIZE = 32;
static const int STR_STATIC_SIZE = STR_FINAL_SIZE - sizeof(MemoryLayout) + sizeof(char*);
int roundedSize(int len);
......
......@@ -12,7 +12,7 @@ private slots:
void TestLexer::testNumbers()
{
char *data = "0 1 -1 2147483647 -2147483647 2147483648 -2147483648 4294967297 -2147483649 0.1 1.1 -1.1 2147483647.1 -2147483647.1 2147483648.1 -2147483648.1 4294967297.1 -2147483649.1 9223372036854775807 18446744073709551615";
char data[] = "0 1 -1 2147483647 -2147483647 2147483648 -2147483648 4294967297 -2147483649 0.1 1.1 -1.1 2147483647.1 -2147483647.1 2147483648.1 -2147483648.1 4294967297.1 -2147483649.1 9223372036854775807 18446744073709551615";
Object dummy;
MemStream *stream = new MemStream(data, 0, strlen(data), &dummy);
Lexer *lexer = new Lexer(NULL, stream);
......
......@@ -12,7 +12,7 @@ private slots:
void TestLexer::testNumbers()
{
char *data = "0 1 -1 2147483647 -2147483647 2147483648 -2147483648 4294967297 -2147483649 0.1 1.1 -1.1 2147483647.1 -2147483647.1 2147483648.1 -2147483648.1 4294967297.1 -2147483649.1 9223372036854775807 18446744073709551615";
char data[] = "0 1 -1 2147483647 -2147483647 2147483648 -2147483648 4294967297 -2147483649 0.1 1.1 -1.1 2147483647.1 -2147483647.1 2147483648.1 -2147483648.1 4294967297.1 -2147483649.1 9223372036854775807 18446744073709551615";
Object dummy;
MemStream *stream = new MemStream(data, 0, strlen(data), &dummy);
Lexer *lexer = new Lexer(NULL, stream);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment