// allocate creates an entry for key in localKeys if needed and increments the
// refcnt. The value associated with the key must match the local cache or an
// error is returned
320019
func (lk *localKeys) allocate(key string, val idpool.ID) (idpool.ID, error) {
	lk.Lock()
	defer lk.Unlock()

	if k, ok := lk.keys[key]; ok {
		if val != k.val {
			return idpool.NoID, fmt.Errorf("local key already allocated with different value (%s != %s)", val, k.val)
		}

		k.refcnt++
		kvstore.Trace("Incremented local key refcnt", nil, logrus.Fields{fieldKey: key, fieldID: val, fieldRefCnt: k.refcnt})
		return k.val, nil
	}

	k := &localKey{key: key, val: val, refcnt: 1}
	lk.keys[key] = k
	lk.ids[val] = k
	kvstore.Trace("New local key", nil, logrus.Fields{fieldKey: key, fieldID: val, fieldRefCnt: 1})
	return val, nil
}
1087792
func (rc *Memcache) Put(key string, value interface{}) error {
	val, err := Encode(value)
	if err != nil {
		if rc.Debug {
			log.Println("[Memcache]EncodeErr: ", err, "Key:", key)
		}
		return err
	}
	item := &memcache.Item{Key: Md5(key), Value: val, Expiration: rc.LifeTime}
	err = rc.c.Set(item)
	if err != nil {
		if rc.Debug {
			log.Println("[Memcache]PutErr: ", err, "Key:", key)
		}
		return err
	}
	if rc.Debug {
		log.Println("[Memcache]Put: ", key)
	}
	return err
}
1043544
func (s *PendingModifiedValues) SetAllocatedStorage(v int64) *PendingModifiedValues {
	s.AllocatedStorage = &v
	return s
}
1157936
func (c *Cacher) Put(key string, value interface{}) {
	c.mu.Lock()
	defer c.mu.Unlock()
	if value == nil {
		delete(c.data, key)
	} else {
		c.data[key] = value
	}
}
709983
func (s *DBClusterSnapshot) SetAllocatedStorage(v int64) *DBClusterSnapshot {
	s.AllocatedStorage = &v
	return s
}
934504
func (r *AutoVLANCfgResource) Allocate(reqVal interface{}) (interface{}, error) {
	oper := &AutoVLANOperResource{}
	oper.StateDriver = r.StateDriver
	err := oper.Read(r.ID)
	if err != nil {
		return nil, err
	}

	var vlan uint
	if (reqVal != nil) && (reqVal.(uint) != 0) {
		vlan = reqVal.(uint)
		if !oper.FreeVLANs.Test(vlan) {
			return nil, fmt.Errorf("requested vlan not available - vlan:%d", vlan)
		}
	} else {
		ok := false
		vlan, ok = oper.FreeVLANs.NextSet(0)
		if !ok {
			return nil, errors.New("no vlans available")
		}
	}
	oper.FreeVLANs.Clear(vlan)

	err = oper.Write()
	if err != nil {
		return nil, err
	}
	return vlan, nil
}
640945
func (s *MemoryStore) Put(key string, value interface{}) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	if s.data == nil {
		s.data = map[string]string{}
	}

	buf, err := json.Marshal(value)
	if err != nil {
		return err
	}
	s.data[key] = string(buf)
	return nil
}
215938
func (c *Client) updateAlloc(update *structs.Allocation) {
	ar, err := c.getAllocRunner(update.ID)
	if err != nil {
		c.logger.Warn("cannot update nonexistent alloc", "alloc_id", update.ID)
		return
	}

	// Update local copy of alloc
	if err := c.stateDB.PutAllocation(update); err != nil {
		c.logger.Error("error persisting updated alloc locally", "error", err, "alloc_id", update.ID)
	}

	// Update alloc runner
	ar.Update(update)
}
430222
func (c *Cache) Add(key string, value interface{}) {
	if !c.nolock {
		c.mu.Lock()
		defer c.mu.Unlock()
	}

	// Already in cache?
	if ee, ok := c.cache[key]; ok {
		c.ll.MoveToFront(ee)
		ee.Value.(*entry).value = value
		return
	}

	// Add to cache if not present
	ele := c.ll.PushFront(&entry{key, value})
	c.cache[key] = ele

	if c.maxEntries > 0 && c.ll.Len() > c.maxEntries {
		c.removeOldest()
	}
}
226273
func (p *IDPool) AllocateID() ID {
	p.mutex.Lock()
	defer p.mutex.Unlock()

	return p.idCache.allocateID()
}
382593
func (ma *markAllocator) put(mark uint32) {
	ma.lock.Lock()
	defer ma.lock.Unlock()
	ma.marks = append(ma.marks, mark)
}
641133
func (r *AutoVXLANCfgResource) Allocate(reqVal interface{}) (interface{}, error) {
	oper := &AutoVXLANOperResource{}
	oper.StateDriver = r.StateDriver
	err := oper.Read(r.ID)
	if err != nil {
		return nil, err
	}

	var vxlan uint
	if (reqVal != nil) && (reqVal.(uint) != 0) {
		vxlan = reqVal.(uint)
		if !oper.FreeVXLANs.Test(vxlan) {
			return nil, fmt.Errorf("requested vxlan not available")
		}
	} else {
		ok := false
		vxlan, ok = oper.FreeVXLANs.NextSet(0)
		if !ok {
			return nil, errors.New("no vxlans available")
		}
	}

	vlan, ok := oper.FreeLocalVLANs.NextSet(0)
	if !ok {
		return nil, errors.New("no local vlans available")
	}

	oper.FreeVXLANs.Clear(vxlan)
	oper.FreeLocalVLANs.Clear(vlan)

	err = oper.Write()
	if err != nil {
		return nil, err
	}
	return VXLANVLANPair{VXLAN: vxlan, VLAN: vlan}, nil
}
772409
func (b *LRUCache) Init(capacity uint) {
	b.table = make(map[string]*entry, capacity)
	b.priorityQueue = make([]*entry, 0, capacity)
	b.lruList.Init()
	b.freeList.Init()
	heap.Init(&b.priorityQueue)

	// Reserve all the entries in one giant continous block of memory
	arrayOfEntries := make([]entry, capacity)
	for i := uint(0); i < capacity; i++ {
		e := &arrayOfEntries[i]
		e.element.Value = e
		e.index = -1
		b.freeList.PushElementBack(&e.element)
	}
}
1174294
func (f *freelist) allocate(n int) pgid {
	if len(f.ids) == 0 {
		return 0
	}

	var initial, previd pgid
	for i, id := range f.ids {
		if id <= 1 {
			panic(fmt.Sprintf("invalid page allocation: %d", id))
		}

		// Reset initial page if this is not contiguous.
		if previd == 0 || id-previd != 1 {
			initial = id
		}

		// If we found a contiguous block then remove it and return it.
		if (id-initial)+1 == pgid(n) {
			// If we're allocating off the beginning then take the fast path
			// and just adjust the existing slice. This will use extra memory
			// temporarily but the append() in free() will realloc the slice
			// as is necessary.
			if (i + 1) == n {
				f.ids = f.ids[i+1:]
			} else {
				copy(f.ids[i-n+1:], f.ids[i+1:])
				f.ids = f.ids[:len(f.ids)-n]
			}

			// Remove from the free cache.
			for i := pgid(0); i < pgid(n); i++ {
				delete(f.cache, initial+i)
			}

			return initial
		}

		previd = id
	}
	return 0
}
220767
func AllocateSlice(slicePtr interface{}, capacity int) *Allocation {
	slicePtrType := reflect.TypeOf(slicePtr)
	if slicePtrType.Kind() != reflect.Ptr {
		panic("AllocateSlice: slicePtr must be a pointer to a slice")
	}
	sliceType := slicePtrType.Elem()
	if sliceType.Kind() != reflect.Slice {
		panic("AllocateSlice: slicePtr must be a pointer to a slice")
	}

	size := sliceType.Size() * uintptr(capacity)
	alloc := Allocate(size)

	// The slicePtr is actually two words: typeTable, sliceHeaderPtr
	slice := (*reflect.SliceHeader)((*[2]unsafe.Pointer)(unsafe.Pointer(&slicePtr))[1])

	// Set the underlying fields in the slice header.
	slice.Len = 0
	slice.Cap = capacity
	slice.Data = uintptr(alloc.Ptr())

	return alloc
}
582051
func (r *Allocator) Allocate(label *mcs.Label) error {
	ok, offset := r.contains(label)
	if !ok {
		return ErrNotInRange
	}

	allocated, err := r.alloc.Allocate(int(offset))
	if err != nil {
		return err
	}
	if !allocated {
		return ErrAllocated
	}
	return nil
}
8610
func fsFAllocate(fd int, offset int64, len int64) (err error) {
	e := Fallocate(fd, offset, len)
	// Ignore errors when Fallocate is not supported in the current system
	if e != nil && !isSysErrNoSys(e) && !isSysErrOpNotSupported(e) {
		switch {
		case isSysErrNoSpace(e):
			err = errDiskFull
		case isSysErrIO(e):
			err = e
		default:
			// For errors: EBADF, EINTR, EINVAL, ENODEV, EPERM, ESPIPE  and ETXTBSY
			// Appending was failed anyway, returns unexpected error
			err = errUnexpected
		}
		return err
	}

	return nil
}