00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00037 #include "pipe/p_debug.h"
00038 #include "util/u_memory.h"
00039 #include "pb_buffer.h"
00040 #include "pb_bufmgr.h"
00041
00042
00043 struct malloc_buffer
00044 {
00045 struct pb_buffer base;
00046 void *data;
00047 };
00048
00049
00050 extern const struct pb_vtbl malloc_buffer_vtbl;
00051
00052 static INLINE struct malloc_buffer *
00053 malloc_buffer(struct pb_buffer *buf)
00054 {
00055 assert(buf);
00056 assert(buf->vtbl == &malloc_buffer_vtbl);
00057 return (struct malloc_buffer *)buf;
00058 }
00059
00060
00061 static void
00062 malloc_buffer_destroy(struct pb_buffer *buf)
00063 {
00064 align_free(malloc_buffer(buf)->data);
00065 FREE(buf);
00066 }
00067
00068
00069 static void *
00070 malloc_buffer_map(struct pb_buffer *buf,
00071 unsigned flags)
00072 {
00073 return malloc_buffer(buf)->data;
00074 }
00075
00076
00077 static void
00078 malloc_buffer_unmap(struct pb_buffer *buf)
00079 {
00080
00081 }
00082
00083
00084 static void
00085 malloc_buffer_get_base_buffer(struct pb_buffer *buf,
00086 struct pb_buffer **base_buf,
00087 unsigned *offset)
00088 {
00089 *base_buf = buf;
00090 *offset = 0;
00091 }
00092
00093
00094 const struct pb_vtbl
00095 malloc_buffer_vtbl = {
00096 malloc_buffer_destroy,
00097 malloc_buffer_map,
00098 malloc_buffer_unmap,
00099 malloc_buffer_get_base_buffer
00100 };
00101
00102
00103 struct pb_buffer *
00104 pb_malloc_buffer_create(size_t size,
00105 const struct pb_desc *desc)
00106 {
00107 struct malloc_buffer *buf;
00108
00109
00110
00111 buf = CALLOC_STRUCT(malloc_buffer);
00112 if(!buf)
00113 return NULL;
00114
00115 buf->base.base.refcount = 1;
00116 buf->base.base.alignment = desc->alignment;
00117 buf->base.base.usage = desc->usage;
00118 buf->base.base.size = size;
00119 buf->base.vtbl = &malloc_buffer_vtbl;
00120
00121 buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment);
00122 if(!buf->data) {
00123 FREE(buf);
00124 return NULL;
00125 }
00126
00127 return &buf->base;
00128 }
00129
00130
00131 static struct pb_buffer *
00132 pb_malloc_bufmgr_create_buffer(struct pb_manager *mgr,
00133 size_t size,
00134 const struct pb_desc *desc)
00135 {
00136 return pb_malloc_buffer_create(size, desc);
00137 }
00138
00139
00140 static void
00141 pb_malloc_bufmgr_flush(struct pb_manager *mgr)
00142 {
00143
00144 }
00145
00146
00147 static void
00148 pb_malloc_bufmgr_destroy(struct pb_manager *mgr)
00149 {
00150
00151 }
00152
00153
00154 static struct pb_manager
00155 pb_malloc_bufmgr = {
00156 pb_malloc_bufmgr_destroy,
00157 pb_malloc_bufmgr_create_buffer,
00158 pb_malloc_bufmgr_flush
00159 };
00160
00161
00162 struct pb_manager *
00163 pb_malloc_bufmgr_create(void)
00164 {
00165 return &pb_malloc_bufmgr;
00166 }