gallium: refactor/replace p_util.h with util/u_memory.h and util/u_math.h
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_bufmgr_alt.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * Allocate buffers from two alternative buffer providers.
31 *
32 * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35
36 #include "pipe/p_compiler.h"
37 #include "pipe/p_debug.h"
38 #include "util/u_memory.h"
39
40 #include "pb_buffer.h"
41 #include "pb_bufmgr.h"
42
43
44 struct pb_alt_manager
45 {
46 struct pb_manager base;
47
48 struct pb_manager *provider1;
49 struct pb_manager *provider2;
50 };
51
52
53 static INLINE struct pb_alt_manager *
54 pb_alt_manager(struct pb_manager *mgr)
55 {
56 assert(mgr);
57 return (struct pb_alt_manager *)mgr;
58 }
59
60
61 static struct pb_buffer *
62 pb_alt_manager_create_buffer(struct pb_manager *_mgr,
63 size_t size,
64 const struct pb_desc *desc)
65 {
66 struct pb_alt_manager *mgr = pb_alt_manager(_mgr);
67 struct pb_buffer *buf;
68
69 buf = mgr->provider1->create_buffer(mgr->provider1, size, desc);
70 if(buf)
71 return buf;
72
73 buf = mgr->provider2->create_buffer(mgr->provider2, size, desc);
74 return buf;
75 }
76
77
78 static void
79 pb_alt_manager_destroy(struct pb_manager *mgr)
80 {
81 FREE(mgr);
82 }
83
84
85 struct pb_manager *
86 pb_alt_manager_create(struct pb_manager *provider1,
87 struct pb_manager *provider2)
88 {
89 struct pb_alt_manager *mgr;
90
91 if(!provider1 || !provider2)
92 return NULL;
93
94 mgr = CALLOC_STRUCT(pb_alt_manager);
95 if (!mgr)
96 return NULL;
97
98 mgr->base.destroy = pb_alt_manager_destroy;
99 mgr->base.create_buffer = pb_alt_manager_create_buffer;
100 mgr->provider1 = provider1;
101 mgr->provider2 = provider2;
102
103 return &mgr->base;
104 }