summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorerg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-19 17:04:46 +0000
committererg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-19 17:04:46 +0000
commit6de2799a47cbc201d943b32e0ac62555dc45f662 (patch)
tree6c64342df90783c5f334d370d6f8cb08472fcd7f /base
parent5f1b13bc6952eb89149716930e654c9d989b9e13 (diff)
downloadchromium_src-6de2799a47cbc201d943b32e0ac62555dc45f662.zip
chromium_src-6de2799a47cbc201d943b32e0ac62555dc45f662.tar.gz
chromium_src-6de2799a47cbc201d943b32e0ac62555dc45f662.tar.bz2
First fix to minimize copying of image data.
This is the first of multiple patches that clean up handling of memory regarding images. Previously, the code did several memcpy()s or equivalents. This change: - Creates an abstract interface RefCountedMemory which provides access to the front() of a memory range and the size() of it. It is a RefCountedThreadSafe. - Adds a RefCountedStaticMemory class which isa RefCountedMemory. - Pushes RefCountedBytes up into base/ from chrome/ and make it conform to RefCountedMemory. - Have ResourceBundle return RefCountedStaticMemory to the mmaped() or DLL loaded resources instead of memcpy()ing them. - General cleanups to minimize copies in constructing RefCountedBytes. - Use the above consistent interface in the BrowserThemeProvider, along with special casing the loading of the new tab page background. This patch is mostly cleanups and there should only be a slight performance gain if any. Most of the real speedups should come in subsequent patches. BUG=http://crbug.com/24493 TEST=Slightly faster on Perf bot; does not introduce crashes. Review URL: http://codereview.chromium.org/288005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29412 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp1
-rw-r--r--base/ref_counted.h2
-rw-r--r--base/ref_counted_memory.h76
3 files changed, 77 insertions, 2 deletions
diff --git a/base/base.gyp b/base/base.gyp
index c423484..2ca5cfb 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -238,6 +238,7 @@
'rand_util_win.cc',
'ref_counted.cc',
'ref_counted.h',
+ 'ref_counted_memory.h',
'registry.cc',
'registry.h',
'resource_util.cc',
diff --git a/base/ref_counted.h b/base/ref_counted.h
index 70536b9..e04bb7c 100644
--- a/base/ref_counted.h
+++ b/base/ref_counted.h
@@ -62,8 +62,6 @@ class RefCountedThreadSafeBase {
DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase);
};
-
-
} // namespace subtle
//
diff --git a/base/ref_counted_memory.h b/base/ref_counted_memory.h
new file mode 100644
index 0000000..ef05abd
--- /dev/null
+++ b/base/ref_counted_memory.h
@@ -0,0 +1,76 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_REF_COUNTED_MEMORY_H_
+#define BASE_REF_COUNTED_MEMORY_H_
+
+#include <vector>
+
+#include "base/ref_counted.h"
+
+// TODO(erg): The contents of this file should be in a namespace. This would
+// require touching >100 files in chrome/ though.
+
+// A generic interface to memory. This object is reference counted because one
+// of its two subclasses own the data they carry, and we need to have
+// heterogeneous containers of these two types of memory.
+class RefCountedMemory : public base::RefCountedThreadSafe< RefCountedMemory > {
+ public:
+ virtual ~RefCountedMemory() {}
+
+ // Retrieves a pointer to the beginning of the data we point to.
+ virtual const unsigned char* front() const = 0;
+
+ // Size of the memory pointed to.
+ virtual size_t size() const = 0;
+};
+
+// An implementation of RefCountedMemory, where the ref counting does not
+// matter.
+class RefCountedStaticMemory : public RefCountedMemory {
+ public:
+ RefCountedStaticMemory()
+ : data_(NULL), length_(0) {}
+ RefCountedStaticMemory(const unsigned char* data, size_t length)
+ : data_(data), length_(length) {}
+
+ virtual const unsigned char* front() const { return data_; }
+ virtual size_t size() const { return length_; }
+
+ private:
+ const unsigned char* data_;
+ size_t length_;
+
+ DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
+};
+
+// An implementation of RefCountedMemory, where we own our the data in a
+// vector.
+class RefCountedBytes : public RefCountedMemory {
+ public:
+ // Constructs a RefCountedBytes object by performing a swap. (To non
+ // destructively build a RefCountedBytes, use the constructor that takes a
+ // vector.)
+ static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy) {
+ RefCountedBytes* bytes = new RefCountedBytes;
+ bytes->data.swap(*to_destroy);
+ return bytes;
+ }
+
+ RefCountedBytes() {}
+
+ // Constructs a RefCountedBytes object by _copying_ from |initializer|.
+ RefCountedBytes(const std::vector<unsigned char>& initializer)
+ : data(initializer) {}
+
+ virtual const unsigned char* front() const { return &data.front(); }
+ virtual size_t size() const { return data.size(); }
+
+ std::vector<unsigned char> data;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
+};
+
+#endif // BASE_REF_COUNTED_MEMORY_H_