/* Makes a copy of the current state of @gstate and saves it to @gstate->next, then put the address of the newly allcated copy into @gstate. _cairo_gstate_restore() reverses this.  */
cairo_status_t _cairo_gstate_save(cairo_gstate_t **gstate, cairo_gstate_t **freelist)
/* Makes a copy of the current state of @gstate and saves it to @gstate->next, then put the address of the newly allcated copy into @gstate. _cairo_gstate_restore() reverses this.  */
cairo_status_t _cairo_gstate_save(cairo_gstate_t **gstate, cairo_gstate_t **freelist)
{
    cairo_gstate_t *top;
    cairo_status_t status;
    if (CAIRO_INJECT_FAULT ())
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    top = *freelist;
    if (top == NULL) {
	top = _cairo_malloc (sizeof (cairo_gstate_t));
	if (unlikely (top == NULL))
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    } else
	*freelist = top->next;
    status = _cairo_gstate_init_copy (top, *gstate);
    if (unlikely (status)) {
	top->next = *freelist;
	*freelist = top;
	return status;
    }
    top->next = *gstate;
    *gstate = top;
    return CAIRO_STATUS_SUCCESS;
}