Make memory allocation/deallocation consistent: use new char[] instead of operator new()

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@724 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
SiyangXie@gmail.com 2010-11-03 23:54:01 +00:00
parent 3b4ac42ff8
commit bbd8e82a7f
6 changed files with 26 additions and 28 deletions

View File

@ -102,8 +102,7 @@ char *StdMapSerializer<Key, Value>::Serialize(
// Compute size of memory to be allocated.
unsigned int size_to_alloc = SizeOf(m);
// Allocate memory.
char *serialized_data =
reinterpret_cast<char*>(operator new(size_to_alloc));
char *serialized_data = new char[size_to_alloc];
if (!serialized_data) {
BPLOG(INFO) << "StdMapSerializer memory allocation failed.";
if (size) *size = 0;
@ -172,8 +171,7 @@ char *RangeMapSerializer<Address, Entry>::Serialize(
// Compute size of memory to be allocated.
unsigned int size_to_alloc = SizeOf(m);
// Allocate memory.
char *serialized_data =
reinterpret_cast<char*>(operator new(size_to_alloc));
char *serialized_data = new char[size_to_alloc];
if (!serialized_data) {
BPLOG(INFO) << "RangeMapSerializer memory allocation failed.";
if (size) *size = 0;
@ -252,7 +250,7 @@ char *ContainedRangeMapSerializer<AddrType, EntryType>::Serialize(
const ContainedRangeMap<AddrType, EntryType> *m, unsigned int *size) const {
unsigned int size_to_alloc = SizeOf(m);
// Allocating memory.
char *serialized_data = reinterpret_cast<char*>(operator new(size_to_alloc));
char *serialized_data = new char[size_to_alloc];
if (!serialized_data) {
BPLOG(INFO) << "ContainedRangeMapSerializer memory allocation failed.";
if (size) *size = 0;

View File

@ -65,6 +65,7 @@ class StdMapSerializer {
// described in "StaticMap.h" comment.
// Returns a pointer to the serialized data. If size != NULL, *size is set
// to the size of serialized data, i.e., SizeOf(m).
// Caller has the ownership of memory allocated as "new char[]".
char* Serialize(const std::map<Key, Value> &m, unsigned int *size) const;
private:
@ -92,6 +93,7 @@ class AddressMapSerializer {
// Serializes an AddressMap object into a chunk of memory data.
// Returns a pointer to the serialized data. If size != NULL, *size is set
// to the size of serialized data, i.e., SizeOf(m).
// Caller has the ownership of memory allocated as "new char[]".
char* Serialize(const AddressMap<Addr, Entry> &m, unsigned int *size) const {
return std_map_serializer_.Serialize(m.map_, size);
}
@ -118,6 +120,7 @@ class RangeMapSerializer {
// Serializes a RangeMap object into a chunk of memory data.
// Returns a pointer to the serialized data. If size != NULL, *size is set
// to the size of serialized data, i.e., SizeOf(m).
// Caller has the ownership of memory allocated as "new char[]".
char* Serialize(const RangeMap<Address, Entry> &m, unsigned int *size) const;
private:
@ -147,6 +150,7 @@ class ContainedRangeMapSerializer {
// Serializes a ContainedRangeMap object into a chunk of memory data.
// Returns a pointer to the serialized data. If size != NULL, *size is set
// to the size of serialized data, i.e., SizeOf(m).
// Caller has the ownership of memory allocated as "new char[]".
char* Serialize(const ContainedRangeMap<AddrType, EntryType> *m,
unsigned int *size) const;

View File

@ -59,7 +59,7 @@ class TestStdMapSerializer : public ::testing::Test {
}
void TearDown() {
delete serialized_data_;
delete [] serialized_data_;
}
std::map<AddrType, EntryType> std_map_;

View File

@ -53,33 +53,28 @@
namespace google_breakpad {
bool ModuleComparer::Compare(const string &symbol_data) {
// Empty CodeModule with only a name "test":
BasicCodeModule code_module(0, 0, "test", "", "", "", "");
scoped_ptr<BasicModule> basic_module(new BasicModule("test_module"));
scoped_ptr<FastModule> fast_module(new FastModule("test_module"));
// Load BasicSourceLineResolver::Module.
BPLOG(INFO) << "Unserialized size = " << symbol_data.size() << " Bytes";
basic_resolver_->LoadModuleUsingMapBuffer(&code_module, symbol_data);
BasicModule *old_module = dynamic_cast<BasicModule*>(
basic_resolver_->modules_->at("test"));
// Load symbol data into basic_module
scoped_array<char> buffer(new char[symbol_data.size() + 1]);
strcpy(buffer.get(), symbol_data.c_str());
ASSERT_TRUE(basic_module->LoadMapFromMemory(buffer.get()));
buffer.reset();
// Serialize BasicSourceLineResolver::Module.
unsigned int serialized_size = 0;
char *mem = serializer_.Serialize(*old_module, &serialized_size);
ASSERT_TRUE(mem);
scoped_array<char> serialized_data(
serializer_.Serialize(*(basic_module.get()), &serialized_size));
ASSERT_TRUE(serialized_data.get());
BPLOG(INFO) << "Serialized size = " << serialized_size << " Bytes";
// Load FastSourceLineResolver::Module using serialized data.
ASSERT_TRUE(fast_resolver_->LoadModuleUsingMemoryBuffer(&code_module, mem));
FastModule *new_module = dynamic_cast<FastModule*>(
fast_resolver_->modules_->at("test"));
ASSERT_TRUE(fast_module->LoadMapFromMemory(serialized_data.get()));
// Compare FastSourceLineResolver::Module with
// BasicSourceLineResolver::Module.
ASSERT_TRUE(CompareModule(old_module, new_module));
// Clean up.
basic_resolver_->UnloadModule(&code_module);
fast_resolver_->UnloadModule(&code_module);
ASSERT_TRUE(CompareModule(basic_module.get(), fast_module.get()));
return true;
}

View File

@ -100,7 +100,7 @@ char* ModuleSerializer::Serialize(
unsigned int size_to_alloc = SizeOf(module);
// Allocate memory for serialized data.
char *serialized_data = reinterpret_cast<char*>(operator new(size_to_alloc));
char *serialized_data = new char[size_to_alloc];
if (!serialized_data) {
BPLOG(ERROR) << "ModuleSerializer: memory allocation failed, "
<< "size to alloc: " << size_to_alloc;
@ -134,7 +134,7 @@ bool ModuleSerializer::SerializeModuleAndLoadIntoFastResolver(
dynamic_cast<BasicSourceLineResolver::Module*>(iter->second);
unsigned int size = 0;
scoped_ptr<char> symbol_data(Serialize(*basic_module, &size));
scoped_array<char> symbol_data(Serialize(*basic_module, &size));
if (!symbol_data.get()) {
BPLOG(ERROR) << "Serialization failed for module: " << basic_module->name_;
return false;
@ -193,6 +193,7 @@ char* ModuleSerializer::SerializeSymbolFileData(
if (!module->LoadMapFromMemory(buffer.get())) {
return NULL;
}
buffer.reset(NULL);
return Serialize(*(module.get()), size);
}

View File

@ -71,13 +71,13 @@ class ModuleSerializer {
// the address of memory chunk. If size != NULL, *size is set to the memory
// size allocated for the serialized data.
// Caller takes the ownership of the memory chunk (allocated on heap), and
// should call delete instead of delete [] to free it.
// owner should call delete [] to free the memory after use.
char* Serialize(const BasicSourceLineResolver::Module &module,
unsigned int *size = NULL);
// Given the string format symbol_data, produces a chunk of serialized data.
// Caller takes ownership of the serialized data (on heap), and should call
// delete instead of delete [] to free it.
// Caller takes ownership of the serialized data (on heap), and owner should
// call delete [] to free the memory after use.
char* SerializeSymbolFileData(const string &symbol_data,
unsigned int *size = NULL);