Skip to content

Commit

Permalink
Extract some logic from transport_dib into shared_memory.
Browse files Browse the repository at this point in the history
This CL contains no intended behavior change.

Some of the logic in transport_dib_posix.cc made assumptions about the internal
structure of SharedMemoryHandle. I've moved that logic into
shared_memory_posix.cc.

BUG=466437

Review URL: https://codereview.chromium.org/1143243007

Cr-Commit-Position: refs/heads/master@{#332246}
  • Loading branch information
erikchen authored and Commit bot committed Jun 1, 2015
1 parent a391f4a commit 38aef4f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions base/memory/shared_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ class BASE_EXPORT SharedMemory {
bool clean_up_resources_on_destruction);
#endif

#if defined(OS_POSIX) && !defined(OS_ANDROID)
// Returns the size of the shared memory region referred to by |handle|.
// Returns '-1' on a failure to determine the size.
static int GetSizeFromSharedMemoryHandle(const SharedMemoryHandle& handle);
#endif // defined(OS_POSIX) && !defined(OS_ANDROID)

// Creates a shared memory object as described by the options struct.
// Returns true on success and false on failure.
bool Create(const SharedMemoryCreateOptions& options);
Expand Down
9 changes: 9 additions & 0 deletions base/memory/shared_memory_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ bool SharedMemory::CreateAndMapAnonymous(size_t size) {
}

#if !defined(OS_ANDROID)
// static
int SharedMemory::GetSizeFromSharedMemoryHandle(
const SharedMemoryHandle& handle) {
struct stat st;
if (fstat(handle.fd, &st) != 0)
return -1;
return st.st_size;
}

// Chromium mostly only uses the unique/private shmem as specified by
// "name == L"". The exception is in the StatsTable.
// TODO(jrg): there is no way to "clean up" all unused named shmem if
Expand Down
13 changes: 6 additions & 7 deletions ui/surface/transport_dib_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TransportDIB* TransportDIB::CreateWithHandle(Handle handle) {

// static
bool TransportDIB::is_valid_handle(Handle dib) {
return dib.fd >= 0;
return base::SharedMemory::IsHandleValid(dib);
}

// static
Expand All @@ -66,7 +66,7 @@ bool TransportDIB::is_valid_id(Id id) {
skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) {
if ((!memory() && !Map()) || !VerifyCanvasSize(w, h))
return NULL;
return skia::CreatePlatformCanvas(w, h, true,
return skia::CreatePlatformCanvas(w, h, true,
reinterpret_cast<uint8_t*>(memory()),
skia::RETURN_NULL_ON_FAILURE);
}
Expand All @@ -82,13 +82,12 @@ bool TransportDIB::Map() {
if (memory())
return true;

struct stat st;
if ((fstat(shared_memory_.handle().fd, &st) != 0) ||
(!shared_memory_.Map(st.st_size))) {
int size = base::SharedMemory::GetSizeFromSharedMemoryHandle(
shared_memory_.handle());
if (size == -1 || !shared_memory_.Map(size))
return false;
}

size_ = st.st_size;
size_ = size;
#endif
return true;
}
Expand Down

0 comments on commit 38aef4f

Please sign in to comment.