+
+ vm_object_lock_assert_exclusive(object);
+}
+
+void
+vm_purgeable_accounting(
+ vm_object_t object,
+ vm_purgable_t old_state,
+ boolean_t disown)
+{
+ task_t owner;
+ int resident_page_count;
+ int wired_page_count;
+ int compressed_page_count;
+ boolean_t disown_on_the_fly;
+
+ vm_object_lock_assert_exclusive(object);
+
+ owner = object->vo_purgeable_owner;
+ if (owner == NULL)
+ return;
+
+ if (!disown && owner->task_purgeable_disowning) {
+ /* task is disowning its purgeable objects: help it */
+ disown_on_the_fly = TRUE;
+ } else {
+ disown_on_the_fly = FALSE;
+ }
+
+ resident_page_count = object->resident_page_count;
+ wired_page_count = object->wired_page_count;
+ if ((COMPRESSED_PAGER_IS_ACTIVE ||
+ DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) &&
+ object->pager != NULL) {
+ compressed_page_count =
+ vm_compressor_pager_get_count(object->pager);
+ } else {
+ compressed_page_count = 0;
+ }
+
+ if (old_state == VM_PURGABLE_VOLATILE ||
+ old_state == VM_PURGABLE_EMPTY) {
+ /* less volatile bytes in ledger */
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_volatile,
+ ptoa(resident_page_count - wired_page_count));
+ /* less compressed volatile bytes in ledger */
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_volatile_compressed,
+ ptoa(compressed_page_count));
+
+ if (disown || !object->alive || object->terminating) {
+ /* wired pages were accounted as "non-volatile"... */
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile,
+ ptoa(wired_page_count));
+ /* ... and in phys_footprint */
+ ledger_debit(owner->ledger,
+ task_ledgers.phys_footprint,
+ ptoa(wired_page_count));
+
+ if (!disown_on_the_fly &&
+ (object->purgeable_queue_type ==
+ PURGEABLE_Q_TYPE_MAX)) {
+ /*
+ * Not on a volatile queue: must be empty
+ * or emptying.
+ */
+ vm_purgeable_nonvolatile_owner_update(owner,-1);
+ } else {
+ /* on a volatile queue */
+ vm_purgeable_volatile_owner_update(owner, -1);
+ }
+ /* no more accounting for this dead object */
+ object->vo_purgeable_owner = NULL;
+#if DEBUG
+ object->vo_purgeable_volatilizer = NULL;
+#endif /* DEBUG */
+ return;
+ }
+
+ /* more non-volatile bytes in ledger */
+ ledger_credit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile,
+ ptoa(resident_page_count - wired_page_count));
+ /* more compressed non-volatile bytes in ledger */
+ ledger_credit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile_compressed,
+ ptoa(compressed_page_count));
+ /* more footprint */
+ ledger_credit(owner->ledger,
+ task_ledgers.phys_footprint,
+ ptoa(resident_page_count
+ + compressed_page_count
+ - wired_page_count));
+
+ } else if (old_state == VM_PURGABLE_NONVOLATILE) {
+
+ /* less non-volatile bytes in ledger */
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile,
+ ptoa(resident_page_count - wired_page_count));
+ /* less compressed non-volatile bytes in ledger */
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile_compressed,
+ ptoa(compressed_page_count));
+ /* less footprint */
+ ledger_debit(owner->ledger,
+ task_ledgers.phys_footprint,
+ ptoa(resident_page_count
+ + compressed_page_count
+ - wired_page_count));
+
+ if (disown || !object->alive || object->terminating) {
+ /* wired pages still accounted as "non-volatile" */
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile,
+ ptoa(wired_page_count));
+ ledger_debit(owner->ledger,
+ task_ledgers.phys_footprint,
+ ptoa(wired_page_count));
+
+ /* one less "non-volatile" object for the owner */
+ if (!disown_on_the_fly) {
+ assert(object->purgeable_queue_type ==
+ PURGEABLE_Q_TYPE_MAX);
+ }
+ vm_purgeable_nonvolatile_owner_update(owner, -1);
+ /* no more accounting for this dead object */
+ object->vo_purgeable_owner = NULL;
+#if DEBUG
+ object->vo_purgeable_volatilizer = NULL;
+#endif /* DEBUG */
+ return;
+ }
+ /* more volatile bytes in ledger */
+ ledger_credit(owner->ledger,
+ task_ledgers.purgeable_volatile,
+ ptoa(resident_page_count - wired_page_count));
+ /* more compressed volatile bytes in ledger */
+ ledger_credit(owner->ledger,
+ task_ledgers.purgeable_volatile_compressed,
+ ptoa(compressed_page_count));
+ } else {
+ panic("vm_purgeable_accounting(%p): "
+ "unexpected old_state=%d\n",
+ object, old_state);
+ }
+
+ vm_object_lock_assert_exclusive(object);
+}
+
+void
+vm_purgeable_nonvolatile_owner_update(
+ task_t owner,
+ int delta)
+{
+ if (owner == NULL || delta == 0) {
+ return;
+ }
+
+ if (delta > 0) {
+ assert(owner->task_nonvolatile_objects >= 0);
+ OSAddAtomic(delta, &owner->task_nonvolatile_objects);
+ assert(owner->task_nonvolatile_objects > 0);
+ } else {
+ assert(owner->task_nonvolatile_objects > delta);
+ OSAddAtomic(delta, &owner->task_nonvolatile_objects);
+ assert(owner->task_nonvolatile_objects >= 0);
+ }
+}
+
+void
+vm_purgeable_volatile_owner_update(
+ task_t owner,
+ int delta)
+{
+ if (owner == NULL || delta == 0) {
+ return;
+ }
+
+ if (delta > 0) {
+ assert(owner->task_volatile_objects >= 0);
+ OSAddAtomic(delta, &owner->task_volatile_objects);
+ assert(owner->task_volatile_objects > 0);
+ } else {
+ assert(owner->task_volatile_objects > delta);
+ OSAddAtomic(delta, &owner->task_volatile_objects);
+ assert(owner->task_volatile_objects >= 0);
+ }
+}
+
+void
+vm_purgeable_compressed_update(
+ vm_object_t object,
+ int delta)
+{
+ task_t owner;
+
+ vm_object_lock_assert_exclusive(object);
+
+ if (delta == 0 ||
+ !object->internal ||
+ object->purgable == VM_PURGABLE_DENY ||
+ object->vo_purgeable_owner == NULL) {
+ /* not an owned purgeable VM object: nothing to update */
+ return;
+ }
+
+ owner = object->vo_purgeable_owner;
+ switch (object->purgable) {
+ case VM_PURGABLE_DENY:
+ break;
+ case VM_PURGABLE_NONVOLATILE:
+ if (delta > 0) {
+ ledger_credit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile_compressed,
+ ptoa(delta));
+ ledger_credit(owner->ledger,
+ task_ledgers.phys_footprint,
+ ptoa(delta));
+ } else {
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_nonvolatile_compressed,
+ ptoa(-delta));
+ ledger_debit(owner->ledger,
+ task_ledgers.phys_footprint,
+ ptoa(-delta));
+ }
+ break;
+ case VM_PURGABLE_VOLATILE:
+ case VM_PURGABLE_EMPTY:
+ if (delta > 0) {
+ ledger_credit(owner->ledger,
+ task_ledgers.purgeable_volatile_compressed,
+ ptoa(delta));
+ } else {
+ ledger_debit(owner->ledger,
+ task_ledgers.purgeable_volatile_compressed,
+ ptoa(-delta));
+ }
+ break;
+ default:
+ panic("vm_purgeable_compressed_update(): "
+ "unexpected purgable %d for object %p\n",
+ object->purgable, object);
+ }