Blame source/vdo/base/allocationSelector.c

Packit b55c50
/*
Packit b55c50
 * Copyright (c) 2020 Red Hat, Inc.
Packit b55c50
 *
Packit b55c50
 * This program is free software; you can redistribute it and/or
Packit b55c50
 * modify it under the terms of the GNU General Public License
Packit b55c50
 * as published by the Free Software Foundation; either version 2
Packit b55c50
 * of the License, or (at your option) any later version.
Packit b55c50
 * 
Packit b55c50
 * This program is distributed in the hope that it will be useful,
Packit b55c50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit b55c50
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit b55c50
 * GNU General Public License for more details.
Packit b55c50
 * 
Packit b55c50
 * You should have received a copy of the GNU General Public License
Packit b55c50
 * along with this program; if not, write to the Free Software
Packit b55c50
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit b55c50
 * 02110-1301, USA. 
Packit b55c50
 *
Packit b55c50
 * $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/allocationSelector.c#1 $
Packit b55c50
 */
Packit b55c50
Packit b55c50
#include "allocationSelector.h"
Packit b55c50
#include "allocationSelectorInternals.h"
Packit b55c50
Packit b55c50
#include "memoryAlloc.h"
Packit b55c50
Packit b55c50
#include "types.h"
Packit b55c50
Packit b55c50
enum {
Packit b55c50
  ALLOCATIONS_PER_ZONE = 128,
Packit b55c50
};
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
int makeAllocationSelector(ZoneCount            physicalZoneCount,
Packit b55c50
                           ThreadID             threadID,
Packit b55c50
                           AllocationSelector **selectorPtr)
Packit b55c50
{
Packit b55c50
  AllocationSelector *selector;
Packit b55c50
  int result = ALLOCATE(1, AllocationSelector, __func__, &selector);
Packit b55c50
  if (result != VDO_SUCCESS) {
Packit b55c50
    return result;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  *selector = (AllocationSelector) {
Packit b55c50
    .nextAllocationZone = threadID % physicalZoneCount,
Packit b55c50
    .lastPhysicalZone   = physicalZoneCount - 1,
Packit b55c50
  };
Packit b55c50
Packit b55c50
  *selectorPtr = selector;
Packit b55c50
  return VDO_SUCCESS;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
void freeAllocationSelector(AllocationSelector **selectorPtr)
Packit b55c50
{
Packit b55c50
  AllocationSelector *selector = *selectorPtr;
Packit b55c50
  if (selector == NULL) {
Packit b55c50
    return;
Packit b55c50
  }
Packit b55c50
Packit b55c50
  FREE(selector);
Packit b55c50
  *selectorPtr = NULL;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
ZoneCount getNextAllocationZone(AllocationSelector *selector)
Packit b55c50
{
Packit b55c50
  if (selector->lastPhysicalZone > 0) {
Packit b55c50
    if (selector->allocationCount < ALLOCATIONS_PER_ZONE) {
Packit b55c50
      selector->allocationCount++;
Packit b55c50
    } else {
Packit b55c50
      selector->allocationCount = 1;
Packit b55c50
      if (selector->nextAllocationZone < selector->lastPhysicalZone) {
Packit b55c50
        selector->nextAllocationZone++;
Packit b55c50
      } else {
Packit b55c50
        selector->nextAllocationZone = 0;
Packit b55c50
      }
Packit b55c50
    }
Packit b55c50
  }
Packit b55c50
Packit b55c50
  return selector->nextAllocationZone;
Packit b55c50
}