Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "sandbox/win/src/shared_handles.h" |
michael@0 | 6 | |
michael@0 | 7 | namespace sandbox { |
michael@0 | 8 | |
michael@0 | 9 | // Note once again the the assumption here is that the shared memory is |
michael@0 | 10 | // initialized with zeros in the process that calls SetHandle and that |
michael@0 | 11 | // the process that calls GetHandle 'sees' this memory. |
michael@0 | 12 | |
michael@0 | 13 | SharedHandles::SharedHandles() { |
michael@0 | 14 | shared_.items = NULL; |
michael@0 | 15 | shared_.max_items = 0; |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | bool SharedHandles::Init(void* raw_mem, size_t size_bytes) { |
michael@0 | 19 | if (size_bytes < sizeof(shared_.items[0])) { |
michael@0 | 20 | // The shared memory is too small! |
michael@0 | 21 | return false; |
michael@0 | 22 | } |
michael@0 | 23 | shared_.items = static_cast<SharedItem*>(raw_mem); |
michael@0 | 24 | shared_.max_items = size_bytes / sizeof(shared_.items[0]); |
michael@0 | 25 | return true; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | // Note that an empty slot is marked with a tag == 0 that is why is |
michael@0 | 29 | // not a valid imput tag |
michael@0 | 30 | bool SharedHandles::SetHandle(uint32 tag, HANDLE handle) { |
michael@0 | 31 | if (0 == tag) { |
michael@0 | 32 | // Invalid tag |
michael@0 | 33 | return false; |
michael@0 | 34 | } |
michael@0 | 35 | // Find empty slot and put the tag and the handle there |
michael@0 | 36 | SharedItem* empty_slot = FindByTag(0); |
michael@0 | 37 | if (NULL == empty_slot) { |
michael@0 | 38 | return false; |
michael@0 | 39 | } |
michael@0 | 40 | empty_slot->tag = tag; |
michael@0 | 41 | empty_slot->item = handle; |
michael@0 | 42 | return true; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | bool SharedHandles::GetHandle(uint32 tag, HANDLE* handle) { |
michael@0 | 46 | if (0 == tag) { |
michael@0 | 47 | // Invalid tag |
michael@0 | 48 | return false; |
michael@0 | 49 | } |
michael@0 | 50 | SharedItem* found = FindByTag(tag); |
michael@0 | 51 | if (NULL == found) { |
michael@0 | 52 | return false; |
michael@0 | 53 | } |
michael@0 | 54 | *handle = found->item; |
michael@0 | 55 | return true; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | SharedHandles::SharedItem* SharedHandles::FindByTag(uint32 tag) { |
michael@0 | 59 | for (size_t ix = 0; ix != shared_.max_items; ++ix) { |
michael@0 | 60 | if (tag == shared_.items[ix].tag) { |
michael@0 | 61 | return &shared_.items[ix]; |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | return NULL; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | } // namespace sandbox |