pb_buffer_malloc.c revision 1672e8e05996d48e51a1998bd7e9b08b78e012f5
1/**************************************************************************
2 *
3 * Copyright 2007 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 * Implementation of malloc-based buffers to store data that can't be processed
31 * by the hardware.
32 *
33 * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
34 */
35
36
37#include "pipe/p_debug.h"
38#include "util/u_memory.h"
39#include "pb_buffer.h"
40#include "pb_bufmgr.h"
41
42
43struct malloc_buffer
44{
45   struct pb_buffer base;
46   void *data;
47};
48
49
50extern const struct pb_vtbl malloc_buffer_vtbl;
51
52static INLINE struct malloc_buffer *
53malloc_buffer(struct pb_buffer *buf)
54{
55   assert(buf);
56   assert(buf->vtbl == &malloc_buffer_vtbl);
57   return (struct malloc_buffer *)buf;
58}
59
60
61static void
62malloc_buffer_destroy(struct pb_buffer *buf)
63{
64   align_free(malloc_buffer(buf)->data);
65   FREE(buf);
66}
67
68
69static void *
70malloc_buffer_map(struct pb_buffer *buf,
71                  unsigned flags)
72{
73   return malloc_buffer(buf)->data;
74}
75
76
77static void
78malloc_buffer_unmap(struct pb_buffer *buf)
79{
80   /* No-op */
81}
82
83
84static void
85malloc_buffer_get_base_buffer(struct pb_buffer *buf,
86                              struct pb_buffer **base_buf,
87                              unsigned *offset)
88{
89   *base_buf = buf;
90   *offset = 0;
91}
92
93
94const struct pb_vtbl
95malloc_buffer_vtbl = {
96      malloc_buffer_destroy,
97      malloc_buffer_map,
98      malloc_buffer_unmap,
99      malloc_buffer_get_base_buffer
100};
101
102
103struct pb_buffer *
104pb_malloc_buffer_create(size_t size,
105                   	const struct pb_desc *desc)
106{
107   struct malloc_buffer *buf;
108
109   /* TODO: do a single allocation */
110
111   buf = CALLOC_STRUCT(malloc_buffer);
112   if(!buf)
113      return NULL;
114
115   buf->base.base.refcount = 1;
116   buf->base.base.alignment = desc->alignment;
117   buf->base.base.usage = desc->usage;
118   buf->base.base.size = size;
119   buf->base.vtbl = &malloc_buffer_vtbl;
120
121   buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment);
122   if(!buf->data) {
123      FREE(buf);
124      return NULL;
125   }
126
127   return &buf->base;
128}
129
130
131static struct pb_buffer *
132pb_malloc_bufmgr_create_buffer(struct pb_manager *mgr,
133                               size_t size,
134                               const struct pb_desc *desc)
135{
136   return pb_malloc_buffer_create(size, desc);
137}
138
139
140static void
141pb_malloc_bufmgr_flush(struct pb_manager *mgr)
142{
143   /* No-op */
144}
145
146
147static void
148pb_malloc_bufmgr_destroy(struct pb_manager *mgr)
149{
150   /* No-op */
151}
152
153
154static struct pb_manager
155pb_malloc_bufmgr = {
156   pb_malloc_bufmgr_destroy,
157   pb_malloc_bufmgr_create_buffer,
158   pb_malloc_bufmgr_flush
159};
160
161
162struct pb_manager *
163pb_malloc_bufmgr_create(void)
164{
165  return &pb_malloc_bufmgr;
166}
167