/** * Try to reserve more bytes. * * @param count The number of bytes to add. We will round this up to the page size. * @return The new number of usedBytes if we succeeded; -1 if we failed. */ long reserve(long count) { count = rounder.round(count); while (true) { long cur = usedBytes.get(); long next = cur + count; if (next > maxBytes) { return -1; } if (usedBytes.compareAndSet(cur, next)) { return next; } } }
/** * Release some bytes that we're using. * * @param count The number of bytes to release. We will round this up to the page size. * @return The new number of usedBytes. */ long release(long count) { count = rounder.round(count); return usedBytes.addAndGet(-count); }