Skip to question
ASIC.FYI
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
ASIC.FYI/Interview questions/Make the allocator thread-safe

Q263·Free·SystemVerilog

Make the allocator thread-safe

Difficulty
Medium
Topic
Memory Systems
Language
SV
Interview prompt

Question

Protect allocator invariants when several SystemVerilog processes may allocate, deallocate, or inspect state concurrently.

Starting point

Question code

class LockedRangeAllocator;
  task alloc_exclusive(
    int unsigned size,
    output int unsigned addr,
    output bit success
  );
  task dealloc_exclusive(
    int unsigned addr,
    int unsigned size,
    output bit success
  );
  task fragmentation_metrics_exclusive(
    output longint unsigned total_free,
    output int unsigned largest_block,
    output real external_ratio
  );
  task compact_exclusive(
    ref int unsigned relocated_addr[int unsigned]
  );
endclass
Reviewed example

Trace one case

Input
process A calls alloc_exclusive(16, addr, ok) while process B calls dealloc_exclusive(0x1000,16,ok)
Expected output
one complete operation runs before the other; observers see either whole pre-state or whole post-state

A single lock spans every metadata update and is released on success and failure, preventing partially coalesced state from escaping.

What to cover

Requirements

  1. Use one lock for every operation touching allocator state.
  2. Release the lock on every control path.
  3. Do not expose partially updated range or allocation metadata.
  4. Document that the wrapper blocks while another process owns the lock.
Exact question handoffPractice Q263

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

Open in question bank →
Solution accessA Free account unlocks the complete reviewed solution.
Continue learning

SystemVerilog

  • Memory Systems
  • Allocator
  • Semaphore
  • Concurrency
Interview preparation guide →
Continue practicing

Related questions

Q212 · Memory SystemsBuild a first-fit range allocator→Q276 · Memory SystemsRandomize free-range allocation→Q283 · Memory SystemsFind the densest 4 KiB page→
ASIC.FYI · Learn silicon end to end.info@asic.fyi