Home | History | Annotate | Download | only in common
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This file contains the implementation of IdAllocator.
      6 
      7 #include "gpu/command_buffer/common/id_allocator.h"
      8 #include "gpu/command_buffer/common/logging.h"
      9 
     10 namespace gpu {
     11 
     12 IdAllocatorInterface::~IdAllocatorInterface() {
     13 }
     14 
     15 IdAllocator::IdAllocator() {}
     16 
     17 IdAllocator::~IdAllocator() {}
     18 
     19 ResourceId IdAllocator::AllocateID() {
     20   ResourceId id;
     21   ResourceIdSet::iterator iter = free_ids_.begin();
     22   if (iter != free_ids_.end()) {
     23     id = *iter;
     24   } else {
     25     id = LastUsedId() + 1;
     26     if (!id) {
     27       // We wrapped around to 0.
     28       id = FindFirstUnusedId();
     29     }
     30   }
     31   MarkAsUsed(id);
     32   return id;
     33 }
     34 
     35 ResourceId IdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
     36   ResourceId id;
     37   ResourceIdSet::iterator iter = free_ids_.lower_bound(desired_id);
     38   if (iter != free_ids_.end()) {
     39     id = *iter;
     40   } else if (LastUsedId() < desired_id) {
     41     id = desired_id;
     42   } else {
     43     id = LastUsedId() + 1;
     44     if (!id) {
     45       // We wrapped around to 0.
     46       id = FindFirstUnusedId();
     47     }
     48   }
     49   MarkAsUsed(id);
     50   return id;
     51 }
     52 
     53 bool IdAllocator::MarkAsUsed(ResourceId id) {
     54   GPU_DCHECK(id);
     55   free_ids_.erase(id);
     56   std::pair<ResourceIdSet::iterator, bool> result = used_ids_.insert(id);
     57   return result.second;
     58 }
     59 
     60 void IdAllocator::FreeID(ResourceId id) {
     61   if (id) {
     62     used_ids_.erase(id);
     63     free_ids_.insert(id);
     64   }
     65 }
     66 
     67 bool IdAllocator::InUse(ResourceId id) const {
     68   return id == kInvalidResource || used_ids_.find(id) != used_ids_.end();
     69 }
     70 
     71 ResourceId IdAllocator::LastUsedId() const {
     72   if (used_ids_.empty()) {
     73     return 0u;
     74   } else {
     75     return *used_ids_.rbegin();
     76   }
     77 }
     78 
     79 ResourceId IdAllocator::FindFirstUnusedId() const {
     80   ResourceId id = 1;
     81   for (ResourceIdSet::const_iterator it = used_ids_.begin();
     82        it != used_ids_.end(); ++it) {
     83     if ((*it) != id) {
     84       return id;
     85     }
     86     ++id;
     87   }
     88   return id;
     89 }
     90 
     91 NonReusedIdAllocator::NonReusedIdAllocator() : last_id_(0) {
     92 }
     93 
     94 NonReusedIdAllocator::~NonReusedIdAllocator() {
     95 }
     96 
     97 ResourceId NonReusedIdAllocator::AllocateID() {
     98   return ++last_id_;
     99 }
    100 
    101 ResourceId NonReusedIdAllocator::AllocateIDAtOrAbove(ResourceId desired_id) {
    102   if (desired_id > last_id_)
    103     last_id_ = desired_id;
    104 
    105   return ++last_id_;
    106 }
    107 
    108 bool NonReusedIdAllocator::MarkAsUsed(ResourceId id) {
    109   GPU_NOTREACHED();
    110   return false;
    111 }
    112 
    113 void NonReusedIdAllocator::FreeID(ResourceId id) {
    114 }
    115 
    116 bool NonReusedIdAllocator::InUse(ResourceId id) const {
    117   GPU_NOTREACHED();
    118   return false;
    119 }
    120 
    121 }  // namespace gpu
    122