michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "sandbox/win/src/shared_handles.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: // Note once again the the assumption here is that the shared memory is michael@0: // initialized with zeros in the process that calls SetHandle and that michael@0: // the process that calls GetHandle 'sees' this memory. michael@0: michael@0: SharedHandles::SharedHandles() { michael@0: shared_.items = NULL; michael@0: shared_.max_items = 0; michael@0: } michael@0: michael@0: bool SharedHandles::Init(void* raw_mem, size_t size_bytes) { michael@0: if (size_bytes < sizeof(shared_.items[0])) { michael@0: // The shared memory is too small! michael@0: return false; michael@0: } michael@0: shared_.items = static_cast(raw_mem); michael@0: shared_.max_items = size_bytes / sizeof(shared_.items[0]); michael@0: return true; michael@0: } michael@0: michael@0: // Note that an empty slot is marked with a tag == 0 that is why is michael@0: // not a valid imput tag michael@0: bool SharedHandles::SetHandle(uint32 tag, HANDLE handle) { michael@0: if (0 == tag) { michael@0: // Invalid tag michael@0: return false; michael@0: } michael@0: // Find empty slot and put the tag and the handle there michael@0: SharedItem* empty_slot = FindByTag(0); michael@0: if (NULL == empty_slot) { michael@0: return false; michael@0: } michael@0: empty_slot->tag = tag; michael@0: empty_slot->item = handle; michael@0: return true; michael@0: } michael@0: michael@0: bool SharedHandles::GetHandle(uint32 tag, HANDLE* handle) { michael@0: if (0 == tag) { michael@0: // Invalid tag michael@0: return false; michael@0: } michael@0: SharedItem* found = FindByTag(tag); michael@0: if (NULL == found) { michael@0: return false; michael@0: } michael@0: *handle = found->item; michael@0: return true; michael@0: } michael@0: michael@0: SharedHandles::SharedItem* SharedHandles::FindByTag(uint32 tag) { michael@0: for (size_t ix = 0; ix != shared_.max_items; ++ix) { michael@0: if (tag == shared_.items[ix].tag) { michael@0: return &shared_.items[ix]; michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: } // namespace sandbox