Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Build a first-fit range allocator

Q128·Free·SystemVerilog

Build a first-fit range allocator

Difficulty
Medium
Topic
Memory Systems
Language
SV
Interview prompt

Question

Maintain inclusive free address ranges. Allocate the first range large enough for a request, and return freed ranges while merging every overlap or adjacency.

Candidate starting point

Implementation scaffold

typedef struct {
  int unsigned lo;
  int unsigned hi;
} range_t;

class RangeAllocator;
  range_t free_ranges[$];
  int unsigned managed_base;
  int unsigned managed_limit;

  function new(int unsigned base, int unsigned size);
    if (size == 0) $fatal(1, "allocator size must be positive");
    if (base > 32'hffff_ffff - (size - 1))
      $fatal(1, "managed range wraps the address space");
    managed_base = base;
    managed_limit = base + size - 1;
    free_ranges.push_back('{lo: managed_base, hi: managed_limit});
  endfunction

  function bit alloc(int unsigned size, output int unsigned addr);
    // Implement here: alloc.
  endfunction

  function void dealloc(int unsigned addr, int unsigned size);
    // Implement here: dealloc.
  endfunction

  function void coalesce();
    range_t merged[$];
    range_t current;
    // Implement here: coalesce.
  endfunction
endclass
Reviewed example

Trace one case

Input
free=[[0x1000,0x10FF]]; alloc(0x20, addr); dealloc(addr=0x1000,size=0x20)
Expected output
allocation base=0x1000; final free list=[[0x1000,0x10FF]]

Allocation preserves suffix [0x1020,0x10FF], and the exact deallocation coalesces the returned prefix back into one maximal range.

What to cover

Requirements

  1. Reject zero-sized construction, allocation, and deallocation.
  2. Return the first sufficient range and its lowest address.
  3. Remove exact fits and preserve the remainder of partial fits.
  4. Keep the free list sorted, disjoint, and maximally coalesced after deallocation.
  5. The caller returns only valid, nonwrapping byte ranges within this allocator’s original pool and guarantees the returned bytes are no longer allocated to another owner. Ownership validation is outside this free-list core.
Exact question handoffPractice Q128

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

Firmware Guide

Review algorithms, data structures, fixed-memory reasoning, concurrency, and silicon bring-up.

  • Memory Systems
  • Allocator
  • Intervals
  • Coalescing
Firmware Guide →
Continue practicing

Related questions

Q260 · Memory SystemsRandomize free-range allocationMediumP→Q278 · Memory SystemsImplement best-fit allocationMediumP→Q034 · Memory SystemsMeasure external fragmentationEasy→Q271 · Memory SystemsCompact live allocationsHardP→Q230 · Memory SystemsMake the allocator thread-safeMediumP→
ASIC.FYI · Learn silicon end to end.info@asic.fyi