Mozilla 2/Memory/OOM API: Difference between revisions

m (reserve_cur() --> reserve_current())
 
(5 intermediate revisions by the same user not shown)
Line 27: Line 27:
== Memory reserve API ==
== Memory reserve API ==


The allocator will maintain a memory reserve that reduces the likelihood of unsatisfiable allocation requests.  The reserve can be configured and queriedAdditionally, callback functions can be set, so that when the reserve is low/insufficient, those callback functions are given an opportunity to free memory.
<pre>/*
* The allocator maintains a memory reserve that is used to satisfy allocation
* requests when no additional memory can be acquired from the operating
* system.  Under normal operating conditions, the reserve size is at least
* reserve_min bytes.  If the reserve is depleted or insufficient to satisfy an
* allocation request, then condition notifications are sent to one or more of
* the registered callback functions:
*
*  RESERVE_CND_LOW: The reserve had to be used to satisfy an allocation
*                    request, which dropped the reserve size below the
*                    minimum.  The callee should try to free memory in order
*                    to restore the reserve.
  *
*  RESERVE_CND_CRIT: The reserve was not large enough to satisfy a pending
*                    allocation request.  Some callee must free adequate
*                    memory in order to prevent application failure (unless
*                    the condition spontaneously desists due to concurrent
*                    deallocation).
*
*  RESERVE_CND_FAIL: An allocation request could not be satisfied, despite all
*                    attempts.  The allocator is about to terminate the
*                    application.
  *
* The order in which the callback functions are called is only loosely
* specified: in the absence of interposing callback
* registrations/unregistrations, enabled callbacks will be called in an
* arbitrary round-robin order.
*
* Condition notifications are sent to callbacks only while conditions exist.
* For example, just before the allocator sends a RESERVE_CND_LOW condition
* notification to a callback, the reserve is in fact depleted.  However, due
* to allocator concurrency, the reserve may have been restored by the time the
* callback function executes.  Furthermore, if the reserve is restored at some
* point during the delivery of condition notifications to callbacks, no
* further deliveries will occur, since the condition no longer exists.
*
* Callback functions can freely call back into the allocator (i.e. the
* allocator releases all internal resources before calling each callback
* function), though allocation is discouraged, since recursive callbacks are
* likely to result, which places extra burden on the application to avoid
* deadlock.
*
* Callback functions must be thread-safe, since it is possible that multiple
* threads will call into the same callback function concurrently.
*/


reserve_cur() returns the current reserve size. Under normal operating conditions, the current reserve will be &ge; the value accessed via reserve_[set_]threshold().
/* Memory reserve condition types. */
typedef enum {
        RESERVE_CND_LOW,
        RESERVE_CND_CRIT,
        RESERVE_CND_FAIL
} reserve_cnd_t;


'''size_t reserve_current(void);'''<br>
/*
'''size_t reserve_threshold(void);'''<br>
* Reserve condition notification callback function type definition.
'''bool reserve_set_threshold(size_t threshold);'''<br>
*
* Inputs:
*  ctx: Opaque application data, as passed to reserve_cb_register().
*  cnd: Condition type being delivered.
*  size: Allocation request size for the allocation that caused the condition.
*/
typedef void reserve_cb_t(void *ctx, reserve_cnd_t cnd, size_t size);


If the reserve size drops below reserve_threshold(), the "reserve low" callback will be calledThe callback function will not be called again unless reserve_low_cb_reset() is called by the application or the callback function returns true (semantically equivalent to reserve_low_cb_reset()).  The default callback function does nothing and returns false, thus potentially allowing the reserve to become exhausted.
/*
* Register a callback function.
*
  * Inputs:
*  cb: Callback function pointer.
*  ctx: Opaque application data, passed to cb().
  *
* Output:
*  ret: If true, failure due to OOM; success otherwise.
*/
bool    reserve_cb_register(reserve_cb_t *cb, void *ctx);


'''typedef bool reserve_low_cb_t(void *ctx);''' /* ctx is opaque app data. */<br>
/*
'''void reserve_set_low_cb(reserve_low_cb_t *cb, void *ctx);'''<br>
* Unregister a callback function.
'''void reserve_low_cb_reset(void);'''<br>
*
* Inputs:
*  cb: Callback function pointer.
*   ctx: Opaque application data, same as that passed to reserve_cb_register().
*
* Output:
*   ret: False upon success, true if the {cb,ctx} registration could not be
*        found.
*/
bool    reserve_cb_unregister(reserve_cb_t *cb, void *ctx);


If an allocation request cannot be satisfied, the "reserve critical" callback will be calledThe callback function may be called repeatedly unless it returns true, indicating that no more memory can be made availableThe default callback function does nothing and returns true, thus allowing the allocator to terminate the application.
/*
* Get the current reserve size.
*
  * ret: Current reserve size.
  */
size_t  reserve_cur_get(void);


'''typedef bool reserve_crit_cb_t(void *ctx, size_t size);'''<br> /* size is allocation request size. */
/*
'''void reserve_set_crit_cb(reserve_crit_cb_t *cb, void *ctx);'''<br>
* Get the minimum acceptable reserve size.  If the reserve drops below this
* value, the RESERVE_CND_LOW condition notification is sent to the callbacks.
*
* ret: Minimum acceptable reserve size.
*/
size_t  reserve_min_get(void);


If an allocation request permanently fails (i.e. the "reserve critical" callback fails to provide adequate memory to satisfy the request), the "reserve fail" callback will be calledIf the callback function returns, the allocator will abort the applicationThe default callback function does nothing, thus allowing the allocator to abort.
/*
 
* Set the minimum acceptable reserve size.
'''typedef void reserve_fail_cb_t(void *ctx);'''<br>
*
'''void reserve_set_fail_cb(reserve_fail_cb_t *cb, void *ctx);'''<br>
* min: Reserve threshold. This value may be internally rounded up.
  * ret: False if the reserve was successfully resized; true otherwiseNote
*      that failure to resize the reserve also results in a RESERVE_CND_LOW
*      condition.
*/
bool    reserve_min_set(size_t min);</pre>


== Implementation (non-)details ==
== Implementation (non-)details ==
13

edits