pb_buffer_malloc.c revision 92fcbf6e7bc622dcace226bb70ff6d5cdbdbaecb
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 Jos� Fonseca <jrfonseca@tungstengraphics.com> 34 */ 35 36 37#include "pipe/p_debug.h" 38#include "pipe/p_util.h" 39#include "pb_buffer.h" 40 41 42struct malloc_buffer 43{ 44 struct pb_buffer base; 45 void *data; 46}; 47 48 49extern const struct pb_vtbl malloc_buffer_vtbl; 50 51static INLINE struct malloc_buffer * 52malloc_buffer(struct pb_buffer *buf) 53{ 54 assert(buf); 55 assert(buf->vtbl == &malloc_buffer_vtbl); 56 return (struct malloc_buffer *)buf; 57} 58 59 60static void 61malloc_buffer_destroy(struct pb_buffer *buf) 62{ 63 align_free(malloc_buffer(buf)->data); 64 FREE(buf); 65} 66 67 68static void * 69malloc_buffer_map(struct pb_buffer *buf, 70 unsigned flags) 71{ 72 return malloc_buffer(buf)->data; 73} 74 75 76static void 77malloc_buffer_unmap(struct pb_buffer *buf) 78{ 79 /* No-op */ 80} 81 82 83static void 84malloc_buffer_get_base_buffer(struct pb_buffer *buf, 85 struct pb_buffer **base_buf, 86 unsigned *offset) 87{ 88 *base_buf = buf; 89 *offset = 0; 90} 91 92 93const struct pb_vtbl 94malloc_buffer_vtbl = { 95 malloc_buffer_destroy, 96 malloc_buffer_map, 97 malloc_buffer_unmap, 98 malloc_buffer_get_base_buffer 99}; 100 101 102struct pb_buffer * 103pb_malloc_buffer_create(size_t size, 104 const struct pb_desc *desc) 105{ 106 struct malloc_buffer *buf; 107 108 /* TODO: do a single allocation */ 109 110 buf = CALLOC_STRUCT(malloc_buffer); 111 if(!buf) 112 return NULL; 113 114 buf->base.base.refcount = 1; 115 buf->base.base.alignment = desc->alignment; 116 buf->base.base.usage = desc->usage; 117 buf->base.base.size = size; 118 buf->base.vtbl = &malloc_buffer_vtbl; 119 120 buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment); 121 if(!buf->data) { 122 align_free(buf); 123 return NULL; 124 } 125 126 return &buf->base; 127} 128