Mozilla 2/Memory/OOM API: Difference between revisions

(Add thread safety note, add temporary notification disabling.)
 
(2 intermediate revisions by the same user not shown)
Line 39: Line 39:
  *                    minimum.  The callee should try to free memory in order
  *                    minimum.  The callee should try to free memory in order
  *                    to restore the reserve.
  *                    to restore the reserve.
*
*                    The allocator will repeatedly deliver condition
*                    notifications until the condition desists or all callback
*                    functions have disabled RESERVE_CND_LOW notifications for
*                    the event that caused the current condition.
  *
  *
  *  RESERVE_CND_CRIT: The reserve was not large enough to satisfy a pending
  *  RESERVE_CND_CRIT: The reserve was not large enough to satisfy a pending
Line 50: Line 45:
  *                    the condition spontaneously desists due to concurrent
  *                    the condition spontaneously desists due to concurrent
  *                    deallocation).
  *                    deallocation).
*
*                    The allocator will repeatedly deliver condition
*                    notifications until the condition desists or all callback
*                    functions have disabled RESERVE_CND_CRIT notifications
*                    for the event that caused the current condition.
  *
  *
  *  RESERVE_CND_FAIL: An allocation request could not be satisfied, despite all
  *  RESERVE_CND_FAIL: An allocation request could not be satisfied, despite all
Line 62: Line 52:
  * The order in which the callback functions are called is only loosely
  * The order in which the callback functions are called is only loosely
  * specified: in the absence of interposing callback
  * specified: in the absence of interposing callback
  * registrations/unregistrations or condition notification mask modifications,
  * registrations/unregistrations, enabled callbacks will be called in an
* enabled callbacks will be called in an arbitrary round-robin order.
* arbitrary round-robin order.
  *
  *
  * Condition notifications are sent to callbacks only while conditions exist.
  * Condition notifications are sent to callbacks only while conditions exist.
Line 75: Line 65:
  * Callback functions can freely call back into the allocator (i.e. the
  * Callback functions can freely call back into the allocator (i.e. the
  * allocator releases all internal resources before calling each callback
  * allocator releases all internal resources before calling each callback
  * function), though use of the x*() functions is discouraged, since recursive
  * function), though allocation is discouraged, since recursive callbacks are
  * callbacks are likely to result (which places extra burden on the application
  * likely to result, which places extra burden on the application to avoid
  * to avoid deadlocking).
  * deadlock.
  *
  *
  * Callback functions must be thread-safe, since it is possible that multiple
  * Callback functions must be thread-safe, since it is possible that multiple
Line 85: Line 75:
/* Memory reserve condition types. */
/* Memory reserve condition types. */
typedef enum {
typedef enum {
        RESERVE_CND_NONE        = 0x0, /* No condition. */
         RESERVE_CND_LOW,
         RESERVE_CND_LOW         = 0x1,
         RESERVE_CND_CRIT,
         RESERVE_CND_CRIT       = 0x2,
         RESERVE_CND_FAIL
         RESERVE_CND_FAIL       = 0x4,
        RESERVE_CND_ALL        = 0x7  /* Union of all conditions. */
} reserve_cnd_t;
} reserve_cnd_t;


Line 99: Line 87:
  *  cnd: Condition type being delivered.
  *  cnd: Condition type being delivered.
  *  size: Allocation request size for the allocation that caused the condition.
  *  size: Allocation request size for the allocation that caused the condition.
*
* Output:
*  ret: If false, the allocator will cease re-notification for the specific
*        event that caused the current condition.  If true, the allocator may
*        re-notify the callback of the specific event that caused the current
*        condition.
  */
  */
typedef bool reserve_cb_t(void *ctx, reserve_cnd_t cnd, size_t size);
typedef void reserve_cb_t(void *ctx, reserve_cnd_t cnd, size_t size);


/*
/*
Line 114: Line 96:
  *  cb: Callback function pointer.
  *  cb: Callback function pointer.
  *  ctx: Opaque application data, passed to cb().
  *  ctx: Opaque application data, passed to cb().
*  mask: Mask of condition types to deliver to the callback function.  To
*        receive callbacks for a condition type, include it in the mask.
*/
void    reserve_cb_register(reserve_cb_t *cb, void *ctx, reserve_cnd_t mask);
/* Unregister a callback function. */
void    reserve_cb_unregister(reserve_cb_t *cb);
/*
* Get the mask of condition types that are currently enabled for a callback
* function.
*
* Input:
*  cb: Callback function pointer.
*
* Output:
*  ret: Mask of condition types that may be delivered to cb.
*/
reserve_cnd_t  reserve_cb_getmask(reserve_cb_t *cb);
/*
* Set the mask of conditions that may be delivered to a callback.
*
* Inputs:
*  cb: Callback function pointer.
*  mask: Mask of condition types to deliver to the callback function.  To
*        receive callbacks for a condition type, include it in the mask.
*
* Output:
*  ret: Previous mask of condition types that were to be delivered to the
*        callback function.
*/
reserve_cnd_t  reserve_cb_setmask(reserve_cb_t *cb, reserve_cnd_t mask);
/*
* Enable delivery of condition types that are included in mask.
*
* Inputs:
*  cb: Callback function pointer.
*  mask: Mask of condition types to merge into the current set of enabled
*        condition types.
  *
  *
  * Output:
  * Output:
  *  ret: Previous mask of condition types that were to be delivered to the
  *  ret: If true, failure due to OOM; success otherwise.
*        callback function.
  */
  */
reserve_cnd_t  reserve_cb_enable(reserve_cb_t *cb, reserve_cnd_t mask);
bool    reserve_cb_register(reserve_cb_t *cb, void *ctx);


/*
/*
  * Disable delivery of condition types that are included in mask.
  * Unregister a callback function.
  *
  *
  * Inputs:
  * Inputs:
  *  cb: Callback function pointer.
  *  cb: Callback function pointer.
  *  mask: Mask of condition types to remove from current set of enabled
  *  ctx: Opaque application data, same as that passed to reserve_cb_register().
*        condition types.
  *
  *
  * Output:
  * Output:
  *  ret: Previous mask of condition types that were to be delivered to the
  *  ret: False upon success, true if the {cb,ctx} registration could not be
  *        callback function.
  *        found.
  */
  */
reserve_cnd_t  reserve_cb_disable(reserve_cb_t *cb, reserve_cnd_t mask);
bool    reserve_cb_unregister(reserve_cb_t *cb, void *ctx);


/*
/*
Line 194: Line 133:
  * Set the minimum acceptable reserve size.
  * Set the minimum acceptable reserve size.
  *
  *
  * min: Reserve threshold.
  * min: Reserve threshold.  This value may be internally rounded up.
  * ret: False if the reserve was successfully resized; true otherwise.  Note
  * ret: False if the reserve was successfully resized; true otherwise.  Note
  *      that failure to resize the reserve also results in a RESERVE_CND_LOW
  *      that failure to resize the reserve also results in a RESERVE_CND_LOW
13

edits